7 years, 1 month ago.

4 simultaneous instances of PWM

I am using a nRF52 DK to control 4 different LEDs on four output pins p26,p25,p24,p23. I have declared all of them as PwmOut. Problem is that only upto 3 PwmOut work simultaneously. Once I add the fourth one, my program fails. This post ( https://github.com/ARMmbed/mbed-os/issues/3027 - "on NRF52 target up to 3 PWM instances simultaneously are supported" ) seems to suggest that the 3 channel limitation is a known issue on nRF52 DK. Can someone please confirm this? And if yes, can you please suggest a work around?

1 Answer

7 years, 1 month ago.

I suppose it simply doesn't have the hardware for more PWM modules. Since it is just LEDs being controlled, I would have a look at SoftPWM library, which emulates PWM using tickers. You would need to check however if it works properly for your requirements, for example if it is not (visibly) interrupted during BLE transfers.

Accepted Answer

I would still like to be able to use the hardware timer based PWM functionality.

According to Nordic's documentation (http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v10.0.0%2Flib_pwm.html) it looks like they have 2 channels per instance, so with three instances, I will at least get 6 channels. Since the period for my four PWM channels can be same, I could use two instances to create these 4 channels. However, the mbed PwmOut api seems to be encapsulating this in a way that doesn't let me distinguish between instance and channel - so when I create three PwmOuts they are three different instances and I run out of instances for the fourth one. Do you know how one can tie two channels to the same instance?

Alternatively, do you know what I need to do to access this Nordic API through mbed? I could just program everything through a Keil or other IDE using Nordic API, but I'd rather not do that.

posted by Shruti Koparkar 17 Feb 2017

Here is the PWM source code for the Nordic one: https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/pwmout_api.c, which is quite complicated compared to most other PWM drivers. It even has an interrupt handler, I have no idea why it has an interrupt handler.

Anyway for some others the manufacturer API is also available through the mbed lib, but I don't know/think this is the case for the NRF device. You can simply see if it works, if not you would need to import that driver code yourself, or access the hardware yourself directly.

Checking the NRF51822 reference manual: The mbed lib is getting the max number of PWM channels you can get from one Timer. One other Timer is used for the SoftDevice apparently. I don't know why the third timer is not used also for PWM, maybe it is used for something else.

posted by Erik - 17 Feb 2017

I looked further in it, and the Nordic PWM lib must use a different setup than the mbed one, which also uses some kind of cross between software and hardware PWM. In general it is a surprise how complicated it is to generate something simple as a PWM signal on the NRF51822.

posted by Erik - 19 Feb 2017

Hi Erik, Thank you for looking into this. I looked at the Nordic PWM driver code and had further questions that I have posted as a new question here https://developer.mbed.org/questions/77096/PwmOuts-grab-3-unique-instances-supporte/

If you get a chance, please take a look and let me know your thoughts!

posted by Shruti Koparkar 22 Feb 2017