Skip to content

Commit 84269c7

Browse files
committed
STM32 serial: improve irq index management for L4 devices
1 parent 665d4a8 commit 84269c7

File tree

1 file changed

+78
-65
lines changed

1 file changed

+78
-65
lines changed

targets/TARGET_STM/TARGET_STM32L4/serial_device.c

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -45,69 +45,79 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
4545

4646
static uart_irq_handler irq_handler;
4747

48+
// Defined in serial_api.c
49+
inline int8_t get_uart_index(UARTName uart_name);
50+
4851
/******************************************************************************
4952
* INTERRUPTS HANDLING
5053
******************************************************************************/
5154

52-
static void uart_irq(int id)
55+
static void uart_irq(UARTName uart_name)
5356
{
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+
}
6066
}
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+
}
6672
}
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+
}
7177
}
7278
}
7379
}
7480
}
7581

82+
#if defined(USART1_BASE)
7683
static void uart1_irq(void)
7784
{
78-
uart_irq(0);
85+
uart_irq(UART_1);
7986
}
87+
#endif
8088

89+
#if defined(USART2_BASE)
8190
static void uart2_irq(void)
8291
{
83-
uart_irq(1);
92+
uart_irq(UART_2);
8493
}
94+
#endif
8595

8696
#if defined(USART3_BASE)
8797
static void uart3_irq(void)
8898
{
89-
uart_irq(2);
99+
uart_irq(UART_3);
90100
}
91101
#endif
92102

93103
#if defined(UART4_BASE)
94104
static void uart4_irq(void)
95105
{
96-
uart_irq(3);
106+
uart_irq(UART_4);
97107
}
98108
#endif
99109

100110
#if defined(UART5_BASE)
101111
static void uart5_irq(void)
102112
{
103-
uart_irq(4);
113+
uart_irq(UART_5);
104114
}
105115
#endif
106116

107117
#if defined(LPUART1_BASE)
108118
static void lpuart1_irq(void)
109119
{
110-
uart_irq(5);
120+
uart_irq(LPUART_1);
111121
}
112122
#endif
113123

@@ -126,41 +136,44 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
126136
IRQn_Type irq_n = (IRQn_Type)0;
127137
uint32_t vector = 0;
128138

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
138152
#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;
143157
#endif
144158
#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;
149163
#endif
150-
151164
#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;
156169
#endif
157-
158170
#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;
163175
#endif
176+
}
164177

165178
if (enable) {
166179
if (irq == RxIrq) {
@@ -311,48 +324,48 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
311324
/**
312325
* Get index of serial object TX IRQ, relating it to the physical peripheral.
313326
*
314-
* @param obj pointer to serial object
327+
* @param uart_name i.e. UART_1, UART_2, ...
315328
* @return internal NVIC TX IRQ index of U(S)ART peripheral
316329
*/
317-
static IRQn_Type serial_get_irq_n(serial_t *obj)
330+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
318331
{
319-
struct serial_s *obj_s = SERIAL_S(obj);
320332
IRQn_Type irq_n;
321333

322-
switch (obj_s->index) {
323-
case 0:
334+
switch (uart_name) {
335+
#if defined(USART1_BASE)
336+
case UART_1:
324337
irq_n = USART1_IRQn;
325338
break;
326-
327-
case 1:
339+
#endif
340+
#if defined(USART2_BASE)
341+
case UART_2:
328342
irq_n = USART2_IRQn;
329343
break;
330-
344+
#endif
331345
#if defined(USART3_BASE)
332-
case 2:
346+
case UART_3:
333347
irq_n = USART3_IRQn;
334348
break;
335349
#endif
336350
#if defined(UART4_BASE)
337-
case 3:
351+
case UART_4:
338352
irq_n = UART4_IRQn;
339353
break;
340354
#endif
341355
#if defined(UART5_BASE)
342-
case 4:
356+
case UART_5:
343357
irq_n = UART5_IRQn;
344358
break;
345359
#endif
346360
#if defined(LPUART1_BASE)
347-
case 5:
361+
case LPUART_1:
348362
irq_n = LPUART1_IRQn;
349363
break;
350364
#endif
351-
352365
default:
353366
irq_n = (IRQn_Type)0;
354367
}
355-
368+
356369
return irq_n;
357370
}
358371

@@ -397,7 +410,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
397410
serial_enable_event(obj, event, 1); // Set only the wanted events
398411

399412
// Enable interrupt
400-
IRQn_Type irq_n = serial_get_irq_n(obj);
413+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
401414
NVIC_ClearPendingIRQ(irq_n);
402415
NVIC_DisableIRQ(irq_n);
403416
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
447460

448461
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
449462

450-
IRQn_Type irq_n = serial_get_irq_n(obj);
463+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
451464
NVIC_ClearPendingIRQ(irq_n);
452465
NVIC_DisableIRQ(irq_n);
453466
NVIC_SetPriority(irq_n, 0);

0 commit comments

Comments
 (0)