@@ -45,29 +45,35 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
45
45
46
46
static uart_irq_handler irq_handler ;
47
47
48
+ // Defined in serial_api.c
49
+ inline int8_t get_uart_index (UARTName uart_name );
50
+
48
51
/******************************************************************************
49
52
* INTERRUPTS HANDLING
50
53
******************************************************************************/
51
54
52
- static void uart_irq (int id )
55
+ static void uart_irq (UARTName uart_name )
53
56
{
54
- UART_HandleTypeDef * huart = & uart_handlers [id ];
55
-
56
- if (serial_irq_ids [id ] != 0 ) {
57
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
58
- if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
59
- irq_handler (serial_irq_ids [id ], TxIrq );
57
+ int8_t id = get_uart_index (uart_name );
58
+
59
+ if (id >= 0 ) {
60
+ UART_HandleTypeDef * huart = & uart_handlers [id ];
61
+ if (serial_irq_ids [id ] != 0 ) {
62
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
63
+ if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
64
+ irq_handler (serial_irq_ids [id ], TxIrq );
65
+ }
60
66
}
61
- }
62
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_RXNE ) != RESET ) {
63
- if ( __HAL_UART_GET_IT ( huart , UART_IT_RXNE ) != RESET ) {
64
- irq_handler ( serial_irq_ids [ id ], RxIrq );
65
- /* Flag has been cleared when reading the content */
67
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_RXNE ) != RESET ) {
68
+ if (__HAL_UART_GET_IT (huart , UART_IT_RXNE ) != RESET ) {
69
+ irq_handler ( serial_irq_ids [ id ], RxIrq );
70
+ /* Flag has been cleared when reading the content */
71
+ }
66
72
}
67
- }
68
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
69
- if ( __HAL_UART_GET_IT ( huart , UART_IT_ORE ) != RESET ) {
70
- __HAL_UART_CLEAR_FLAG ( huart , UART_CLEAR_OREF );
73
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_ORE ) != RESET ) {
74
+ if (__HAL_UART_GET_IT (huart , UART_IT_ORE ) != RESET ) {
75
+ __HAL_UART_CLEAR_FLAG ( huart , UART_CLEAR_OREF );
76
+ }
71
77
}
72
78
}
73
79
}
@@ -76,31 +82,35 @@ static void uart_irq(int id)
76
82
#if defined(USART1_BASE )
77
83
static void uart1_irq (void )
78
84
{
79
- uart_irq (0 );
85
+ uart_irq (UART_1 );
80
86
}
81
87
#endif
82
88
89
+ #if defined(USART2_BASE )
83
90
static void uart2_irq (void )
84
91
{
85
- uart_irq (1 );
86
- }
87
-
88
- static void lpuart1_irq (void )
89
- {
90
- uart_irq (2 );
92
+ uart_irq (UART_2 );
91
93
}
94
+ #endif
92
95
93
96
#if defined(USART4_BASE )
94
97
static void uart4_irq (void )
95
98
{
96
- uart_irq (3 );
99
+ uart_irq (UART_4 );
97
100
}
98
101
#endif
99
102
100
103
#if defined(USART5_BASE )
101
104
static void uart5_irq (void )
102
105
{
103
- uart_irq (4 );
106
+ uart_irq (UART_5 );
107
+ }
108
+ #endif
109
+
110
+ #if defined(LPUART1_BASE )
111
+ static void lpuart1_irq (void )
112
+ {
113
+ uart_irq (LPUART_1 );
104
114
}
105
115
#endif
106
116
@@ -126,15 +136,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
126
136
}
127
137
#endif
128
138
139
+ #if defined(USART2_BASE )
129
140
if (obj_s -> uart == UART_2 ) {
130
141
irq_n = USART2_IRQn ;
131
142
vector = (uint32_t )& uart2_irq ;
132
143
}
133
-
134
- if (obj_s -> uart == LPUART_1 ) {
135
- irq_n = RNG_LPUART1_IRQn ;
136
- vector = (uint32_t )& lpuart1_irq ;
137
- }
144
+ #endif
138
145
139
146
#if defined(USART4_BASE )
140
147
if (obj_s -> uart == UART_4 ) {
@@ -150,6 +157,13 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
150
157
}
151
158
#endif
152
159
160
+ #if defined(LPUART1_BASE )
161
+ if (obj_s -> uart == LPUART_1 ) {
162
+ irq_n = RNG_LPUART1_IRQn ;
163
+ vector = (uint32_t )& lpuart1_irq ;
164
+ }
165
+ #endif
166
+
153
167
if (enable ) {
154
168
if (irq == RxIrq ) {
155
169
__HAL_UART_ENABLE_IT (huart , UART_IT_RXNE );
@@ -291,41 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
291
305
/**
292
306
* Get index of serial object TX IRQ, relating it to the physical peripheral.
293
307
*
294
- * @param obj pointer to serial object
308
+ * @param uart_name i.e. UART_1, UART_2, ...
295
309
* @return internal NVIC TX IRQ index of U(S)ART peripheral
296
310
*/
297
- static IRQn_Type serial_get_irq_n (serial_t * obj )
311
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
298
312
{
299
- struct serial_s * obj_s = SERIAL_S (obj );
300
313
IRQn_Type irq_n ;
301
314
302
- switch (obj_s -> index ) {
315
+ switch (uart_name ) {
303
316
#if defined(USART1_BASE )
304
- case 0 :
317
+ case UART_1 :
305
318
irq_n = USART1_IRQn ;
306
319
break ;
307
320
#endif
308
- case 1 :
321
+ #if defined(USART2_BASE )
322
+ case UART_2 :
309
323
irq_n = USART2_IRQn ;
310
324
break ;
311
-
312
- case 2 :
313
- irq_n = RNG_LPUART1_IRQn ;
314
- break ;
325
+ #endif
315
326
#if defined(USART4_BASE )
316
- case 3 :
327
+ case UART_4 :
317
328
irq_n = USART4_5_IRQn ;
318
329
break ;
319
330
#endif
320
331
#if defined(USART5_BASE )
321
- case 4 :
332
+ case UART_5 :
322
333
irq_n = USART4_5_IRQn ;
323
334
break ;
335
+ #endif
336
+ #if defined(LPUART1_BASE )
337
+ case LPUART_1 :
338
+ irq_n = RNG_LPUART1_IRQn ;
339
+ break ;
324
340
#endif
325
341
default :
326
342
irq_n = (IRQn_Type )0 ;
327
343
}
328
-
344
+
329
345
return irq_n ;
330
346
}
331
347
@@ -371,7 +387,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
371
387
serial_enable_event (obj , event , 1 ); // Set only the wanted events
372
388
373
389
// Enable interrupt
374
- IRQn_Type irq_n = serial_get_irq_n (obj );
390
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
375
391
NVIC_ClearPendingIRQ (irq_n );
376
392
NVIC_DisableIRQ (irq_n );
377
393
NVIC_SetPriority (irq_n , 1 );
@@ -421,7 +437,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
421
437
422
438
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
423
439
424
- IRQn_Type irq_n = serial_get_irq_n (obj );
440
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
425
441
NVIC_ClearPendingIRQ (irq_n );
426
442
NVIC_DisableIRQ (irq_n );
427
443
NVIC_SetPriority (irq_n , 0 );
0 commit comments