@@ -39,83 +39,93 @@ 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 , USART_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 , USART_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
80
90
#if defined(USART3_BASE )
81
91
static void uart3_irq (void )
82
92
{
83
- uart_irq (2 );
93
+ uart_irq (UART_3 );
84
94
}
85
95
#endif
86
96
87
97
#if defined(UART4_BASE )
88
98
static void uart4_irq (void )
89
99
{
90
- uart_irq (3 );
100
+ uart_irq (UART_4 );
91
101
}
92
102
#endif
93
103
94
104
#if defined(UART5_BASE )
95
105
static void uart5_irq (void )
96
106
{
97
- uart_irq (4 );
107
+ uart_irq (UART_5 );
98
108
}
99
109
#endif
100
110
101
111
#if defined(USART6_BASE )
102
112
static void uart6_irq (void )
103
113
{
104
- uart_irq (5 );
114
+ uart_irq (UART_6 );
105
115
}
106
116
#endif
107
117
108
118
#if defined(UART7_BASE )
109
119
static void uart7_irq (void )
110
120
{
111
- uart_irq (6 );
121
+ uart_irq (UART_7 );
112
122
}
113
123
#endif
114
124
115
125
#if defined(UART8_BASE )
116
126
static void uart8_irq (void )
117
127
{
118
- uart_irq (7 );
128
+ uart_irq (UART8 );
119
129
}
120
130
#endif
121
131
@@ -134,48 +144,51 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
134
144
IRQn_Type irq_n = (IRQn_Type )0 ;
135
145
uint32_t vector = 0 ;
136
146
137
- switch (obj_s -> index ) {
138
- case 0 :
147
+ switch (obj_s -> uart ) {
148
+ #if defined(USART1_BASE )
149
+ case UART_1 :
139
150
irq_n = USART1_IRQn ;
140
151
vector = (uint32_t )& uart1_irq ;
141
152
break ;
142
-
143
- case 1 :
153
+ #endif
154
+ #if defined(USART2_BASE )
155
+ case UART_2 :
144
156
irq_n = USART2_IRQn ;
145
157
vector = (uint32_t )& uart2_irq ;
146
158
break ;
159
+ #endif
147
160
#if defined(USART3_BASE )
148
- case 2 :
161
+ case UART_3 :
149
162
irq_n = USART3_IRQn ;
150
163
vector = (uint32_t )& uart3_irq ;
151
164
break ;
152
165
#endif
153
166
#if defined(UART4_BASE )
154
- case 3 :
167
+ case UART_4 :
155
168
irq_n = UART4_IRQn ;
156
169
vector = (uint32_t )& uart4_irq ;
157
170
break ;
158
171
#endif
159
172
#if defined(UART5_BASE )
160
- case 4 :
173
+ case UART_5 :
161
174
irq_n = UART5_IRQn ;
162
175
vector = (uint32_t )& uart5_irq ;
163
176
break ;
164
177
#endif
165
178
#if defined(USART6_BASE )
166
- case 5 :
179
+ case UART_6 :
167
180
irq_n = USART6_IRQn ;
168
181
vector = (uint32_t )& uart6_irq ;
169
182
break ;
170
183
#endif
171
184
#if defined(UART7_BASE )
172
- case 6 :
185
+ case UART_7 :
173
186
irq_n = UART7_IRQn ;
174
187
vector = (uint32_t )& uart7_irq ;
175
188
break ;
176
189
#endif
177
190
#if defined(UART8_BASE )
178
- case 7 :
191
+ case UART_8 :
179
192
irq_n = UART8_IRQn ;
180
193
vector = (uint32_t )& uart8_irq ;
181
194
break ;
@@ -188,8 +201,8 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
188
201
} else { // TxIrq
189
202
__HAL_UART_ENABLE_IT (huart , UART_IT_TXE );
190
203
}
191
- NVIC_SetVector (irq_n , vector );
192
- NVIC_EnableIRQ (irq_n );
204
+ NVIC_SetVector (irq_n , vector );
205
+ NVIC_EnableIRQ (irq_n );
193
206
194
207
} else { // disable
195
208
int all_disabled = 0 ;
@@ -323,55 +336,54 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
323
336
/**
324
337
* Get index of serial object TX IRQ, relating it to the physical peripheral.
325
338
*
326
- * @param obj pointer to serial object
339
+ * @param uart_name i.e. UART_1, UART_2, ...
327
340
* @return internal NVIC TX IRQ index of U(S)ART peripheral
328
341
*/
329
- static IRQn_Type serial_get_irq_n (serial_t * obj )
342
+ static IRQn_Type serial_get_irq_n (UARTName uart_name )
330
343
{
331
- struct serial_s * obj_s = SERIAL_S (obj );
332
344
IRQn_Type irq_n ;
333
345
334
- switch (obj_s -> index ) {
346
+ switch (uart_name ) {
335
347
#if defined(USART1_BASE )
336
- case 0 :
348
+ case UART_1 :
337
349
irq_n = USART1_IRQn ;
338
350
break ;
339
351
#endif
340
352
#if defined(USART2_BASE )
341
- case 1 :
353
+ case UART_2 :
342
354
irq_n = USART2_IRQn ;
343
355
break ;
344
356
#endif
345
357
#if defined(USART3_BASE )
346
- case 2 :
358
+ case UART_3 :
347
359
irq_n = USART3_IRQn ;
348
360
break ;
349
361
#endif
350
362
#if defined(UART4_BASE )
351
- case 3 :
363
+ case UART_4 :
352
364
irq_n = UART4_IRQn ;
353
365
break ;
354
366
#endif
355
367
#if defined(UART5_BASE )
356
- case 4 :
368
+ case UART_5 :
357
369
irq_n = UART5_IRQn ;
358
370
break ;
359
371
#endif
360
372
#if defined(USART6_BASE )
361
- case 5 :
373
+ case UART_6 :
362
374
irq_n = USART6_IRQn ;
363
375
break ;
364
376
#endif
365
377
#if defined(UART7_BASE )
366
- case 6 :
378
+ case UART_7 :
367
379
irq_n = UART7_IRQn ;
368
380
break ;
369
381
#endif
370
382
#if defined(UART8_BASE )
371
- case 7 :
383
+ case UART_8 :
372
384
irq_n = UART8_IRQn ;
373
385
break ;
374
- #endif
386
+ #endif
375
387
default :
376
388
irq_n = (IRQn_Type )0 ;
377
389
}
@@ -421,7 +433,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
421
433
serial_enable_event (obj , event , 1 ); // Set only the wanted events
422
434
423
435
// Enable interrupt
424
- IRQn_Type irq_n = serial_get_irq_n (obj );
436
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
425
437
NVIC_ClearPendingIRQ (irq_n );
426
438
NVIC_DisableIRQ (irq_n );
427
439
NVIC_SetPriority (irq_n , 1 );
@@ -471,7 +483,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
471
483
472
484
serial_rx_buffer_set (obj , rx , rx_length , rx_width );
473
485
474
- IRQn_Type irq_n = serial_get_irq_n (obj );
486
+ IRQn_Type irq_n = serial_get_irq_n (obj_s -> uart );
475
487
NVIC_ClearPendingIRQ (irq_n );
476
488
NVIC_DisableIRQ (irq_n );
477
489
NVIC_SetPriority (irq_n , 0 );
@@ -626,7 +638,7 @@ void serial_tx_abort_asynch(serial_t *obj)
626
638
627
639
// clear flags
628
640
__HAL_UART_CLEAR_FLAG (huart , UART_FLAG_TC );
629
-
641
+
630
642
// reset states
631
643
huart -> TxXferCount = 0 ;
632
644
// update handle state
0 commit comments