Skip to content

Commit 1fa0557

Browse files
committed
STM32 serial: improve irq index management for F2 devices
1 parent 6974da2 commit 1fa0557

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

targets/TARGET_STM/TARGET_STM32F2/serial_device.c

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,83 +39,93 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
3939

4040
static uart_irq_handler irq_handler;
4141

42+
// Defined in serial_api.c
43+
inline int8_t get_uart_index(UARTName uart_name);
44+
4245
/******************************************************************************
4346
* INTERRUPTS HANDLING
4447
******************************************************************************/
4548

46-
static void uart_irq(int id)
49+
static void uart_irq(UARTName uart_name)
4750
{
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+
}
5460
}
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+
}
6066
}
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+
}
6571
}
6672
}
6773
}
6874
}
6975

76+
#if defined(USART1_BASE)
7077
static void uart1_irq(void)
7178
{
72-
uart_irq(0);
79+
uart_irq(UART_1);
7380
}
81+
#endif
7482

83+
#if defined(USART2_BASE)
7584
static void uart2_irq(void)
7685
{
77-
uart_irq(1);
86+
uart_irq(UART_2);
7887
}
88+
#endif
7989

8090
#if defined(USART3_BASE)
8191
static void uart3_irq(void)
8292
{
83-
uart_irq(2);
93+
uart_irq(UART_3);
8494
}
8595
#endif
8696

8797
#if defined(UART4_BASE)
8898
static void uart4_irq(void)
8999
{
90-
uart_irq(3);
100+
uart_irq(UART_4);
91101
}
92102
#endif
93103

94104
#if defined(UART5_BASE)
95105
static void uart5_irq(void)
96106
{
97-
uart_irq(4);
107+
uart_irq(UART_5);
98108
}
99109
#endif
100110

101111
#if defined(USART6_BASE)
102112
static void uart6_irq(void)
103113
{
104-
uart_irq(5);
114+
uart_irq(UART_6);
105115
}
106116
#endif
107117

108118
#if defined(UART7_BASE)
109119
static void uart7_irq(void)
110120
{
111-
uart_irq(6);
121+
uart_irq(UART_7);
112122
}
113123
#endif
114124

115125
#if defined(UART8_BASE)
116126
static void uart8_irq(void)
117127
{
118-
uart_irq(7);
128+
uart_irq(UART8);
119129
}
120130
#endif
121131

@@ -134,48 +144,51 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
134144
IRQn_Type irq_n = (IRQn_Type)0;
135145
uint32_t vector = 0;
136146

137-
switch (obj_s->index) {
138-
case 0:
147+
switch (obj_s->uart) {
148+
#if defined(USART1_BASE)
149+
case UART_1:
139150
irq_n = USART1_IRQn;
140151
vector = (uint32_t)&uart1_irq;
141152
break;
142-
143-
case 1:
153+
#endif
154+
#if defined(USART2_BASE)
155+
case UART_2:
144156
irq_n = USART2_IRQn;
145157
vector = (uint32_t)&uart2_irq;
146158
break;
159+
#endif
147160
#if defined(USART3_BASE)
148-
case 2:
161+
case UART_3:
149162
irq_n = USART3_IRQn;
150163
vector = (uint32_t)&uart3_irq;
151164
break;
152165
#endif
153166
#if defined(UART4_BASE)
154-
case 3:
167+
case UART_4:
155168
irq_n = UART4_IRQn;
156169
vector = (uint32_t)&uart4_irq;
157170
break;
158171
#endif
159172
#if defined(UART5_BASE)
160-
case 4:
173+
case UART_5:
161174
irq_n = UART5_IRQn;
162175
vector = (uint32_t)&uart5_irq;
163176
break;
164177
#endif
165178
#if defined(USART6_BASE)
166-
case 5:
179+
case UART_6:
167180
irq_n = USART6_IRQn;
168181
vector = (uint32_t)&uart6_irq;
169182
break;
170183
#endif
171184
#if defined(UART7_BASE)
172-
case 6:
185+
case UART_7:
173186
irq_n = UART7_IRQn;
174187
vector = (uint32_t)&uart7_irq;
175188
break;
176189
#endif
177190
#if defined(UART8_BASE)
178-
case 7:
191+
case UART_8:
179192
irq_n = UART8_IRQn;
180193
vector = (uint32_t)&uart8_irq;
181194
break;
@@ -188,8 +201,8 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
188201
} else { // TxIrq
189202
__HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
190203
}
191-
NVIC_SetVector(irq_n, vector);
192-
NVIC_EnableIRQ(irq_n);
204+
NVIC_SetVector(irq_n, vector);
205+
NVIC_EnableIRQ(irq_n);
193206

194207
} else { // disable
195208
int all_disabled = 0;
@@ -323,55 +336,54 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
323336
/**
324337
* Get index of serial object TX IRQ, relating it to the physical peripheral.
325338
*
326-
* @param obj pointer to serial object
339+
* @param uart_name i.e. UART_1, UART_2, ...
327340
* @return internal NVIC TX IRQ index of U(S)ART peripheral
328341
*/
329-
static IRQn_Type serial_get_irq_n(serial_t *obj)
342+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
330343
{
331-
struct serial_s *obj_s = SERIAL_S(obj);
332344
IRQn_Type irq_n;
333345

334-
switch (obj_s->index) {
346+
switch (uart_name) {
335347
#if defined(USART1_BASE)
336-
case 0:
348+
case UART_1:
337349
irq_n = USART1_IRQn;
338350
break;
339351
#endif
340352
#if defined(USART2_BASE)
341-
case 1:
353+
case UART_2:
342354
irq_n = USART2_IRQn;
343355
break;
344356
#endif
345357
#if defined(USART3_BASE)
346-
case 2:
358+
case UART_3:
347359
irq_n = USART3_IRQn;
348360
break;
349361
#endif
350362
#if defined(UART4_BASE)
351-
case 3:
363+
case UART_4:
352364
irq_n = UART4_IRQn;
353365
break;
354366
#endif
355367
#if defined(UART5_BASE)
356-
case 4:
368+
case UART_5:
357369
irq_n = UART5_IRQn;
358370
break;
359371
#endif
360372
#if defined(USART6_BASE)
361-
case 5:
373+
case UART_6:
362374
irq_n = USART6_IRQn;
363375
break;
364376
#endif
365377
#if defined(UART7_BASE)
366-
case 6:
378+
case UART_7:
367379
irq_n = UART7_IRQn;
368380
break;
369381
#endif
370382
#if defined(UART8_BASE)
371-
case 7:
383+
case UART_8:
372384
irq_n = UART8_IRQn;
373385
break;
374-
#endif
386+
#endif
375387
default:
376388
irq_n = (IRQn_Type)0;
377389
}
@@ -421,7 +433,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
421433
serial_enable_event(obj, event, 1); // Set only the wanted events
422434

423435
// Enable interrupt
424-
IRQn_Type irq_n = serial_get_irq_n(obj);
436+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
425437
NVIC_ClearPendingIRQ(irq_n);
426438
NVIC_DisableIRQ(irq_n);
427439
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
471483

472484
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
473485

474-
IRQn_Type irq_n = serial_get_irq_n(obj);
486+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
475487
NVIC_ClearPendingIRQ(irq_n);
476488
NVIC_DisableIRQ(irq_n);
477489
NVIC_SetPriority(irq_n, 0);
@@ -626,7 +638,7 @@ void serial_tx_abort_asynch(serial_t *obj)
626638

627639
// clear flags
628640
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
629-
641+
630642
// reset states
631643
huart->TxXferCount = 0;
632644
// update handle state

0 commit comments

Comments
 (0)