aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hc32f460/driver/src/hc32f460_emb.c
blob: 5e50088e6f0a9c437a66118e9ce83b510e670daf (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 */
/******************************************************************************/
/** \file hc32f460_emb.c
 **
 ** A detailed description is available at
 ** @link EMBGroup EMB description @endlink
 **
 **   - 2018-11-24  CDT  First version for Device Driver Library of EMB.
 **
 ******************************************************************************/

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32f460_emb.h"
#include "hc32f460_utility.h"

/**
 *******************************************************************************
 ** \addtogroup EMBGroup
 ******************************************************************************/
//@{

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/*!< Parameter valid check for emb unit */
#define IS_VALID_EMB_UNIT(__EMBx__)                                            \
(   (M4_EMB1 == (__EMBx__))                     ||                             \
    (M4_EMB2 == (__EMBx__))                     ||                             \
    (M4_EMB3 == (__EMBx__))                     ||                             \
    (M4_EMB4 == (__EMBx__)))

/*!< Parameter valid check for emb status*/
#define IS_VALID_EMB_STATUS_TYPE(x)                                            \
(   (EMBFlagPortIn == (x))                      ||                             \
    (EMBFlagPWMSame == (x))                     ||                             \
    (EMBFlagCmp == (x))                         ||                             \
    (EMBFlagOSCFail == (x))                     ||                             \
    (EMBPortInState == (x))                     ||                             \
    (EMBPWMState == (x)))

/*!< Parameter valid check for emb status clear*/
#define IS_VALID_EMB_STATUS_CLR(x)                                             \
(   (EMBPortInFlagClr == (x))                   ||                             \
    (EMBPWMSameFlagCLr == (x))                  ||                             \
    (EMBCmpFlagClr == (x))                      ||                             \
    (EMBOSCFailFlagCLr == (x)))

/*!< Parameter valid check for emb irq enable*/
#define IS_VALID_EMB_IRQ(x)                                                    \
(   (PORTBrkIrq == (x))                         ||                             \
    (PWMSmBrkIrq == (x))                        ||                             \
    (CMPBrkIrq == (x))                          ||                             \
    (OSCFailBrkIrq == (x)))


/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

/*******************************************************************************
 * \brief   EMB interrupt request enable or disable
 *
 * \param   [in]  EMBx             EMB unit
 * \param   [in]  enEMBIrq         Irq type
 * \param   [in]  bEn              true/false
 *
 * \retval  en_result_t  Ok:  config success
 ******************************************************************************/
en_result_t EMB_ConfigIrq(M4_EMB_TypeDef *EMBx,
                                en_emb_irq_type_t enEMBIrq,
                                bool bEn)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (IS_VALID_EMB_UNIT(EMBx))
    {
        /* Check parameters */
        DDL_ASSERT(IS_VALID_EMB_IRQ(enEMBIrq));

        enRet = Ok;
        switch (enEMBIrq)
        {
            case PORTBrkIrq:
                EMBx->INTEN_f.PORTINTEN = (uint32_t)bEn;
                break;
            case PWMSmBrkIrq:
                EMBx->INTEN_f.PWMINTEN = (uint32_t)bEn;
                break;
            case CMPBrkIrq:
                EMBx->INTEN_f.CMPINTEN = (uint32_t)bEn;
                break;
            case OSCFailBrkIrq:
                EMBx->INTEN_f.OSINTEN = (uint32_t)bEn;
                break;
            default:
                enRet = ErrorInvalidParameter;
                break;
        }
    }

    return enRet;
}

/**
 *******************************************************************************
 ** \brief Get EMB status
 **
 ** \param [in] EMBx          EMB unit
 **
 ** \param [in] enStatus      EMB status type
 **
 ** \retval EMB status
 **
 ******************************************************************************/
bool EMB_GetStatus(M4_EMB_TypeDef *EMBx, en_emb_status_t enStatus)
{
    bool status = false;

    /* Check parameters */
    DDL_ASSERT(IS_VALID_EMB_UNIT(EMBx));
    DDL_ASSERT(IS_VALID_EMB_STATUS_TYPE(enStatus));

    switch (enStatus)
    {
        case EMBFlagPortIn:
            status = EMBx->STAT_f.PORTINF;
            break;
        case EMBFlagPWMSame:
            status = EMBx->STAT_f.PWMSF;
            break;
        case EMBFlagCmp:
            status = EMBx->STAT_f.CMPF;
            break;
        case EMBFlagOSCFail:
            status = EMBx->STAT_f.OSF;
            break;
        case EMBPortInState:
            status = EMBx->STAT_f.PORTINST;
            break;
        case EMBPWMState:
            status = EMBx->STAT_f.PWMST;
            break;
        default:
            break;
    }

    return status;
}

/**
 *******************************************************************************
 ** \brief  EMB clear status(Recover from protection state)
 **
 ** \param  [in] EMBx                EMB unit
 **
 ** \param  [in] enStatusClr         EMB status clear type
 **
 ** \retval en_result_t  Ok:         Config Success
 **
 ******************************************************************************/
en_result_t EMB_ClrStatus(M4_EMB_TypeDef *EMBx,
                                en_emb_status_clr_t enStatusClr)
{
    en_result_t enRet = ErrorInvalidParameter;

    if (IS_VALID_EMB_UNIT(EMBx))
    {
        /* Check parameters */
        DDL_ASSERT(IS_VALID_EMB_STATUS_CLR(enStatusClr));

        enRet = Ok;
        switch (enStatusClr)
        {
            case EMBPortInFlagClr:
                EMBx->STATCLR_f.PORTINFCLR = 1ul;
                break;
            case EMBPWMSameFlagCLr:
                EMBx->STATCLR_f.PWMSFCLR = 1ul;
                break;
            case EMBCmpFlagClr:
                EMBx->STATCLR_f.CMPFCLR = 1ul;
                break;
            case EMBOSCFailFlagCLr:
                EMBx->STATCLR_f.OSFCLR = 1ul;
                break;
            default:
                enRet = ErrorInvalidParameter;
                break;
        }
    }

    return enRet;
}

/*******************************************************************************
 * \brief   EMB Control Register(CR) for timer6
 *
 * \param  [in] EMBx               EMB unit
 * \param  [in] pstcEMBConfigCR    EMB Config CR pointer
 *
 * \retval  en_result_t    Ok:  Set successfully
 * \retval  en_result_t  ErrorInvalidParameter: Provided parameter is not valid
 ******************************************************************************/
en_result_t EMB_Config_CR_Timer6(const stc_emb_ctrl_timer6_t* pstcEMBConfigCR)
{
    uint32_t u32Val = 0ul;
    en_result_t enRet = ErrorInvalidParameter;

    if (NULL != pstcEMBConfigCR)
    {
        if (pstcEMBConfigCR->bEnPortBrake)
        {
            u32Val |= 1ul;
        }
        if (pstcEMBConfigCR->bEnCmp1Brake)
        {
            u32Val |= 1ul << 1;
        }
        if (pstcEMBConfigCR->bEnCmp2Brake)
        {
            u32Val |= 1ul << 2;
        }
        if (pstcEMBConfigCR->bEnCmp3Brake)
        {
            u32Val |= 1ul << 3;
        }
        if (pstcEMBConfigCR->bEnOSCFailBrake)
        {
            u32Val |= 1ul << 5;
        }
        if (pstcEMBConfigCR->bEnTimer61PWMSBrake)
        {
            u32Val |= 1ul << 6;
        }
        if (pstcEMBConfigCR->bEnTimer62PWMSBrake)
        {
            u32Val |= 1ul << 7;
        }
        if (pstcEMBConfigCR->bEnTimer63PWMSBrake)
        {
            u32Val |= 1ul << 8;
        }
        if (EMBPortFltDiv0 == pstcEMBConfigCR->enPortInFltClkSel)
        {
        }
        if (EMBPortFltDiv8 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 1ul << 28;
        }
        if (EMBPortFltDiv32 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 2ul << 28;
        }
        if (EMBPortFltDiv128 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 3ul << 28;
        }
        if (pstcEMBConfigCR->bEnPorInFlt)
        {
            u32Val |= 1ul << 30;
        }
        if (pstcEMBConfigCR->bEnPortInLevelSel_Low)
        {
            u32Val |= 1ul << 31;
        }

        M4_EMB1->CTL = u32Val;
        enRet = Ok;
    }

    return enRet;
}


/*******************************************************************************
 * \brief   EMB Control Register(CR) for timer4
 *
 * \param  [in] EMBx               EMB unit
 * \param  [in] pstcEMBConfigCR    EMB Config CR pointer
 *
 * \retval  en_result_t    Ok:  Set successfully
 * \retval  en_result_t  ErrorInvalidParameter: Provided parameter is not valid
 ******************************************************************************/
en_result_t EMB_Config_CR_Timer4(M4_EMB_TypeDef *EMBx,
                                const stc_emb_ctrl_timer4_t* pstcEMBConfigCR)
{
    uint32_t u32Val = 0ul;
    en_result_t enRet = ErrorInvalidParameter;

    if ((M4_EMB1 != EMBx)           && \
        (IS_VALID_EMB_UNIT(EMBx))   && \
        (NULL != pstcEMBConfigCR))
    {
        if (pstcEMBConfigCR->bEnPortBrake)
        {
            u32Val |= 1ul;
        }
        if (pstcEMBConfigCR->bEnCmp1Brake)
        {
            u32Val |= 1ul << 1;
        }
        if (pstcEMBConfigCR->bEnCmp2Brake)
        {
            u32Val |= 1ul << 2;
        }
        if (pstcEMBConfigCR->bEnCmp3Brake)
        {
            u32Val |= 1ul << 3;
        }
        if (pstcEMBConfigCR->bEnOSCFailBrake)
        {
            u32Val |= 1ul << 5;
        }
        if (pstcEMBConfigCR->bEnTimer4xWHLSammeBrake)
        {
            u32Val |= 1ul << 6;
        }
        if (pstcEMBConfigCR->bEnTimer4xVHLSammeBrake)
        {
            u32Val |= 1ul << 7;
        }
        if (pstcEMBConfigCR->bEnTimer4xUHLSammeBrake)
        {
            u32Val |= 1ul << 8;
        }
        if (EMBPortFltDiv0 == pstcEMBConfigCR->enPortInFltClkSel)
        {
        }
        if (EMBPortFltDiv8 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 1ul << 28;
        }
        if (EMBPortFltDiv32 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 2ul << 28;
        }
        if (EMBPortFltDiv128 == pstcEMBConfigCR->enPortInFltClkSel)
        {
            u32Val |= 3ul << 28;
        }
        if (pstcEMBConfigCR->bEnPorInFlt)
        {
            u32Val |= 1ul << 30;
        }
        if (pstcEMBConfigCR->bEnPortInLevelSel_Low)
        {
            u32Val |= 1ul << 31;
        }

        EMBx->CTL = u32Val;
        enRet = Ok;
    }

    return enRet;
}

/*******************************************************************************
 * \brief   EMB detect PWM atcive level (short detection) selection for timer6
 *
 * \param  [in] EMBx            EMB unit
 * \param  [in] pstcEMBPWMlv    EMB en detect active level pointer
 *
 * \retval  en_result_t    Ok:  Set successfully
 * \retval  en_result_t  ErrorInvalidParameter: Provided parameter is not valid
 ******************************************************************************/
en_result_t EMB_PWMLv_Timer6(const stc_emb_pwm_level_timer6_t* pstcEMBPWMlv)
{
    uint32_t u32Val = 0ul;
    en_result_t enRet = ErrorInvalidParameter;

    if (NULL != pstcEMBPWMlv)
    {
        if (pstcEMBPWMlv->bEnTimer61HighLevelDect)
        {
            u32Val |= 0x1ul;
        }
        if (pstcEMBPWMlv->bEnTimer62HighLevelDect)
        {
            u32Val |= 0x2ul;
        }
        if (pstcEMBPWMlv->bEnTimer63HighLevelDect)
        {
            u32Val |= 0x4ul;
        }

        M4_EMB1->PWMLV = u32Val;
        enRet = Ok;
    }

    return enRet;
}


/*******************************************************************************
 * \brief   EMB detect PWM atcive level (short detection) selection for timer4
 *
 * \param  [in] EMBx            EMB unit
 * \param  [in] pstcEMBPWMlv    EMB en detect active level pointer
 *
 * \retval  en_result_t    Ok:  Set successfully
 * \retval  en_result_t  ErrorInvalidParameter: Provided parameter is not valid
 ******************************************************************************/
en_result_t EMB_PWMLv_Timer4(M4_EMB_TypeDef *EMBx,
                                const stc_emb_pwm_level_timer4_t* pstcEMBPWMlv)
{
    uint32_t u32Val = 0ul;
    en_result_t enRet = ErrorInvalidParameter;

    if ((IS_VALID_EMB_UNIT(EMBx))   && \
        (M4_EMB1 != EMBx)           && \
        (NULL != pstcEMBPWMlv))
    {
        if (pstcEMBPWMlv->bEnWHLphaseHighLevelDect)
        {
            u32Val |= 0x1ul;
        }
        if (pstcEMBPWMlv->bEnVHLPhaseHighLevelDect)
        {
            u32Val |= 0x2ul;
        }
        if (pstcEMBPWMlv->bEnUHLPhaseHighLevelDect)
        {
            u32Val |= 0x4ul;
        }

        EMBx->PWMLV = u32Val;
        enRet = Ok;
    }

    return enRet;
}

/**
 *******************************************************************************
 ** \brief  EMB Software brake
 **
 ** \param  [in] EMBx        EMB unit
 **
** \param  [in] bEn          true: Software Brake Enable / false: Software Brake Disable
 **
 ** \retval en_result_t  Ok:         Config Success
 **
 ******************************************************************************/
en_result_t EMB_SwBrake(M4_EMB_TypeDef *EMBx, bool bEn)
{
    /* Check parameters */
    DDL_ASSERT(IS_VALID_EMB_UNIT(EMBx));

    EMBx->SOE_f.SOE = (uint32_t)bEn;

    return Ok;
}

//@} // EMBGroup

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/