The Pulse Width Modulation (PWM) module driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).
The hardware access layer provides basic APIs for accessing the registers of the PWM peripheral. See the API documentation for the PWM HAL for details.
The driver layer provides APIs on a higher level than the HAL. See the API documentation for the PWM driver for details.
Key features include:
The PWM Driver Example provides sample code that you can use to quickly get started.
The configurable parameters include:
The default configuration for the PWM driver is located in nrf_drv_config.h
. This configuration is used when nrf_drv_pwm_init is called with a NULL pointer to the configuration structure.
Before it can be used for a particular instance of the peripheral, the driver must be explicitly enabled for this instance in nrf_drv_config.h
.
First, call nrf_drv_pwm_init to initialize and configure the driver for a given instance of the peripheral. See the nrf_drv_pwm_config_t structure documentation for possible configuration options.
For example:
Next, define a sequence of duty cycle values using the nrf_pwm_sequence_t structure. For example:
Start the playback of the sequence by calling nrf_drv_pwm_simple_playback.
You can play the sequence several times (for example, 3 times):
Additionally, the playback can be repeated in a loop:
If a more complicated pattern of duty cycles is required that involves two sequence definitions, use nrf_drv_pwm_complex_playback.
If a sequence must be updated during the playback, define a proper event handler and pass it as parameter in the call to nrf_drv_pwm_init. By default, the handler is called at the end of the playback (or right before the next iteration in a loop), but it can be configured to be called when a sequence is completely loaded.
For example:
Entries in the array of duty cycle values can be modified directly, since the peripheral loads the values as the sequence is played back. If the whole sequence definition must be changed, use nrf_drv_pwm_sequence_update. If only some part of the definition must be modified, you can use one of the other updating functions.
At any time, the playback can be stopped by calling nrf_drv_pwm_stop. The pulse generation will stop at the end of the current PWM period.
When the PWM driver is no longer needed or its configuration must be changed, call nrf_drv_pwm_uninit.