InPlay API
hal_pwm.h
1 
13 #ifndef HAL_PWM_H
14 #define HAL_PWM_H
15 
24 #include <stdint.h>
25 #include "in_mmap.h"
26 
27 /*
28  * Defines
29  ****************************************************************************************
30  */
31 
32 
33 /*
34  * Enumeration
35  ****************************************************************************************
36  */
37 
38 enum pwm_id {
39  PWM0_ID,
40  PWM1_ID,
41  PWM2_ID,
42  PWM3_ID,
43  PWM4_ID,
44  PWM5_ID,
45  PWM6_ID,
46  PWM7_ID,
47  PWM_MAX_ID,
48 };
49 
50 enum pwm_err {
51  PWM_ERR_OK = 0,
52  PWM_ERR_INVALID_PARAM = -1,
53  PWM_ERR_BAD_STATE = -2,
54 } ;
55 
56 /*
57  * Inline Functions
58  ****************************************************************************************
59  */
60 static INLINE void pwm_enable(int id)
61 {
62  uint32_t reg = RD_WORD(PWM_REGS_MISC_CTRL);
63  reg |= (1 << id);
64  WR_WORD(PWM_REGS_MISC_CTRL, reg);
65 }
66 
67 static INLINE void pwm_disable(int id)
68 {
69  uint32_t reg = RD_WORD(PWM_REGS_MISC_CTRL);
70  reg &= ~(1 << id);
71  WR_WORD(PWM_REGS_MISC_CTRL, reg);
72 }
73 
74 static INLINE void pwm_pause(int id, int pause)
75 {
76  uint32_t reg = RD_WORD(PWM_REGS_MISC_CTRL);
77  if (pause) {
78  reg |= (1 << (PWM_REGS_MISC_CTRL_CTL_PWM_PAUSE_SHIFT + id));
79  } else {
80  reg &= ~(1 << (PWM_REGS_MISC_CTRL_CTL_PWM_PAUSE_SHIFT + id));
81  }
82  WR_WORD(PWM_REGS_MISC_CTRL, reg);
83 }
84 
85 static INLINE void pwm_polarity(int id, int invert)
86 {
87  uint32_t reg = RD_WORD(PWM_REGS_MISC_CTRL);
88  if (invert) {
89  reg |= (1 << (PWM_REGS_MISC_CTRL_CTL_PWM_POLARITY_SHIFT + id));
90  } else {
91  reg &= ~(1 << (PWM_REGS_MISC_CTRL_CTL_PWM_POLARITY_SHIFT + id));
92  }
93  WR_WORD(PWM_REGS_MISC_CTRL, reg);
94 }
95 
96 static INLINE void pwm_config(int id, uint32_t duration, uint32_t high_duration)
97 {
98  uint32_t addr1, addr2;
99 
100  if (id == PWM0_ID) {
101  addr1 = PWM_REGS_PWM_DURATION_0;
102  addr2 = PWM_REGS_PWM_HIGH_DURATION_0;
103  } else if (id == PWM1_ID) {
104  addr1 = PWM_REGS_PWM_DURATION_1;
105  addr2 = PWM_REGS_PWM_HIGH_DURATION_1;
106  } else if (id == PWM2_ID) {
107  addr1 = PWM_REGS_PWM_DURATION_2;
108  addr2 = PWM_REGS_PWM_HIGH_DURATION_2;
109  } else if (id == PWM3_ID) {
110  addr1 = PWM_REGS_PWM_DURATION_3;
111  addr2 = PWM_REGS_PWM_HIGH_DURATION_3;
112  } else if (id == PWM4_ID) {
113  addr1 = PWM_REGS_PWM_DURATION_4;
114  addr2 = PWM_REGS_PWM_HIGH_DURATION_4;
115  } else if (id == PWM5_ID) {
116  addr1 = PWM_REGS_PWM_DURATION_5;
117  addr2 = PWM_REGS_PWM_HIGH_DURATION_5;
118  } else if (id == PWM6_ID) {
119  addr1 = PWM_REGS_PWM_DURATION_6;
120  addr2 = PWM_REGS_PWM_HIGH_DURATION_6;
121  } else {
122  addr1 = PWM_REGS_PWM_DURATION_7;
123  addr2 = PWM_REGS_PWM_HIGH_DURATION_7;
124  }
125 
126  WR_WORD(addr1, duration);
127  WR_WORD(addr2, high_duration);
128 }
129 
130 static INLINE void pwm_high_duration(int id, uint32_t high_duration)
131 {
132  uint32_t addr;
133 
134  if (id == PWM0_ID) {
135  addr = PWM_REGS_PWM_HIGH_DURATION_0;
136  } else if (id == PWM1_ID) {
137  addr = PWM_REGS_PWM_HIGH_DURATION_1;
138  } else if (id == PWM2_ID) {
139  addr = PWM_REGS_PWM_HIGH_DURATION_2;
140  } else if (id == PWM3_ID) {
141  addr = PWM_REGS_PWM_HIGH_DURATION_3;
142  } else if (id == PWM4_ID) {
143  addr = PWM_REGS_PWM_HIGH_DURATION_4;
144  } else if (id == PWM5_ID) {
145  addr = PWM_REGS_PWM_HIGH_DURATION_5;
146  } else if (id == PWM6_ID) {
147  addr = PWM_REGS_PWM_HIGH_DURATION_6;
148  } else {
149  addr = PWM_REGS_PWM_HIGH_DURATION_7;
150  }
151 
152  WR_WORD(addr, high_duration);
153 }
154 
155 static INLINE int pwm_output(int id)
156 {
157  uint32_t addr;
158 
159  if (id == PWM0_ID) {
160  addr = PWM_REGS_PWM_OUTPUT_0;
161  } else if (id == PWM1_ID) {
162  addr = PWM_REGS_PWM_OUTPUT_1;
163  } else if (id == PWM2_ID) {
164  addr = PWM_REGS_PWM_OUTPUT_2;
165  } else if (id == PWM3_ID) {
166  addr = PWM_REGS_PWM_OUTPUT_3;
167  } else if (id == PWM4_ID) {
168  addr = PWM_REGS_PWM_OUTPUT_4;
169  } else if (id == PWM5_ID) {
170  addr = PWM_REGS_PWM_OUTPUT_5;
171  } else if (id == PWM6_ID) {
172  addr = PWM_REGS_PWM_OUTPUT_6;
173  } else {
174  addr = PWM_REGS_PWM_OUTPUT_7;
175  }
176 
177  return (RD_WORD(addr) & 1);
178 }
179 
180 static INLINE uint32_t pwm_int_status(void)
181 {
182  return (RD_WORD(PWM_REGS_INTR_STATUS) & 0xFF);
183 }
184 
185 static INLINE uint32_t pwm_int_mask_status(void)
186 {
187  return (RD_WORD(PWM_REGS_INTR_MASK_STATUS) & 0xFF);
188 }
189 
190 static INLINE void pwm_int_clr_all(void)
191 {
192  WR_WORD(PWM_REGS_INTR_CLEAR, 0xFF);
193 }
194 
195 static INLINE void pwm_int_clr(int id)
196 {
197  WR_WORD(PWM_REGS_INTR_CLEAR, (1 << id));
198 }
199 
200 static INLINE void pwm_int_mask_all(void)
201 {
202  WR_WORD(PWM_REGS_INTR_MASK_SET, 0xFF);
203 }
204 
205 static INLINE void pwm_int_mask(int id)
206 {
207  WR_WORD(PWM_REGS_INTR_MASK_SET, (1 << id));
208 }
209 
210 static INLINE void pwm_int_unmask(int id)
211 {
212  WR_WORD(PWM_REGS_INTR_MASK_CLEAR, (1 << id));
213 }
214 
215 static INLINE void pwm_int_reset(void)
216 {
217  WR_WORD(PWM_REGS_RESET_INTR, PWM_REGS_RESET_INTR_CTL_PWM_INTR_SRESET);
218 }
219 
220 static INLINE void pwm_duration_load(uint32_t pwm_load)
221 {
222  WR_WORD(PWM_REGS_PWM_HIGH_DURATION_0, pwm_load&PWM_REGS_PWM_HIGH_DURATION_0_DURATION_MASK);
223 }
224 
225 
226 /*
227  * Internal Functions
228  ****************************************************************************************
229  */
230 int hal_pwm_open_status(void);
231 int hal_pwm_isr_status(void);
232 void hal_crc_register_cb(void * arg, void (*cb)(void* arg));
233 
234 /*
235  * APIs
236  ****************************************************************************************
237  */
238 
251 void *hal_pwm_open(int id, int prio, void *arg, void (*callback)(void *));
252 
262 int hal_pwm_close(void *hdl);
263 
264 
274 uint32_t hal_pwm_us_to_ticks(uint32_t us);
275 
289 int hal_pwm_start(void *hdl, uint32_t period_ticks, uint32_t high_ticks);
290 
291 
301 int hal_pwm_stop(void *hdl);
302 
303 
313 int hal_pwm_pause(void *hdl);
314 
324 int hal_pwm_resume(void *hdl);
325 
326 
327 
339 int hal_pwm_update(void *hdl, uint32_t period_ticks, uint32_t high_ticks);
340 
351 int hal_pwm_update_high_duration(void *hdl, uint32_t high_ticks);
352 
353 
355 
356 #endif // HAL_PWM_H
357 
int hal_pwm_update_high_duration(void *hdl, uint32_t high_ticks)
Update PWM high time.
uint32_t hal_pwm_us_to_ticks(uint32_t us)
Convert us to PWM ticks.
int hal_pwm_close(void *hdl)
Close PWM driver.
int hal_pwm_resume(void *hdl)
Resume PWM.
int hal_pwm_update(void *hdl, uint32_t period_ticks, uint32_t high_ticks)
Update PWM period and high time.
int hal_pwm_stop(void *hdl)
Stop PWM.
int hal_pwm_pause(void *hdl)
Pause PWM. When PWM is paused, can&#39;t go to deep sleep.
void * hal_pwm_open(int id, int prio, void *arg, void(*callback)(void *))
Open PWM driver.
int hal_pwm_start(void *hdl, uint32_t period_ticks, uint32_t high_ticks)
Start PWM