summaryrefslogtreecommitdiff
path: root/bsp/lpc408x/Libraries/Drivers/source/lpc_gpio.c
blob: 84440f6977998b69ebdfd33a0a6cc0f6e6c23033 (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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/**********************************************************************
* $Id$      lpc_gpio.c          2011-06-02
*//**
* @file     lpc_gpio.c
* @brief    Contains all functions support for GPIO firmware library
*           on LPC
* @version  1.0
* @date     02. June. 2011
* @author   NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup GPIO
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _GPIO

/* Includes ------------------------------------------------------------------- */
#include "lpc_gpio.h"
#include "lpc_clkpwr.h"

/* definitions ------------------------------------------------------------------- */
#define  GPIO_IS_ENABLED(x)    (((x) != 0)? ENABLE:DISABLE)

/* Private Functions ---------------------------------------------------------- */

static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum);
static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum);
static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum);

/*********************************************************************//**
 * @brief       Get pointer to GPIO peripheral due to GPIO port
 * @param[in]   portNum     Port Number value, should be in range from 0 to 4.
 * @return      Pointer to GPIO peripheral
 **********************************************************************/
static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum)
{
    LPC_GPIO_TypeDef *pGPIO = NULL;

    switch (portNum)
    {
        case 0:
            pGPIO = LPC_GPIO0;
            break;

        case 1:
            pGPIO = LPC_GPIO1;
            break;

        case 2:
            pGPIO = LPC_GPIO2;
            break;

        case 3:
            pGPIO = LPC_GPIO3;
            break;

        case 4:
            pGPIO = LPC_GPIO4;
            break;

        case 5:
            pGPIO = LPC_GPIO5;
            break;

        default:
            break;
    }

    return pGPIO;
}

/*********************************************************************//**
 * @brief       Get pointer to FIO peripheral in halfword accessible style
 *              due to FIO port
 * @param[in]   portNum     Port Number value, should be in range from 0 to 4.
 * @return      Pointer to FIO peripheral
 **********************************************************************/
static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum)
{
    GPIO_HalfWord_TypeDef *pFIO = NULL;

    switch (portNum)
    {
        case 0:
            pFIO = GPIO0_HalfWord;
            break;

        case 1:
            pFIO = GPIO1_HalfWord;
            break;

        case 2:
            pFIO = GPIO2_HalfWord;
            break;

        case 3:
            pFIO = GPIO3_HalfWord;
            break;

        case 4:
            pFIO = GPIO4_HalfWord;
            break;

        case 5:
            pFIO = GPIO5_HalfWord;
            break;

        default:
            break;
    }

    return pFIO;
}

/*********************************************************************//**
 * @brief       Get pointer to FIO peripheral in byte accessible style
 *              due to FIO port
 * @param[in]   portNum     Port Number value, should be in range from 0 to 4.
 * @return      Pointer to FIO peripheral
 **********************************************************************/
static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum)
{
    GPIO_Byte_TypeDef *pFIO = NULL;

    switch (portNum)
    {
        case 0:
            pFIO = GPIO0_Byte;
            break;

        case 1:
            pFIO = GPIO1_Byte;
            break;

        case 2:
            pFIO = GPIO2_Byte;
            break;

        case 3:
            pFIO = GPIO3_Byte;
            break;

        case 4:
            pFIO = GPIO4_Byte;
            break;

        case 5:
            pFIO = GPIO5_Byte;
            break;

        default:
            break;
    }

    return pFIO;
}

/* End of Private Functions --------------------------------------------------- */


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup GPIO_Public_Functions
 * @{
 */


/* GPIO ------------------------------------------------------------------------------ */

/*********************************************************************//**
 * @brief       Initialize the GPIO component by power on the clock 
 *              supplied to it.
 * @return      None
 *
 * Note: 
 **********************************************************************/
void GPIO_Init(void)
{
    CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, ENABLE);

    return;
}


/*********************************************************************//**
 * @brief       De-initialize the GPIO component by power off the clock 
 *              supplied to it.
 * @return      None
 *
 * Note: 
 **********************************************************************/
void GPIO_Deinit(void)
{
    CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, DISABLE);

    return;
}


/*********************************************************************//**
 * @brief       Set Direction for GPIO port.
 * @param[in]   portNum     Port Number value, should be in range from 0 to 4
 * @param[in]   bitValue    Value that contains all bits to set direction,
 *                          in range from 0 to 0xFFFFFFFF.
 *                          example: value 0x5 to set direction for bit 0 and bit 1.
 * @param[in]   dir         Direction value, should be:
 *                          - 0: Input.
 *                          - 1: Output.
 * @return      None
 *
 * Note: All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void GPIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
{
    LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);

    if (pGPIO != NULL)
    {
        // Enable Output
        if (dir)
        {
            pGPIO->DIR |= bitValue;
        }
        // Enable Input
        else
        {
            pGPIO->DIR &= ~bitValue;
        }
    }
}


/*********************************************************************//**
 * @brief       Set Value for bits that have output direction on GPIO port.
 * @param[in]   portNum     Port number value, should be in range from 0 to 4
 * @param[in]   bitValue    Value that contains all bits on GPIO to set,
 *                          in range from 0 to 0xFFFFFFFF.
 *                          example: value 0x5 to set bit 0 and bit 1.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void GPIO_SetValue(uint8_t portNum, uint32_t bitValue)
{
    LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);

    if (pGPIO != NULL)
    {
        pGPIO->SET = bitValue;
    }
}

/*********************************************************************//**
 * @brief       Clear Value for bits that have output direction on GPIO port.
 * @param[in]   portNum     Port number value, should be in range from 0 to 4
 * @param[in]   bitValue    Value that contains all bits on GPIO to clear,
 *                          in range from 0 to 0xFFFFFFFF.
 *                          example: value 0x5 to clear bit 0 and bit 1.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void GPIO_ClearValue(uint8_t portNum, uint32_t bitValue)
{
    LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);

    if (pGPIO != NULL)
    {
        pGPIO->CLR = bitValue;
    }
}

/*********************************************************************//**
 * @brief       Output to the GPIO pin an expected value
 * @param[in]   portNum     Port number value, should be in range from 0 to 4
 * @param[in]   bitValue    Value that contains all bits on GPIO to clear,
 *                          in range from 0 to 0xFFFFFFFF.
 *                          example: value 0x5 to clear bit 0 and bit 1.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void GPIO_OutputValue(uint8_t portNum, uint32_t bitMask, uint8_t value)
{
    if (value == 0)
    {
        GPIO_ClearValue(portNum, bitMask);
    }
    else
    {
        GPIO_SetValue(portNum, bitMask);
    }

}


/*********************************************************************//**
 * @brief       Read Current state on port pin that have input direction of GPIO
 * @param[in]   portNum     Port number to read value, in range from 0 to 4
 * @return      Current value of GPIO port.
 *
 * Note: Return value contain state of each port pin (bit) on that GPIO regardless
 * its direction is input or output.
 **********************************************************************/
uint32_t GPIO_ReadValue(uint8_t portNum)
{
    LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);

    if (pGPIO != NULL)
    {
        return pGPIO->PIN;
    }

    return (0);
}

/*********************************************************************//**
 * @brief       Enable GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
 * @param[in]   portNum     Port number to read value, should be: 0 or 2
 * @param[in]   bitValue    Value that contains all bits on GPIO to enable,
 *                          in range from 0 to 0xFFFFFFFF.
 * @param[in]   edgeState   state of edge, should be:
 *                          - 0: Rising edge
 *                          - 1: Falling edge
 * @return      None
 **********************************************************************/
void GPIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
{
    if((portNum == 0)&&(edgeState == 0))
        LPC_GPIOINT->IO0IntEnR = bitValue;
    else if ((portNum == 2)&&(edgeState == 0))
        LPC_GPIOINT->IO2IntEnR = bitValue;
    else if ((portNum == 0)&&(edgeState == 1))
        LPC_GPIOINT->IO0IntEnF = bitValue;
    else if ((portNum == 2)&&(edgeState == 1))
        LPC_GPIOINT->IO2IntEnF = bitValue;
    else
        //Error
        while(1);
}

/*********************************************************************//**
 * @brief       Get GPIO Interrupt Status (just used for P0.0-P0.30, P2.0-P2.13)
 * @param[in]   portNum     Port number to read value, should be: 0 or 2
 * @param[in]   pinNum      Pin number, should be: 0..30(with port 0) and 0..13
 *                          (with port 2)
 * @param[in]   edgeState   state of edge, should be:
 *                          - 0: Rising edge
 *                          - 1: Falling edge
 * @return      Bool    could be:
 *                      - ENABLE: Interrupt has been generated due to a rising
 *                              edge on P0.0
 *                      - DISABLE: A rising edge has not been detected on P0.0
 **********************************************************************/
FunctionalState GPIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
{
    if((portNum == 0) && (edgeState == 0))//Rising Edge
        return GPIO_IS_ENABLED(((LPC_GPIOINT->IO0IntStatR)>>pinNum)& 0x1);
    else if ((portNum == 2) && (edgeState == 0))
        return GPIO_IS_ENABLED(((LPC_GPIOINT->IO2IntStatR)>>pinNum)& 0x1);
    else if ((portNum == 0) && (edgeState == 1))//Falling Edge
        return GPIO_IS_ENABLED(((LPC_GPIOINT->IO0IntStatF)>>pinNum)& 0x1);
    else if ((portNum == 2) && (edgeState == 1))
        return GPIO_IS_ENABLED(((LPC_GPIOINT->IO2IntStatF)>>pinNum)& 0x1);
    else
        //Error
        while(1);
}
/*********************************************************************//**
 * @brief       Clear GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
 * @param[in]   portNum     Port number to read value, should be: 0 or 2
 * @param[in]   bitValue    Value that contains all bits on GPIO to enable,
 *                          in range from 0 to 0xFFFFFFFF.
 * @return      None
 **********************************************************************/
void GPIO_ClearInt(uint8_t portNum, uint32_t bitValue)
{
    if(portNum == 0)
        LPC_GPIOINT->IO0IntClr = bitValue;
    else if (portNum == 2)
        LPC_GPIOINT->IO2IntClr = bitValue;
    else
        //Invalid portNum
        while(1);
}

/* FIO word accessible ----------------------------------------------------------------- */
/* Stub function for FIO (word-accessible) style */

/**
 * @brief The same with GPIO_SetDir()
 */
void FIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
{
    GPIO_SetDir(portNum, bitValue, dir);
}

/**
 * @brief The same with GPIO_SetValue()
 */
void FIO_SetValue(uint8_t portNum, uint32_t bitValue)
{
    GPIO_SetValue(portNum, bitValue);
}

/**
 * @brief The same with GPIO_ClearValue()
 */
void FIO_ClearValue(uint8_t portNum, uint32_t bitValue)
{
    GPIO_ClearValue(portNum, bitValue);
}

/**
 * @brief The same with GPIO_ReadValue()
 */
uint32_t FIO_ReadValue(uint8_t portNum)
{
    return (GPIO_ReadValue(portNum));
}

/**
 * @brief The same with GPIO_IntCmd()
 */
void FIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
{
    GPIO_IntCmd(portNum, bitValue, edgeState);
}

/**
 * @brief The same with GPIO_GetIntStatus()
 */
FunctionalState FIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
{
    return (GPIO_GetIntStatus(portNum, pinNum, edgeState));
}

/**
 * @brief The same with GPIO_ClearInt()
 */
void FIO_ClearInt(uint8_t portNum, uint32_t bitValue)
{
    GPIO_ClearInt(portNum, bitValue);
}
/*********************************************************************//**
 * @brief       Set mask value for bits in FIO port
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   bitValue    Value that contains all bits in to set,
 *                          in range from 0 to 0xFFFFFFFF.
 * @param[in]   maskValue   Mask value contains state value for each bit:
 *                          - 0: not mask.
 *                          - 1: mask.
 * @return      None
 *
 * Note:
 * - All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 * - After executing this function, in mask register, value '0' on each bit
 * enables an access to the corresponding physical pin via a read or write access,
 * while value '1' on bit (masked) that corresponding pin will not be changed
 * with write access and if read, will not be reflected in the updated pin.
 **********************************************************************/
void FIO_SetMask(uint8_t portNum, uint32_t bitValue, uint8_t maskValue)
{
    LPC_GPIO_TypeDef *pFIO = GPIO_GetPointer(portNum);

    if(pFIO != NULL)
    {
        // Mask
        if (maskValue)
        {
            pFIO->MASK |= bitValue;
        }
        // Un-mask
        else
        {
            pFIO->MASK &= ~bitValue;
        }
    }
}


/* FIO halfword accessible ------------------------------------------------------------- */

/*********************************************************************//**
 * @brief       Set direction for FIO port in halfword accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
 * @param[in]   bitValue    Value that contains all bits in to set direction,
 *                          in range from 0 to 0xFFFF.
 * @param[in]   dir         Direction value, should be:
 *                          - 0: Input.
 *                          - 1: Output.
 * @return      None
 *
 * Note: All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_HalfWordSetDir(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t dir)
{
    GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Output direction
        if (dir)
        {
            // Upper
            if(halfwordNum)
            {
                pFIO->FIODIRU |= bitValue;
            }
            // lower
            else
            {
                pFIO->FIODIRL |= bitValue;
            }
        }
        // Input direction
        else
        {
            // Upper
            if(halfwordNum)
            {
                pFIO->FIODIRU &= ~bitValue;
            }
            // lower
            else
            {
                pFIO->FIODIRL &= ~bitValue;
            }
        }
    }
}


/*********************************************************************//**
 * @brief       Set mask value for bits in FIO port in halfword accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
 * @param[in]   bitValue    Value that contains all bits in to set,
 *                          in range from 0 to 0xFFFF.
 * @param[in]   maskValue   Mask value contains state value for each bit:
 *                  - 0: not mask.
 *                  - 1: mask.
 * @return      None
 *
 * Note:
 * - All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 * - After executing this function, in mask register, value '0' on each bit
 * enables an access to the corresponding physical pin via a read or write access,
 * while value '1' on bit (masked) that corresponding pin will not be changed
 * with write access and if read, will not be reflected in the updated pin.
 **********************************************************************/
void FIO_HalfWordSetMask(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t maskValue)
{
    GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Mask
        if (maskValue)
        {
            // Upper
            if(halfwordNum)
            {
                pFIO->FIOMASKU |= bitValue;
            }
            // lower
            else
            {
                pFIO->FIOMASKL |= bitValue;
            }
        }
        // Un-mask
        else
        {
            // Upper
            if(halfwordNum)
            {
                pFIO->FIOMASKU &= ~bitValue;
            }
            // lower
            else
            {
                pFIO->FIOMASKL &= ~bitValue;
            }
        }
    }
}


/*********************************************************************//**
 * @brief       Set bits for FIO port in halfword accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
 * @param[in]   bitValue    Value that contains all bits in to set,
 *                          in range from 0 to 0xFFFF.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_HalfWordSetValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
{
    GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Upper
        if(halfwordNum)
        {
            pFIO->FIOSETU = bitValue;
        }
        // lower
        else
        {
            pFIO->FIOSETL = bitValue;
        }
    }
}


/*********************************************************************//**
 * @brief       Clear bits for FIO port in halfword accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
 * @param[in]   bitValue    Value that contains all bits in to clear,
 *                          in range from 0 to 0xFFFF.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_HalfWordClearValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
{
    GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Upper
        if(halfwordNum)
        {
            pFIO->FIOCLRU = bitValue;
        }
        // lower
        else
        {
            pFIO->FIOCLRL = bitValue;
        }
    }
}


/*********************************************************************//**
 * @brief       Read Current state on port pin that have input direction of GPIO
 *              in halfword accessible style.
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
 * @return      Current value of FIO port pin of specified halfword.
 * Note: Return value contain state of each port pin (bit) on that FIO regardless
 * its direction is input or output.
 **********************************************************************/
uint16_t FIO_HalfWordReadValue(uint8_t portNum, uint8_t halfwordNum)
{
    GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Upper
        if(halfwordNum)
        {
            return (pFIO->FIOPINU);
        }
        // lower
        else
        {
            return (pFIO->FIOPINL);
        }
    }

    return (0);
}


/* FIO Byte accessible ------------------------------------------------------------ */

/*********************************************************************//**
 * @brief       Set direction for FIO port in byte accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   byteNum     Byte part number, should be in range from 0 to 3
 * @param[in]   bitValue    Value that contains all bits in to set direction,
 *                          in range from 0 to 0xFF.
 * @param[in]   dir         Direction value, should be:
 *                          - 0: Input.
 *                          - 1: Output.
 * @return      None
 *
 * Note: All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_ByteSetDir(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t dir)
{
    GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Output direction
        if (dir)
        {
            if (byteNum <= 3)
            {
                pFIO->FIODIR[byteNum] |= bitValue;
            }
        }
        // Input direction
        else
        {
            if (byteNum <= 3)
            {
                pFIO->FIODIR[byteNum] &= ~bitValue;
            }
        }
    }
}

/*********************************************************************//**
 * @brief       Set mask value for bits in FIO port in byte accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   byteNum     Byte part number, should be in range from 0 to 3
 * @param[in]   bitValue    Value that contains all bits in to set mask,
 *                          in range from 0 to 0xFF.
 * @param[in]   maskValue   Mask value contains state value for each bit:
 *                          - 0: not mask.
 *                          - 1: mask.
 * @return      None
 *
 * Note:
 * - All remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 * - After executing this function, in mask register, value '0' on each bit
 * enables an access to the corresponding physical pin via a read or write access,
 * while value '1' on bit (masked) that corresponding pin will not be changed
 * with write access and if read, will not be reflected in the updated pin.
 **********************************************************************/
void FIO_ByteSetMask(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t maskValue)
{
    GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);

    if(pFIO != NULL)
    {
        // Mask
        if (maskValue)
        {
            if (byteNum <= 3)
            {
                pFIO->FIOMASK[byteNum] |= bitValue;
            }
        }
        // Un-mask
        else {
            if (byteNum <= 3)
            {
                pFIO->FIOMASK[byteNum] &= ~bitValue;
            }
        }
    }
}


/*********************************************************************//**
 * @brief       Set bits for FIO port in byte accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   byteNum     Byte part number, should be in range from 0 to 3
 * @param[in]   bitValue    Value that contains all bits in to set,
 *                          in range from 0 to 0xFF.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_ByteSetValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
{
    GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);

    if (pFIO != NULL) {
        if (byteNum <= 3)
        {
            pFIO->FIOSET[byteNum] = bitValue;
        }
    }
}


/*********************************************************************//**
 * @brief       Clear bits for FIO port in byte accessible style
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   byteNum     Byte part number, should be in range from 0 to 3
 * @param[in]   bitValue    Value that contains all bits in to clear,
 *                          in range from 0 to 0xFF.
 * @return      None
 *
 * Note:
 * - For all bits that has been set as input direction, this function will
 * not effect.
 * - For all remaining bits that are not activated in bitValue (value '0')
 * will not be effected by this function.
 **********************************************************************/
void FIO_ByteClearValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
{
    GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);

    if (pFIO != NULL)
    {
        if (byteNum <= 3)
        {
            pFIO->FIOCLR[byteNum] = bitValue;
        }
    }
}


/*********************************************************************//**
 * @brief       Read Current state on port pin that have input direction of GPIO
 *              in byte accessible style.
 * @param[in]   portNum     Port number, in range from 0 to 4
 * @param[in]   byteNum     Byte part number, should be in range from 0 to 3
 * @return      Current value of FIO port pin of specified byte part.
 * Note: Return value contain state of each port pin (bit) on that FIO regardless
 * its direction is input or output.
 **********************************************************************/
uint8_t FIO_ByteReadValue(uint8_t portNum, uint8_t byteNum)
{
    GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);

    if (pFIO != NULL)
    {
        if (byteNum <= 3)
        {
            return (pFIO->FIOPIN[byteNum]);
        }
    }
    return (0);
}

/**
 * @}
 */

#endif /* _GPIO */

/**
 * @}
 */

/* --------------------------------- End Of File ------------------------------ */