dotnet / iot

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

If define a PWM Output on , eg GPIO18 (pin12), Does it create a ValueChanged Event if I enable this ? #2274

Closed jldigi40 closed 9 months ago

jldigi40 commented 9 months ago

Hi All,

Just general question does the ValueChanged if attaced to a OpenPin , fire on PWM change of such Pin ?

Example - Setup

PWM Setup... /boot/config.text .

Enable PWM

dtoverlay=pwm,pin=18,func=2 . .... ./99-com.rules

Edited for permission... .

PWM export results in a "change" action on the pwmchip device (not "add" of a new device), so match actions other than "remove".

SUBSYSTEM=="pwm", PROGRAM="/bin/sh -c '\ chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;\ chown -R root:gpio /sys/devices/platform/soc/.pwm/pwm/pwmchip && chmod -R 770 /sys/devices/platform/soc/.pwm/pwm/pwmchip\ '" .

Reboot.... ...

Code Example....

//Start PWM Channel - GPIO18 as in config.text overlay ? var pwm = PwmChannel.Create(0,0,100,0.5); // 100hz, 50/50 duty pwm.Start();

... Can I add a event to GPIO18 and see each time PWM swiitches ?

controller.OpenPin(18,Pinmode.Output).ValueChanged += GPIO18ValueChangeEvent;

Just want make sure not looking at something not possible ?

Joshua

huesla commented 9 months ago

Not sure whether using the same GPIO for PWM signal and input monitoring works.... test it maybe? What would work for sure is wiring the PWM pin to some other pin (with a resistor to be safe) and monitor that.

pgrawehr commented 9 months ago

Generally, this will not work, at least not reliability. If the pin is set to pwm, it is internally routed to a different piece of hardware. You might see something on the gpio line, but this will mostly be random noise. I recommend connecting to a different pin with a resistor, as mentioned above.

raffaeler commented 9 months ago

From the Broadcom manual (chapter 6, page 89), it appears that:

For this reason I believe it can't work and agree with @pgrawehr.

The resistor trick also mentioned by @huesla is a great workaround but be careful because the amount of the events could substantially hit the overall performance because of the overwhelming amount of interrupts in kernel mode.

jldigi40 commented 9 months ago

Hello Raf

Thanks just wanted some thoughts here , idea was more for a self check is PWM is working for diagnostic page on a POC project , based on Blazor based Web app interface I developing , all good just need get out a my good old oscilloscope to check PWM is active and working !!!

Thanks. Joshua

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Raf (Raffaele Rialdi) @.> Sent: Wednesday, January 31, 2024 6:42:58 AM To: dotnet/iot @.> Cc: jldigi40 @.>; Author @.> Subject: Re: [dotnet/iot] If define a PWM Output on , eg GPIO18 (pin12), Does it create a ValueChanged Event if I enable this ? (Issue #2274)

From the Broadcom manualhttps://datasheets.raspberrypi.com/bcm2835/bcm2835-peripherals.pdf (chapter 6, page 89), it appears that:

For this reason I believe it can't work and agree with @pgrawehrhttps://github.com/pgrawehr.

The resistor trick also mentioned by @hueslahttps://github.com/huesla is a great workaround but be careful because the amount of the events could substantially hit the overall performance because of the overwhelming amount of interrupts in kernel mode.

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/iot/issues/2274#issuecomment-1917765510, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A5DG5L2PPWU5DEH66NN75VDYRFEMFAVCNFSM6AAAAABCQEXA7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJXG43DKNJRGA. You are receiving this because you authored the thread.Message ID: @.***>

raffaeler commented 9 months ago

Hi @jldigi40 it's great to hear that.

I'll give you some alternative ideas:

  1. the cheapest solution. You can use a small Atmel ATTINY85 microcontroller (below 1$) or equivalent to detect the presence of the PWM and, for example, turn on a led.
  2. a super-cheap logic state analyzer (around 4-8$). This is a great board: https://www.aliexpress.com/item/1005005100640836.html that you can use in conjunction with SigRok PulseView to monitor up to 16 digital lines. SigRok is a great open source project (easy to use) allowing to display and decode signals/protocols of every nature.
  3. Bus Pirate. A well known board (around 30$) http://dangerousprototypes.com/docs/Bus_Pirate to mess up with any kind of bus/signal/protocol you want to detect/understand. This is probably the most difficult to use among this list
  4. the swiss army knife in electronics (around 50$) https://www.aliexpress.com/item/1005005287756149.html which can work as a (low frequency) oscilloscope, multimeter and component detector.

Even if I have more powerful instruments, I own all of those in my own lab and happily use all of them :-)

krwq commented 9 months ago

Hey @jldigi40 thanks for your questions, since this is not actionable for us I'm going to go ahead and close. If you have any further questions please feel free to open new issue or start Discord discussion