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