1 /**
2 ******************************************************************************
3 * @file tae32f53xx_ll_gpio.c
4 * @author MCD Application Team
5 * @brief GPIO LL module driver.
6 *
7 ******************************************************************************
8 * @attention
9 *
10 * <h2><center>© Copyright (c) 2020 Tai-Action.
11 * All rights reserved.</center></h2>
12 *
13 * This software is licensed by Tai-Action under BSD 3-Clause license,
14 * the "License"; You may not use this file except in compliance with the
15 * License. You may obtain a copy of the License at:
16 * opensource.org/licenses/BSD-3-Clause
17 *
18 ******************************************************************************
19 */
20
21 /* Includes ------------------------------------------------------------------*/
22 #include "tae32f53xx_ll.h"
23
24
25 #define DBG_TAG "GPIO LL"
26 #define DBG_LVL DBG_ERROR
27 #include "dbg/tae32f53xx_dbg.h"
28
29
30 /** @addtogroup TAE32F53xx_LL_Driver
31 * @{
32 */
33
34 /** @addtogroup GPIO_LL GPIO LL
35 * @brief GPIO LL module driver
36 * @{
37 */
38
39 #ifdef LL_GPIO_MODULE_ENABLED
40
41 /* Private typedef -----------------------------------------------------------*/
42 /* Private define ------------------------------------------------------------*/
43 /* Private macro -------------------------------------------------------------*/
44 /* Private variables ---------------------------------------------------------*/
45 /* Private function prototypes -----------------------------------------------*/
46 /* Exported functions --------------------------------------------------------*/
47 /** @defgroup GPIO_LL_Exported_Functions GPIO LL Exported Functions
48 * @brief GPIO LL Exported Functions
49 * @{
50 */
51
52 /** @defgroup GPIO_LL_Exported_Functions_Group1 Initialization and de-initialization functions
53 * @brief Initialization and Configuration functions
54 *
55 @verbatim
56 ===============================================================================
57 ##### Initialization and de-initialization functions #####
58 ===============================================================================
59 [..]
60 This section provides functions allowing to initialize and de-initialize the GPIOs
61 to be ready for use.
62
63 @endverbatim
64 * @{
65 */
66
67 /**
68 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
69 * @param GPIOx: where x can be (A, B, ... depending on device used) to select the GPIO peripheral
70 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
71 * the configuration information for the specified GPIO peripheral.
72 * @return status of the initialization
73 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)74 LL_StatusETypeDef LL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
75 {
76 uint32_t position = 0x00U;
77 uint32_t iocurrent;
78 /* Check the parameters */
79 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
80 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
81
82
83 /* Configure the port pins */
84 while (((GPIO_Init->Pin) >> position) != 0x00U) {
85 /* Get current io position */
86 iocurrent = (GPIO_Init->Pin) & (1UL << position);
87
88 if (iocurrent != 0x00U) {
89 /*--------------------- GPIO Mode Configuration ------------------------*/
90 switch (GPIO_Init->Mode & 0x03U) {
91 /*In case of Alternate function mode selection*/
92 case GPIO_MODE_AF:
93
94 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
95 MODIFY_REG(GPIOx->PMUXR[position >> 3U], 0x0FU << ((position & 0x07U) * 4U),
96 (GPIO_Init->Alternate & 0x0FU) << ((position & 0x07U) * 4U));
97
98 /* Configure the IO Output Type */
99 MODIFY_REG(GPIOx->OTYPR, 0x01U << position, (GPIO_Init->OType & 0x01U) << position);
100
101 /* Configure the IO Speed */
102 MODIFY_REG(GPIOx->OSRR, 0x01U << position, (GPIO_Init->Speed & 0x01U) << position);
103 break;
104
105 /* In case of Input function mode selection */
106 case GPIO_MODE_INPUT:
107 /* Configure IO Direction mode (Input) */
108 MODIFY_REG(GPIOx->PMUXR[position >> 3U], 0x0FU << ((position & 0x07U) * 4U),
109 GPIO_AF0_INPUT << ((position & 0x07U) * 4U));
110
111 /* Interrupt Eanble */
112 MODIFY_REG(GPIOx->ITER, 0x01U << position, ((GPIO_Init->Mode >> 28U) & 0x01U) << position);
113
114 /* Rising/Falling Edge */
115 MODIFY_REG(GPIOx->RFTSR, 0x01U << position, ((GPIO_Init->Mode >> 20U) & 0x01U) << position);
116 MODIFY_REG(GPIOx->RFTSR, 0x01U << (position + 16U), ((GPIO_Init->Mode >> 21U) & 0x01U) << (position + 16U));
117
118 /* GPIO Port Interrupt Enable */
119 WRITE_REG(GPIOx->IER, (READ_REG(GPIOx->ITER) == 0x00UL) ? 0x00UL : 0x01UL);
120
121 /* Clear Pending */
122 WRITE_REG(GPIOx->PR, 0x01U << position);
123 break;
124
125 /* In case of Output function mode selection */
126 case GPIO_MODE_OUTPUT:
127
128 /* Configure the IO Output Type */
129 MODIFY_REG(GPIOx->PMUXR[position >> 3U], 0x0FU << ((position & 0x07U) * 4U),
130 GPIO_AF1_OUTPUT << ((position & 0x07U) * 4U));
131
132 /* Configure the IO Output Type */
133 MODIFY_REG(GPIOx->OTYPR, 0x01U << position, (GPIO_Init->OType & 0x01U) << position);
134
135 /* Configure the IO Speed */
136 MODIFY_REG(GPIOx->OSRR, 0x01U << position, (GPIO_Init->Speed & 0x01U) << position);
137 break;
138 }
139
140
141 /* Activate the Pull-up resistor for the current IO */
142 if (GPIO_Init->Pull == GPIO_PULLUP) {
143 SET_BIT(GPIOx->PUR, 0x01U << position);
144 CLEAR_BIT(GPIOx->PDR, 0x01U << position);
145 }
146
147 /* Activate the Pull-dowm resistor for the current IO */
148 if (GPIO_Init->Pull == GPIO_PULLDOWN) {
149 CLEAR_BIT(GPIOx->PUR, 0x01U << position);
150 SET_BIT(GPIOx->PDR, 0x01U << position);
151 }
152
153 /* Unactivate the Pull-up or Pull down resistor for the current IO */
154 if (GPIO_Init->Pull == GPIO_NOPULL) {
155 CLEAR_BIT(GPIOx->PUR, 0x01U << position);
156 CLEAR_BIT(GPIOx->PDR, 0x01U << position);
157 }
158 }
159
160 position++;
161 }
162
163 return LL_OK;
164 }
165
166 /**
167 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
168 * @param GPIOx: where x can be (A, B, ... depending on device used) to select the GPIO peripheral
169 * @param GPIO_Pin: specifies the port bit to be written.
170 * This parameter can be any combination of @ref GPIO_pins_define
171 * @return status of the de-initialization
172 */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)173 LL_StatusETypeDef LL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
174 {
175 uint32_t position = 0x00U;
176 uint32_t iocurrent;
177 /* Check the parameters */
178 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
179 assert_param(IS_GPIO_PIN(GPIO_Pin));
180
181 /* Configure the port pins */
182 while ((GPIO_Pin >> position) != 0x00U) {
183 /* Get current io position */
184 iocurrent = GPIO_Pin & (0x1U << position);
185
186 if (iocurrent != 0x00U) {
187 /*------------------------- GPIO Mode Configuration --------------------*/
188 /* Configure the default Alternate Function in current IO */
189 MODIFY_REG(GPIOx->PMUXR[position >> 3U], (0x0FU << ((uint32_t)(position & 0x07U) * 4U)),
190 (GPIO_AF15_ANALOG << ((uint32_t)(position & 0x07U) * 4U)));
191
192 /* Configure the default value for IO Speed */
193 CLEAR_BIT(GPIOx->OSRR, 0x01U << position);
194
195 /* Configure the default value IO Output Type */
196 CLEAR_BIT(GPIOx->OTYPR, 0x01U << position);
197
198 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
199 CLEAR_BIT(GPIOx->PDR, 0x01U << position);
200 CLEAR_BIT(GPIOx->PUR, 0x01U << position);
201
202 /* Disable Interrupt Mask */
203 CLEAR_BIT(GPIOx->ITER, 0x01U << position);
204
205 /* Clear Rising Falling edge configuration */
206 CLEAR_BIT(GPIOx->RFTSR, 0x01U << position);
207 CLEAR_BIT(GPIOx->RFTSR, 0x01U << (position + 16U));
208
209 /* Disable Interrupt */
210 WRITE_REG(GPIOx->IER, (READ_REG(GPIOx->ITER) == 0) ? 0x00 : 0x01);
211
212 /* Clear Pending */
213 WRITE_REG(GPIOx->PR, 0x01U << position);
214
215 /* Defalut value of data */
216 CLEAR_BIT(GPIOx->DR, 0x01U << position);
217 }
218
219 position++;
220 }
221
222 return LL_OK;
223 }
224
225 /**
226 * @}
227 */
228
229
230 /** @defgroup GPIO_LL_Exported_Functions_Group2 GPIO Peripheral Control functions
231 * @brief GPIO Peripheral Control functions
232 @verbatim
233 ===============================================================================
234 ##### Peripheral Control functions #####
235 ===============================================================================
236 [..]
237 This section provides functions allowing to:
238 (+) GPIO configure functions
239
240 @endverbatim
241 * @{
242 */
243
244 /**
245 * @brief Config the alternate function on runtime.
246 * @param GPIOx where x can be (A, B, ...) to select the GPIO peripheral for TXF53xx
247 * @param GPIO_Pin specifies the port bit to read.
248 * This parameter can be GPIO_PIN_x where x can be (0..15).
249 * @param Alternate Peripheral to be connected to the selected pins.
250 * This parameter can be a value of @ref GPIO_Alternate_function_selection
251 * @return None
252 */
LL_GPIO_AF_Config(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_AFETypeDef Alternate)253 void LL_GPIO_AF_Config(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_AFETypeDef Alternate)
254 {
255 uint32_t position = 0x00U;
256 uint32_t iocurrent;
257
258 /* Check the parameters */
259 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
260 assert_param(IS_GPIO_PIN(GPIO_Pin));
261
262 /* Configure the port pins */
263 while (((GPIO_Pin) >> position) != 0x00U) {
264 /* Get current io position */
265 iocurrent = (GPIO_Pin) & (1UL << position);
266
267 if (iocurrent != 0x00U) {
268 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
269 MODIFY_REG(GPIOx->PMUXR[position >> 3U], 0x0FU << ((position & 0x07U) * 4U),
270 (Alternate) << ((position & 0x07U) * 4U));
271 }
272
273 /* Next pin */
274 position++;
275 }
276 }
277
278 /**
279 * @}
280 */
281
282
283 /** @defgroup GPIO_LL_Exported_Functions_Group3 GPIO IO operation functions
284 * @brief GPIO Read and Write
285 *
286 @verbatim
287 ===============================================================================
288 ##### IO operation functions #####
289 ===============================================================================
290 [..]
291 This subsection provides a set of functions allowing to manage the GPIOs:
292 (+) Read the GPIOx Pins
293 (+) Write the GPIOx Pins
294 (+) Toggle the GPIOx Pins
295 (+) Write the GPIOx Port
296 (+) Read the GPIOx Port
297
298 @endverbatim
299 * @{
300 */
301
302 /**
303 * @brief Read the specified input port pin
304 * @param GPIOx where x can be (A, B, ...) to select the GPIO peripheral for TXF53xx
305 * @param GPIO_Pin specifies the port bit to read.
306 * This parameter can be GPIO_PIN_x where x can be (0..15).
307 * @return The input port pin value.
308 */
LL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)309 GPIO_PinStateETypeDef LL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
310 {
311 GPIO_PinStateETypeDef bitstatus;
312 /* Check the parameters */
313 assert_param(IS_GPIO_PIN(GPIO_Pin));
314
315 if ((GPIOx->DR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET) {
316 bitstatus = GPIO_PIN_SET;
317 } else {
318 bitstatus = GPIO_PIN_RESET;
319 }
320
321 return bitstatus;
322 }
323
324 /**
325 * @brief Set or clear the selected data port bit.
326 * @param GPIOx: where x can be (A, B, ...) to select the GPIO peripheral.
327 * @param bit_field: specifies the port bit to read.
328 * This parameter can be GPIO_Pin_x where x can be (0..15).
329 * @param PinState specifies the value to be written to the selected bit.
330 * This parameter can be one of the GPIO_PinState enum values:
331 * @arg GPIO_PIN_RESET: to clear the port pin
332 * @arg GPIO_PIN_SET: to set the port pin
333 * @return None
334 */
LL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinStateETypeDef PinState)335 void LL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinStateETypeDef PinState)
336 {
337 /* Check the parameters */
338 assert_param(IS_GPIO_PIN(GPIO_Pin));
339
340 if (PinState != GPIO_PIN_RESET) {
341 GPIOx->BSRR = GPIO_Pin;
342 } else {
343 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
344 }
345 }
346
347 /**
348 * @brief Toggle the specified GPIO pin.
349 * @param GPIOx where x can be (A, B, ...) to select the GPIO peripheral.
350 * @param GPIO_Pin specifies the pin to be toggled.
351 * @return None
352 */
LL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)353 void LL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
354 {
355 /* Check the parameters */
356 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
357 assert_param(IS_GPIO_PIN(GPIO_Pin));
358
359 GPIOx->DR ^= (GPIO_Pin & GPIO_PIN_All);
360 }
361
362 /**
363 * @brief Write data to the specified GPIO port.
364 * @param GPIOx where x can be (A, B, ...) to select the GPIO peripheral.
365 * @param Data data to write.
366 * @return None
367 */
LL_GPIO_WriteData(GPIO_TypeDef * GPIOx,uint16_t Data)368 void LL_GPIO_WriteData(GPIO_TypeDef *GPIOx, uint16_t Data)
369 {
370 /* Check the parameters */
371 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
372
373 WRITE_REG(GPIOx->DR, Data);
374 }
375
376
377 /**
378 * @brief Read data from the specified GPIO port.
379 * @param GPIOx where x can be (A, B, ...) to select the GPIO peripheral.
380 * @return GPIO port data
381 */
LL_GPIO_ReadData(GPIO_TypeDef * GPIOx)382 uint16_t LL_GPIO_ReadData(GPIO_TypeDef *GPIOx)
383 {
384 /* Check the parameters */
385 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
386
387 return READ_REG(GPIOx->DR) & 0xFFFFUL;
388 }
389
390 /**
391 * @}
392 */
393
394
395 /** @defgroup GPIO_LL_Exported_Functions_Interrupt GPIO Initerrupt management
396 * @brief GPIO Initerrupt management
397 *
398 @verbatim
399 ==============================================================================
400 ##### IRQ handler management #####
401 ==============================================================================
402 [..]
403 This section provides GPIO IRQ handler function.
404
405 @endverbatim
406 * @{
407 */
408
409 /**
410 * @brief This function handles GPIO interrupts requests.
411 * @param GPIOx GPIO Port
412 * @return None
413 */
LL_GPIO_IRQHandler(GPIO_TypeDef * GPIOx)414 void LL_GPIO_IRQHandler(GPIO_TypeDef *GPIOx)
415 {
416 /* Check the parameters */
417 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
418
419 for (uint8_t position = 0x00U; position < GPIO_NUMBER; position++) {
420 if ((__LL_GPIO_IT_CHECK_SOURCE(GPIOx, 0x01U << position) != RESET) &&
421 (__LL_GPIO_GET_IT(GPIOx, 0x01U << position)) != RESET) {
422
423 __LL_GPIO_CLEAR_IT(GPIOx, 0x01U << position);
424
425 /*Handle something*/
426 LL_GPIO_ExtTrigCallback(GPIOx, 0x01U << position);
427 }
428 }
429 }
430
431 /**
432 * @brief GPIO External Trigger detection callback.
433 * @param GPIOx GPIO Port
434 * @param GPIO_Pin witch trigger the interruption.
435 * @return None
436 */
LL_GPIO_ExtTrigCallback(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)437 __WEAK void LL_GPIO_ExtTrigCallback(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
438 {
439 /* Prevent unused argument(s) compilation warning */
440 LL_UNUSED(GPIOx);
441 LL_UNUSED(GPIO_Pin);
442 /* NOTE: This function should not be modified, when the callback is needed,
443 the LL_GPIO_ExtTrigCallback could be implemented in the user file
444 */
445 }
446 /**
447 * @}
448 */
449
450 /**
451 * @}
452 */
453
454
455 /* Private functions ---------------------------------------------------------*/
456
457
458 #endif /* LL_GPIO_MODULE_ENABLE */
459
460
461 /**
462 * @}
463 */
464
465 /**
466 * @}
467 */
468
469
470 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
471
472