@@ -39,48 +39,60 @@ 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
void serial_irq_handler (serial_t * obj , uart_irq_handler handler , uint32_t id )
86
98
{
@@ -97,20 +109,26 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
97
109
IRQn_Type irq_n = (IRQn_Type )0 ;
98
110
uint32_t vector = 0 ;
99
111
112
+ #if defined(USART1_BASE )
100
113
if (obj_s -> uart == UART_1 ) {
101
114
irq_n = USART1_IRQn ;
102
115
vector = (uint32_t )& uart1_irq ;
103
116
}
117
+ #endif
104
118
119
+ #if defined(USART2_BASE )
105
120
if (obj_s -> uart == UART_2 ) {
106
121
irq_n = USART2_IRQn ;
107
122
vector = (uint32_t )& uart2_irq ;
108
123
}
124
+ #endif
109
125
126
+ #if defined(USART3_BASE )
110
127
if (obj_s -> uart == UART_3 ) {
111
128
irq_n = USART3_IRQn ;
112
129
vector = (uint32_t )& uart3_irq ;
113
130
}
131
+ #endif
114
132
115
133
if (enable ) {
116
134
if (irq == RxIrq ) {
@@ -261,31 +279,33 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
261
279
/**
262
280
* Get index of serial object TX IRQ, relating it to the physical peripheral.
263
281
*
264
- * @param obj pointer to serial object
282
+ * @param uart_name i.e. UART_1, UART_2, ...
265
283
* @return internal NVIC TX IRQ index of U(S)ART peripheral
266
284
*/
267
- static IRQn_Type serial_get_irq_n (serial_t * obj )
285
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
268
286
{
269
- struct serial_s * obj_s = SERIAL_S (obj );
270
287
IRQn_Type irq_n ;
271
288
272
- switch (obj_s -> index ) {
273
- case 0 :
289
+ switch (uart_name ) {
290
+ #if defined(USART1_BASE )
291
+ case UART_1 :
274
292
irq_n = USART1_IRQn ;
275
293
break ;
276
-
277
- case 1 :
294
+ #endif
295
+ #if defined(USART2_BASE )
296
+ case UART_2 :
278
297
irq_n = USART2_IRQn ;
279
298
break ;
280
-
281
- case 2 :
299
+ #endif
300
+ #if defined(USART3_BASE )
301
+ case UART_3 :
282
302
irq_n = USART3_IRQn ;
283
303
break ;
284
-
304
+ #endif
285
305
default :
286
306
irq_n = (IRQn_Type )0 ;
287
307
}
288
-
308
+
289
309
return irq_n ;
290
310
}
291
311
@@ -330,7 +350,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
330
350
serial_enable_event (obj , event , 1 ); // Set only the wanted events
331
351
332
352
// Enable interrupt
333
- IRQn_Type irq_n = serial_get_irq_n (obj );
353
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
334
354
NVIC_ClearPendingIRQ (irq_n );
335
355
NVIC_DisableIRQ (irq_n );
336
356
NVIC_SetPriority (irq_n , 1 );
@@ -380,7 +400,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
380
400
381
401
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
382
402
383
- IRQn_Type irq_n = serial_get_irq_n (obj );
403
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
384
404
NVIC_ClearPendingIRQ (irq_n );
385
405
NVIC_DisableIRQ (irq_n );
386
406
NVIC_SetPriority (irq_n , 0 );
@@ -471,8 +491,8 @@ int serial_irq_handler_asynch(serial_t *obj)
471
491
if (__HAL_UART_GET_IT_SOURCE (huart , USART_IT_ERR ) != RESET ) {
472
492
return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s -> events );
473
493
}
474
- }
475
-
494
+ }
495
+
476
496
if (__HAL_UART_GET_FLAG (huart , UART_FLAG_FE ) != RESET ) {
477
497
if (__HAL_UART_GET_IT_SOURCE (huart , USART_IT_ERR ) != RESET ) {
478
498
return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s -> events );
@@ -535,7 +555,7 @@ void serial_tx_abort_asynch(serial_t *obj)
535
555
536
556
// clear flags
537
557
__HAL_UART_CLEAR_FLAG (huart , UART_FLAG_TC );
538
-
558
+
539
559
// reset states
540
560
huart -> TxXferCount = 0 ;
541
561
// update handle state
0 commit comments