aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hc32f460/driver/src/hc32f460_gpio.c
blob: d9b31e5e9d6c7704a3e9992e4daa15d4f92d5631 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/******************************************************************************
 * 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_gpio.c
 **
 ** A detailed description is available at
 ** @link GpioGroup Gpio description @endlink
 **
 **   - 2018-10-12 CDT First version for Device Driver Library of Gpio.
 **
 ******************************************************************************/

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

/**
 *******************************************************************************
 ** \addtogroup GpioGroup
 ******************************************************************************/
//@{

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

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#define GPIO_BASE       (0x40053800ul)
#define PODR_BASE       (0x0004ul)
#define POER_BASE       (0x0006ul)
#define POSR_BASE       (0x0008ul)
#define PORR_BASE       (0x000Aul)
#define PCR_BASE        (0x0400ul)
#define PFSR_BASE       (0x0402ul)

/*! Parameter validity check for port group. */
#define IS_VALID_PORT(x)                                                        \
(   ((x) == PortA)                              ||                              \
    ((x) == PortB)                              ||                              \
    ((x) == PortC)                              ||                              \
    ((x) == PortD)                              ||                              \
    ((x) == PortE)                              ||                              \
    ((x) == PortH))

/*! Parameter validity check for pin. */
#define IS_VALID_PIN(x)                                                         \
(   ((x) == Pin00)                              ||                              \
    ((x) == Pin01)                              ||                              \
    ((x) == Pin02)                              ||                              \
    ((x) == Pin03)                              ||                              \
    ((x) == Pin04)                              ||                              \
    ((x) == Pin05)                              ||                              \
    ((x) == Pin06)                              ||                              \
    ((x) == Pin07)                              ||                              \
    ((x) == Pin08)                              ||                              \
    ((x) == Pin09)                              ||                              \
    ((x) == Pin10)                              ||                              \
    ((x) == Pin11)                              ||                              \
    ((x) == Pin12)                              ||                              \
    ((x) == Pin13)                              ||                              \
    ((x) == Pin14)                              ||                              \
    ((x) == Pin15))

/*! Parameter validity check for debug pins. */
#define IS_VALID_DEBUGPIN(x)                    ((x) <= 0x1Fu)

/*! Parameter validity check for pin mode. */
#define IS_VALID_PINMODE(x)                                                     \
(   ((x) == Pin_Mode_In)                        ||                              \
    ((x) == Pin_Mode_Out)                       ||                              \
    ((x) == Pin_Mode_Ana))

/*! Parameter validity check for pin drive capacity. */
#define IS_VALID_PINDRV(x)                                                      \
(   ((x) == Pin_Drv_L)                          ||                              \
    ((x) == Pin_Drv_M)                          ||                              \
    ((x) == Pin_Drv_H))

/*! Parameter validity check for pin output type. */
#define IS_VALID_PINTYPE(x)                                                     \
(   ((x) == Pin_OType_Cmos)                     ||                              \
    ((x) == Pin_OType_Od))

/*! Parameter validity check for pin read wait cycle. */
#define IS_VALID_READWAIT(x)                                                    \
(   ((x) == WaitCycle0)                         ||                              \
    ((x) == WaitCycle1)                         ||                              \
    ((x) == WaitCycle2)                         ||                              \
    ((x) == WaitCycle3))

/*! Parameter validity check for pin function */
#define IS_VALID_FUNC(x)                                                        \
(   ((x) == Func_Gpio)                          ||                              \
    (((x) >= Func_Fcmref)                       &&                              \
    ((x) <= Func_I2s))                          ||                              \
    ((x) == Func_Evnpt)                         ||                              \
    ((x) == Func_Eventout)                      ||                              \
    (((x) >= Func_Usart1_Tx)                    &&                              \
    ((x) <= Func_I2s2_Ck)))

/*! Parameter validity check for pin sub-function */
#define IS_VALID_SUBFUNC(x)                                                     \
(   ((x) == Func_Gpio)                          ||                              \
    ((x) == Func_Fcmref)                        ||                              \
    ((x) == Func_Rtcout)                        ||                              \
    ((x) == Func_Vcout)                         ||                              \
    ((x) == Func_Adtrg)                         ||                              \
    ((x) == Func_Mclkout)                       ||                              \
    ((x) == Func_Tim4)                          ||                              \
    ((x) == Func_Tim6)                          ||                              \
    ((x) == Func_Tima0)                         ||                              \
    ((x) == Func_Tima1)                         ||                              \
    ((x) == Func_Tima2)                         ||                              \
    ((x) == Func_Emb)                           ||                              \
    ((x) == Func_Usart_Ck)                      ||                              \
    ((x) == Func_Spi_Nss)                       ||                              \
    ((x) == Func_Qspi)                          ||                              \
    ((x) == Func_Key)                           ||                              \
    ((x) == Func_Sdio)                          ||                              \
    ((x) == Func_I2s)                           ||                              \
    ((x) == Func_UsbF)                          ||                              \
    ((x) == Func_Evnpt)                         ||                              \
    ((x) == Func_Eventout))

/*******************************************************************************
 * 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   Port init
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 ** \param   [in]  pstcPortInit         Structure pointer of port configuration
 **
 ** \retval  Ok                         Port initial successful
 **
 ******************************************************************************/
en_result_t PORT_Init(en_port_t enPort, uint16_t u16Pin, const stc_port_init_t *pstcPortInit)
{
    stc_port_pcr_field_t *PCRx;
    stc_port_pfsr_field_t * PFSRx;
    uint8_t u8PinPos = 0u;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    DDL_ASSERT(IS_VALID_PINMODE(pstcPortInit->enPinMode));
    DDL_ASSERT(IS_VALID_PINDRV(pstcPortInit->enPinDrv));
    DDL_ASSERT(IS_VALID_PINTYPE(pstcPortInit->enPinOType));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcPortInit->enLatch));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcPortInit->enExInt));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcPortInit->enInvert));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcPortInit->enPullUp));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(pstcPortInit->enPinSubFunc));

    PORT_Unlock();
    for (u8PinPos = 0u; u8PinPos < 16u; u8PinPos ++)
    {
        if (u16Pin & (1ul<<u8PinPos))
        {
            PCRx = (stc_port_pcr_field_t *)((uint32_t)(&M4_PORT->PCRA0) +      \
                                            enPort * 0x40ul + u8PinPos * 0x04ul);
            PFSRx = (stc_port_pfsr_field_t *)((uint32_t)(&M4_PORT->PFSRA0) +   \
                                              enPort * 0x40ul + u8PinPos * 0x04ul);

            /* Input latch function setting */
            PCRx->LTE = pstcPortInit->enLatch;

            /* External interrupt input enable setting */
            PCRx->INTE = pstcPortInit->enExInt;

            /* In_Out invert setting */
            PCRx->INVE = pstcPortInit->enInvert;

            /* Pin pull-up setting */
            PCRx->PUU = pstcPortInit->enPullUp;

            /* CMOS/OD output setting */
            PCRx->NOD = pstcPortInit->enPinOType;

            /* Pin drive mode setting */
            PCRx->DRV = pstcPortInit->enPinDrv;

            /* Pin mode setting */
            switch (pstcPortInit->enPinMode)
            {
                case Pin_Mode_In:
                    PCRx->DDIS  = 0u;
                    PCRx->POUTE = 0u;
                break;
                case Pin_Mode_Out:
                    PCRx->DDIS  = 0u;
                    PCRx->POUTE = 1u;
                break;
                case Pin_Mode_Ana:
                    PCRx->DDIS  = 1u;
                break;
                default:
                break;
            }
            /* Sub function enable setting */
            PFSRx->BFE = pstcPortInit->enPinSubFunc;
        }
    }
    PORT_Lock();
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Port de-init
 **
 ** \param   None
 **
 ** \retval  Ok                         GPIO de-initial successful
 **
 ******************************************************************************/
en_result_t PORT_DeInit(void)
{
    uint8_t u8PortIdx, u8PinIdx;
    PORT_Unlock();

    for (u8PortIdx = (uint8_t)PortA; u8PortIdx <= (uint8_t)PortH; u8PortIdx++)
    {
        *(uint16_t *)(GPIO_BASE + PODR_BASE + u8PortIdx * 0x10ul) = 0u;
        *(uint16_t *)(GPIO_BASE + POER_BASE + u8PortIdx * 0x10ul) = 0u;
        *(uint16_t *)(GPIO_BASE + POSR_BASE + u8PortIdx * 0x10ul) = 0u;
        *(uint16_t *)(GPIO_BASE + PORR_BASE + u8PortIdx * 0x10ul) = 0u;
        for (u8PinIdx = 0u; u8PinIdx < 16u; u8PinIdx++)
        {
            if (((uint8_t)PortH == u8PortIdx) && (3u == u8PinIdx))
            {
                break;
            }
            *(uint16_t *)(GPIO_BASE + PCR_BASE + u8PortIdx * 0x40ul + u8PinIdx * 0x4ul) = 0u;
            *(uint16_t *)(GPIO_BASE + PFSR_BASE + u8PortIdx * 0x40ul + u8PinIdx * 0x4ul) = 0u;
        }
    }
    M4_PORT->PCCR   = 0u;
    M4_PORT->PINAER = 0u;
    M4_PORT->PSPCR  = 0x1Fu;

    PORT_Lock();
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Special control register Setting
 **
 ** \param   [in]  u8DebugPort          Debug port setting register, This parameter
 **                                     can be any composed value of @ref en_debug_port_t
 **
 ** \param   [in] enFunc                The new state of the debug ports.
 ** \arg     Enable                     Enable.
 ** \arg     Disable                    Disable.
 **
 ** \retval  Ok                         Debug port set successful
 **
 ******************************************************************************/
en_result_t PORT_DebugPortSetting(uint8_t u8DebugPort, en_functional_state_t enFunc)
{
    /* parameter check */
    DDL_ASSERT(IS_VALID_DEBUGPIN(u8DebugPort));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enFunc));

    PORT_Unlock();

    if (Enable == enFunc)
    {
        M4_PORT->PSPCR |= (uint16_t)(u8DebugPort & 0x1Ful);
    }
    else
    {
        M4_PORT->PSPCR &= (uint16_t)(~(u8DebugPort & 0x1Ful));
    }

    PORT_Lock();
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Port Public Setting
 **
 ** \param   [in]  pstcPortPubSet       Structure pointer of public setting (PCCR)
 **
 ** \retval  Ok                         Port public register set successful
 **
 ******************************************************************************/
en_result_t PORT_PubSetting(const stc_port_pub_set_t *pstcPortPubSet)
{
    DDL_ASSERT(IS_VALID_FUNC(pstcPortPubSet->enSubFuncSel));
    DDL_ASSERT(IS_VALID_READWAIT(pstcPortPubSet->enReadWait));
    PORT_Unlock();

    /* PCCR setting */
    /* Sub function setting */
    M4_PORT->PCCR_f.BFSEL = pstcPortPubSet->enSubFuncSel;

    /* PIDRx, PCRxy read wait cycle setting */
    M4_PORT->PCCR_f.RDWT = pstcPortPubSet->enReadWait;

    PORT_Lock();
    return Ok;
}


/**
 *******************************************************************************
 ** \brief   PSPCR, PCCR, PINAER, PCRxy, PFSRxy write enable
 **
 ** \param   None
 **
 ** \retval  None
 **
 ******************************************************************************/
void PORT_Unlock(void)
{
    M4_PORT->PWPR = 0xA501u;
}

/**
 *******************************************************************************
 ** \brief  SPCR, PCCR, PINAER, PCRxy, PFSRxy write disable
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void PORT_Lock(void)
{
    M4_PORT->PWPR = 0xA500u;
}

/**
 *******************************************************************************
 ** \brief   Read Port value
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 **
 ** \retval  uint16_t                   The output port value
 **
 ******************************************************************************/
uint16_t PORT_GetData(en_port_t enPort)
{
    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    uint32_t *PIDRx;
    PIDRx = (uint32_t *)((uint32_t)(&M4_PORT->PIDRA) + 0x10u * enPort);
    return (uint16_t)(*PIDRx);
}

/**
 *******************************************************************************
 ** \brief   Read Pin value
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  enPin                GPIO pin index, This parameter can be
 **                                     any value of @ref en_pin_t
 ** \retval  en_flag_status_t           The output port pin value
 **
 ******************************************************************************/
en_flag_status_t PORT_GetBit(en_port_t enPort, en_pin_t enPin)
{
    uint32_t *PIDRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));
    DDL_ASSERT(IS_VALID_PIN(enPin));

    PIDRx = (uint32_t *)((uint32_t)(&M4_PORT->PIDRA) + 0x10u * enPort);
    return (en_flag_status_t)((bool)(!!(*PIDRx & (enPin))));
}

/**
 *******************************************************************************
 ** \brief   Set Port value
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 **
 ** \retval  Ok                         Data be set to corresponding port
 **
 ******************************************************************************/
en_result_t PORT_SetPortData(en_port_t enPort, uint16_t u16Pin)
{
    uint16_t *PODRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    PODRx = (uint16_t *)((uint32_t)(&M4_PORT->PODRA) + 0x10u * enPort);
    *PODRx |= u16Pin;
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Set Port value
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 **
 ** \retval  Ok                         Data be reset to corresponding port
 **
 ******************************************************************************/
en_result_t PORT_ResetPortData(en_port_t enPort, uint16_t u16Pin)
{
    uint16_t *PODRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    PODRx = (uint16_t *)((uint32_t)(&M4_PORT->PODRA) + 0x10u * enPort);
    *PODRx &= (uint16_t)(~u16Pin);
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Port Pin Output enable
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 ** \param   [in]  enNewState           The new state of pin direction setting
 ** \retval  Ok                         Set successful to corresponding port/pin
 **
 ******************************************************************************/
en_result_t PORT_OE(en_port_t enPort, uint16_t u16Pin, en_functional_state_t enNewState)
{
    uint16_t *POERx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    POERx = (uint16_t *)((uint32_t)(&M4_PORT->POERA) + 0x10ul * enPort);
    if (Enable == enNewState)
    {
        *POERx |= u16Pin;
    }
    else
    {
        *POERx &= (uint16_t)(~u16Pin);
    }
    return Ok;

}

/**
 *******************************************************************************
 ** \brief   Set Port Pin
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_SetBits(en_port_t enPort, uint16_t u16Pin)
{
    uint16_t *POSRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    POSRx = (uint16_t *)((uint32_t)(&M4_PORT->POSRA) + 0x10u * enPort);
    *POSRx |= u16Pin;
    return Ok;

}

/**
 *******************************************************************************
 ** \brief   Reset Port Pin
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_ResetBits(en_port_t enPort, uint16_t u16Pin)
{
    uint16_t *PORRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    PORRx = (uint16_t *)((uint32_t)(&M4_PORT->PORRA) + 0x10u * enPort);
    *PORRx |= u16Pin;
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Toggle Port Pin
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any composed value of @ref en_pin_t
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_Toggle(en_port_t enPort, uint16_t u16Pin)
{
    uint16_t *POTRx;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));

    POTRx = (uint16_t *)((uint32_t)(&M4_PORT->POTRA) + 0x10u * enPort);
    *POTRx |= u16Pin;
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Set port always ON
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in] enNewState            The new state of the port always ON function.
 ** \arg     Enable                     Enable.
 ** \arg     Disable                    Disable.
 **
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_AlwaysOn(en_port_t enPort, en_functional_state_t enNewState)
{
    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enNewState));

    PORT_Unlock();

    if (Enable == enNewState)
    {
        M4_PORT->PINAER |= Enable << (uint8_t)enPort;
    }
    else
    {
        M4_PORT->PINAER &= (uint16_t)(~(((1ul << (uint8_t)enPort)) & 0x1Ful));
    }

    PORT_Lock();
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Set Port Pin function
 **
 ** \param   [in]  enPort               GPIO port index, This parameter can be
 **                                     any value of @ref en_port_t
 ** \param   [in]  u16Pin               GPIO pin index, This parameter can be
 **                                     any value of @ref en_pin_t
 ** \param   [in]  enFuncSel            Function selection, This parameter can be
 **                                     any value of @ref en_port_func_t
 **
 ** \param   [in] enSubFunc             The new state of the gpio sub-function.
 ** \arg     Enable                     Enable.
 ** \arg     Disable                    Disable.
 **
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_SetFunc(en_port_t enPort, uint16_t u16Pin, en_port_func_t enFuncSel,  \
                  en_functional_state_t enSubFunc)
{
    stc_port_pfsr_field_t *PFSRx;
    uint8_t u8PinPos = 0u;

    /* parameter check */
    DDL_ASSERT(IS_VALID_PORT(enPort));
    DDL_ASSERT(IS_VALID_FUNC(enFuncSel));
    DDL_ASSERT(IS_FUNCTIONAL_STATE(enSubFunc));

    PORT_Unlock();

    for (u8PinPos = 0u; u8PinPos < 16u; u8PinPos ++)
    {
        if (u16Pin & (uint16_t)(1ul<<u8PinPos))
        {
            PFSRx = (stc_port_pfsr_field_t *)((uint32_t)(&M4_PORT->PFSRA0) \
                    + 0x40ul * enPort + 0x4ul * u8PinPos);

            /* main function setting */
            PFSRx->FSEL = enFuncSel;

            /* sub function enable setting */
            PFSRx->BFE = (Enable == enSubFunc ? Enable : Disable);
        }
    }

    PORT_Lock();
    return Ok;
}

/**
 *******************************************************************************
 ** \brief   Set global sub-function
 **
 ** \param   [in]  enFuncSel            Function selection, This parameter can be
 **                                     some values of @ref en_port_func_t, cannot
 **                                     large than 15u
 **
 ** \retval  Ok                         Set successful to corresponding pins
 **
 ******************************************************************************/
en_result_t PORT_SetSubFunc(en_port_func_t enFuncSel)
{
    /* parameter check */
    DDL_ASSERT(IS_VALID_SUBFUNC(enFuncSel));

    PORT_Unlock();

    M4_PORT->PCCR_f.BFSEL = enFuncSel;

    PORT_Lock();
    return Ok;
}

//@} // GpioGroup

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