Windows 10 Iot Core 应用程序在我尝试打开 PWM 引脚时崩溃

Windows 10 Iot Core app crashes if I try to open a PWM pin

我想打开蜂鸣器的 PWM 引脚。但是如果我尝试调用 pwmController.OpenPin(6) 方法,应用程序会崩溃并显示 System.Runtime.InteropServices.SEHException.

我已经仔细检查了 ms-iot-samples 等示例源。但是我看不出我的问题是什么。

我的想法是缺少某些权限,但如果我尝试添加例如 <iot:Capability Name="lowLevelDevices" />,我将无法再构建应用程序。

来源

private PwmPin buzzerPin;
private PwmController pwmController;

public RainbowHAT()
{
    // ... do something else
    InitAsync();
}

private async void InitAsync()
{
    Logger.Log(this, "Init");

    // Setup PWM controller.
    if (LightningProvider.IsLightningEnabled)
    {
        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
    }

    var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
    if (pwmControllers == null || pwmControllers.Count < 2)
    {
        throw new OperationCanceledException("Operation canceled due missing GPIO controller");
    }

    pwmController = pwmControllers[1];
    pwmController.SetDesiredFrequency(50);

    // Setup buzzer
    buzzerPin = pwmController.OpenPin(13); <-- CRASH
    buzzerPin.SetActiveDutyCyclePercentage(0.05);
    buzzerPin.Start();
}

我也尝试了以下 tip to reduce the min required Windows version,但这也没有帮助。

PWM控制器需要Lightning支持。因此,您需要设置控制器驱动程序,因为 Direct Memory Mapped Driver. Here 是 Raspberry Pi 上关于 PWM 的示例。

您还需要修改如下代码:

    private async void InitAsync()
    {
        Logger.Log(this, "Init");

        // Setup PWM controller.
        if (LightningProvider.IsLightningEnabled)
        {
            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
            if (pwmControllers == null || pwmControllers.Count < 2)
            {
                throw new OperationCanceledException("Operation canceled due missing GPIO controller");
            }

            pwmController = pwmControllers[1];
            pwmController.SetDesiredFrequency(50);

            // Setup buzzer
            buzzerPin = pwmController.OpenPin(13);
            buzzerPin.SetActiveDutyCyclePercentage(0.05);
            buzzerPin.Start();
        }
    }