WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**************************************************************************//**
 * @file     PWM.c
 * @version  V1.00
 * $Revision: 3 $
 * $Date: 19/11/18 19:41 $
 * @brief    Panchip series PWM driver source file
 *
 * @note
 * Copyright (C) 2016 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
 
#include "PanSeries.h"
#include "pan_pwm.h"
#include "pan_clk.h"
 
/** @addtogroup Panchip_Device_Driver Panchip Device Driver
  @{
*/
 
/** @addtogroup Panchip_PWM_Driver PWM Driver
  @{
*/
 
 
/** @addtogroup Panchip_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  @{
*/
 
/**
 * @brief This function config PWM generator and get the nearest frequency in edge aligned auto-reload mode
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~7
 * @param[in] u32Frequency Target generator frequency
 * @param[in] u32DutyCycle Target generator duty cycle percentage. Valid range are between 0 ~ 100. 10 means 10%, 20 means 20%...
 * @param[in] OperateType  Target operation type.Valid value Edge-aligned,Center-Aligned,Precise Center-Aligned 
 * @return Nearest frequency clock in nano second
 * @note Since every two channels, (0 & 1), (2 & 3), (4 & 5),  (6 & 7) shares a prescaler. Call this API to configure PWM frequency may affect
 *       existing frequency of other channel.
 */
uint32_t PWM_ConfigOutputChannel (PWM_T *pwm,
                                  uint32_t u32ChannelNum,
                                  uint32_t u32Frequency,
                                  uint32_t u32DutyCycle,
                                                                    PWM_OperateTypeDef OperateType)
{
    uint32_t i = SystemCoreClock / u32Frequency;
    uint8_t u8Divider = 1, u8Prescale = 0xFF;
    uint16_t u16period = 0xFFFF;
    // uint32_t u32Hclk = SystemCoreClock;
    uint32_t u32clk;
 
    // Judge clock source
    if (!(CLK->APB1_CLK_CTRL & (0x1ul << ((u32ChannelNum / 2) + CLK_APB1CLK_PWM01_CLK_SEL_Pos))))
    {
        u32clk = CLK_GetPCLK1Freq();
    }
    else
    {
        if (CLK->CLK_TOP_CTRL_3V & CLK_TOPCTL_32K_CLK_SEL_Msk_3v)
        {
            u32clk = FREQ_XTL;
        }
        else
        {
            u32clk = FREQ_RCL;
        }
    }
    for (; u8Divider < 17; u8Divider <<= 1)
    {   // clk divider could only be 1, 2, 4, 8, 16
        /*u32Frequency = SystemCoreClock /((u8Prescale+1)*(u8Divider))/( PERIODn+1) ==>
        (u8Prescale+1)*(PERIODn+1) = SystemCoreClock/u32Frequency/u8Divider                    */
        i = (u32clk / u32Frequency) / u8Divider;
 
        /* If target value is larger than (u16period+1)*(u8Prescale+1), need to use a larger divider*/
        if (i > (0x10000 * 0x100))
            continue;
 
        /* u16period = 0xFFFF + 1, get a prescaler that u16period value is below 0xFFFF
                 Must first determine a variable value, now we choose u16period*/
        if ((pwm->CLKPSC >> ((u32ChannelNum >> 1) * 8) & 0xff) > 0)
        {
            u8Prescale = (pwm->CLKPSC >> ((u32ChannelNum >> 1) * 8) & 0xff) + 1;
        }
        else
        {
            u8Prescale = (i + 0xFFFF) / 0x10000;
        }
        /*  u8Prescale must at least be 2, otherwise the output stop*/
        if (u8Prescale < 3)
            u8Prescale = 2;
 
        /*  (PERIODn+1) = i / u8Prescale */
        i /= u8Prescale;
 
        /*u16period must less than  0xFFFF*/
        if (i <= 0x10000)
        {
            if (i == 1)
            {
                u16period = 1; // Too fast, and PWM cannot generate expected frequency...
            }
            else
            {
                if (OperateType == OPERATION_EDGE_ALIGNED)
                    u16period = i;
                else if (OperateType == OPERATION_CENTER_ALIGNED)
                    u16period = i / 2;
                else if (OperateType == OPERATION_PRECISE_CENTER_ALIGNED)
                    u16period = i;
            }
            break;
        }
    }
    /*  Store return value here 'cos we're gonna change u8Divider & u8Prescale & u16period to the real value to fill into register*/
    i = u32clk / (u8Prescale * u8Divider * u16period);
 
    u8Prescale -= 1;
        if(OperateType == OPERATION_PRECISE_CENTER_ALIGNED)
            u16period = u16period;
        else
            u16period -= 1;
    
    /* convert to real register value*/
    if(u8Divider == 1)
        u8Divider = 4;
    else if (u8Divider == 2)
        u8Divider = 0;
    else if (u8Divider == 4)
        u8Divider = 1;
    else if (u8Divider == 8)
        u8Divider = 2;
    else // 16
        u8Divider = 3;
 
    /* every two channels share a prescaler*/
    pwm->CLKPSC = (pwm->CLKPSC & ~(PWM_CLKPSC_CLKPSC01_Msk << ((u32ChannelNum >> 1) * 8))) | (u8Prescale << ((u32ChannelNum >> 1) * 8));
    pwm->CLKDIV = (pwm->CLKDIV & ~(PWM_CLKDIV_CLKDIV0_Msk << (4 * u32ChannelNum))) | (u8Divider << (4 * u32ChannelNum));
        /*set cnt mode as auto reload*/
        /* PWM_SetCntMode(pwm,u32ChannelNum,PWM_CNTMODE_AUTO_RELOAD); */
 
        /*set cnt type as edge align*/
        PWM_SetAlignedType(pwm, u32ChannelNum, PWM_EDGE_ALIGNED);
    
        if((OperateType == OPERATION_CENTER_ALIGNED)||(OperateType == OPERATION_PRECISE_CENTER_ALIGNED)){
            /*set cnt type as center align*/
            PWM_SetAlignedType(pwm, u32ChannelNum, PWM_CENTER_ALIGNED);
            if(OperateType == OPERATION_PRECISE_CENTER_ALIGNED)
                PWM_EnablePCA(pwm); //Precise center-aligned type Enabled.
            else
                PWM_DisablePCA(pwm);
        }
            
        /*update compare data register, Duty ratio = (CMPn+1)/(PERIODn+1).*/
    if(u32DutyCycle == 0){
        *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMPDAT0)) + u32ChannelNum * 4))) = 0;
    }else{
            if(OperateType == OPERATION_EDGE_ALIGNED){
                /*set cnt type as edge align compare data*/
                *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMPDAT0)) + u32ChannelNum * 4))) = u32DutyCycle * (u16period + 1) / 100 - 1;
            }
            else if(OperateType == OPERATION_CENTER_ALIGNED){
                /*set cnt type as center align compare data*/
                *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMPDAT0)) + u32ChannelNum * 4))) = u16period-(u32DutyCycle * (u16period + 1) / 100) ;    
            }
            else if(OperateType == OPERATION_PRECISE_CENTER_ALIGNED){
                /*set cnt type as precise center align compare data*/
                *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMPDAT0)) + u32ChannelNum * 4))) = (u16period-(u16period*u32DutyCycle/100))/2-1 ;
            }
        }
        /*update Counter data register*/
    *((__IO uint32_t *)((((uint32_t) & ((pwm)->PERIOD0)) + (u32ChannelNum) * 4))) = u16period;
 
    return(i);
}
 
/**
 * @brief This function configures PWM generator to set period/duty cycles and output level mode.
 * @param[in] pwm               The base address of PWM module
 * @param[in] u32ChannelNum     PWM channel number. Valid values are between 0~7
 * @param[in] u16PeriodCycle    Target period cycles to set
 * @param[in] u16PulseCycle     Target pulse (high level) cycles to set
 * @note IF u16PeriodCycle = 0 OR u16PulseCycle = 0, THEN the output is always low;
 *       ELSE IF u16PeriodCycle < u16PulseCycle, THEN the output is always high;
 *       ELSE the final output PWM waveform meets the following formulas:
 *          Actual period cycles        = u16PeriodCycle + 1
 *          Actual high-level cycles    = u16PulseCycle + 1
 *          Actual low-level cycles     = u16PeriodCycle - u16PulseCycle
 *          Actual duty cycle           = (u16PulseCycle + 1) / (u16PeriodCycle + 1)
 * @return None
 */
void PWM_SetPeriodAndDuty(PWM_T *pwm, uint32_t u32ChannelNum, uint16_t u16PeriodCycle, uint16_t u16PulseCycle)
{
    PWM_SetCNR(pwm, u32ChannelNum, u16PeriodCycle);
    PWM_SetCMR(pwm, u32ChannelNum, u16PulseCycle);
}
 
/**
 * @brief This function start PWM module
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
 *                           Bit 0 is channel 0, bit 1 is channel 1...
 * @return None
 */
void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask)
{
    uint32_t u32Mask = 0, i;
    for(i = 0; i < PWM_CHANNEL_NUM; i ++) {
        if(u32ChannelMask & (1 << i)) {
            u32Mask |= (PWM_CTL_CNTEN0_Msk << (i * 4));
        }
    }
    pwm->CTL |= u32Mask;
}
 
/**
 * @brief This function stop PWM module
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
 *                           Bit 0 is channel 0, bit 1 is channel 1...
 * @return None
 */
void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask)
{
    uint32_t i;
    for(i = 0; i < PWM_CHANNEL_NUM; i ++) {
        if(u32ChannelMask & (1 << i)) {
            *((__IO uint32_t *)((((uint32_t) & ((pwm)->PERIOD0)) + (i) * 4))) = 0;
        }
    }
}
 
/**
 * @brief This function stop PWM generation immediately by clear channel enable bit
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
 *                           Bit 0 is channel 0, bit 1 is channel 1...
 * @return None
 */
void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask)
{
    uint32_t u32Mask = 0, i;
    for(i = 0; i < PWM_CHANNEL_NUM; i ++) {
        if(u32ChannelMask & (1 << i)) {
            u32Mask |= (PWM_CTL_CNTEN0_Msk << (i * 4));
        }
    }
    pwm->CTL &= ~u32Mask;
}
 
/**
 * @brief This function enable output inverter of specified channel(s)
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
 *                           Bit 0 represents channel 0, bit 1 represents channel 1...
 * @return None
 * \hideinitializer
 */
void PWM_EnableOutputInverter(PWM_T *pwm, uint32_t u32ChannelMask)
{
    //pwm->CTL &= ~(PWM_CTL_PINV0_Msk|PWM_CTL_PINV1_Msk|PWM_CTL_PINV2_Msk|PWM_CTL_PINV3_Msk|PWM_CTL_PINV4_Msk|PWM_CTL_PINV5_Msk|PWM_CTL_PINV6_Msk|PWM_CTL_PINV7_Msk);
    uint32_t tmpCtlReg = pwm->CTL;
    for(size_t i = 0; i < 8; i++)
    {
        if((u32ChannelMask) & (1 << i))
            tmpCtlReg |= (1 << (PWM_CTL_PINV0_Pos + (i * 4)));
    }
    pwm->CTL = tmpCtlReg;
}
 
/**
 * @brief This function disable output inverter of specified channel(s)
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelMask Combination of channels to disable. Each bit corresponds to a channel
 *                           Bit 0 represents channel 0, bit 1 represents channel 1...
 * @return None
 * \hideinitializer
 */
void PWM_DisableOutputInverter(PWM_T *pwm, uint32_t u32ChannelMask)
{
    uint32_t tmpCtlReg = pwm->CTL;
    for(size_t i = 0; i < 8; i++)
    {
        if((u32ChannelMask) & (1 << i))
            tmpCtlReg &= ~(1 << (PWM_CTL_PINV0_Pos + (i * 4)));
    }
    pwm->CTL = tmpCtlReg;
}
 
#if 0
/**
 * @brief This function enable selected channel to trigger ADC
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~7
 * @param[in] u32Condition The condition to trigger ADC. Combination of following conditions:
 *                  - \ref PWM_TRIGGER_ADC_CNTR_IS_0
 *                  - \ref PWM_TRIGGER_ADC_CNTR_IS_CMR_D
 *                  - \ref PWM_TRIGGER_ADC_CNTR_IS_CNR
 *                  - \ref PWM_TRIGGER_ADC_CNTR_IS_CMR_U
 * @return None
 */
void PWM_EnableADCTrigger (PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition)
{
    if(u32ChannelNum < 4) {
        pwm->ADCTCTL0 = (pwm->ADCTCTL0 & ~((PWM_TRIGGER_ADC_CNTR_IS_0 |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_D |
                                            PWM_TRIGGER_ADC_CNTR_IS_CNR |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_U ) << (8 * u32ChannelNum))) | (u32Condition << (8 * u32ChannelNum));
    } else {
        pwm->ADCTCTL1 = (pwm->ADCTCTL1 & ~((PWM_TRIGGER_ADC_CNTR_IS_0 |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_D |
                                            PWM_TRIGGER_ADC_CNTR_IS_CNR |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_U ) << (8 * (u32ChannelNum - 4)))) | (u32Condition << (8 * (u32ChannelNum - 4)));
    }
}
 
/**
 * @brief This function disable selected channel to trigger ADC
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~7
 * @return None
 */
void PWM_DisableADCTrigger (PWM_T *pwm, uint32_t u32ChannelNum)
{
    if(u32ChannelNum < 4) {
        pwm->ADCTCTL0 = (pwm->ADCTCTL0 & ~((PWM_TRIGGER_ADC_CNTR_IS_0 |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_D |
                                            PWM_TRIGGER_ADC_CNTR_IS_CNR |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_U ) << (8 * u32ChannelNum)));
    } else {
        pwm->ADCTCTL1 = (pwm->ADCTCTL1 & ~((PWM_TRIGGER_ADC_CNTR_IS_0 |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_D |
                                            PWM_TRIGGER_ADC_CNTR_IS_CNR |
                                            PWM_TRIGGER_ADC_CNTR_IS_CMR_U ) << (8 * (u32ChannelNum - 4))));
    }
}
 
/**
 * @brief This function clear selected channel trigger ADC flag
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~7
 * @param[in] u32Condition PWM triggered ADC flag to be cleared.
 * @return None
 */
void PWM_ClearADCTriggerFlag (PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition)
{
    if(u32ChannelNum < 4) {
        pwm->ADCTSTS0 |= (u32Condition << (8 * u32ChannelNum));
    } else {
        pwm->ADCTSTS1 |= (u32Condition << (8 * (u32ChannelNum - 4)));
    }
}
 
/**
 * @brief This function get selected channel trigger ADC flag
 * @param[in] pwm The base address of PWM module
 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~7
 * @param[in] u32Condition PWM triggered ADC flag to be selected.
 * @return Get status of the selected channel trigger ADC
 */
uint32_t PWM_GetADCTriggerFlag (PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition)
{
    if(u32ChannelNum < 4) {
        return(pwm->ADCTSTS0 & (u32Condition << (8 * u32ChannelNum)) ? 1 : 0);
    } else {
        return(pwm->ADCTSTS1 & (u32Condition << (8 * (u32ChannelNum - 4))) ? 1 : 0);
    }
}
#endif
 
 
/*@}*/ /* end of group Panchip_PWM_EXPORTED_FUNCTIONS */
 
/*@}*/ /* end of group Panchip_PWM_Driver */
 
/*@}*/ /* end of group Panchip_Device_Driver */
 
/*** (C) COPYRIGHT 2016 Panchip Technology Corp. ***/