Skip to content

Commit e8454ff

Browse files
committed
STM32 serial: improve irq index management for L0 devices
1 parent 78a5516 commit e8454ff

File tree

1 file changed

+61
-45
lines changed

1 file changed

+61
-45
lines changed

targets/TARGET_STM/TARGET_STM32L0/serial_device.c

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,35 @@ 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-
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
73+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
74+
if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) {
75+
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
76+
}
7177
}
7278
}
7379
}
@@ -76,31 +82,35 @@ static void uart_irq(int id)
7682
#if defined(USART1_BASE)
7783
static void uart1_irq(void)
7884
{
79-
uart_irq(0);
85+
uart_irq(UART_1);
8086
}
8187
#endif
8288

89+
#if defined(USART2_BASE)
8390
static void uart2_irq(void)
8491
{
85-
uart_irq(1);
86-
}
87-
88-
static void lpuart1_irq(void)
89-
{
90-
uart_irq(2);
92+
uart_irq(UART_2);
9193
}
94+
#endif
9295

9396
#if defined(USART4_BASE)
9497
static void uart4_irq(void)
9598
{
96-
uart_irq(3);
99+
uart_irq(UART_4);
97100
}
98101
#endif
99102

100103
#if defined(USART5_BASE)
101104
static void uart5_irq(void)
102105
{
103-
uart_irq(4);
106+
uart_irq(UART_5);
107+
}
108+
#endif
109+
110+
#if defined(LPUART1_BASE)
111+
static void lpuart1_irq(void)
112+
{
113+
uart_irq(LPUART_1);
104114
}
105115
#endif
106116

@@ -126,15 +136,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
126136
}
127137
#endif
128138

139+
#if defined(USART2_BASE)
129140
if (obj_s->uart == UART_2) {
130141
irq_n = USART2_IRQn;
131142
vector = (uint32_t)&uart2_irq;
132143
}
133-
134-
if (obj_s->uart == LPUART_1) {
135-
irq_n = RNG_LPUART1_IRQn;
136-
vector = (uint32_t)&lpuart1_irq;
137-
}
144+
#endif
138145

139146
#if defined(USART4_BASE)
140147
if (obj_s->uart == UART_4) {
@@ -150,6 +157,13 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
150157
}
151158
#endif
152159

160+
#if defined(LPUART1_BASE)
161+
if (obj_s->uart == LPUART_1) {
162+
irq_n = RNG_LPUART1_IRQn;
163+
vector = (uint32_t)&lpuart1_irq;
164+
}
165+
#endif
166+
153167
if (enable) {
154168
if (irq == RxIrq) {
155169
__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
@@ -291,41 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
291305
/**
292306
* Get index of serial object TX IRQ, relating it to the physical peripheral.
293307
*
294-
* @param obj pointer to serial object
308+
* @param uart_name i.e. UART_1, UART_2, ...
295309
* @return internal NVIC TX IRQ index of U(S)ART peripheral
296310
*/
297-
static IRQn_Type serial_get_irq_n(serial_t *obj)
311+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
298312
{
299-
struct serial_s *obj_s = SERIAL_S(obj);
300313
IRQn_Type irq_n;
301314

302-
switch (obj_s->index) {
315+
switch (uart_name) {
303316
#if defined(USART1_BASE)
304-
case 0:
317+
case UART_1:
305318
irq_n = USART1_IRQn;
306319
break;
307320
#endif
308-
case 1:
321+
#if defined(USART2_BASE)
322+
case UART_2:
309323
irq_n = USART2_IRQn;
310324
break;
311-
312-
case 2:
313-
irq_n = RNG_LPUART1_IRQn;
314-
break;
325+
#endif
315326
#if defined(USART4_BASE)
316-
case 3:
327+
case UART_4:
317328
irq_n = USART4_5_IRQn;
318329
break;
319330
#endif
320331
#if defined(USART5_BASE)
321-
case 4:
332+
case UART_5:
322333
irq_n = USART4_5_IRQn;
323334
break;
335+
#endif
336+
#if defined(LPUART1_BASE)
337+
case LPUART_1:
338+
irq_n = RNG_LPUART1_IRQn;
339+
break;
324340
#endif
325341
default:
326342
irq_n = (IRQn_Type)0;
327343
}
328-
344+
329345
return irq_n;
330346
}
331347

@@ -371,7 +387,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
371387
serial_enable_event(obj, event, 1); // Set only the wanted events
372388

373389
// Enable interrupt
374-
IRQn_Type irq_n = serial_get_irq_n(obj);
390+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
375391
NVIC_ClearPendingIRQ(irq_n);
376392
NVIC_DisableIRQ(irq_n);
377393
NVIC_SetPriority(irq_n, 1);
@@ -421,7 +437,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
421437

422438
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
423439

424-
IRQn_Type irq_n = serial_get_irq_n(obj);
440+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
425441
NVIC_ClearPendingIRQ(irq_n);
426442
NVIC_DisableIRQ(irq_n);
427443
NVIC_SetPriority(irq_n, 0);

0 commit comments

Comments
 (0)