1 /**************************************************************************//**
2 * @file gpio.c
3 * @version V3.00
4 * @brief M2351 series General Purpose I/O (GPIO) driver source file
5 *
6 * @copyright SPDX-License-Identifier: Apache-2.0
7 * @copyright Copyright (C) 2016-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 ~ 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 ~ 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 * @param[in] port GPIO port. It could be PA, PB, PC, PD, PE, PF, PG or PH.
94 * @param[in] u32Pin The pin of specified GPIO port. \n
95 * It could be 0 ~ 15 for PA, PB and PE. \n
96 * It could be 0 ~ 13 for PC GPIO port. \n
97 * It could be 0 ~ 14 for PD GPIO port. \n
98 * It could be 0 ~ 11 for PF GPIO port. \n
99 * It could be 2 ~ 4, 9 ~ 15 for PG GPIO port. \n
100 * It could be 4 ~ 11 for PH GPIO port.
101 *
102 * @return None
103 *
104 * @details This function is used to enable specified GPIO pin interrupt.
105 */
GPIO_DisableInt(GPIO_T * port,uint32_t u32Pin)106 void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin)
107 {
108 /* Configure interrupt mode of specified pin */
109 port->INTTYPE &= ~(1UL << u32Pin);
110
111 /* Disable interrupt function of specified pin */
112 port->INTEN &= ~((0x00010001UL) << u32Pin);
113 }
114
115 /**
116 * @brief Set GPIO slew rate control
117 *
118 * @param[in] port GPIO port. It could be PA, PB, PC, PD, PE, PF, PG or PH.
119 * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n
120 * It could be BIT0 ~ BIT15 for PA, PB and PE. \n
121 * It could be BIT0 ~ BIT13 for PC. \n
122 * It could be BIT0 ~ BIT14 for PD. \n
123 * It could be BIT0 ~ BIT11 for PF. \n
124 * It could be BIT2 ~ BIT3, BIT9 ~ BIT15 for PG. \n
125 * It could be BIT4 ~ BIT11 for PH.
126 * @param[in] u32Mode Slew rate mode. It could be
127 * - \ref GPIO_SLEWCTL_NORMAL (maximum 40 MHz at 2.7V)
128 * - \ref GPIO_SLEWCTL_HIGH (maximum 80 MHz at 2.7V)
129 * - \ref GPIO_SLEWCTL_FAST (maximum 100 MHz at 2.7V)
130 *
131 * @return None
132 *
133 * @details This function is used to set specified GPIO operation mode.
134 */
GPIO_SetSlewCtl(GPIO_T * port,uint32_t u32PinMask,uint32_t u32Mode)135 void GPIO_SetSlewCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
136 {
137 uint32_t u32Idx;
138
139 for(u32Idx = 0ul; u32Idx < GPIO_PIN_MAX; u32Idx++)
140 {
141 if(u32PinMask & (1ul << u32Idx))
142 {
143 port->SLEWCTL = (port->SLEWCTL & ~(0x3ul << (u32Idx << 1))) | (u32Mode << (u32Idx << 1));
144 }
145 }
146 }
147
148 /**
149 * @brief Set GPIO Pull-up and Pull-down control
150 *
151 * @param[in] port GPIO port. It could be PA, PB, PC, PD, PE, PF, PG or PH.
152 * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n
153 * It could be BIT0 ~ BIT15 for PA, PB and PE. \n
154 * It could be BIT0 ~ BIT13 for PC. \n
155 * It could be BIT0 ~ BIT14 for PD. \n
156 * It could be BIT0 ~ BIT11 for PF. \n
157 * It could be BIT2 ~ BIT3, BIT9 ~ BIT15 for PG. \n
158 * It could be BIT4 ~ BIT11 for PH.
159 * @param[in] u32Mode The pin mode of specified GPIO pin. It could be
160 * - \ref GPIO_PUSEL_DISABLE
161 * - \ref GPIO_PUSEL_PULL_UP
162 * - \ref GPIO_PUSEL_PULL_DOWN
163 *
164 * @return None
165 *
166 * @details Set the pin mode of specified GPIO pin.
167 */
GPIO_SetPullCtl(GPIO_T * port,uint32_t u32PinMask,uint32_t u32Mode)168 void GPIO_SetPullCtl(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
169 {
170 uint32_t u32Idx;
171
172 for(u32Idx = 0ul; u32Idx < GPIO_PIN_MAX; u32Idx++)
173 {
174 if(u32PinMask & (1ul << u32Idx))
175 {
176 port->PUSEL = (port->PUSEL & ~(0x3ul << (u32Idx << 1))) | (u32Mode << (u32Idx << 1));
177 }
178 }
179 }
180
181 /*@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */
182
183 /*@}*/ /* end of group GPIO_Driver */
184
185 /*@}*/ /* end of group Standard_Driver */
186
187