PWM API's

hi team,

I using nrf52840DK controller and nRF5_SDK_17.1.0.  I want to change PWM module Frequency dynamically (it has to change frequency automatically) at different time instants. 

Is there any APIs or reference  example code to achieve the task?

Also what is the difference between PWM driver - legacy layerPWM driver and PWM HAL APIs?

Parents
  • Hi,

    what is the difference between PWM driver - legacy layerPWM driver and PWM HAL APIs?

    The PWM HAL is a set of low level functions for reading and writing to the WPM registers. The nrfx PWM driver contains the implementation of the higher level driver. And the legacy layer / legacy driver is a shim or wrapper that provides the old nrf_drv_pwm API. In this case, it just redefines function names and type names, so it does not matter if you use the nrfx_ or nrf_drv API.

    (There are also other PWM libraries in the SDK that implement PWM using different methods, either TIMER and GPIOTE via PPI without software other than for setting up, or RTC and GPIO where SW needs to run for every pin state change).

    I want to change PWM module Frequency dynamically (it has to change frequency automatically) at different time instants. 

    When using the PWM peripheral, the frequency is given by the PRESCALER value. Using the driver this is set by the base_clock parameter in the nrfx_pwm_config_t struct that you pass to nrfx_pwm_init() if using the nrfx API (if using the nrf_drv the names will be different, but all else the same). To adjust this via the driver you need to call nrfx_pwm_uninit() first, then call nrfx_pwm_init() again with a modified struct with a different base_clock.

  • Thank you for the response. We have tried using nrfx_pwm_uninit() first and nrfx_pwm_init() next. However, when I tried to flash on the controller board, it displays "Fatal error" in the logger. 

    please help.

  • When you see "Fatal error" that means that an APP_ERROR_CHECK has been called with an error code. If you make a debug build and run again you should see an extended output with file name and line number, so that you can know which function returned which error code. (To get a debug build, define DEBUG for the project or select the Debug build target if using an example SES project as starting point).

  • Hi, actually i have one doubt . Is it mandatory to provide software delay between nrfx_pwm_uninit()  and nrfx_pwm_init()? without delay can not we switch? because i tried without delay i am getting fatal error and also i am getting fatal error whenever i am using hardware timer also.  But i am getting exact output with software delay only. why like that? may i know the reason. And why  i am not getting output with hardware timers. also i am not understanding what is the meaning of fatal error and in which case this type of errors occurs . can you please explain.

  • Hi, actually i have another doubt.i am getting fluctuation in terminal emulator and debug terminal while printing pwm output frequency. can this controller gpios have a capacity to sense 1000 interrupts per sec can you please explain why i getting  fluctuation. is it  gpios problem or terminal output problem or my code problem. please explain me.

Reply Children
  • Hi,

    kunchalaRajesh said:
    i tried without delay i am getting fatal error

    I would not expect a delay to be needed, but it would be interesting to know what happens. As stated, you can check what the "fatal error" really was as explained in my previous post. That needs to be done in order to know what happened. Once you know which function returned which error code, you can look more into the details.

    kunchalaRajesh said:
    this controller gpios have a capacity to sense 1000 interrupts per sec can you please explain why i getting  fluctuation.

    Which fluctuations are you describing now? What is the problem? If you are loogging too much/fast it could be that you loose some UART data, if that is what you are seeing and asking about. That is not unexpected. 

    PS: Please share code by using Insert -> Insert Code instead of screenshots. That makes it much easier to get a full picture.

Related