1 /**
2   ******************************************************************************
3   * @file    lib_gpio.h
4   * @author  Application Team
5   * @version V1.1.0
6   * @date    2019-10-28
7   * @brief   GPIO library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #ifndef __LIB_GPIO_H
14 #define __LIB_GPIO_H
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 #include "target.h"
21 
22 #define GET_BITBAND_ADDR(addr, bitnum)  ((((uint32_t)addr) & 0xF0000000) + \
23                                         0x2000000 + ((((uint32_t)addr) & 0xFFFFF) << 5) + (bitnum << 2))
24 
25 typedef struct
26 {
27   uint32_t GPIO_Pin;
28   uint32_t GPIO_Mode;
29 } GPIO_InitType;
30 
31 typedef struct
32 {
33   __IO uint32_t DATBitBand[16];
34 } GPIO_DATInitType;
35 
36 /**
37   * @brief Bit_State_enumeration
38   */
39 typedef enum {
40     Bit_RESET = 0,
41     Bit_SET
42 } BitState;
43 
44 #define GPIO_A  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x40014018,0)))
45 #define GPIO_B  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x40000028,0)))
46 #define GPIO_C  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x40000048,0)))
47 #define GPIO_D  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x40000068,0)))
48 #define GPIO_E  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x40000088,0)))
49 #define GPIO_F  ((GPIO_DATInitType*) (uint32_t)(GET_BITBAND_ADDR(0x400000A8,0)))
50 #define IS_GPIO_DAT(__GPIODAT__) (((__GPIODAT__) == GPIO_A)   ||\
51                                   ((__GPIODAT__) == GPIO_B)   ||\
52                                   ((__GPIODAT__) == GPIO_C)   ||\
53                                   ((__GPIODAT__) == GPIO_D)   ||\
54                                   ((__GPIODAT__) == GPIO_E)   ||\
55                                   ((__GPIODAT__) == GPIO_F))
56 
57 #define IS_GPIO_PINNUM(__PINNUM__)  ((__PINNUM__) < 16U)
58 
59 #define IS_GPIO_BITVAL(__BITVAL__)  (((__BITVAL__) == 1U) || ((__BITVAL__) == 0U))
60 
61 //GPIO_Pin
62 #define GPIO_Pin_0                 ((uint16_t)0x0001)
63 #define GPIO_Pin_1                 ((uint16_t)0x0002)
64 #define GPIO_Pin_2                 ((uint16_t)0x0004)
65 #define GPIO_Pin_3                 ((uint16_t)0x0008)
66 #define GPIO_Pin_4                 ((uint16_t)0x0010)
67 #define GPIO_Pin_5                 ((uint16_t)0x0020)
68 #define GPIO_Pin_6                 ((uint16_t)0x0040)
69 #define GPIO_Pin_7                 ((uint16_t)0x0080)
70 #define GPIO_Pin_8                 ((uint16_t)0x0100)
71 #define GPIO_Pin_9                 ((uint16_t)0x0200)
72 #define GPIO_Pin_10                ((uint16_t)0x0400)
73 #define GPIO_Pin_11                ((uint16_t)0x0800)
74 #define GPIO_Pin_12                ((uint16_t)0x1000)
75 #define GPIO_Pin_13                ((uint16_t)0x2000)
76 #define GPIO_Pin_14                ((uint16_t)0x4000)
77 #define GPIO_Pin_15                ((uint16_t)0x8000)
78 #define GPIO_Pin_All               ((uint16_t)0xFFFF)
79 #define IS_GPIO_PIN(__PIN__)  ((((__PIN__) & GPIO_Pin_All) != 0UL) &&\
80                                (((__PIN__) & ~GPIO_Pin_All) == 0UL))
81 #define IS_GPIO_PINR(__PINR__)  (((__PINR__) == GPIO_Pin_0) ||\
82                                  ((__PINR__) == GPIO_Pin_1) ||\
83                                  ((__PINR__) == GPIO_Pin_2) ||\
84                                  ((__PINR__) == GPIO_Pin_3) ||\
85                                  ((__PINR__) == GPIO_Pin_4) ||\
86                                  ((__PINR__) == GPIO_Pin_5) ||\
87                                  ((__PINR__) == GPIO_Pin_6) ||\
88                                  ((__PINR__) == GPIO_Pin_7) ||\
89                                  ((__PINR__) == GPIO_Pin_8) ||\
90                                  ((__PINR__) == GPIO_Pin_9) ||\
91                                  ((__PINR__) == GPIO_Pin_10) ||\
92                                  ((__PINR__) == GPIO_Pin_11) ||\
93                                  ((__PINR__) == GPIO_Pin_12) ||\
94                                  ((__PINR__) == GPIO_Pin_13) ||\
95                                  ((__PINR__) == GPIO_Pin_14) ||\
96                                  ((__PINR__) == GPIO_Pin_15))
97 
98 //GPIO_Mode
99 #define GPIO_MODE_INPUT           (0xCU)
100 #define GPIO_MODE_OUTPUT_CMOS     (0x2U)
101 #define GPIO_MODE_OUTPUT_OD       (0x3U)
102 #define GPIO_MODE_INOUT_OD        (0xBU)
103 #define GPIO_MODE_INOUT_CMOS      (0xAU)
104 #define GPIO_MODE_FORBIDDEN       (0x4U)
105 #define IS_GPIO_MODE(__MODE__)  (((__MODE__) ==  GPIO_MODE_INPUT)       ||\
106                                  ((__MODE__) ==  GPIO_MODE_OUTPUT_CMOS) ||\
107                                  ((__MODE__) ==  GPIO_MODE_OUTPUT_OD)   ||\
108                                  ((__MODE__) ==  GPIO_MODE_INOUT_OD)    ||\
109                                  ((__MODE__) ==  GPIO_MODE_INOUT_CMOS)  ||\
110                                  ((__MODE__) ==  GPIO_MODE_FORBIDDEN))
111 
112 /**************  Bits definition for IO_MISC register        ******************/
113 #define IO_MISC_PLLHDIV_1             (0x0U << GPIOAF_IO_MISC_PLLHDIV_Pos)
114 #define IO_MISC_PLLHDIV_2             (0x1U << GPIOAF_IO_MISC_PLLHDIV_Pos)
115 #define IO_MISC_PLLHDIV_4             (0x2U << GPIOAF_IO_MISC_PLLHDIV_Pos)
116 #define IO_MISC_PLLHDIV_8             (0x3U << GPIOAF_IO_MISC_PLLHDIV_Pos)
117 #define IO_MISC_PLLHDIV_16            (0x4U << GPIOAF_IO_MISC_PLLHDIV_Pos)
118 
119 //GPIO AF
120 #define GPIOB_AF_PLLHDIV     GPIOAF_IOB_SEL_SEL1
121 #define GPIOB_AF_PLLLOUT     GPIOAF_IOB_SEL_SEL2
122 #define GPIOB_AF_OSC         GPIOAF_IOB_SEL_SEL6
123 #define GPIOE_AF_CMP1O       GPIOAF_IOE_SEL_SEL7
124 #define IS_GPIO_GPIOAF(__GPIOAF__)  (((__GPIOAF__) == GPIOB_AF_PLLHDIV) ||\
125                                      ((__GPIOAF__) == GPIOB_AF_PLLLOUT) ||\
126                                      ((__GPIOAF__) == GPIOB_AF_OSC)     ||\
127                                      ((__GPIOAF__) == GPIOE_AF_CMP1O))
128 
129 #define IS_GPIOB_GPIOAF(__GPIOAF__)  (((__GPIOAF__) == GPIOB_AF_PLLHDIV) ||\
130                                       ((__GPIOAF__) == GPIOB_AF_PLLLOUT) ||\
131                                       ((__GPIOAF__) == GPIOB_AF_OSC))
132 
133 #define IS_GPIOE_GPIOAF(__GPIOAF__)  ((__GPIOAF__) == GPIOE_AF_CMP1O)
134 
135 
136 //PMUIO AF
137 #define PMUIO7_AF_PLLDIV     GPIOA_SEL_SEL7
138 #define PMUIO6_AF_CMP2O      GPIOA_SEL_SEL6
139 #define PMUIO3_AF_PLLDIV     GPIOA_SEL_SEL3
140 #define PMUIO_AF_Msk        (PMUIO7_AF_PLLDIV | PMUIO6_AF_CMP2O | PMUIO3_AF_PLLDIV)
141 
142 //GPIO pin remap
143 #define GPIO_REMAP_I2C       GPIOAF_IO_MISC_I2CIOC
144 #define IS_GPIO_REMAP(__REMAP__)     ((__REMAP__) == GPIO_REMAP_I2C)
145 
146 //PLLDIV
147 #define  GPIO_PLLDIV_1            IO_MISC_PLLHDIV_1
148 #define  GPIO_PLLDIV_2            IO_MISC_PLLHDIV_2
149 #define  GPIO_PLLDIV_4            IO_MISC_PLLHDIV_4
150 #define  GPIO_PLLDIV_8            IO_MISC_PLLHDIV_8
151 #define  GPIO_PLLDIV_16           IO_MISC_PLLHDIV_16
152 #define IS_GPIO_PLLDIV(__PLLDIV__)  (((__PLLDIV__) == GPIO_PLLDIV_1) ||\
153                                      ((__PLLDIV__) == GPIO_PLLDIV_2) ||\
154                                      ((__PLLDIV__) == GPIO_PLLDIV_4) ||\
155                                      ((__PLLDIV__) == GPIO_PLLDIV_8) ||\
156                                      ((__PLLDIV__) == GPIO_PLLDIV_16))
157 
158 
159 #define IS_GPIO_ALL_INSTANCE(INSTANCE)  (((INSTANCE) == GPIOB) || \
160                                          ((INSTANCE) == GPIOC) || \
161                                          ((INSTANCE) == GPIOD) || \
162                                          ((INSTANCE) == GPIOE) || \
163                                          ((INSTANCE) == GPIOF))
164 
165 #define IS_PMUIO_ALL_INSTANCE(INSTANCE)  ((INSTANCE) == GPIOA)
166 
167 #define IS_GPIOAF_ALL_INSTANCE(INSTANCE) (((INSTANCE) == GPIOB) || \
168                                           ((INSTANCE) == GPIOE))
169 
170 #define IS_GPIOE_GPIOAF(__GPIOAF__)  ((__GPIOAF__) == GPIOE_AF_CMP1O)
171 
172 #define IS_GPIO_PMUIOAF(__PMUIOAF__)  ((((__PMUIOAF__) & PMUIO_AF_Msk) != 0U) &&\
173                                        (((__PMUIOAF__) & ~PMUIO_AF_Msk) == 0U))
174 
175 #define IS_GPIO_REMAP(__REMAP__)  ((__REMAP__) == GPIO_REMAP_I2C)
176 
177 #define IS_GPIO_PLLDIV(__PLLDIV__)  (((__PLLDIV__) == GPIO_PLLDIV_1) ||\
178                                      ((__PLLDIV__) == GPIO_PLLDIV_2) ||\
179                                      ((__PLLDIV__) == GPIO_PLLDIV_4) ||\
180                                      ((__PLLDIV__) == GPIO_PLLDIV_8) ||\
181                                      ((__PLLDIV__) == GPIO_PLLDIV_16))
182 
183 /* Exported Functions ------------------------------------------------------- */
184 /* GPIO Exported Functions Group1:
185                                    Initialization and functions --------------*/
186 void GPIOBToF_Init(GPIO_Type *GPIOx, GPIO_InitType *InitStruct);
187 void GPIOA_Init(GPIOA_Type *GPIOx, GPIO_InitType *InitStruct);
188 /* GPIO Exported Functions Group2:
189                                    Read input data ---------------------------*/
190 uint8_t GPIOBToF_ReadInputDataBit(GPIO_Type *GPIOx, uint16_t GPIO_Pin);
191 uint8_t GPIOA_ReadInputDataBit(GPIOA_Type *GPIOx, uint16_t GPIO_Pin);
192 uint16_t GPIOBToF_ReadInputData(GPIO_Type* GPIOx);
193 uint16_t GPIOA_ReadInputData(GPIOA_Type* GPIOx);
194 /* GPIO Exported Functions Group3:
195                                    Read output data --------------------------*/
196 uint8_t GPIOBToF_ReadOutputDataBit(GPIO_Type* GPIOx, uint16_t GPIO_Pin);
197 uint8_t GPIOA_ReadOutputDataBit(GPIOA_Type* GPIOx, uint16_t GPIO_Pin);
198 uint16_t GPIOBToF_ReadOutputData(GPIO_Type* GPIOx);
199 uint16_t GPIOA_ReadOutputData(GPIOA_Type* GPIOx);
200 /* GPIO Exported Functions Group4:
201                                    Write output data -------------------------*/
202 void GPIO_WriteBit(GPIO_DATInitType* DATx, uint8_t PinNum, uint8_t val);
203 void GPIOBToF_WriteBit(GPIO_Type* GPIOx, uint16_t GPIO_Pin, uint8_t val);
204 void GPIOA_WriteBit(GPIOA_Type* GPIOx, uint16_t GPIO_Pin, uint8_t val);
205 void GPIOBToF_Write(GPIO_Type* GPIOx, uint16_t val);
206 void GPIOA_Write(GPIOA_Type* GPIOx, uint16_t val);
207 /* GPIO Exported Functions Group5:
208                                    IO AF configure ---------------------------*/
209 void GPIOBToF_AFConfig(GPIO_Type* GPIOx, uint32_t GPIO_AFx, uint8_t NewState);
210 void GPIOA_AFConfig(uint32_t PMUIO_AFx, uint8_t NewState);
211 /* GPIO Exported Functions Group6:
212                                    IO Remap configure ------------------------*/
213 void GPIO_PinRemapConfig(uint32_t GPIO_Remap, uint8_t NewState);
214 /* GPIO Exported Functions Group7:
215                                    Others ------------------------------------*/
216 void GPIO_PLLDIVConfig(uint32_t Divider);
217 void GPIOA_DeGlitchCmd( uint16_t GPIO_Pin, uint8_t NewState);
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif  /* __LIB_GPIO_H */
224 
225 /*********************************** END OF FILE ******************************/
226