Skip to content

Commit eeb4d2c

Browse files
committed
STM32 serial: improve irq index management for F3 devices
1 parent 1fa0557 commit eeb4d2c

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

targets/TARGET_STM/TARGET_STM32F3/serial_device.c

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,62 +43,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
4343

4444
static uart_irq_handler irq_handler;
4545

46+
// Defined in serial_api.c
47+
inline int8_t get_uart_index(UARTName uart_name);
48+
4649
/******************************************************************************
4750
* INTERRUPTS HANDLING
4851
******************************************************************************/
4952

50-
static void uart_irq(int id)
53+
static void uart_irq(UARTName uart_name)
5154
{
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);
6463
}
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+
}
6975
}
7076
}
7177
}
7278
}
7379

80+
#if defined(USART1_BASE)
7481
static void uart1_irq(void)
7582
{
76-
uart_irq(0);
83+
uart_irq(UART_1);
7784
}
85+
#endif
7886

87+
#if defined(USART2_BASE)
7988
static void uart2_irq(void)
8089
{
81-
uart_irq(1);
90+
uart_irq(UART_2);
8291
}
92+
#endif
8393

8494
#if defined(USART3_BASE)
8595
static void uart3_irq(void)
8696
{
87-
uart_irq(2);
97+
uart_irq(UART_3);
8898
}
8999
#endif
90100

91101
#if defined(UART4_BASE)
92102
static void uart4_irq(void)
93103
{
94-
uart_irq(3);
104+
uart_irq(UART_4);
95105
}
96106
#endif
97107

98108
#if defined(UART5_BASE)
99109
static void uart5_irq(void)
100110
{
101-
uart_irq(4);
111+
uart_irq(UART_5);
102112
}
103113
#endif
104114

@@ -117,15 +127,19 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
117127
IRQn_Type irq_n = (IRQn_Type)0;
118128
uint32_t vector = 0;
119129

130+
#if defined(USART1_BASE)
120131
if (obj_s->uart == UART_1) {
121132
irq_n = USART1_IRQn;
122133
vector = (uint32_t)&uart1_irq;
123134
}
135+
#endif
124136

137+
#if defined(USART2_BASE)
125138
if (obj_s->uart == UART_2) {
126139
irq_n = USART2_IRQn;
127140
vector = (uint32_t)&uart2_irq;
128141
}
142+
#endif
129143

130144
#if defined(USART3_BASE)
131145
if (obj_s->uart == UART_3) {
@@ -297,42 +311,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
297311
/**
298312
* Get index of serial object TX IRQ, relating it to the physical peripheral.
299313
*
300-
* @param obj pointer to serial object
314+
* @param uart_name i.e. UART_1, UART_2, ...
301315
* @return internal NVIC TX IRQ index of U(S)ART peripheral
302316
*/
303-
static IRQn_Type serial_get_irq_n(serial_t *obj)
317+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
304318
{
305-
struct serial_s *obj_s = SERIAL_S(obj);
306319
IRQn_Type irq_n;
307320

308-
switch (obj_s->index) {
309-
case 0:
321+
switch (uart_name) {
322+
#if defined(USART1_BASE)
323+
case UART_1:
310324
irq_n = USART1_IRQn;
311325
break;
312-
313-
case 1:
326+
#endif
327+
#if defined(USART2_BASE)
328+
case UART_2:
314329
irq_n = USART2_IRQn;
315330
break;
316-
331+
#endif
317332
#if defined(USART3_BASE)
318-
case 2:
333+
case UART_3:
319334
irq_n = USART3_IRQn;
320335
break;
321336
#endif
322337
#if defined(UART4_BASE)
323-
case 3:
338+
case UART_4:
324339
irq_n = UART4_IRQn;
325340
break;
326341
#endif
327342
#if defined(UART5_BASE)
328-
case 4:
343+
case UART_5:
329344
irq_n = UART5_IRQn;
330345
break;
331346
#endif
332347
default:
333348
irq_n = (IRQn_Type)0;
334349
}
335-
350+
336351
return irq_n;
337352
}
338353

@@ -378,7 +393,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
378393
serial_enable_event(obj, event, 1); // Set only the wanted events
379394

380395
// Enable interrupt
381-
IRQn_Type irq_n = serial_get_irq_n(obj);
396+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
382397
NVIC_ClearPendingIRQ(irq_n);
383398
NVIC_DisableIRQ(irq_n);
384399
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
428443

429444
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
430445

431-
IRQn_Type irq_n = serial_get_irq_n(obj);
446+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
432447
NVIC_ClearPendingIRQ(irq_n);
433448
NVIC_DisableIRQ(irq_n);
434449
NVIC_SetPriority(irq_n, 0);
@@ -586,7 +601,7 @@ void serial_tx_abort_asynch(serial_t *obj)
586601

587602
// clear flags
588603
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
589-
604+
590605
// reset states
591606
huart->TxXferCount = 0;
592607
// update handle state

0 commit comments

Comments
 (0)