nRF51 SDK - S130 SoftDevice
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Pulse-width modulation (PWM)

Module for generating a pulse-width modulated output signal. More...

Data Structures

struct  app_pwm_config_t
 PWM configuration structure used for initialization. More...
 
struct  app_pwm_t
 PWM instance structure. More...
 

Macros

#define APP_PWM_NOPIN   0xFFFFFFFF
 
#define APP_PWM_CHANNELS_PER_INSTANCE   2
 Number of channels for one timer instance (fixed to 2 due to timer properties).
 
#define APP_PWM_CB_SIZE   9
 Size of the PWM instance control block (fixed value).
 
#define APP_PWM_INSTANCE(name, num)
 Macro for creating a PWM instance. More...
 
#define APP_PWM_DEFAULT_CONFIG_1CH(period_in_us, pin)
 PWM instance default configuration (1 channel). More...
 
#define APP_PWM_DEFAULT_CONFIG_2CH(period_in_us, pin0, pin1)
 PWM instance default configuration (2 channels). More...
 

Typedefs

typedef uint16_t app_pwm_duty_t
 
typedef void(* app_pwm_callback_t )(uint32_t)
 PWM callback that is executed when a PWM duty change has been completed. More...
 

Enumerations

enum  app_pwm_polarity_t {
  APP_PWM_POLARITY_ACTIVE_LOW = 0,
  APP_PWM_POLARITY_ACTIVE_HIGH = 1
}
 Channel polarity.
 

Functions

ret_code_t app_pwm_init (app_pwm_t const *const p_instance, app_pwm_config_t const *const p_config, app_pwm_callback_t p_ready_callback)
 Function for initializing a PWM instance. More...
 
uint32_t app_pwm_uninit (app_pwm_t const *const p_instance)
 Function for uninitializing a PWM instance and releasing the allocated resources. More...
 
void app_pwm_enable (app_pwm_t const *const p_instance)
 Function for enabling a PWM instance after initialization. More...
 
void app_pwm_disable (app_pwm_t const *const p_instance)
 Function for stopping a PWM instance after initialization. More...
 
uint32_t app_pwm_channel_duty_set (app_pwm_t const *const p_instance, uint8_t channel, app_pwm_duty_t duty)
 Function for setting the PWM channel duty cycle in percents. More...
 
app_pwm_duty_t app_pwm_channel_duty_get (app_pwm_t const *const p_instance, uint8_t channel)
 Function for retrieving the PWM channel duty cycle in percents. More...
 

Detailed Description

Module for generating a pulse-width modulated output signal.

This module provides a PWM implementation using timers, GPIOTE, and PPI.

Each PWM instance utilizes 1 timer, 2 PPI channels, and 1 PPI channel group plus 2 PPI and 1 GPIOTE channels per PWM channel. The maximum number of PWM channels per instance is 2.

Macro Definition Documentation

#define APP_PWM_DEFAULT_CONFIG_1CH (   period_in_us,
  pin 
)
Value:
{ \
.pins = {pin, APP_PWM_NOPIN}, \
.pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
.num_of_channels = 1, \
.period_us = period_in_us \
}

PWM instance default configuration (1 channel).

#define APP_PWM_DEFAULT_CONFIG_2CH (   period_in_us,
  pin0,
  pin1 
)
Value:
{ \
.pins = {pin0, pin1}, \
.pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
.num_of_channels = 2, \
.period_us = period_in_us \
}

PWM instance default configuration (2 channels).

#define APP_PWM_INSTANCE (   name,
  num 
)
Value:
const nrf_drv_timer_t m_pwm_##name##_timer = NRF_DRV_TIMER_INSTANCE(num); \
uint32_t m_pwm_##name##_cb[APP_PWM_CB_SIZE]; \
/*lint -e{545}*/ \
const app_pwm_t name = { \
.p_cb = &m_pwm_##name##_cb, \
.p_timer = &m_pwm_##name##_timer, \
}

Macro for creating a PWM instance.

Typedef Documentation

typedef void(* app_pwm_callback_t)(uint32_t)

PWM callback that is executed when a PWM duty change has been completed.

Parameters
[in]pwm_idPWM instance ID.

Function Documentation

app_pwm_duty_t app_pwm_channel_duty_get ( app_pwm_t const *const  p_instance,
uint8_t  channel 
)

Function for retrieving the PWM channel duty cycle in percents.

Parameters
[in]p_instancePWM instance.
[in]channelChannel number.
Returns
Duty cycle value.
uint32_t app_pwm_channel_duty_set ( app_pwm_t const *const  p_instance,
uint8_t  channel,
app_pwm_duty_t  duty 
)

Function for setting the PWM channel duty cycle in percents.

A duty cycle change requires one full PWM clock period to finish. If another change is attempted for any channel of given instance before the current change is complete, the new attempt will result in the error NRF_ERROR_BUSY.

Parameters
[in]p_instancePWM instance.
[in]channelChannel number.
[in]dutyDuty cycle (0 - 100).
Return values
NRF_SUCCESSIf the operation was successful.
NRF_ERROR_BUSYIf the PWM is not ready yet.
NRF_ERROR_INVALID_STATEIf the given instance was not initialized.
void app_pwm_disable ( app_pwm_t const *const  p_instance)

Function for stopping a PWM instance after initialization.

Parameters
[in]p_instancePWM instance.
Return values
NRF_SUCCESSIf the operation was successful.
NRF_ERROR_INVALID_STATEIf the given instance was not initialized.
void app_pwm_enable ( app_pwm_t const *const  p_instance)

Function for enabling a PWM instance after initialization.

Parameters
[in]p_instancePWM instance.
Return values
NRF_SUCCESSIf the operation was successful.
NRF_ERROR_INVALID_STATEIf the given instance was not initialized.
ret_code_t app_pwm_init ( app_pwm_t const *const  p_instance,
app_pwm_config_t const *const  p_config,
app_pwm_callback_t  p_ready_callback 
)

Function for initializing a PWM instance.

Parameters
[in]p_instancePWM instance.
[in]p_configInitial configuration.
[in]p_ready_callbackPointer to ready callback function (or NULL to disable).
Return values
NRF_SUCCESSIf initialization was successful.
NRF_ERROR_NO_MEMIf there were not enough free resources.
NRF_ERROR_INVALID_PARAMIf an invalid configuration structure was passed.
NRF_ERROR_INVALID_STATEIf the timer/PWM is already in use or if initialization failed.
uint32_t app_pwm_uninit ( app_pwm_t const *const  p_instance)

Function for uninitializing a PWM instance and releasing the allocated resources.

Parameters
[in]p_instancePWM instance.
Return values
NRF_SUCCESSIf uninitialization was successful.
NRF_ERROR_INVALID_STATEIf the given instance was not initialized.