@@ -45,69 +45,79 @@ 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
- volatile uint32_t tmpval __attribute__(( unused )) = huart -> Instance -> RDR ; // Clear ORE flag
73
+ if ( __HAL_UART_GET_FLAG ( huart , UART_FLAG_ORE ) != RESET ) {
74
+ if (__HAL_UART_GET_IT (huart , UART_IT_ORE ) != RESET ) {
75
+ volatile uint32_t tmpval __attribute__(( unused )) = huart -> Instance -> RDR ; // Clear ORE flag
76
+ }
71
77
}
72
78
}
73
79
}
74
80
}
75
81
82
+ #if defined(USART1_BASE )
76
83
static void uart1_irq (void )
77
84
{
78
- uart_irq (0 );
85
+ uart_irq (UART_1 );
79
86
}
87
+ #endif
80
88
89
+ #if defined(USART2_BASE )
81
90
static void uart2_irq (void )
82
91
{
83
- uart_irq (1 );
92
+ uart_irq (UART_2 );
84
93
}
94
+ #endif
85
95
86
96
#if defined(USART3_BASE )
87
97
static void uart3_irq (void )
88
98
{
89
- uart_irq (2 );
99
+ uart_irq (UART_3 );
90
100
}
91
101
#endif
92
102
93
103
#if defined(UART4_BASE )
94
104
static void uart4_irq (void )
95
105
{
96
- uart_irq (3 );
106
+ uart_irq (UART_4 );
97
107
}
98
108
#endif
99
109
100
110
#if defined(UART5_BASE )
101
111
static void uart5_irq (void )
102
112
{
103
- uart_irq (4 );
113
+ uart_irq (UART_5 );
104
114
}
105
115
#endif
106
116
107
117
#if defined(LPUART1_BASE )
108
118
static void lpuart1_irq (void )
109
119
{
110
- uart_irq (5 );
120
+ uart_irq (LPUART_1 );
111
121
}
112
122
#endif
113
123
@@ -126,41 +136,44 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
126
136
IRQn_Type irq_n = (IRQn_Type )0 ;
127
137
uint32_t vector = 0 ;
128
138
129
- if (obj_s -> uart == UART_1 ) {
130
- irq_n = USART1_IRQn ;
131
- vector = (uint32_t )& uart1_irq ;
132
- }
133
-
134
- if (obj_s -> uart == UART_2 ) {
135
- irq_n = USART2_IRQn ;
136
- vector = (uint32_t )& uart2_irq ;
137
- }
139
+ switch (obj_s -> uart ) {
140
+ #if defined(USART1_BASE )
141
+ case UART_1 :
142
+ irq_n = USART1_IRQn ;
143
+ vector = (uint32_t )& uart1_irq ;
144
+ break ;
145
+ #endif
146
+ #if defined(USART2_BASE )
147
+ case UART_2 :
148
+ irq_n = USART2_IRQn ;
149
+ vector = (uint32_t )& uart2_irq ;
150
+ break ;
151
+ #endif
138
152
#if defined(USART3_BASE )
139
- if ( obj_s -> uart == UART_3 ) {
140
- irq_n = USART3_IRQn ;
141
- vector = (uint32_t )& uart3_irq ;
142
- }
153
+ case UART_3 :
154
+ irq_n = USART3_IRQn ;
155
+ vector = (uint32_t )& uart3_irq ;
156
+ break ;
143
157
#endif
144
158
#if defined(UART4_BASE )
145
- if ( obj_s -> uart == UART_4 ) {
146
- irq_n = UART4_IRQn ;
147
- vector = (uint32_t )& uart4_irq ;
148
- }
159
+ case UART_4 :
160
+ irq_n = UART4_IRQn ;
161
+ vector = (uint32_t )& uart4_irq ;
162
+ break ;
149
163
#endif
150
-
151
164
#if defined(UART5_BASE )
152
- if ( obj_s -> uart == UART_5 ) {
153
- irq_n = UART5_IRQn ;
154
- vector = (uint32_t )& uart5_irq ;
155
- }
165
+ case UART_5 :
166
+ irq_n = UART5_IRQn ;
167
+ vector = (uint32_t )& uart5_irq ;
168
+ break ;
156
169
#endif
157
-
158
170
#if defined(LPUART1_BASE )
159
- if ( obj_s -> uart == LPUART_1 ) {
160
- irq_n = LPUART1_IRQn ;
161
- vector = (uint32_t )& lpuart1_irq ;
162
- }
171
+ case LPUART_1 :
172
+ irq_n = LPUART1_IRQn ;
173
+ vector = (uint32_t )& lpuart1_irq ;
174
+ break ;
163
175
#endif
176
+ }
164
177
165
178
if (enable ) {
166
179
if (irq == RxIrq ) {
@@ -311,48 +324,48 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
311
324
/**
312
325
* Get index of serial object TX IRQ, relating it to the physical peripheral.
313
326
*
314
- * @param obj pointer to serial object
327
+ * @param uart_name i.e. UART_1, UART_2, ...
315
328
* @return internal NVIC TX IRQ index of U(S)ART peripheral
316
329
*/
317
- static IRQn_Type serial_get_irq_n (serial_t * obj )
330
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
318
331
{
319
- struct serial_s * obj_s = SERIAL_S (obj );
320
332
IRQn_Type irq_n ;
321
333
322
- switch (obj_s -> index ) {
323
- case 0 :
334
+ switch (uart_name ) {
335
+ #if defined(USART1_BASE )
336
+ case UART_1 :
324
337
irq_n = USART1_IRQn ;
325
338
break ;
326
-
327
- case 1 :
339
+ #endif
340
+ #if defined(USART2_BASE )
341
+ case UART_2 :
328
342
irq_n = USART2_IRQn ;
329
343
break ;
330
-
344
+ #endif
331
345
#if defined(USART3_BASE )
332
- case 2 :
346
+ case UART_3 :
333
347
irq_n = USART3_IRQn ;
334
348
break ;
335
349
#endif
336
350
#if defined(UART4_BASE )
337
- case 3 :
351
+ case UART_4 :
338
352
irq_n = UART4_IRQn ;
339
353
break ;
340
354
#endif
341
355
#if defined(UART5_BASE )
342
- case 4 :
356
+ case UART_5 :
343
357
irq_n = UART5_IRQn ;
344
358
break ;
345
359
#endif
346
360
#if defined(LPUART1_BASE )
347
- case 5 :
361
+ case LPUART_1 :
348
362
irq_n = LPUART1_IRQn ;
349
363
break ;
350
364
#endif
351
-
352
365
default :
353
366
irq_n = (IRQn_Type )0 ;
354
367
}
355
-
368
+
356
369
return irq_n ;
357
370
}
358
371
@@ -397,7 +410,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
397
410
serial_enable_event (obj , event , 1 ); // Set only the wanted events
398
411
399
412
// Enable interrupt
400
- IRQn_Type irq_n = serial_get_irq_n (obj );
413
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
401
414
NVIC_ClearPendingIRQ (irq_n );
402
415
NVIC_DisableIRQ (irq_n );
403
416
NVIC_SetPriority (irq_n , 1 );
@@ -447,7 +460,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
447
460
448
461
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
449
462
450
- IRQn_Type irq_n = serial_get_irq_n (obj );
463
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
451
464
NVIC_ClearPendingIRQ (irq_n );
452
465
NVIC_DisableIRQ (irq_n );
453
466
NVIC_SetPriority (irq_n , 0 );
0 commit comments