@@ -39,59 +39,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
39
39
40
40
static uart_irq_handler irq_handler ;
41
41
42
+ // Defined in serial_api.c
43
+ inline int8_t get_uart_index (UARTName uart_name );
44
+
42
45
/******************************************************************************
43
46
* INTERRUPTS HANDLING
44
47
******************************************************************************/
45
48
46
- static void uart_irq (int id )
49
+ static void uart_irq (UARTName uart_name )
47
50
{
48
- UART_HandleTypeDef * huart = & uart_handlers [id ];
49
-
50
- if (serial_irq_ids [id ] != 0 ) {
51
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
52
- if (__HAL_UART_GET_IT_SOURCE (huart , UART_IT_TXE ) != RESET ) {
53
- irq_handler (serial_irq_ids [id ], TxIrq );
51
+ int8_t id = get_uart_index (uart_name );
52
+
53
+ if (id >= 0 ) {
54
+ UART_HandleTypeDef * huart = & uart_handlers [id ];
55
+ if (serial_irq_ids [id ] != 0 ) {
56
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
57
+ if (__HAL_UART_GET_IT_SOURCE (huart , UART_IT_TXE ) != RESET ) {
58
+ irq_handler (serial_irq_ids [id ], TxIrq );
59
+ }
54
60
}
55
- }
56
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_RXNE ) != RESET ) {
57
- if ( __HAL_UART_GET_IT_SOURCE ( huart , UART_IT_RXNE ) != RESET ) {
58
- irq_handler ( serial_irq_ids [ id ], RxIrq );
59
- /* Flag has been cleared when reading the content */
61
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_RXNE ) != RESET ) {
62
+ if (__HAL_UART_GET_IT_SOURCE (huart , UART_IT_RXNE ) != RESET ) {
63
+ irq_handler ( serial_irq_ids [ id ], RxIrq );
64
+ /* Flag has been cleared when reading the content */
65
+ }
60
66
}
61
- }
62
- if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
63
- if ( __HAL_UART_GET_IT_SOURCE ( huart , UART_IT_ERR ) != RESET ) {
64
- volatile uint32_t tmpval __attribute__(( unused )) = huart -> Instance -> DR ; // Clear ORE flag
67
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_ORE ) != RESET ) {
68
+ if (__HAL_UART_GET_IT_SOURCE (huart , UART_IT_ERR ) != RESET ) {
69
+ volatile uint32_t tmpval __attribute__(( unused )) = huart -> Instance -> DR ; // Clear ORE flag
70
+ }
65
71
}
66
72
}
67
73
}
68
74
}
69
75
76
+ #if defined(USART1_BASE )
70
77
static void uart1_irq (void )
71
78
{
72
- uart_irq (0 );
79
+ uart_irq (UART_1 );
73
80
}
81
+ #endif
74
82
83
+ #if defined(USART2_BASE )
75
84
static void uart2_irq (void )
76
85
{
77
- uart_irq (1 );
86
+ uart_irq (UART_2 );
78
87
}
88
+ #endif
79
89
90
+ #if defined(USART3_BASE )
80
91
static void uart3_irq (void )
81
92
{
82
- uart_irq (2 );
93
+ uart_irq (UART_3 );
83
94
}
95
+ #endif
84
96
85
97
#if defined(UART4_BASE )
86
98
static void uart4_irq (void )
87
99
{
88
- uart_irq (3 );
100
+ uart_irq (UART_4 );
89
101
}
90
102
#endif
103
+
91
104
#if defined(UART5_BASE )
92
105
static void uart5_irq (void )
93
106
{
94
- uart_irq (4 );
107
+ uart_irq (UART_5 );
95
108
}
96
109
#endif
97
110
@@ -110,32 +123,38 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
110
123
IRQn_Type irq_n = (IRQn_Type )0 ;
111
124
uint32_t vector = 0 ;
112
125
113
- if (obj_s -> uart == UART_1 ) {
114
- irq_n = USART1_IRQn ;
115
- vector = (uint32_t )& uart1_irq ;
116
- }
117
-
118
- if (obj_s -> uart == UART_2 ) {
119
- irq_n = USART2_IRQn ;
120
- vector = (uint32_t )& uart2_irq ;
121
- }
122
-
123
- if (obj_s -> uart == UART_3 ) {
124
- irq_n = USART3_IRQn ;
125
- vector = (uint32_t )& uart3_irq ;
126
- }
126
+ switch (obj_s -> uart ) {
127
+ #if defined(USART1_BASE )
128
+ case UART_1 :
129
+ irq_n = USART1_IRQn ;
130
+ vector = (uint32_t )& uart1_irq ;
131
+ break ;
132
+ #endif
133
+ #if defined(USART2_BASE )
134
+ case UART_2 :
135
+ irq_n = USART2_IRQn ;
136
+ vector = (uint32_t )& uart2_irq ;
137
+ break ;
138
+ #endif
139
+ #if defined(USART3_BASE )
140
+ case UART_3 :
141
+ irq_n = USART3_IRQn ;
142
+ vector = (uint32_t )& uart3_irq ;
143
+ break ;
144
+ #endif
127
145
#if defined(UART4_BASE )
128
- if ( obj_s -> uart == UART_4 ) {
129
- irq_n = UART4_IRQn ;
130
- vector = (uint32_t )& uart4_irq ;
131
- }
146
+ case UART_4 :
147
+ irq_n = UART4_IRQn ;
148
+ vector = (uint32_t )& uart4_irq ;
149
+ break ;
132
150
#endif
133
151
#if defined(UART5_BASE )
134
- if ( obj_s -> uart == UART_5 ) {
135
- irq_n = UART5_IRQn ;
136
- vector = (uint32_t )& uart5_irq ;
137
- }
152
+ case UART_5 :
153
+ irq_n = UART5_IRQn ;
154
+ vector = (uint32_t )& uart5_irq ;
155
+ break ;
138
156
#endif
157
+ }
139
158
140
159
if (enable ) {
141
160
if (irq == RxIrq ) {
@@ -286,40 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
286
305
/**
287
306
* Get index of serial object TX IRQ, relating it to the physical peripheral.
288
307
*
289
- * @param obj pointer to serial object
308
+ * @param uart_name i.e. UART_1, UART_2, ...
290
309
* @return internal NVIC TX IRQ index of U(S)ART peripheral
291
310
*/
292
- static IRQn_Type serial_get_irq_n (serial_t * obj )
311
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
293
312
{
294
- struct serial_s * obj_s = SERIAL_S (obj );
295
313
IRQn_Type irq_n ;
296
314
297
- switch (obj_s -> index ) {
298
- case 0 :
315
+ switch (uart_name ) {
316
+ #if defined(USART1_BASE )
317
+ case UART_1 :
299
318
irq_n = USART1_IRQn ;
300
319
break ;
301
-
302
- case 1 :
320
+ #endif
321
+ #if defined(USART2_BASE )
322
+ case UART_2 :
303
323
irq_n = USART2_IRQn ;
304
324
break ;
305
-
306
- case 2 :
325
+ #endif
326
+ #if defined(USART3_BASE )
327
+ case UART_3 :
307
328
irq_n = USART3_IRQn ;
308
329
break ;
330
+ #endif
309
331
#if defined(UART4_BASE )
310
- case 3 :
332
+ case UART_4 :
311
333
irq_n = UART4_IRQn ;
312
334
break ;
313
335
#endif
314
336
#if defined(UART5_BASE )
315
- case 4 :
337
+ case UART_5 :
316
338
irq_n = UART5_IRQn ;
317
339
break ;
318
340
#endif
319
341
default :
320
342
irq_n = (IRQn_Type )0 ;
321
343
}
322
-
344
+
323
345
return irq_n ;
324
346
}
325
347
@@ -364,7 +386,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
364
386
serial_enable_event (obj , event , 1 ); // Set only the wanted events
365
387
366
388
// Enable interrupt
367
- IRQn_Type irq_n = serial_get_irq_n (obj );
389
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
368
390
NVIC_ClearPendingIRQ (irq_n );
369
391
NVIC_DisableIRQ (irq_n );
370
392
NVIC_SetPriority (irq_n , 1 );
@@ -414,7 +436,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
414
436
415
437
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
416
438
417
- IRQn_Type irq_n = serial_get_irq_n (obj );
439
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
418
440
NVIC_ClearPendingIRQ (irq_n );
419
441
NVIC_DisableIRQ (irq_n );
420
442
NVIC_SetPriority (irq_n , 0 );
0 commit comments