@@ -43,62 +43,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
43
43
44
44
static uart_irq_handler irq_handler ;
45
45
46
+ // Defined in serial_api.c
47
+ inline int8_t get_uart_index (UARTName uart_name );
48
+
46
49
/******************************************************************************
47
50
* INTERRUPTS HANDLING
48
51
******************************************************************************/
49
52
50
- static void uart_irq (int id )
53
+ static void uart_irq (UARTName uart_name )
51
54
{
52
- UART_HandleTypeDef * huart = & uart_handlers [id ];
53
-
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
- }
59
- }
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 */
55
+ int8_t id = get_uart_index (uart_name );
56
+
57
+ if (id >= 0 ) {
58
+ UART_HandleTypeDef * huart = & uart_handlers [id ];
59
+ if (serial_irq_ids [id ] != 0 ) {
60
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_TXE ) != RESET ) {
61
+ if (__HAL_UART_GET_IT (huart , UART_IT_TXE ) != RESET ) {
62
+ irq_handler (serial_irq_ids [id ], TxIrq );
64
63
}
65
- }
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_FLAG (huart , UART_CLEAR_OREF );
64
+ }
65
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_RXNE ) != RESET ) {
66
+ if (__HAL_UART_GET_IT (huart , UART_IT_RXNE ) != RESET ) {
67
+ irq_handler (serial_irq_ids [id ], RxIrq );
68
+ /* Flag has been cleared when reading the content */
69
+ }
70
+ }
71
+ if (__HAL_UART_GET_FLAG (huart , UART_FLAG_ORE ) != RESET ) {
72
+ if (__HAL_UART_GET_IT (huart , UART_IT_ORE ) != RESET ) {
73
+ __HAL_UART_CLEAR_FLAG (huart , UART_CLEAR_OREF );
74
+ }
69
75
}
70
76
}
71
77
}
72
78
}
73
79
80
+ #if defined(USART1_BASE )
74
81
static void uart1_irq (void )
75
82
{
76
- uart_irq (0 );
83
+ uart_irq (UART_1 );
77
84
}
85
+ #endif
78
86
87
+ #if defined(USART2_BASE )
79
88
static void uart2_irq (void )
80
89
{
81
- uart_irq (1 );
90
+ uart_irq (UART_2 );
82
91
}
92
+ #endif
83
93
84
94
#if defined(USART3_BASE )
85
95
static void uart3_irq (void )
86
96
{
87
- uart_irq (2 );
97
+ uart_irq (UART_3 );
88
98
}
89
99
#endif
90
100
91
101
#if defined(UART4_BASE )
92
102
static void uart4_irq (void )
93
103
{
94
- uart_irq (3 );
104
+ uart_irq (UART_4 );
95
105
}
96
106
#endif
97
107
98
108
#if defined(UART5_BASE )
99
109
static void uart5_irq (void )
100
110
{
101
- uart_irq (4 );
111
+ uart_irq (UART_5 );
102
112
}
103
113
#endif
104
114
@@ -117,15 +127,19 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
117
127
IRQn_Type irq_n = (IRQn_Type )0 ;
118
128
uint32_t vector = 0 ;
119
129
130
+ #if defined(USART1_BASE )
120
131
if (obj_s -> uart == UART_1 ) {
121
132
irq_n = USART1_IRQn ;
122
133
vector = (uint32_t )& uart1_irq ;
123
134
}
135
+ #endif
124
136
137
+ #if defined(USART2_BASE )
125
138
if (obj_s -> uart == UART_2 ) {
126
139
irq_n = USART2_IRQn ;
127
140
vector = (uint32_t )& uart2_irq ;
128
141
}
142
+ #endif
129
143
130
144
#if defined(USART3_BASE )
131
145
if (obj_s -> uart == UART_3 ) {
@@ -297,42 +311,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
297
311
/**
298
312
* Get index of serial object TX IRQ, relating it to the physical peripheral.
299
313
*
300
- * @param obj pointer to serial object
314
+ * @param uart_name i.e. UART_1, UART_2, ...
301
315
* @return internal NVIC TX IRQ index of U(S)ART peripheral
302
316
*/
303
- static IRQn_Type serial_get_irq_n (serial_t * obj )
317
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
304
318
{
305
- struct serial_s * obj_s = SERIAL_S (obj );
306
319
IRQn_Type irq_n ;
307
320
308
- switch (obj_s -> index ) {
309
- case 0 :
321
+ switch (uart_name ) {
322
+ #if defined(USART1_BASE )
323
+ case UART_1 :
310
324
irq_n = USART1_IRQn ;
311
325
break ;
312
-
313
- case 1 :
326
+ #endif
327
+ #if defined(USART2_BASE )
328
+ case UART_2 :
314
329
irq_n = USART2_IRQn ;
315
330
break ;
316
-
331
+ #endif
317
332
#if defined(USART3_BASE )
318
- case 2 :
333
+ case UART_3 :
319
334
irq_n = USART3_IRQn ;
320
335
break ;
321
336
#endif
322
337
#if defined(UART4_BASE )
323
- case 3 :
338
+ case UART_4 :
324
339
irq_n = UART4_IRQn ;
325
340
break ;
326
341
#endif
327
342
#if defined(UART5_BASE )
328
- case 4 :
343
+ case UART_5 :
329
344
irq_n = UART5_IRQn ;
330
345
break ;
331
346
#endif
332
347
default :
333
348
irq_n = (IRQn_Type )0 ;
334
349
}
335
-
350
+
336
351
return irq_n ;
337
352
}
338
353
@@ -378,7 +393,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
378
393
serial_enable_event (obj , event , 1 ); // Set only the wanted events
379
394
380
395
// Enable interrupt
381
- IRQn_Type irq_n = serial_get_irq_n (obj );
396
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
382
397
NVIC_ClearPendingIRQ (irq_n );
383
398
NVIC_DisableIRQ (irq_n );
384
399
NVIC_SetPriority (irq_n , 1 );
@@ -428,7 +443,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
428
443
429
444
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
430
445
431
- IRQn_Type irq_n = serial_get_irq_n (obj );
446
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
432
447
NVIC_ClearPendingIRQ (irq_n );
433
448
NVIC_DisableIRQ (irq_n );
434
449
NVIC_SetPriority (irq_n , 0 );
@@ -586,7 +601,7 @@ void serial_tx_abort_asynch(serial_t *obj)
586
601
587
602
// clear flags
588
603
__HAL_UART_CLEAR_FLAG (huart , UART_CLEAR_TCF );
589
-
604
+
590
605
// reset states
591
606
huart -> TxXferCount = 0 ;
592
607
// update handle state
0 commit comments