1 /**************************************************************************//**
2  * @file     gpio.c
3  * @version  V3.00
4  * @brief    M460 series GPIO driver source file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 
10 #include "NuMicro.h"
11 
12 /** @addtogroup Standard_Driver Standard Driver
13   @{
14 */
15 
16 /** @addtogroup GPIO_Driver GPIO Driver
17   @{
18 */
19 
20 /** @addtogroup GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
21   @{
22 */
23 
24 /**
25  * @brief       Set GPIO operation mode
26  *
27  * @param[in]   port        GPIO port. It could be \ref PA, \ref PB, \ref PC, \ref PD, \ref PE, \ref PF, \ref PG, \ref PH, \ref PI or \ref PJ.
28  * @param[in]   u32PinMask  The single or multiple pins of specified GPIO port. \n
29  *                          It could be BIT0 ~ BIT15 for PA, PB, PE, PG and PH GPIO port. \n
30  *                          It could be BIT0 ~ BIT14 for PC and PD GPIO port. \n
31  *                          It could be BIT0 ~ BIT11 for PF GPIO port. \n
32  *                          It could be BIT6 ~ BIT15 for PI GPIO port. \n
33  *                          It could be BIT0 ~ BIT13 for PJ GPIO port.
34  * @param[in]   u32Mode     Operation mode. It could be
35  *                          - \ref GPIO_MODE_INPUT
36  *                          - \ref GPIO_MODE_OUTPUT
37  *                          - \ref GPIO_MODE_OPEN_DRAIN
38  *                          - \ref GPIO_MODE_QUASI
39  *
40  * @return      None
41  *
42  * @details     This function is used to set specified GPIO operation mode.
43  */
GPIO_SetMode(GPIO_T * port,uint32_t u32PinMask,uint32_t u32Mode)44 void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
45 {
46     uint32_t u32Idx;
47 
48     for(u32Idx = 0ul; u32Idx < GPIO_PIN_MAX; u32Idx++)
49     {
50         if((u32PinMask & (1ul << u32Idx)) == (1ul << u32Idx))
51         {
52             port->MODE = (port->MODE & ~(0x3ul << (u32Idx << 1))) | (u32Mode << (u32Idx << 1));
53         }
54     }
55 }
56 
57 /**
58  * @brief       Enable GPIO interrupt
59  *
60  * @param[in]   port            GPIO port. It could be \ref PA, \ref PB, \ref PC, \ref PD, \ref PE, \ref PF, \ref PG, \ref PH, \ref PI or \ref PJ.
61  * @param[in]   u32Pin          The pin of specified GPIO port. \n
62  *                              It could be 0 ~ 15 for PA, PB, PE, PG and PH GPIO port. \n
63  *                              It could be 0 ~ 14 for PC and PD GPIO port. \n
64  *                              It could be 0 ~ 11 for PF GPIO port. \n
65  *                              It could be 6 ~ 15 for PI GPIO port. \n
66  *                              It could be 0 ~ 13 for PJ GPIO port.
67  * @param[in]   u32IntAttribs   The interrupt attribute of specified GPIO pin. It could be
68  *                              - \ref GPIO_INT_RISING
69  *                              - \ref GPIO_INT_FALLING
70  *                              - \ref GPIO_INT_BOTH_EDGE
71  *                              - \ref GPIO_INT_HIGH
72  *                              - \ref GPIO_INT_LOW
73  *
74  * @return      None
75  *
76  * @details     This function is used to enable specified GPIO pin interrupt.
77  */
GPIO_EnableInt(GPIO_T * port,uint32_t u32Pin,uint32_t u32IntAttribs)78 void GPIO_EnableInt(GPIO_T *port, uint32_t u32Pin, uint32_t u32IntAttribs)
79 {
80     /* Configure interrupt mode of specified pin */
81     port->INTTYPE = (port->INTTYPE & ~(1ul << u32Pin)) | (((u32IntAttribs >> 24) & 0xFFUL) << u32Pin);
82 
83     /* Enable interrupt function of specified pin */
84     port->INTEN = (port->INTEN & ~(0x00010001ul << u32Pin)) | ((u32IntAttribs & 0xFFFFFFUL) << u32Pin);
85 }
86 
87 
88 /**
89  * @brief       Disable GPIO interrupt
90  *
91  * @param[in]   port        GPIO port. It could be \ref PA, \ref PB, \ref PC, \ref PD, \ref PE, \ref PF, \ref PG, \ref PH, \ref PI or \ref PJ.
92  * @param[in]   u32Pin      The pin of specified GPIO port. \n
93  *                          It could be 0 ~ 15 for PA, PB, PE, PG and PH GPIO port. \n
94  *                          It could be 0 ~ 14 for PC and PD GPIO port. \n
95  *                          It could be 0 ~ 11 for PF GPIO port. \n
96  *                          It could be 6 ~ 15 for PI GPIO port. \n
97  *                          It could be 0 ~ 13 for PJ GPIO port.
98  *
99  * @return      None
100  *
101  * @details     This function is used to disable specified GPIO pin interrupt.
102  */
GPIO_DisableInt(GPIO_T * port,uint32_t u32Pin)103 void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin)
104 {
105     /* Configure interrupt mode of specified pin */
106     port->INTTYPE &= ~(1UL << u32Pin);
107 
108     /* Disable interrupt function of specified pin */
109     port->INTEN &= ~((0x00010001UL) << u32Pin);
110 }
111 
112 /**
113  * @brief       Set GPIO slew rate control
114  *
115  * @param[in]   port        GPIO port. It could be \ref PA, \ref PB, \ref PC, \ref PD, \ref PE, \ref PF, \ref PG, \ref PH, \ref PI or \ref PJ.
116  * @param[in]   u32PinMask  The single or multiple pins of specified GPIO port. \n
117  *                          It could be BIT0 ~ BIT15 for PA, PB, PE, PG and PH GPIO port. \n
118  *                          It could be BIT0 ~ BIT14 for PC and PD GPIO port. \n
119  *                          It could be BIT0 ~ BIT11 for PF GPIO port. \n
120  *                          It could be BIT6 ~ BIT15 for PI GPIO port. \n
121  *                          It could be BIT0 ~ BIT13 for PJ GPIO port.
122  * @param[in]   u32Mode     Slew rate mode. It could be
123  *                          - \ref GPIO_SLEWCTL_NORMAL
124  *                          - \ref GPIO_SLEWCTL_HIGH
125  *                          - \ref GPIO_SLEWCTL_FAST
126  *
127  * @return      None
128  *
129  * @details     This function is used to set specified GPIO slew rate control.
130  */
GPIO_SetSlewCtl(GPIO_T * port,uint32_t u32PinMask,uint32_t u32Mode)131 void GPIO_SetSlewCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
132 {
133     uint32_t u32Idx;
134 
135     for(u32Idx = 0ul; u32Idx < GPIO_PIN_MAX; u32Idx++)
136     {
137         if(u32PinMask & (1ul << u32Idx))
138         {
139             port->SLEWCTL = (port->SLEWCTL & ~(0x3ul << (u32Idx << 1))) | (u32Mode << (u32Idx << 1));
140         }
141     }
142 }
143 
144 /**
145  * @brief       Set GPIO Pull-up and Pull-down control
146  *
147  * @param[in]   port        GPIO port. It could be \ref PA, \ref PB, \ref PC, \ref PD, \ref PE, \ref PF, \ref PG, \ref PH, \ref PI or \ref PJ.
148  * @param[in]   u32PinMask  The single or multiple pins of specified GPIO port. \n
149  *                          It could be BIT0 ~ BIT15 for PA, PB, PE, PG and PH GPIO port. \n
150  *                          It could be BIT0 ~ BIT14 for PC and PD GPIO port. \n
151  *                          It could be BIT0 ~ BIT11 for PF GPIO port. \n
152  *                          It could be BIT6 ~ BIT15 for PI GPIO port. \n
153  *                          It could be BIT0 ~ BIT13 for PJ GPIO port.
154  * @param[in]   u32Mode     The pin mode of specified GPIO pin. It could be
155  *                          - \ref GPIO_PUSEL_DISABLE
156  *                          - \ref GPIO_PUSEL_PULL_UP
157  *                          - \ref GPIO_PUSEL_PULL_DOWN
158  *
159  * @return      None
160  *
161  * @details     This function is used to set specified GPIO pull-up and pull-down control.
162  */
GPIO_SetPullCtl(GPIO_T * port,uint32_t u32PinMask,uint32_t u32Mode)163 void GPIO_SetPullCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
164 {
165     uint32_t u32Idx;
166 
167     for(u32Idx = 0ul; u32Idx < GPIO_PIN_MAX; u32Idx++)
168     {
169         if(u32PinMask & (1ul << u32Idx))
170         {
171             port->PUSEL = (port->PUSEL & ~(0x3ul << (u32Idx << 1))) | (u32Mode << (u32Idx << 1));
172         }
173     }
174 }
175 
176 /**@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */
177 
178 /*@}*/ /* end of group GPIO_Driver */
179 
180 /*@}*/ /* end of group Standard_Driver */
181