embox

Форк
0
513 строк · 11.6 Кб
1
/**
2
 * @file
3
 * @brief
4
 * @date 20.11.2019
5
 * @author: Denis Deryugin <deryugin.denis@gmail.com>
6
 */
7

8
#include <assert.h>
9

10
#include <bsp/stm32cube_hal.h>
11

12
#include <config/board_config.h>
13

14
static void USART_CLK_ENABLE(void *usart_base) {
15
	switch((uintptr_t)usart_base) {
16
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
17
	case (uintptr_t)USART1: {
18
		CONF_USART1_CLK_ENABLE_UART();
19
		break;
20
	}
21
#endif
22
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
23
	case (uintptr_t)USART2: {
24
		CONF_USART2_CLK_ENABLE_UART();
25
		break;
26
	}
27
#endif
28
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
29
	case (uintptr_t)USART3: {
30
		CONF_USART3_CLK_ENABLE_UART();
31
		break;
32
	}
33
#endif
34
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
35
	case (uintptr_t)UART4: {
36
		CONF_UART4_CLK_ENABLE_UART();
37
		break;
38
	}
39
#endif
40
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
41
	case (uintptr_t)UART5: {
42
		CONF_UART5_CLK_ENABLE_UART();
43
		break;
44
	}
45
#endif
46
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
47
	case (uintptr_t)USART6: {
48
		CONF_USART6_CLK_ENABLE_UART();
49
		break;
50
	}
51
#endif
52
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
53
	case (uintptr_t)UART7: {
54
		CONF_UART7_CLK_ENABLE_UART();
55
		break;
56
	}
57
#endif
58
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
59
	case (uintptr_t)UART8: {
60
		CONF_UART8_CLK_ENABLE_UART();
61
		break;
62
	}
63
#endif
64
	default:
65
		assert(0);
66
	}
67
}
68

69
static void USART_TX_GPIO_CLK_ENABLE(void *usart_base) {
70
	switch((uintptr_t)usart_base) {
71
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
72
	case (uintptr_t)USART1: {
73
		CONF_USART1_CLK_ENABLE_TX();
74
		break;
75
	}
76
#endif
77
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
78
	case (uintptr_t)USART2: {
79
		CONF_USART2_CLK_ENABLE_TX();
80
		break;
81
	}
82
#endif
83
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
84
	case (uintptr_t)USART3: {
85
		CONF_USART3_CLK_ENABLE_TX();
86
		break;
87
	}
88
#endif
89
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
90
	case (uintptr_t)UART4: {
91
		CONF_UART4_CLK_ENABLE_TX();
92
		break;
93
	}
94
#endif
95
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
96
	case (uintptr_t)UART5: {
97
		CONF_UART5_CLK_ENABLE_TX();
98
		break;
99
	}
100
#endif
101
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
102
	case (uintptr_t)USART6: {
103
		CONF_USART6_CLK_ENABLE_TX();
104
		break;
105
	}
106
#endif
107
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
108
	case (uintptr_t)UART7: {
109
		CONF_UART7_CLK_ENABLE_TX();
110
		break;
111
	}
112
#endif
113
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
114
	case (uintptr_t)UART8: {
115
		CONF_UART8_CLK_ENABLE_TX();
116
		break;
117
	}
118
#endif
119
	default:
120
		assert(0);
121
	}
122
}
123

124
static void USART_RX_GPIO_CLK_ENABLE(void *usart_base) {
125
	switch((uintptr_t)usart_base) {
126
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
127
	case (uintptr_t)USART1: {
128
		CONF_USART1_CLK_ENABLE_RX();
129
		break;
130
	}
131
#endif
132
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
133
	case (uintptr_t)USART2: {
134
		CONF_USART2_CLK_ENABLE_RX();
135
		break;
136
	}
137
#endif
138
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
139
	case (uintptr_t)USART3: {
140
		CONF_USART3_CLK_ENABLE_RX();
141
		break;
142
	}
143
#endif
144
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
145
	case (uintptr_t)UART4: {
146
		CONF_UART4_CLK_ENABLE_RX();
147
		break;
148
	}
149
#endif
150
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
151
	case (uintptr_t)UART5: {
152
		CONF_UART5_CLK_ENABLE_RX();
153
		break;
154
	}
155
#endif
156
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
157
	case (uintptr_t)USART6: {
158
		CONF_USART6_CLK_ENABLE_RX();
159
		break;
160
	}
161
#endif
162
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
163
	case (uintptr_t)UART7: {
164
		CONF_UART7_CLK_ENABLE_RX();
165
		break;
166
	}
167
#endif
168
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
169
	case (uintptr_t)UART8: {
170
		CONF_UART8_CLK_ENABLE_RX();
171
		break;
172
	}
173
#endif
174
	default:
175
		assert(0);
176
	}
177
}
178

179
static uint16_t USART_RX_PIN(void *usart_base) {
180
	switch((uintptr_t)usart_base) {
181
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
182
	case (uintptr_t)USART1: {
183
		return CONF_USART1_PIN_RX_NR;
184
	}
185
#endif
186
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
187
	case (uintptr_t)USART2: {
188
		return CONF_USART2_PIN_RX_NR;
189
	}
190
#endif
191
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
192
	case (uintptr_t)USART3: {
193
		return CONF_USART3_PIN_RX_NR;
194
	}
195
#endif
196
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
197
	case (uintptr_t)UART4: {
198
		return CONF_UART4_PIN_RX_NR;
199
	}
200
#endif
201
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
202
	case (uintptr_t)UART5: {
203
		return CONF_UART5_PIN_RX_NR;
204
	}
205
#endif
206
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
207
	case (uintptr_t)USART6: {
208
		return CONF_USART6_PIN_RX_NR;
209
	}
210
#endif
211
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
212
	case (uintptr_t)UART7: {
213
		return CONF_UART7_PIN_RX_NR;
214
	}
215
#endif
216
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
217
	case (uintptr_t)UART8: {
218
		return CONF_UART8_PIN_RX_NR;
219
	}
220
#endif
221
	default:
222
		assert(0);
223
	}
224
	return 0;
225
}
226

227
static uint16_t USART_TX_PIN(void *usart_base) {
228
	switch((uintptr_t)usart_base) {
229
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
230
	case (uintptr_t)USART1: {
231
		return CONF_USART1_PIN_TX_NR;
232
	}
233
#endif
234
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
235
	case (uintptr_t)USART2: {
236
		return CONF_USART2_PIN_TX_NR;
237
	}
238
#endif
239
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
240
	case (uintptr_t)USART3: {
241
		return CONF_USART3_PIN_TX_NR;
242
	}
243
#endif
244
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
245
	case (uintptr_t)UART4: {
246
		return CONF_UART4_PIN_TX_NR;
247
	}
248
#endif
249
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
250
	case (uintptr_t)UART5: {
251
		return CONF_UART5_PIN_TX_NR;
252
	}
253
#endif
254
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
255
	case (uintptr_t)USART6: {
256
		return CONF_USART6_PIN_TX_NR;
257
	}
258
#endif
259
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
260
	case (uintptr_t)UART7: {
261
		return CONF_UART7_PIN_TX_NR;
262
	}
263
#endif
264
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
265
	case (uintptr_t)UART8: {
266
		return CONF_UART8_PIN_TX_NR;
267
	}
268
#endif
269
	default:
270
		assert(0);
271
	}
272
	return 0;
273
}
274

275
static GPIO_TypeDef *USART_RX_GPIO_PORT(void *usart_base) {
276
	switch((uintptr_t)usart_base) {
277
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
278
	case (uintptr_t)USART1: {
279
		return CONF_USART1_PIN_RX_PORT;
280
	}
281
#endif
282
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
283
	case (uintptr_t)USART2: {
284
		return CONF_USART2_PIN_RX_PORT;
285
	}
286
#endif
287
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
288
	case (uintptr_t)USART3: {
289
		return CONF_USART3_PIN_RX_PORT;
290
	}
291
#endif
292
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
293
	case (uintptr_t)UART4: {
294
		return CONF_UART4_PIN_RX_PORT;
295
	}
296
#endif
297
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
298
	case (uintptr_t)UART5: {
299
		return CONF_UART5_PIN_RX_PORT;
300
	}
301
#endif
302
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
303
	case (uintptr_t)USART6: {
304
		return CONF_USART6_PIN_RX_PORT;
305
	}
306
#endif
307
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
308
	case (uintptr_t)UART7: {
309
		return CONF_UART7_PIN_RX_PORT;
310
	}
311
#endif
312
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
313
	case (uintptr_t)UART8: {
314
		return CONF_UART8_PIN_RX_PORT;
315
	}
316
#endif
317
	default:
318
		assert(0);
319
	}
320
	return 0;
321
}
322

323
static GPIO_TypeDef *USART_TX_GPIO_PORT(void *usart_base) {
324
	switch((uintptr_t)usart_base) {
325
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
326
	case (uintptr_t)USART1: {
327
		return CONF_USART1_PIN_TX_PORT;
328
	}
329
#endif
330
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
331
	case (uintptr_t)USART2: {
332
		return CONF_USART2_PIN_TX_PORT;
333
	}
334
#endif
335
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
336
	case (uintptr_t)USART3: {
337
		return CONF_USART3_PIN_TX_PORT;
338
	}
339
#endif
340
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
341
	case (uintptr_t)UART4: {
342
		return CONF_UART4_PIN_TX_PORT;
343
	}
344
#endif
345
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
346
	case (uintptr_t)UART5: {
347
		return CONF_UART5_PIN_TX_PORT;
348
	}
349
#endif
350
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
351
	case (uintptr_t)USART6: {
352
		return CONF_USART6_PIN_TX_PORT;
353
	}
354
#endif
355
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
356
	case (uintptr_t)UART7: {
357
		return CONF_UART7_PIN_TX_PORT;
358
	}
359
#endif
360
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
361
	case (uintptr_t)UART8: {
362
		return CONF_UART8_PIN_TX_PORT;
363
	}
364
#endif
365
	default:
366
		assert(0);
367
	}
368
	return 0;
369
}
370

371
#ifndef STM32F1_CUBE
372
static uint8_t USART_TX_AF(void *usart_base) {
373
	switch((uintptr_t)usart_base) {
374
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
375
	case (uintptr_t)USART1: {
376
		return CONF_USART1_PIN_TX_AF;
377
	}
378
#endif
379
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
380
	case (uintptr_t)USART2: {
381
		return CONF_USART2_PIN_TX_AF;
382
	}
383
#endif
384
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
385
	case (uintptr_t)USART3: {
386
		return CONF_USART3_PIN_TX_AF;
387
	}
388
#endif
389
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
390
	case (uintptr_t)UART4: {
391
		return CONF_UART4_PIN_TX_AF;
392
	}
393
#endif
394
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
395
	case (uintptr_t)UART5: {
396
		return CONF_UART5_PIN_TX_AF;
397
	}
398
#endif
399
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
400
	case (uintptr_t)USART6: {
401
		return CONF_USART6_PIN_TX_AF;
402
	}
403
#endif
404
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
405
	case (uintptr_t)UART7: {
406
		return CONF_UART7_PIN_TX_AF;
407
	}
408
#endif
409
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
410
	case (uintptr_t)UART8: {
411
		return CONF_UART8_PIN_TX_AF;
412
	}
413
#endif
414
	default:
415
		assert(0);
416
	}
417
	return 0;
418
}
419

420
static uint8_t USART_RX_AF(void *usart_base) {
421
	switch((uintptr_t)usart_base) {
422
#if defined(USART1) &&  defined(CONF_USART1_ENABLED)
423
	case (uintptr_t)USART1: {
424
		return CONF_USART1_PIN_RX_AF;
425
	}
426
#endif
427
#if defined(USART2) &&  defined(CONF_USART2_ENABLED)
428
	case (uintptr_t)USART2: {
429
		return CONF_USART2_PIN_RX_AF;
430
	}
431
#endif
432
#if defined(USART3) &&  defined(CONF_USART3_ENABLED)
433
	case (uintptr_t)USART3: {
434
		return CONF_USART3_PIN_RX_AF;
435
	}
436
#endif
437
#if defined(UART4) &&  defined(CONF_UART4_ENABLED)
438
	case (uintptr_t)UART4: {
439
		return CONF_UART4_PIN_RX_AF;
440
	}
441
#endif
442
#if defined(UART5) &&  defined(CONF_UART5_ENABLED)
443
	case (uintptr_t)UART5: {
444
		return CONF_UART5_PIN_RX_AF;
445
	}
446
#endif
447
#if defined(USART6) &&  defined(CONF_USART6_ENABLED)
448
	case (uintptr_t)USART6: {
449
		return CONF_USART6_PIN_RX_AF;
450
	}
451
#endif
452
#if defined(UART7) &&  defined(CONF_UART7_ENABLED)
453
	case (uintptr_t)UART7: {
454
		return CONF_UART7_PIN_RX_AF;
455
	}
456
#endif
457
#if defined(UART8) &&  defined(CONF_UART8_ENABLED)
458
	case (uintptr_t)UART8: {
459
		return CONF_UART8_PIN_RX_AF;
460
	}
461
#endif
462
	default:
463
		assert(0);
464
	}
465
	return 0;
466
}
467
#endif /* STM32F1_CUBE */
468

469
void HAL_UART_MspInit(UART_HandleTypeDef *huart) {
470
	GPIO_InitTypeDef  GPIO_InitStruct;
471
	void *uart_base = huart->Instance;
472

473
	/*##-1- Enable peripherals and GPIO Clocks #################################*/
474
	/* Enable GPIO TX/RX clock */
475
	USART_TX_GPIO_CLK_ENABLE(uart_base);
476
	USART_RX_GPIO_CLK_ENABLE(uart_base);
477
	/* Enable USART2 clock */
478
	USART_CLK_ENABLE(uart_base);
479

480
#if 0
481
	/*TODO It can be required for some STM32 series (H7 for example)
482
	 * but now all boards work without it
483
	 */
484
	RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
485
	/* Select HSI as source of USARTx clocks */
486
	RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;
487
	RCC_PeriphClkInit.Usart234578ClockSelection = RCC_USART3CLKSOURCE_HSI;
488
	HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
489
#endif
490

491
	/*##-2- Configure peripheral GPIO ##########################################*/
492
	/* UART TX GPIO pin configuration  */
493
	GPIO_InitStruct.Pin       = USART_TX_PIN(uart_base);
494
	GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
495
	GPIO_InitStruct.Pull      = GPIO_NOPULL;
496
	GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
497
#ifndef STM32F1_CUBE
498
	GPIO_InitStruct.Alternate = USART_TX_AF(uart_base);
499
#else
500
	GPIO_InitStruct.Pull      = GPIO_PULLUP;
501
#endif /* STM32F1_CUBE */
502
	HAL_GPIO_Init(USART_TX_GPIO_PORT(uart_base), &GPIO_InitStruct);
503

504
	/* UART RX GPIO pin configuration  */
505
	GPIO_InitStruct.Pin = USART_RX_PIN(uart_base);
506
#ifndef STM32F1_CUBE
507
	GPIO_InitStruct.Alternate = USART_RX_AF(uart_base);
508
#else
509
	GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;
510
#endif /* STM32F1_CUBE */
511
	HAL_GPIO_Init(USART_RX_GPIO_PORT(uart_base), &GPIO_InitStruct);
512
	/* TODO ##-3- Configure the NVIC for UART ########################################*/
513
}
514

515

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.