-
Notifications
You must be signed in to change notification settings - Fork 32
/
tm_stm32_gpio.c
261 lines (218 loc) · 7.73 KB
/
tm_stm32_gpio.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* |----------------------------------------------------------------------
* | Copyright (c) 2020 Tilen MAJERLE
* |
* | Permission is hereby granted, free of charge, to any person
* | obtaining a copy of this software and associated documentation
* | files (the "Software"), to deal in the Software without restriction,
* | including without limitation the rights to use, copy, modify, merge,
* | publish, distribute, sublicense, and/or sell copies of the Software,
* | and to permit persons to whom the Software is furnished to do so,
* | subject to the following conditions:
* |
* | The above copyright notice and this permission notice shall be
* | included in all copies or substantial portions of the Software.
* |
* | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* | OTHER DEALINGS IN THE SOFTWARE.
* |----------------------------------------------------------------------
*/
#include "tm_stm32_gpio.h"
/* Private function */
static uint16_t GPIO_UsedPins[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
/* Private functions */
void TM_GPIO_INT_EnableClock(GPIO_TypeDef* GPIOx);
void TM_GPIO_INT_DisableClock(GPIO_TypeDef* GPIOx);
void TM_GPIO_INT_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_Mode_t GPIO_Mode, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed);
void TM_GPIO_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_Mode_t GPIO_Mode, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed) {
/* Check input */
if (GPIO_Pin == 0x00) {
return;
}
/* Enable clock for GPIO */
TM_GPIO_INT_EnableClock(GPIOx);
/* Do initialization */
TM_GPIO_INT_Init(GPIOx, GPIO_Pin, GPIO_Mode, GPIO_OType, GPIO_PuPd, GPIO_Speed);
}
void TM_GPIO_InitAlternate(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed, uint8_t Alternate) {
uint32_t pinpos;
/* Check input */
if (GPIO_Pin == 0x00) {
return;
}
/* Enable GPIOx clock */
TM_GPIO_INT_EnableClock(GPIOx);
/* Set alternate functions for all pins */
for (pinpos = 0; pinpos < 0x10; pinpos++) {
/* Check pin */
if ((GPIO_Pin & (1 << pinpos)) == 0) {
continue;
}
/* Set alternate function */
GPIOx->AFR[pinpos >> 0x03] = (GPIOx->AFR[pinpos >> 0x03] & ~(0x0F << (4 * (pinpos & 0x07)))) | (Alternate << (4 * (pinpos & 0x07)));
}
/* Do initialization */
TM_GPIO_INT_Init(GPIOx, GPIO_Pin, TM_GPIO_Mode_AF, GPIO_OType, GPIO_PuPd, GPIO_Speed);
}
void TM_GPIO_DeInit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint8_t i;
uint8_t ptr = TM_GPIO_GetPortSource(GPIOx);
/* Go through all pins */
for (i = 0x00; i < 0x10; i++) {
/* Pin is set */
if (GPIO_Pin & (1 << i)) {
/* Set 11 bits combination for analog mode */
GPIOx->MODER |= (0x03 << (2 * i));
/* Pin is not used */
GPIO_UsedPins[ptr] &= ~(1 << i);
}
}
}
void TM_GPIO_SetPinAsInput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint8_t i;
/* Go through all pins */
for (i = 0x00; i < 0x10; i++) {
/* Pin is set */
if (GPIO_Pin & (1 << i)) {
/* Set 00 bits combination for input */
GPIOx->MODER &= ~(0x03 << (2 * i));
}
}
}
void TM_GPIO_SetPinAsOutput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint8_t i;
/* Go through all pins */
for (i = 0x00; i < 0x10; i++) {
/* Pin is set */
if (GPIO_Pin & (1 << i)) {
/* Set 01 bits combination for output */
GPIOx->MODER = (GPIOx->MODER & ~(0x03 << (2 * i))) | (0x01 << (2 * i));
}
}
}
void TM_GPIO_SetPinAsAnalog(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint8_t i;
/* Go through all pins */
for (i = 0x00; i < 0x10; i++) {
/* Pin is set */
if (GPIO_Pin & (1 << i)) {
/* Set 11 bits combination for analog mode */
GPIOx->MODER |= (0x03 << (2 * i));
}
}
}
void TM_GPIO_SetPinAsAlternate(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint8_t i;
/* Set alternate functions for all pins */
for (i = 0; i < 0x10; i++) {
/* Check pin */
if ((GPIO_Pin & (1 << i)) == 0) {
continue;
}
/* Set alternate mode */
GPIOx->MODER = (GPIOx->MODER & ~(0x03 << (2 * i))) | (0x02 << (2 * i));
}
}
void TM_GPIO_SetPullResistor(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_PuPd_t GPIO_PuPd) {
uint8_t pinpos;
/* Go through all pins */
for (pinpos = 0; pinpos < 0x10; pinpos++) {
/* Check if pin available */
if ((GPIO_Pin & (1 << pinpos)) == 0) {
continue;
}
/* Set GPIO PUPD register */
GPIOx->PUPDR = (GPIOx->PUPDR & ~(0x03 << (2 * pinpos))) | ((uint32_t)(GPIO_PuPd << (2 * pinpos)));
}
}
void TM_GPIO_Lock(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
uint32_t d;
/* Set GPIO pin with 16th bit set to 1 */
d = 0x00010000 | GPIO_Pin;
/* Write to LCKR register */
GPIOx->LCKR = d;
GPIOx->LCKR = GPIO_Pin;
GPIOx->LCKR = d;
/* Read twice */
(void)GPIOx->LCKR;
(void)GPIOx->LCKR;
}
uint16_t TM_GPIO_GetPinSource(uint16_t GPIO_Pin) {
uint16_t pinsource = 0;
/* Get pinsource */
while (GPIO_Pin > 1) {
/* Increase pinsource */
pinsource++;
/* Shift right */
GPIO_Pin >>= 1;
}
/* Return source */
return pinsource;
}
uint16_t TM_GPIO_GetPortSource(GPIO_TypeDef* GPIOx) {
/* Get port source number */
/* Offset from GPIOA Difference between 2 GPIO addresses */
return ((uint32_t)GPIOx - (GPIOA_BASE)) / ((GPIOB_BASE) - (GPIOA_BASE));
}
/* Private functions */
void TM_GPIO_INT_EnableClock(GPIO_TypeDef* GPIOx) {
/* Set bit according to the 1 << portsourcenumber */
#if defined(STM32F0xx)
RCC->AHBENR |= (1 << (TM_GPIO_GetPortSource(GPIOx) + 17));
#else
RCC->AHB1ENR |= (1 << TM_GPIO_GetPortSource(GPIOx));
#endif
}
void TM_GPIO_INT_DisableClock(GPIO_TypeDef* GPIOx) {
/* Clear bit according to the 1 << portsourcenumber */
#if defined(STM32F0xx)
RCC->AHBENR &= ~(1 << (TM_GPIO_GetPortSource(GPIOx) + 17));
#else
RCC->AHB1ENR &= ~(1 << TM_GPIO_GetPortSource(GPIOx));
#endif
}
void TM_GPIO_INT_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_Mode_t GPIO_Mode, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed) {
uint8_t pinpos;
uint8_t ptr = TM_GPIO_GetPortSource(GPIOx);
#if defined(STM32F0xx)
/* STM32F0xx series does not have FAST speed mode available */
if (GPIO_Speed == TM_GPIO_Speed_Fast) {
/* Set speed to high mode */
GPIO_Speed = TM_GPIO_Speed_High;
}
#endif
/* Go through all pins */
for (pinpos = 0; pinpos < 0x10; pinpos++) {
/* Check if pin available */
if ((GPIO_Pin & (1 << pinpos)) == 0) {
continue;
}
/* Pin is used */
GPIO_UsedPins[ptr] |= 1 << pinpos;
/* Set GPIO PUPD register */
GPIOx->PUPDR = (GPIOx->PUPDR & ~(0x03 << (2 * pinpos))) | ((uint32_t)(GPIO_PuPd << (2 * pinpos)));
/* Set GPIO MODE register */
GPIOx->MODER = (GPIOx->MODER & ~((uint32_t)(0x03 << (2 * pinpos)))) | ((uint32_t)(GPIO_Mode << (2 * pinpos)));
/* Set only if output or alternate functions */
if (GPIO_Mode == TM_GPIO_Mode_OUT || GPIO_Mode == TM_GPIO_Mode_AF) {
/* Set GPIO OTYPE register */
GPIOx->OTYPER = (GPIOx->OTYPER & ~(uint16_t)(0x01 << pinpos)) | ((uint16_t)(GPIO_OType << pinpos));
/* Set GPIO OSPEED register */
GPIOx->OSPEEDR = (GPIOx->OSPEEDR & ~((uint32_t)(0x03 << (2 * pinpos)))) | ((uint32_t)(GPIO_Speed << (2 * pinpos)));
}
}
}
uint16_t TM_GPIO_GetUsedPins(GPIO_TypeDef* GPIOx) {
/* Return used */
return GPIO_UsedPins[TM_GPIO_GetPortSource(GPIOx)];
}
uint16_t TM_GPIO_GetFreePins(GPIO_TypeDef* GPIOx) {
/* Return free pins */
return ~GPIO_UsedPins[TM_GPIO_GetPortSource(GPIOx)];
}