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