pwm.h
Go to the documentation of this file.
1 /********************************************************************************************************
2  * @file pwm.h
3  *
4  * @brief This is the header file for TLSR8258
5  *
6  * @author Driver Group
7  * @date July 26, 2019
8  *
9  * @par Copyright (c) 2018, Telink Semiconductor (Shanghai) Co., Ltd.
10  * All rights reserved.
11  *
12  * The information contained herein is confidential property of Telink
13  * Semiconductor (Shanghai) Co., Ltd. and is available under the terms
14  * of Commercial License Agreement between Telink Semiconductor (Shanghai)
15  * Co., Ltd. and the licensee or the terms described here-in. This heading
16  * MUST NOT be removed from this file.
17  *
18  * Licensees are granted free, non-transferable use of the information in this
19  * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
20  * @par History:
21  * 1.initial release(DEC. 26 2018)
22  *
23  * @version A001
24  *
25  *******************************************************************************************************/
26 #ifndef PWM_H_
27 #define PWM_H_
28 
29 #include "register.h"
30 #include "clock.h"
31 #include "timer.h"
32 
37 typedef enum {
38  PWM0_ID = 0,
44 }pwm_id;
45 
49 typedef enum{
52  PWM_IR_MODE = 0x03,
55 }pwm_mode;
56 
60 typedef enum{
69 }PWM_IRQ;
70 
71 typedef enum{
72  PWM0_PULSE_NORMAL = 0, // duty cycle and period from TCMP0/TMAX0 0x794~0x797
73  PWM0_PULSE_SHADOW = BIT(14), // duty cycle and period from TCMP0_SHADOW / TMAX0_SHADOW 0x7c4~0x7c7
75 
76 
83 static inline void pwm_set_mode(pwm_id id, pwm_mode mode){
84  if(PWM0_ID == id){
85  reg_pwm0_mode = mode; //only PWM0 has count/IR/fifo IR mode
86  }
87 }
88 
89 
96 static inline void pwm_set_clk(int system_clock_hz, int pwm_clk){
97  reg_pwm_clk = (int)system_clock_hz /pwm_clk - 1;
98 }
99 
106 static inline void pwm_set_cmp(pwm_id id, unsigned short cmp_tick)
107 {
108  reg_pwm_cmp(id) = cmp_tick;
109 }
110 
117 static inline void pwm_set_cycle(pwm_id id, unsigned short cycle_tick){
118  reg_pwm_max(id) = cycle_tick;
119 }
120 
128 static inline void pwm_set_cycle_and_duty(pwm_id id, unsigned short cycle_tick, unsigned short cmp_tick){
129  reg_pwm_cycle(id) = MASK_VAL(FLD_PWM_CMP, cmp_tick, FLD_PWM_MAX, cycle_tick);
130 }
131 
132 
139 static inline void pwm_set_pwm0_shadow_cycle_and_duty(unsigned short cycle_tick, unsigned short cmp_tick)
140 {
141  reg_pwm_tcmp0_shadow = cmp_tick;
142  reg_pwm_tmax0_shadow = cycle_tick;
143 }
144 
145 
152 static inline void pwm_set_phase(pwm_id id, unsigned short phase){
153  //PWM phase can not set in 825x now, do nothing here
154 }
155 
162 static inline void pwm_set_pulse_num(pwm_id id, unsigned short pulse_num){
163  if(PWM0_ID == id){
164  reg_pwm0_pulse_num = pulse_num;
165  }
166 
167 }
168 
174 static inline void pwm_start(pwm_id id)
175 {
176 
177  if(PWM0_ID == id)
178  {
180  }
181  else{
182  BM_SET(reg_pwm_enable, BIT(id));
183  }
184 }
185 
191 static inline void pwm_stop(pwm_id id)
192 {
193  if(PWM0_ID == id)
194  {
196  }
197  else{
198  BM_CLR(reg_pwm_enable, BIT(id));
199  }
200 }
201 
207 static inline void pwm_revert(pwm_id id)
208 {
209  reg_pwm_invert |= BIT(id);
210 }
211 
217 static inline void pwm_n_revert(pwm_id id)
218 {
219  reg_pwm_n_invert |= BIT(id);
220 }
221 
228 static inline void pwm_polo_enable(pwm_id id, int en)
229 {
230  if(en){
231  BM_SET(reg_pwm_pol, BIT(id));
232  }else{
233  BM_CLR(reg_pwm_pol, BIT(id));
234  }
235 }
236 
242 static inline void pwm_set_interrupt_enable(PWM_IRQ irq){
243  BM_SET(reg_pwm_irq_mask, irq);
244 }
245 static inline void pwm_set_interrupt_disable(PWM_IRQ irq){
246  BM_CLR(reg_pwm_irq_mask, irq);
247 }
248 
249 
255 static inline void pwm_clear_interrupt_status( PWM_IRQ irq)
256 {
257  reg_pwm_irq_sta = irq;
258 }
259 
266 static inline void pwm_ir_fifo_set_irq_trig_level(unsigned char trig_level)
267 {
268  reg_pwm_ir_fifo_irq_trig_level = trig_level;
269 }
276 static inline void pwm_ir_fifo_clr_data(void)
277 {
279 }
285 static inline unsigned char pwm_ir_fifo_get_data_num(void)
286 {
288 }
294 static inline unsigned char pwm_ir_fifo_is_empty(void)
295 {
297 }
298 
305 static inline unsigned char pwm_ir_fifo_is_full(void)
306 {
308 }
309 
318 static inline void pwm_ir_fifo_set_data_entry(unsigned short pulse_num, unsigned char use_shadow, unsigned char carrier_en)
319 {
320  static unsigned char index=0;
321  unsigned short cfg_data = pulse_num + ((use_shadow&BIT(0))<<14) + ((carrier_en&BIT(0))<<15);
322  while(pwm_ir_fifo_is_full());
323  reg_pwm_ir_fifo_dat(index) = cfg_data;
324  index++;
325  index&=0x01;
326 }
327 
335 static inline unsigned short pwm_config_dma_fifo_waveform(int carrier_en, Pwm0Pulse_SelectDef pulse_type, unsigned short pulse_num)
336 
337 {
338  return ( carrier_en<<15 | pulse_type | (pulse_num & 0x3fff) );
339 }
340 
346 static inline void pwm_set_dma_address(void * pdat)
347 {
348  reg_dma_pwm_addr = (unsigned short)((unsigned int)pdat);
349  reg_dma7_addrHi = 0x04;
351 }
352 
358 static inline void pwm_start_dma_ir_sending(void)
359 {
362 }
363 
370 static inline void pwm_stop_dma_ir_sending(void)
371 {
373  sleep_us(20); //1us <-> 4 byte
374  reg_rst0 = 0;
375 }
376 #endif
377  /* PWM_H_ */
378 
379  //end of GP8
Definition: pwm.h:62
static void pwm_polo_enable(pwm_id id, int en)
This fuction servers to enable the pwm polarity.
Definition: pwm.h:228
static void pwm_set_cmp(pwm_id id, unsigned short cmp_tick)
This fuction servers to set pwm count status(CMP) time.
Definition: pwm.h:106
#define reg_pwm_ir_fifo_dat(i)
Definition: register_8258.h:880
static void pwm_set_cycle_and_duty(pwm_id id, unsigned short cycle_tick, unsigned short cmp_tick)
This fuction servers to set pwm cycle time & count status.
Definition: pwm.h:128
static void pwm_set_interrupt_enable(PWM_IRQ irq)
This fuction servers to enable the pwm interrupt.
Definition: pwm.h:242
static void pwm_start(pwm_id id)
This fuction servers to start the pwm.
Definition: pwm.h:174
Definition: pwm.h:54
Definition: pwm.h:63
Definition: pwm.h:52
Definition: pwm.h:66
static void pwm_set_clk(int system_clock_hz, int pwm_clk)
This fuction servers to set pwm clock frequency.
Definition: pwm.h:96
Definition: pwm.h:65
#define reg_pwm_ir_fifo_irq_trig_level
Definition: register_8258.h:881
#define reg_pwm_ir_clr_fifo_data
Definition: register_8258.h:890
static unsigned char pwm_ir_fifo_get_data_num(void)
This fuction serves to get the number of data in fifo.
Definition: pwm.h:285
PWM_IRQ
Definition: pwm.h:60
Definition: pwm.h:41
static unsigned char pwm_ir_fifo_is_full(void)
This fuction serves to determine whether data in fifo is full.
Definition: pwm.h:305
#define reg_pwm_max(i)
Definition: register_8258.h:840
static void pwm_revert(pwm_id id)
This fuction servers to revert the PWMx.
Definition: pwm.h:207
#define reg_pwm0_enable
Definition: register_8258.h:828
#define reg_pwm_irq_sta
Definition: register_8258.h:849
Definition: pwm.h:51
Definition: register_8258.h:893
#define reg_dma_chn_en
Definition: register_8258.h:1163
#define reg_pwm_enable
Definition: register_8258.h:827
#define BM_SET(x, mask)
Definition: bsp.h:39
Definition: register_8258.h:1097
static unsigned char pwm_ir_fifo_is_empty(void)
This fuction serves to determine whether data in fifo is empty.
Definition: pwm.h:294
static void pwm_set_mode(pwm_id id, pwm_mode mode)
This fuction servers to set pwm mode.
Definition: pwm.h:83
Definition: pwm.h:64
Definition: register_8258.h:1179
static void pwm_ir_fifo_set_data_entry(unsigned short pulse_num, unsigned char use_shadow, unsigned char carrier_en)
This fuction serves to write data into FiFo.
Definition: pwm.h:318
static void pwm_stop_dma_ir_sending(void)
This fuction servers to stop the pwm&#39;s IRQ sending.
Definition: pwm.h:370
static void pwm_ir_fifo_set_irq_trig_level(unsigned char trig_level)
This fuction serves to set trigger level of interrupt for IR FiFo mode.
Definition: pwm.h:266
#define BIT(n)
Definition: bsp.h:33
static void pwm_set_pwm0_shadow_cycle_and_duty(unsigned short cycle_tick, unsigned short cmp_tick)
This fuction servers to set pwm cycle time & count status.
Definition: pwm.h:139
#define reg_pwm0_mode
Definition: register_8258.h:831
pwm_id
enum variable, the number of PWM channels supported
Definition: pwm.h:37
Definition: pwm.h:72
static void pwm_stop(pwm_id id)
This fuction servers to stop the pwm.
Definition: pwm.h:191
static unsigned short pwm_config_dma_fifo_waveform(int carrier_en, Pwm0Pulse_SelectDef pulse_type, unsigned short pulse_num)
This fuction serves to config the pwm&#39;s dma wave form.
Definition: pwm.h:335
Definition: pwm.h:43
#define reg_dma7_addrHi
Definition: register_8258.h:1214
Definition: pwm.h:53
Definition: register_8258.h:842
#define reg_pwm_clk
Definition: register_8258.h:829
Definition: pwm.h:73
Definition: register_8258.h:886
#define reg_dma_pwm_addr
Definition: register_8258.h:1157
Definition: register_8258.h:885
Definition: register_8258.h:843
Definition: pwm.h:39
#define reg_pwm_cmp(i)
Definition: register_8258.h:839
#define reg_dma_pwm_mode
Definition: register_8258.h:1159
#define reg_rst0
Definition: register_8258.h:143
static void pwm_clear_interrupt_status(PWM_IRQ irq)
This fuction servers to clear the pwm interrupt.
Definition: pwm.h:255
Definition: pwm.h:40
static void pwm_ir_fifo_clr_data(void)
This fuction serves to clear data in fifo. Only when pwm is in not active mode, it is possible to cle...
Definition: pwm.h:276
static void pwm_set_phase(pwm_id id, unsigned short phase)
This fuction servers to set the pwm phase.
Definition: pwm.h:152
pwm_mode
enum variable used for PWM work mode setting
Definition: pwm.h:49
Definition: pwm.h:42
#define reg_pwm_irq_mask
Definition: register_8258.h:848
#define reg_pwm_tcmp0_shadow
Definition: register_8258.h:877
static void pwm_set_dma_address(void *pdat)
This fuction servers to set the pwm&#39;s dma address.
Definition: pwm.h:346
static void pwm_set_cycle(pwm_id id, unsigned short cycle_tick)
This fuction servers to set pwm cycle time.
Definition: pwm.h:117
Definition: pwm.h:38
Definition: pwm.h:68
Definition: pwm.h:67
void sleep_us(unsigned long us)
This function performs to set sleep us.
#define reg_pwm_n_invert
Definition: register_8258.h:835
#define reg_pwm_pol
Definition: register_8258.h:836
#define reg_pwm_cycle(i)
Definition: register_8258.h:838
Definition: register_8258.h:149
#define BM_CLR(x, mask)
Definition: bsp.h:40
#define reg_pwm0_pulse_num
Definition: register_8258.h:846
#define reg_dma_tx_rdy0
Definition: register_8258.h:1165
static void pwm_set_interrupt_disable(PWM_IRQ irq)
Definition: pwm.h:245
Definition: pwm.h:50
static void pwm_set_pulse_num(pwm_id id, unsigned short pulse_num)
This fuction servers to set the pwm pulse number.
Definition: pwm.h:162
#define reg_pwm_tmax0_shadow
Definition: register_8258.h:878
#define reg_pwm_ir_fifo_data_status
Definition: register_8258.h:883
#define reg_pwm_invert
Definition: register_8258.h:834
Definition: pwm.h:61
static void pwm_start_dma_ir_sending(void)
This fuction servers to start the pwm&#39;s IRQ sending.
Definition: pwm.h:358
#define MASK_VAL(...)
Definition: bsp.h:94
Pwm0Pulse_SelectDef
Definition: pwm.h:71
static void pwm_n_revert(pwm_id id)
This fuction servers to revert the PWMx_N.
Definition: pwm.h:217
Definition: register_8258.h:887