-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPIO_ex.c
executable file
·92 lines (77 loc) · 2.42 KB
/
GPIO_ex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*!\file GPIO_ex.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief Extensions for GPIOs (R/W)
**/
/****************************************************************/
#include <stdio.h>
#include "sarmfsw.h"
#include "GPIO_ex.h"
#if defined(HAL_GPIO_MODULE_ENABLED)
/****************************************************************/
void NONNULL__ write_GPIO(GPIO_TypeDef * const GPIOx, const uint16_t GPIO_Pin, const eGPIOState action)
{
if (action == Reset) { HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET); }
else if (action == Set) { HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET); }
else if (action == Toggle) { HAL_GPIO_TogglePin(GPIOx, GPIO_Pin); }
else { return; }
#if defined(VERBOSE)
char port[10] = "";
str_GPIO_name(port, GPIOx, GPIO_Pin);
printf("Written %s to %u (%lums)\r\n", port, HAL_GPIO_ReadPin(GPIOx, GPIO_Pin), HALTicks());
#endif
}
GPIO_PinState NONNULL__ read_GPIO(GPIO_TypeDef * const GPIOx, const uint16_t GPIO_Pin)
{
const GPIO_PinState pin = HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
#if defined(VERBOSE)
char port[10] = "";
str_GPIO_name(port, GPIOx, GPIO_Pin);
printf("Read %s is %u (%lums)\r\n", port, pin, HALTicks());
#endif
return pin;
}
FctERR NONNULL__ str_GPIO_name(char * name, const GPIO_TypeDef * const GPIOx, const uint16_t GPIO_Pin)
{
const uint8_t max_pins = 16; // Maximum pins on a port
const char prt[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '?' };
char port;
// Find port by testing GPIO instance
if (GPIOx == GPIOA) port = prt[0];
#if defined(GPIOB)
else if (GPIOx == GPIOB) port = prt[1];
#endif
#if defined(GPIOC)
else if (GPIOx == GPIOC) port = prt[2];
#endif
#if defined(GPIOD)
else if (GPIOx == GPIOD) port = prt[3];
#endif
#if defined(GPIOE)
else if (GPIOx == GPIOE) port = prt[4];
#endif
#if defined(GPIOF)
else if (GPIOx == GPIOF) port = prt[5];
#endif
#if defined(GPIOG)
else if (GPIOx == GPIOG) port = prt[6];
#endif
#if defined(GPIOH)
else if (GPIOx == GPIOH) port = prt[7];
#endif
else port = prt[8];
// Find pin shifting values to get pin index
for (unsigned int pin = 0 ; pin < max_pins ; pin++)
{
if ((1 << pin) == GPIO_Pin)
{
sprintf(name, "GPIO%c%d", port, pin);
return ERROR_OK; // Match
}
}
// sprintf(name, "GPIO%c%c", port, 'x');
return ERROR_VALUE; // No match
}
/********************************************/
#endif /* defined(HAL_GPIO_MODULE_ENABLED) */
/********************************************/