1 /**
2   ******************************************************************************
3   * @file    lib_pwm.h
4   * @author  Application Team
5   * @version V4.4.0
6   * @date    2018-09-27
7   * @brief   PWM library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #ifndef __LIB_PWM_H
14 #define __LIB_PWM_H
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 #include "target.h"
21 
22 typedef struct
23 {
24   uint32_t ClockDivision;
25   uint32_t Mode;
26   uint32_t ClockSource;
27 } PWM_BaseInitType;
28 //ClockDivision
29 #define PWM_CLKDIV_2     PWM_CTL_ID_DIV2
30 #define PWM_CLKDIV_4     PWM_CTL_ID_DIV4
31 #define PWM_CLKDIV_8     PWM_CTL_ID_DIV8
32 #define PWM_CLKDIV_16    PWM_CTL_ID_DIV16
33 //Mode
34 #define PWM_MODE_STOP           PWM_CTL_MC_STOP
35 #define PWM_MODE_UPCOUNT        PWM_CTL_MC_UP
36 #define PWM_MODE_CONTINUOUS     PWM_CTL_MC_CONTINUE
37 #define PWM_MODE_UPDOWN         PWM_CTL_MC_UPDOWN
38 //ClockSource
39 #define PWM_CLKSRC_APB          PWM_CTL_TESL_APBDIV1
40 #define PWM_CLKSRC_APBD128      PWM_CTL_TESL_APBDIV128
41 
42 typedef struct
43 {
44   uint32_t Period;
45   uint32_t OutMode;
46 } PWM_OCInitType;
47 //OUTMOD
48 #define PWM_OUTMOD_CONST        PWM_CCTL_OUTMOD_CONST
49 #define PWM_OUTMOD_SET          PWM_CCTL_OUTMOD_SET
50 #define PWM_OUTMOD_TOGGLE_RESET PWM_CCTL_OUTMOD_TOGGLE_RESET
51 #define PWM_OUTMOD_SET_RESET    PWM_CCTL_OUTMOD_SET_RESET
52 #define PWM_OUTMOD_TOGGLE       PWM_CCTL_OUTMOD_TOGGLE
53 #define PWM_OUTMOD_RESET        PWM_CCTL_OUTMOD_RESET
54 #define PWM_OUTMOD_TOGGLE_SET   PWM_CCTL_OUTMOD_TOGGLE_SET
55 #define PWM_OUTMOD_RESET_SET    PWM_CCTL_OUTMOD_RESET_SET
56 
57 //PWM CHANNEL
58 #define PWM_CHANNEL_0           0
59 #define PWM_CHANNEL_1           1
60 #define PWM_CHANNEL_2           2
61 
62 #define PWM_OSEL0_T0O0 (0<<0)
63 #define PWM_OSEL0_T0O1 (1<<0)
64 #define PWM_OSEL0_T0O2 (2<<0)
65 #define PWM_OSEL0_T1O0 (4<<0)
66 #define PWM_OSEL0_T1O1 (5<<0)
67 #define PWM_OSEL0_T1O2 (6<<0)
68 #define PWM_OSEL0_T2O0 (8<<0)
69 #define PWM_OSEL0_T2O1 (9<<0)
70 #define PWM_OSEL0_T2O2 (10<<0)
71 #define PWM_OSEL0_T3O0 (12<<0)
72 #define PWM_OSEL0_T3O1 (13<<0)
73 #define PWM_OSEL0_T3O2 (14<<0)
74 //outline
75 #define PWM_OLINE_0       1
76 #define PWM_OLINE_1       2
77 #define PWM_OLINE_2       4
78 #define PWM_OLINE_3       8
79 #define PWM_OLINE_Msk     0xF
80 //PWM output selection
81 #define PWM0_OUT0        PWM_OSEL0_T0O0
82 #define PWM0_OUT1        PWM_OSEL0_T0O1
83 #define PWM0_OUT2        PWM_OSEL0_T0O2
84 #define PWM1_OUT0        PWM_OSEL0_T1O0
85 #define PWM1_OUT1        PWM_OSEL0_T1O1
86 #define PWM1_OUT2        PWM_OSEL0_T1O2
87 #define PWM2_OUT0        PWM_OSEL0_T2O0
88 #define PWM2_OUT1        PWM_OSEL0_T2O1
89 #define PWM2_OUT2        PWM_OSEL0_T2O2
90 #define PWM3_OUT0        PWM_OSEL0_T3O0
91 #define PWM3_OUT1        PWM_OSEL0_T3O1
92 #define PWM3_OUT2        PWM_OSEL0_T3O2
93 
94 //Level
95 #define PWM_LEVEL_HIGH  PWM_CCTL_OUT
96 #define PWM_LEVEL_LOW   0
97 
98 /* Private macros ------------------------------------------------------------*/
99 #define IS_PWM_CLKDIV(__CLKDIV__)  (((__CLKDIV__) == PWM_CLKDIV_2) ||\
100                                     ((__CLKDIV__) == PWM_CLKDIV_4) ||\
101                                     ((__CLKDIV__) == PWM_CLKDIV_8) ||\
102                                     ((__CLKDIV__) == PWM_CLKDIV_16))
103 
104 #define IS_PWM_CNTMODE(__CNTMODE__)  (((__CNTMODE__) == PWM_MODE_STOP)       ||\
105                                       ((__CNTMODE__) == PWM_MODE_UPCOUNT)    ||\
106                                       ((__CNTMODE__) == PWM_MODE_CONTINUOUS) ||\
107                                       ((__CNTMODE__) == PWM_MODE_UPDOWN))
108 
109 #define IS_PWM_CLKSRC(__CLKSRC__)  (((__CLKSRC__) == PWM_CLKSRC_APB) ||\
110                                     ((__CLKSRC__) == PWM_CLKSRC_APBD128))
111 
112 #define IS_PWM_OUTMODE(__OUTMODE__)  (((__OUTMODE__) == PWM_OUTMOD_CONST)        ||\
113                                       ((__OUTMODE__) == PWM_OUTMOD_SET)          ||\
114                                       ((__OUTMODE__) == PWM_OUTMOD_TOGGLE_RESET) ||\
115                                       ((__OUTMODE__) == PWM_OUTMOD_SET_RESET)    ||\
116                                       ((__OUTMODE__) == PWM_OUTMOD_TOGGLE)       ||\
117                                       ((__OUTMODE__) == PWM_OUTMOD_RESET)        ||\
118                                       ((__OUTMODE__) == PWM_OUTMOD_TOGGLE_SET)   ||\
119                                       ((__OUTMODE__) == PWM_OUTMOD_RESET_SET))
120 
121 #define IS_PWM_CCR(__CCR__)  ((__CCR__) < 0x10000U)
122 
123 #define IS_PWM_CHANNEL(__CHANNEL__)  (((__CHANNEL__) == PWM_CHANNEL_0) ||\
124                                       ((__CHANNEL__) == PWM_CHANNEL_1) ||\
125                                       ((__CHANNEL__) == PWM_CHANNEL_2))
126 
127 #define IS_PWM_OUTLINE(__OUTLINE__)  ((((__OUTLINE__) & PWM_OLINE_Msk) != 0U) &&\
128                                       (((__OUTLINE__) & ~PWM_OLINE_Msk) == 0U))
129 
130 #define IS_PWM_OUTSEL(__OUTSEL__)  (((__OUTSEL__) == PWM0_OUT0) ||\
131                                     ((__OUTSEL__) == PWM0_OUT1) ||\
132                                     ((__OUTSEL__) == PWM0_OUT2) ||\
133                                     ((__OUTSEL__) == PWM1_OUT0) ||\
134                                     ((__OUTSEL__) == PWM1_OUT1) ||\
135                                     ((__OUTSEL__) == PWM1_OUT2) ||\
136                                     ((__OUTSEL__) == PWM2_OUT0) ||\
137                                     ((__OUTSEL__) == PWM2_OUT1) ||\
138                                     ((__OUTSEL__) == PWM2_OUT2) ||\
139                                     ((__OUTSEL__) == PWM3_OUT0) ||\
140                                     ((__OUTSEL__) == PWM3_OUT1) ||\
141                                     ((__OUTSEL__) == PWM3_OUT2))
142 
143 #define IS_PWM_OUTLVL(__OUTLVL__)  (((__OUTLVL__) == PWM_LEVEL_HIGH) ||\
144                                     ((__OUTLVL__) == PWM_LEVEL_LOW))
145 
146 /* Exported Functions ------------------------------------------------------- */
147 /* PWM Exported Functions Group1:
148                                    Initialization ----------------------------*/
149 void PWM_BaseInit(PWM_TypeDef *PWMx, PWM_BaseInitType *InitStruct);
150 void PWM_BaseStructInit(PWM_BaseInitType *InitStruct);
151 void PWM_OC0Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
152 void PWM_OC1Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
153 void PWM_OC2Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
154 void PWM_OCStructInit(PWM_OCInitType *OCInitType);
155 /* PWM Exported Functions Group2:
156                                    Interrupt ---------------------------------*/
157 void PWM_BaseINTConfig(PWM_TypeDef *PWMx, uint32_t NewState);
158 uint8_t PWM_GetBaseINTStatus(PWM_TypeDef *PWMx);
159 void PWM_ClearBaseINTStatus(PWM_TypeDef *PWMx);
160 void PWM_ChannelINTConfig(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t NewState);
161 uint8_t PWM_GetChannelINTStatus(PWM_TypeDef *PWMx, uint32_t Channel);
162 void PWM_ClearChannelINTStatus(PWM_TypeDef *PWMx, uint32_t Channel);
163 /* PWM Exported Functions Group3:
164                                    MISC --------------------------------------*/
165 void PWM_ClearCounter(PWM_TypeDef *PWMx);
166 void PWM_CCRConfig(PWM_TypeDef *PWMx, uint32_t Channel, uint16_t Period);
167 //Compare output
168 void PWM_OLineConfig(uint32_t OutSelection, uint32_t OLine);
169 void PWM_OutputCmd(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t NewState);
170 void PWM_SetOutLevel(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t Level);
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif  /* __LIB_PWM_H */
177 
178 /*********************************** END OF FILE ******************************/
179