Appiko
hal_pwm.h
1 
19 #ifndef CODEBASE_HAL_HAL_PWM_H_
20 #define CODEBASE_HAL_HAL_PWM_H_
21 
31 #include "stdint.h"
32 #include "stdbool.h"
33 #include "nrf.h"
34 
35 #ifdef NRF51
36 #error "nRF51 series SoCs don't have a PWM peripheral"
37 #endif
38 
39 #if SYS_CFG_PRESENT == 1
40 #include "sys_config.h"
41 #endif
42 #ifndef HAL_PWM_PERIPH_USED
43 #define HAL_PWM_PERIPH_USED 0
44 #endif
45 
46 
48 #define PWM_USED HAL_PWM_PERIPH_USED
49 
51 #define HAL_PWM_MAX_PIN_NUM 4
52 
54 typedef enum
55 {
56  HAL_PWM_FREQ_16MHz = PWM_PRESCALER_PRESCALER_DIV_1,
57  HAL_PWM_FREQ_8MHz = PWM_PRESCALER_PRESCALER_DIV_2,
58  HAL_PWM_FREQ_4MHz = PWM_PRESCALER_PRESCALER_DIV_4,
59  HAL_PWM_FREQ_2MHz = PWM_PRESCALER_PRESCALER_DIV_8,
60  HAL_PWM_FREQ_1MHz = PWM_PRESCALER_PRESCALER_DIV_16,
61  HAL_PWM_FREQ_500kHz = PWM_PRESCALER_PRESCALER_DIV_32,
62  HAL_PWM_FREQ_250kHz = PWM_PRESCALER_PRESCALER_DIV_64,
63  HAL_PWM_FREQ_125kHz = PWM_PRESCALER_PRESCALER_DIV_128
65 
67 typedef enum
68 {
70  HAL_PWM_MODE_UP = PWM_MODE_UPDOWN_Up,
72  HAL_PWM_MODE_UP_DOWN = PWM_MODE_UPDOWN_UpAndDown,
74 
79 typedef enum
80 {
82  HAL_PWM_SHORT_SEQEND0_STOP_MASK = PWM_SHORTS_SEQEND0_STOP_Msk,
84  HAL_PWM_SHORT_SEQEND1_STOP_MASK = PWM_SHORTS_SEQEND1_STOP_Msk,
86  HAL_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK = PWM_SHORTS_LOOPSDONE_SEQSTART0_Msk,
88  HAL_PWM_SHORT_LOOPSDONE_SEQSTART1_MASK = PWM_SHORTS_LOOPSDONE_SEQSTART1_Msk,
90  HAL_PWM_SHORT_LOOPSDONE_STOP_MASK = PWM_SHORTS_LOOPSDONE_STOP_Msk
92 
95 typedef enum
96 {
98  HAL_PWM_LOAD_COMMON = PWM_DECODER_LOAD_Common,
100  HAL_PWM_LOAD_GROUPED = PWM_DECODER_LOAD_Grouped,
102  HAL_PWM_LOAD_INDIVIDUAL = PWM_DECODER_LOAD_Individual,
104  HAL_PWM_LOAD_WAVE_FORM = PWM_DECODER_LOAD_WaveForm
106 
109 typedef enum
110 {
112  HAL_PWM_STEP_INTERNAL = PWM_DECODER_MODE_RefreshCount,
114  HAL_PWM_STEP_EXTERNAL = PWM_DECODER_MODE_NextStep
116 
120 typedef struct
121 {
128  uint16_t * seq_values;
130  uint16_t len;
133  uint32_t repeats;
136  uint32_t end_delay;
138 
142 typedef enum
143 {
145  HAL_PWM_IRQ_STOPPED_MASK = PWM_INTENSET_STOPPED_Msk,
147  HAL_PWM_IRQ_SEQSTARTED0_MASK = PWM_INTENSET_SEQSTARTED0_Msk,
149  HAL_PWM_IRQ_SEQSTARTED1_MASK = PWM_INTENSET_SEQSTARTED1_Msk,
151  HAL_PWM_IRQ_SEQEND0_MASK = PWM_INTENSET_SEQEND0_Msk,
153  HAL_PWM_IRQ_SEQEND1_MASK = PWM_INTENSET_SEQEND1_Msk,
155  HAL_PWM_IRQ_PWMPERIODEND_MASK = PWM_INTENSET_PWMPERIODEND_Msk,
157  HAL_PWM_IRQ_LOOPSDONE_MASK = PWM_INTENSET_LOOPSDONE_Msk
159 
163 typedef struct
164 {
166  uint32_t * pins;
170  uint32_t pin_num;
176  uint32_t irq_priority;
178 
182 typedef struct
183 {
186  uint32_t countertop;
188  uint32_t loop;
191  uint32_t shorts_mask;
194  uint32_t interrupt_masks;
206  void (*irq_handler)(hal_pwm_irq_mask_t irq_source);
208 
214 void hal_pwm_init(hal_pwm_init_t * init_config);
215 
220 void hal_pwm_start(hal_pwm_start_t * start_config);
221 
225 void hal_pwm_stop(void);
226 
227 #endif /* CODEBASE_HAL_HAL_PWM_H_ */
228 
16 MHz / 64 = 250 kHz.
Definition: hal_pwm.h:63
Each channel has its own 16 bit value.
Definition: hal_pwm.h:102
uint32_t shorts_mask
Select the shortcuts that need to be enabled. OR the values in hal_pwm_short_mask_t to enable them.
Definition: hal_pwm.h:191
Short between LOOPSDONE event and SEQSTART[0] task.
Definition: hal_pwm.h:86
Interrupt on SEQSTARTED[0] event.
Definition: hal_pwm.h:147
hal_pwm_irq_mask_t
PWM interrupts.
Definition: hal_pwm.h:142
uint32_t repeats
Number of times a particular value should be played. Only for HAL_PWM_STEP_INTERNAL mode.
Definition: hal_pwm.h:133
hal_pwm_freq_t
Select the PWM frequency to operate at.
Definition: hal_pwm.h:54
uint32_t * pins
Pointer to array containing the pins number used by hal pwm.
Definition: hal_pwm.h:166
uint32_t interrupt_masks
Select the interrupts that need to be enabled OR the values in hal_pwm_irq_mask_t to enable them.
Definition: hal_pwm.h:194
uint16_t len
Number of 16-bit values in the buffer pointed by seq_values.
Definition: hal_pwm.h:130
After the loaded value is repeated for the number of times in REFRESH register.
Definition: hal_pwm.h:112
uint32_t countertop
Maximum count of the counter. This and oper_freq decide the frequency of the resulting PWM waveform.
Definition: hal_pwm.h:186
hal_pwm_decoder_load_t decoder_load
Select the way in which the data pointed in seq_config is loaded into to the various channels.
Definition: hal_pwm.h:197
hal_pwm_decoder_load_t
PWM decoder's data load modes. This mode selects how the different channels' next value is loaded fro...
Definition: hal_pwm.h:95
hal_pwm_dec_trigger_t
PWM decoder's next load trigger modes. This selects when the next value is loaded from RAM.
Definition: hal_pwm.h:109
Short between LOOPSDONE event and STOP task.
Definition: hal_pwm.h:90
A 16-bit value is used in all four (0-3) PWM channels.
Definition: hal_pwm.h:98
Short between LOOPSDONE event and SEQSTART[1] task.
Definition: hal_pwm.h:88
16 MHz / 8 = 2 MHz.
Definition: hal_pwm.h:60
16 MHz / 1 = 16 MHz.
Definition: hal_pwm.h:57
uint32_t irq_priority
IRQ priority with which the irq_handler in hal_pwm_start_t is called.
Definition: hal_pwm.h:176
void hal_pwm_start(hal_pwm_start_t *start_config)
Start the PWM generation based on the configuration provided.
Definition: hal_pwm.c:192
Up counter, reset to 0 on incrementing to CounterTop.
Definition: hal_pwm.h:70
uint16_t * seq_values
Pointer to an array containing the PWM duty cycle values. This array present in the data RAM should p...
Definition: hal_pwm.h:128
16 MHz / 16 = 1 MHz.
Definition: hal_pwm.h:61
Interrupt on PWMPERIODEND event.
Definition: hal_pwm.h:155
hal_pwm_freq_t oper_freq
Select the operating frequency of the hal pwm module.
Definition: hal_pwm.h:172
16 MHz / 32 = 500 kHz.
Definition: hal_pwm.h:62
Interrupt on SEQEND[1] event.
Definition: hal_pwm.h:153
uint32_t loop
The number of times the pattern of both seq_config must be repeated.
Definition: hal_pwm.h:188
When the Next Step task is set.
Definition: hal_pwm.h:114
Short between SEQEND[1] event and STOP task.
Definition: hal_pwm.h:84
hal_pwm_dec_trigger_t decoder_trigger
Select when the next data is loaded to the PWM.
Definition: hal_pwm.h:199
bool * pin_idle_state
Pointer to array having the state of pins when PWM generation isn't on.
Definition: hal_pwm.h:168
uint32_t pin_num
Number of pins to be used by hal pwm. Maximum is HAL_PWM_MAX_PIN_NUM.
Definition: hal_pwm.h:170
hal_pwm_mode_t
Select the operating mode of the PWM wave counter.
Definition: hal_pwm.h:67
void hal_pwm_stop(void)
Stop the PWM generation.
Definition: hal_pwm.c:229
Short between SEQEND[0] event and STOP task.
Definition: hal_pwm.h:82
Interrupt on SEQEND[0] event.
Definition: hal_pwm.h:151
16 MHz / 4 = 4 MHz.
Definition: hal_pwm.h:59
Interrupt on SEQSTARTED[1] event.
Definition: hal_pwm.h:149
16 MHz / 2 = 8 MHz.
Definition: hal_pwm.h:58
hal_pwm_mode_t oper_mode
Select the operating mode (Up or Up&Down) of the module's counter.
Definition: hal_pwm.h:174
Interrupt on STOPPED event.
Definition: hal_pwm.h:145
Struct for initializing the hal pwm module.
Definition: hal_pwm.h:163
hal_pwm_short_mask_t
Bit masks for the PWM shortcuts. Or the appropriate ones for the required shortcuts.
Definition: hal_pwm.h:79
Up & down counter, decrement back to 0 on reaching CounterTop.
Definition: hal_pwm.h:72
void hal_pwm_init(hal_pwm_init_t *init_config)
Initialize the HAL PWM module. Can be called again to change the initialization configuration.
Definition: hal_pwm.c:126
1st 16-bit value used in channels 0 and 1; 2nd one in channels 2 and 3.
Definition: hal_pwm.h:100
Interrupt on LOOPSDONE event.
Definition: hal_pwm.h:157
uint32_t end_delay
Additional number of cycles that the last PWM value is to be played after the end....
Definition: hal_pwm.h:136
Struct containing the configuration for starting the hal pwm module.
Definition: hal_pwm.h:182
1st three channels have their own 16 bit values, 4th one is the wave counter.
Definition: hal_pwm.h:104
Configuration for a sequence of PWM values. Note that the buffer pointed to in this structure needs t...
Definition: hal_pwm.h:120