dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.16k stars 582 forks source link

RPI4 PWM can't be used with Windows11 #1640

Open ChesterHuang999 opened 3 years ago

ChesterHuang999 commented 3 years ago

Describe the bug When I execute the Sample code, I get an error message like the following.

Unhandled exception. System.ArgumentException: No PWM device exists for PWM chip at index 0. (Parameter 'chip') at System.Device.Pwm.Channels.Windows10PwmChannel..ctor(Int32 chip, Int32 channel, Int32 frequency, Double dutyCycle) at System.Device.Pwm.PwmChannel.CreateWindows10PwmChannel(Int32 chip, Int32 channel, Int32 frequency, Double dutyCyclePercentage) at System.Device.Pwm.PwmChannel.Create(Int32 chip, Int32 channel, Int32 frequency, Double dutyCyclePercentage) at GPIOTry.Program.Main(String[] args) in D:\Chester\Project\RPI4\Windows\GPIOTry\GPIOTry\Program.cs:line 16

I changed the boot/config.txt according to "https://github.com/dotnet/iot/blob/main/Documentation/raspi-pwm.md", But the same problem still occurs.


// put sample code here
using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Device.Spi;
using System.Device.Pwm;
using System.Threading;
namespace GPIOTry
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var gpiocontroller = new GpioController();

            PwmChannel pwmcontroller = PwmChannel.Create(0,0,400,0);
            pwmcontroller.Start();
            gpiocontroller.OpenPin(2, PinMode.Output);
            byte bringth = 0;
            try
            {
                while (true)
                {
                    bringth++;
                    if (bringth == 11)
                        bringth = 0;
                    pwmcontroller.DutyCycle=bringth*0.1;
                    gpiocontroller.Write(2, PinValue.High);
                    Console.WriteLine("Duty is " + bringth.ToString());
                    Console.WriteLine("Set GPIO2 High");
                    Thread.Sleep(1000);
                    gpiocontroller.Write(2, PinValue.Low);
                    Console.WriteLine("Set GPIO2 Low");
                    Thread.Sleep(1000);
                }
            }
            finally
            {
                pwmcontroller.Stop();
                gpiocontroller.ClosePin(2);
            }
        }
    }
}

Platform: RPI4B
OS: Windows11
System.Device.Gpio: 1.5.0
krwq commented 3 years ago

@ChesterHuang999 Windows 11 is not supported at the moment. I'm not sure how could you have changed /boot/config.txt which is linux file on Windows 11... I believe the only scenarios which will work on Window at the moment are:

then once you get I2C you can get Pca9685 for PWM.

I don't think anyone of us have tried to make it work with Win11 but supposedly we could make Linux /dev/mem implementation work on Windows with some tiny modifications... cc: @joperezr @Ellerbach @pgrawehr @raffaeler in case any of you have tried Win 11 on PI already

raffaeler commented 3 years ago

Sorry, I did not try Win11 on the PI.

FWIW in my projects I always use a small external PWM card as I found to be more reliable (because of the timings) than the internal RPi PWM.

pgrawehr commented 3 years ago

@ChesterHuang999 Please describe exactly on what kind of hardware and operating system you were trying to run the code. There's no PWM hardware on typical Windows desktop PCs, and there's no Windows 11 for Raspberry Pi.

joperezr commented 3 years ago

[Triage]: If you could share steps on how to install windows 11 on Pi4 then we can try to get a repro environment and investigate.

ChesterHuang999 commented 3 years ago

Thank you all for your replies.

The hardware platform is RPI4 B which ram is 4GB. Hardware details: https://www.raspberrypi.org/products/raspberry-pi-4-model-b/

First, in addition to PWM, I experimented using "System.Device.GPIO" to control GPIO. The GPIO can work normally.

For how to install Windows 11 on RPI4, please refer to: https://www.tomshardware.com/how-to/install-windows-11-raspberry-pi

@krwq "I'm not sure how could you have changed /boot/config.txt which is linux file on Windows 11" When you have an SD Card made according to "https://www.tomshardware.com/how-to/install-windows-11-raspberry-pi". Use the "WoR-Boot-Mounter" program, you can Modify "Boot\Config.txt". It is possible to modify Config.txt because I have to modify it to adjust the default output screen resolution.

raffaeler commented 3 years ago

@ChesterHuang999 I rapidly read the article and, at a certain point, it says "The Raspberry Pi’s onboard Wi-Fi, Bluetooth and GPIO do not work so you’ll need to use Ethernet or a USB Wi-Fi dongle to get online". If there are those limitations, they will also affect PWM as well. I can't exclude there may be a way to enable them (as raspiconfig does under Linux) but I have no clue how to do it under Windows.

BTW, you could give a try with Windows 10 and see if this is a Win11 specific issue or, as I believe, you need the "Windows IoT" specific SKU to make the peripheral work.

pgrawehr commented 3 years ago

I think there's nothing we can do here, until the low-level drivers are in place.

fkromer commented 2 years ago

Relates to this issue: https://github.com/dotnet/iot/issues/1705

Ellerbach commented 2 years ago

@ChesterHuang999 as @pgrawehr and @raffaeler wrote, there is no PWM drivers. So this is not supported on an already not supported scenario (Windows 11 on the Raspberry Pi). So changes you have to make it working are quite low honestly. Any strong reason why Windows on this non supported scenario and why not simply Raspberry Pi OS?

raffaeler commented 2 years ago

One more point. This thread was created before the RTM Windows build. I never tried Windows on a RPi, but it would definitely be better to test the latest available build and not relying on a pre-release.

Said that, there is a third-party project that I never tested and found by surfing the web. I don't know the author and it has nothing to do with Microsoft of Raspberry PI Foundation. He created a website to run Windows on the Raspberry PI and the following is his repository: https://github.com/worproject/RPi-Windows-Drivers GitHub marked the author as "Verified" but frankly I don't know what does this imply. On his repository there is a list of drivers, including the PWM one. They are unsigned (this is a reason for me to not trust them) but you may eventually download the sources and compile+sign by yourself if you trust the sources. The damage would eventually be restricted to just the RPi, but be aware of the potential risks. All of this is out of support and not recommended or endorsed.

pgrawehr commented 2 years ago

@raffaeler Pretty complex task to get that installed, but since Windows 11 will (very likely) be officially available for ARM64 (see here: https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewARM64) we should start investigating this setup. Not sure if I find time to do this, but it does sound interesting.

raffaeler commented 2 years ago

Yes, it sounds complicated but it looks like being the only practical solution at the moment. I am not familiar with the previous support in Windows 10, but I would expect those drivers to be provided by the Windows team (if they will continute to support the RPi HW in the IoT scenario).

As it is definitely interesting, I'll investigate as well and see.