@@ -38,81 +38,93 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
38
38
39
39
static uart_irq_handler irq_handler ;
40
40
41
+ // Defined in serial_api.c
42
+ inline int8_t get_uart_index (UARTName uart_name );
43
+
41
44
/******************************************************************************
42
45
* INTERRUPTS HANDLING
43
46
******************************************************************************/
44
47
45
- static void uart_irq (int id )
48
+ static void uart_irq (UARTName uart_name )
46
49
{
47
- UART_HandleTypeDef * huart = & uart_handlers [id ];
48
-
49
- if (serial_irq_ids [id ] != 0 ) {
50
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
51
- if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
52
- irq_handler (serial_irq_ids [id ], TxIrq );
50
+ int8_t id = get_uart_index (uart_name );
51
+
52
+ if (id >= 0 ) {
53
+ UART_HandleTypeDef * huart = & uart_handlers [id ];
54
+ if (serial_irq_ids [id ] != 0 ) {
55
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
56
+ if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
57
+ irq_handler (serial_irq_ids [id ], TxIrq );
58
+ }
53
59
}
54
- }
55
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_RXNE ) != RESET ) {
56
- if ( __HAL_UART_GET_IT ( huart , UART_IT_RXNE ) != RESET ) {
57
- irq_handler ( serial_irq_ids [ id ], RxIrq );
58
- /* Flag has been cleared when reading the content */
60
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_RXNE ) != RESET ) {
61
+ if (__HAL_UART_GET_IT (huart , UART_IT_RXNE ) != RESET ) {
62
+ irq_handler ( serial_irq_ids [ id ], RxIrq );
63
+ /* Flag has been cleared when reading the content */
64
+ }
59
65
}
60
- }
61
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
62
- if ( __HAL_UART_GET_IT ( huart , UART_IT_ORE ) != RESET ) {
63
- __HAL_UART_CLEAR_IT ( huart , UART_CLEAR_OREF );
66
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_ORE ) != RESET ) {
67
+ if (__HAL_UART_GET_IT (huart , UART_IT_ORE ) != RESET ) {
68
+ __HAL_UART_CLEAR_IT ( huart , UART_CLEAR_OREF );
69
+ }
64
70
}
65
71
}
66
72
}
67
73
}
68
74
75
+ #if defined(USART1_BASE )
69
76
static void uart1_irq (void )
70
77
{
71
- uart_irq (0 );
78
+ uart_irq (UART_1 );
72
79
}
80
+ #endif
73
81
82
+ #if defined(USART2_BASE )
74
83
static void uart2_irq (void )
75
84
{
76
- uart_irq (1 );
85
+ uart_irq (UART_2 );
77
86
}
87
+ #endif
78
88
79
89
#if defined(USART3_BASE )
80
90
static void uart3_irq (void )
81
91
{
82
- uart_irq (2 );
92
+ uart_irq (UART_3 );
83
93
}
84
94
#endif
85
95
86
96
#if defined(UART4_BASE )
87
97
static void uart4_irq (void )
88
98
{
89
- uart_irq (3 );
99
+ uart_irq (UART_4 );
90
100
}
91
101
#endif
92
102
93
103
#if defined(UART5_BASE )
94
104
static void uart5_irq (void )
95
105
{
96
- uart_irq (4 );
106
+ uart_irq (UART_5 );
97
107
}
98
108
#endif
99
109
110
+ #if defined(USART6_BASE )
100
111
static void uart6_irq (void )
101
112
{
102
- uart_irq (5 );
113
+ uart_irq (UART_6 );
103
114
}
115
+ #endif
104
116
105
117
#if defined(UART7_BASE )
106
118
static void uart7_irq (void )
107
119
{
108
- uart_irq (6 );
120
+ uart_irq (UART_7 );
109
121
}
110
122
#endif
111
123
112
124
#if defined(UART8_BASE )
113
125
static void uart8_irq (void )
114
126
{
115
- uart_irq (7 );
127
+ uart_irq (UART_8 );
116
128
}
117
129
#endif
118
130
@@ -132,15 +144,18 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
132
144
uint32_t vector = 0 ;
133
145
134
146
switch (obj_s -> uart ) {
147
+ #if defined(USART1_BASE )
135
148
case UART_1 :
136
149
irq_n = USART1_IRQn ;
137
150
vector = (uint32_t )& uart1_irq ;
138
151
break ;
139
-
152
+ #endif
153
+ #if defined(USART2_BASE )
140
154
case UART_2 :
141
155
irq_n = USART2_IRQn ;
142
156
vector = (uint32_t )& uart2_irq ;
143
157
break ;
158
+ #endif
144
159
#if defined(USART3_BASE )
145
160
case UART_3 :
146
161
irq_n = USART3_IRQn ;
@@ -159,10 +174,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
159
174
vector = (uint32_t )& uart5_irq ;
160
175
break ;
161
176
#endif
177
+ #if defined(USART6_BASE )
162
178
case UART_6 :
163
179
irq_n = USART6_IRQn ;
164
180
vector = (uint32_t )& uart6_irq ;
165
181
break ;
182
+ #endif
166
183
#if defined(UART7_BASE )
167
184
case UART_7 :
168
185
irq_n = UART7_IRQn ;
@@ -318,55 +335,58 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
318
335
/**
319
336
* Get index of serial object TX IRQ, relating it to the physical peripheral.
320
337
*
321
- * @param obj pointer to serial object
338
+ * @param uart_name i.e. UART_1, UART_2, ...
322
339
* @return internal NVIC TX IRQ index of U(S)ART peripheral
323
340
*/
324
- static IRQn_Type serial_get_irq_n (serial_t * obj )
341
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
325
342
{
326
- struct serial_s * obj_s = SERIAL_S (obj );
327
343
IRQn_Type irq_n ;
328
344
329
- switch (obj_s -> index ) {
330
-
331
- case 0 :
345
+ switch (uart_name ) {
346
+ #if defined( USART1_BASE )
347
+ case UART_1 :
332
348
irq_n = USART1_IRQn ;
333
349
break ;
334
-
335
- case 1 :
350
+ #endif
351
+ #if defined(USART2_BASE )
352
+ case UART_2 :
336
353
irq_n = USART2_IRQn ;
337
354
break ;
355
+ #endif
338
356
#if defined(USART3_BASE )
339
- case 2 :
357
+ case UART_3 :
340
358
irq_n = USART3_IRQn ;
341
359
break ;
342
360
#endif
343
361
#if defined(UART4_BASE )
344
- case 3 :
362
+ case UART_4 :
345
363
irq_n = UART4_IRQn ;
346
364
break ;
347
365
#endif
348
366
#if defined(UART5_BASE )
349
- case 4 :
367
+ case UART_5 :
350
368
irq_n = UART5_IRQn ;
351
369
break ;
352
370
#endif
353
- case 5 :
371
+ #if defined(USART6_BASE )
372
+ case UART_6 :
354
373
irq_n = USART6_IRQn ;
355
374
break ;
375
+ #endif
356
376
#if defined(UART7_BASE )
357
- case 6 :
377
+ case UART_7 :
358
378
irq_n = UART7_IRQn ;
359
379
break ;
360
380
#endif
361
381
#if defined(UART8_BASE )
362
- case 7 :
382
+ case UART_8 :
363
383
irq_n = UART8_IRQn ;
364
384
break ;
365
385
#endif
366
386
default :
367
387
irq_n = (IRQn_Type )0 ;
368
388
}
369
-
389
+
370
390
return irq_n ;
371
391
}
372
392
@@ -411,7 +431,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
411
431
serial_enable_event (obj , event , 1 ); // Set only the wanted events
412
432
413
433
// Enable interrupt
414
- IRQn_Type irq_n = serial_get_irq_n (obj );
434
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
415
435
NVIC_ClearPendingIRQ (irq_n );
416
436
NVIC_DisableIRQ (irq_n );
417
437
NVIC_SetPriority (irq_n , 1 );
@@ -461,7 +481,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
461
481
462
482
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
463
483
464
- IRQn_Type irq_n = serial_get_irq_n (obj );
484
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
465
485
NVIC_ClearPendingIRQ (irq_n );
466
486
NVIC_DisableIRQ (irq_n );
467
487
NVIC_SetPriority (irq_n , 0 );
0 commit comments