PWM Examples

Table of Contents

API-PWM-CASE1:PWM NORMAL

Function Sub-Function APIs Description Update Status
irq_handler() none Interrupt handler function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() pwm_set_clk() pwm_set_clk(CLOCK_SYS_CLOCK_HZ, CLOCK_SYS_CLOCK_HZ) set the clock frequency of PWM
gpio_set_func() gpio_set_func(PWM_PIN, AS_PWMx) set pin as PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_NORMAL_MODE) set mode of PWM
pwm_set_cycle_and_duty() pwm_set_cycle_and_duty(PWM_ID, 2, 1) set the max and cmp for PWM
pwm_start() pwm_start(PWM_ID) start PWM
main_loop() none Main program loop

Variables above are defined as below

#define PWM_PIN GPIO_PA2
#define AS_PWMx AS_PWM0
#define PWM_ID PWM0

API-PWM-CASE2:PWM COUNT

Function Sub-Function APIs Description Update Status
irq_handler() none Interrupt handler function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() pwm_set_clk() pwm_set_clk(CLOCK_SYS_CLOCK_HZ, CLOCK_SYS_CLOCK_HZ) set the clock frequency of PWM
gpio_set_func() gpio_set_func(PWM_PIN, AS_PWMx) set pin as PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_COUNT_MODE) set mode of PWM
pwm_set_pulse_num() pwm_set_pulse_num(PWM_ID,PWM_PULSE_NUM) set the bumber of pulse
pwm_set_cycle_and_duty() pwm_set_cycle_and_duty(PWM_ID, 2, 1) set the max and cmp for PWM
pwm_start() pwm_start(PWM_ID) start PWM
main_loop() none Main program loop

Variables above are defined as below

#define PWM_PIN GPIO_PA2
#define AS_PWMx AS_PWM0
#define PWM_ID PWM0
#define PWM_PULSE_NUM 3

API-PWM-CASE3:PWM IR

Function Sub-Function APIs Description Update Status
irq_handler() none Interrupt handler function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() pwm_set_clk() pwm_set_clk(CLOCK_SYS_CLOCK_HZ, CLOCK_SYS_CLOCK_HZ) set the clock frequency of PWM
gpio_set_func() gpio_set_func(PWM_PIN, AS_PWMx) set pin as PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_IR_MODE) set mode of PWM
pwm_set_pulse_num() pwm_set_pulse_num(PWM_ID,PWM_PULSE_NUM) set the bumber of pulse
pwm_set_cycle_and_duty() pwm_set_cycle_and_duty(PWM_ID, 2, 1) set the max and cmp for PWM
pwm_start() pwm_start(PWM_ID) start PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_COUNT_MODE) switch to count mode to stop IR mode
main_loop() none Main program loop

Variables above are defined as below

#define PWM_PIN GPIO_PA2
#define AS_PWMx AS_PWM0
#define PWM_ID PWM0
#define PWM_PULSE_NUM 2

API-PWM-CASE4:PWM IR FIFO

Function Sub-Function APIs Description Update Status
irq_handler() none Interrupt handler function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() pwm_set_clk() pwm_set_clk(CLOCK_SYS_CLOCK_HZ, CLOCK_SYS_CLOCK_HZ) set the clock frequency of PWM
gpio_set_func() gpio_set_func(PWM_PIN, AS_PWMx) set pin as PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_IR_FIFO_MODE) set mode of PWM
pwm_set_pulse_num() pwm_set_pulse_num(PWM_ID,PWM_PULSE_NUM) set the bumber of pulse
pwm_set_cycle_and_duty() pwm_set_cycle_and_duty(PWM_ID, 2, 1) set the max and cmp for PWM
pwm_set_pwm0_shadow_cycle_and_duty() pwm_set_pwm0_shadow_cycle_and_duty(PWM_ID,4, 2) set the max and cmp of shadow for PWM
pwm_ir_fifo_set_data_entry() pwm_ir_fifo_set_data_entry(PWM_PULSE_NUM1,0,1) set data entry of IR fifo for PWM
pwm_ir_fifo_set_data_entry(PWM_PULSE_NUM2,0,0)
pwm_ir_fifo_set_data_entry(PWM_PULSE_NUM3,1,1)
pwm_start() pwm_start(PWM_ID) start PWM
main_loop() none Main program loop

Variables above are defined as below

#define PWM_PIN GPIO_PA2
#define AS_PWMx AS_PWM0
#define PWM_ID PWM0
#define PWM_PULSE_NUM1 5
#define PWM_PULSE_NUM2 5
#define PWM_PULSE_NUM3 10

API-PWM-CASE5:PWM IR DMA FIFO

Function Sub-Function APIs Description Update Status
irq_handler() if(reg_pwm_irq_sta & FLD_IRQ_PWM0_IR_DMA_FIFO_DONE) determine whether interrupt flag of IR DMA fifo done is right 2019-1-10
reg_pwm_irq_sta Ι= FLD_IRQ_PWM0_IR_DMA_FIFO_DONE clear interrupt flag 2019-1-10
ir_dma_fifo_cnt++ Interrupt processing 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() pwm_set_clk() pwm_set_clk(CLOCK_SYS_CLOCK_HZ, CLOCK_SYS_CLOCK_HZ) set the clock frequency of PWM
gpio_set_func() gpio_set_func(PWM_PIN, AS_PWMx) set pin as PWM
pwm_set_mode() pwm_set_mode(PWM_ID, PWM_IR_DMA_FIFO_MODE) set mode of PWM
pwm_set_pulse_num() pwm_set_pulse_num(PWM_ID,PWM_PULSE_NUM) set the bumber of pulse
pwm_set_cycle_and_duty() pwm_set_cycle_and_duty(PWM_ID, IR_DMA_MAX_TICK, IR_DMA_CMP_TICK) set the max and cmp for PWM
pwm_set_pwm0_shadow_cycle_and_duty() pwm_set_pwm0_shadow_cycle_and_duty(PWM_ID,IR_DMA_SHADOW_MAX_TICK, IR_DMA_SHADOW_CMP_TICK) set the max and cmp of shadow for PWM
pwm_config_dma_fifo_waveform() IR_DMA_Buff[index++]= pwm_config_dma_fifo_waveform(1, PWM0_PULSE_NORMAL, 9000 * CLOCK_SYS_CLOCK_1US/IR_DMA_MAX_TICK) set waveform of IR DMA FIFO mode for PWM
... for details, refer to Driver Demo
pwm_set_dma_address() pwm_set_dma_address(&IR_DMA_Buff) set the address of DMA
reg_irq_mask Ι= FLD_IRQ_SW_PWM_EN enable pwm interrrupt
reg_pwm_irq_mask Ι= FLD_IRQ_PWM0_IR_DMA_FIFO_DONE enable IR DMA FIFO done interrupt of pwm
irq_enable() enable global interrupt
pwm_start_dma_ir_sending() start IR DMA fifo mode of PWM
main_loop() none Main program loop

Variables above are defined as below

#define PWM_PIN GPIO_PA2
#define AS_PWMx AS_PWM0
#define PWM_ID PWM0
#define IR_DMA_CARRIER_FREQ 38000
#define IR_DMA_MAX_TICK (CLOCK_SYS_CLOCK_HZ/IR_DMA_CARRIER_FREQ)
#define IR_DMA_CMP_TICK (IR_DMA_MAX_TICK/2)
#define IR_DMA_SHADOW_CARRIER_FREQ 56000
#define IR_DMA_SHADOW_MAX_TICK (CLOCK_SYS_CLOCK_HZ/IR_DMA_SHADOW_CARRIER_FREQ)
#define IR_DMA_SHADOW_CMP_TICK (IR_DMA_SHADOW_MAX_TICK/2)
unsigned short IR_DMA_Buff[64]={0};
unsigned int ir_dma_fifo_cnt=0;
unsigned char index = 2;

History Record

Date Description Author
2019-1-10 initial release LJW