Contiki-NG
Files

Driver for the CC2538 PWM on GPTIMER. More...

Files

file  pwm.c
 Driver for the CC2538 PWM.
 
file  pwm.h
 Header file for the CC2538 PWM driver.
 

PWM return values

#define PWM_SUCCESS   0
 
#define PWM_ERROR   (-1)
 

PWM recommended values respect to peripheral clock frequency

#define PWM_SYS_16MHZ_NO_PRES_MIN   0xFFFF
 
#define PWM_SYS_16MHZ_NO_PRES_MIN_FREQ   244
 
#define PWM_SYS_16MHZ_PRES_MIN   0x00F42400
 
#define PWM_SYS_16MHZ_PRES_MIN_FREQ   1
 
#define PWM_SYS_16MHZ_NO_PRES_MAX   100
 
#define PWM_SYS_16MHZ_NO_PRES_MAX_FREQ   160000
 

PWM driver definitions and configuration values

#define PWM_TIMER_A   0
 
#define PWM_TIMER_B   1
 
#define PWM_TIMER_0   0
 
#define PWM_TIMER_1   1
 
#define PWM_TIMER_2   2
 
#define PWM_TIMER_3   3
 
#define PWM_TIMER_MIN   PWM_TIMER_0
 
#define PWM_TIMER_MAX   PWM_TIMER_3
 
#define PWM_SIGNAL_STRAIGHT   1
 
#define PWM_SIGNAL_INVERTED   0
 
#define PWM_OFF_WHEN_STOP   0
 
#define PWM_ON_WHEN_STOP   1
 
#define PWM_GPTIMER_CFG_SPLIT_MODE   0x04
 
#define PWM_DUTY_MAX   100
 
#define PWM_DUTY_MIN   0
 
#define PWM_FREQ_MIN   PWM_SYS_16MHZ_PRES_MIN_FREQ
 
#define PWM_FREQ_MAX   PWM_SYS_16MHZ_NO_PRES_MAX_FREQ
 

PWM functions

int8_t pwm_enable (uint32_t freq, uint8_t duty, uint32_t count, uint8_t timer, uint8_t ab)
 Configures the general purpose timer in PWM mode. More...
 
int8_t pwm_stop (uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin, uint8_t state)
 Halts the PWM in a given GPT/timer. More...
 
int8_t pwm_start (uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin)
 Once configured, starts the PWM. More...
 
int8_t pwm_set_direction (uint8_t timer, uint8_t ab, uint8_t dir)
 Sets the PWM duty cycle signal direction (high/low) More...
 
int8_t pwm_toggle_direction (uint8_t timer, uint8_t ab)
 Toggle the PWM signal direction (inverts the current duty cycle) More...
 
int8_t pwm_disable (uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin)
 Disables a previously PWM configured GPTn. More...
 

Detailed Description

Driver for the CC2538 PWM on GPTIMER.

The driver uses the timers A and B of the general purpose timers to create a PWM signal, allowing to set a duty cycle value from 1-100%. This implementation relies on having a peripheral clock of 16MHz, but it can be easily changed (see PWM_FREQ_MIN and PWM_FREQ_MAX values). The reason it is fixed to these frequencies is to have a consistent duty cycle implementation.

Depending on the specific needs these limits can be changed to meet a given duty cycle and lower frequencies by using the prescaler (GPTIMER_TnPR).

Running a PWM timer prevents the LPM driver from dropping to PM1+.

Function Documentation

◆ pwm_disable()

int8_t pwm_disable ( uint8_t  timer,
uint8_t  ab,
uint8_t  port,
uint8_t  pin 
)

Disables a previously PWM configured GPTn.

Parameters
timerGeneral purpose timer to disable [0-3]
abSelect which timer to disable (Timer A or B)
portPort number used as PWM to disable (set as input GPIO)
pinPin number used as PWM to disable (set as input GPIO)
Returns
PWM_SUCCESS if successful, else PWM_ERROR

This function disables a specific timer (A or B) and reset related registers to default values. The user must explicitely pass the port/pin number of the pin to disable as PWM and to be configured as input GPIO. The module clock is not disabled with this function

Definition at line 330 of file pwm.c.

◆ pwm_enable()

int8_t pwm_enable ( uint32_t  freq,
uint8_t  duty,
uint32_t  count,
uint8_t  timer,
uint8_t  ab 
)

Configures the general purpose timer in PWM mode.

Parameters
freqPWM frequency (in Hz)
dutyPWM duty cycle (percentage in integers)
countPWM duty cycle (count number)
timerGeneral purpose timer to use [0-3]
abSelect which timer to use (Timer A or B)
Returns
PWM_SUCCESS if successful, else PWM_ERROR

Definition at line 93 of file pwm.c.

◆ pwm_set_direction()

int8_t pwm_set_direction ( uint8_t  timer,
uint8_t  ab,
uint8_t  dir 
)

Sets the PWM duty cycle signal direction (high/low)

Parameters
timerGeneral purpose timer [0-3]
abSelect which timer to use (Timer A or B)
dirDirection of the PWM signal, PWM_SIGNAL_INVERTED or PWM_SIGNAL_STRAIGHT
Returns
PWM_SUCCESS if successful, else PWM_ERROR

Definition at line 274 of file pwm.c.

◆ pwm_start()

int8_t pwm_start ( uint8_t  timer,
uint8_t  ab,
uint8_t  port,
uint8_t  pin 
)

Once configured, starts the PWM.

Parameters
timerGeneral purpose timer to start [0-3]
abSelect which timer to start (Timer A or B)
portPort number to use as PWM
pinPin number to use as PWM
Returns
PWM_SUCCESS if successful, else PWM_ERROR

Definition at line 235 of file pwm.c.

◆ pwm_stop()

int8_t pwm_stop ( uint8_t  timer,
uint8_t  ab,
uint8_t  port,
uint8_t  pin,
uint8_t  state 
)

Halts the PWM in a given GPT/timer.

Parameters
timerGeneral purpose timer to stop [0-3]
abSelect which timer to stop (Timer A or B)
portPort of the gpio port mapped to the PWM to stop
pinPin of the gpio port mapped to the PWM to stop
stateState to leave the pin once stopped, on (1) or off (0)
Returns
PWM_SUCCESS if successful, else PWM_ERROR

Definition at line 190 of file pwm.c.

◆ pwm_toggle_direction()

int8_t pwm_toggle_direction ( uint8_t  timer,
uint8_t  ab 
)

Toggle the PWM signal direction (inverts the current duty cycle)

Parameters
timerGeneral purpose timer to use [0-3]
abSelect which timer to use (Timer A or B)
Returns
PWM_SUCCESS if successful, else PWM_ERROR

Definition at line 302 of file pwm.c.