Skip to content

Commit 665d4a8

Browse files
committed
STM32 serial: improve irq index management for L1 devices
1 parent 689e15c commit 665d4a8

File tree

1 file changed

+79
-57
lines changed

1 file changed

+79
-57
lines changed

targets/TARGET_STM/TARGET_STM32L1/serial_device.c

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,72 @@ 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, UART_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, UART_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

90+
#if defined(USART3_BASE)
8091
static void uart3_irq(void)
8192
{
82-
uart_irq(2);
93+
uart_irq(UART_3);
8394
}
95+
#endif
8496

8597
#if defined(UART4_BASE)
8698
static void uart4_irq(void)
8799
{
88-
uart_irq(3);
100+
uart_irq(UART_4);
89101
}
90102
#endif
103+
91104
#if defined(UART5_BASE)
92105
static void uart5_irq(void)
93106
{
94-
uart_irq(4);
107+
uart_irq(UART_5);
95108
}
96109
#endif
97110

@@ -110,32 +123,38 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
110123
IRQn_Type irq_n = (IRQn_Type)0;
111124
uint32_t vector = 0;
112125

113-
if (obj_s->uart == UART_1) {
114-
irq_n = USART1_IRQn;
115-
vector = (uint32_t)&uart1_irq;
116-
}
117-
118-
if (obj_s->uart == UART_2) {
119-
irq_n = USART2_IRQn;
120-
vector = (uint32_t)&uart2_irq;
121-
}
122-
123-
if (obj_s->uart == UART_3) {
124-
irq_n = USART3_IRQn;
125-
vector = (uint32_t)&uart3_irq;
126-
}
126+
switch (obj_s->uart) {
127+
#if defined(USART1_BASE)
128+
case UART_1:
129+
irq_n = USART1_IRQn;
130+
vector = (uint32_t)&uart1_irq;
131+
break;
132+
#endif
133+
#if defined(USART2_BASE)
134+
case UART_2:
135+
irq_n = USART2_IRQn;
136+
vector = (uint32_t)&uart2_irq;
137+
break;
138+
#endif
139+
#if defined(USART3_BASE)
140+
case UART_3:
141+
irq_n = USART3_IRQn;
142+
vector = (uint32_t)&uart3_irq;
143+
break;
144+
#endif
127145
#if defined(UART4_BASE)
128-
if (obj_s->uart == UART_4) {
129-
irq_n = UART4_IRQn;
130-
vector = (uint32_t)&uart4_irq;
131-
}
146+
case UART_4:
147+
irq_n = UART4_IRQn;
148+
vector = (uint32_t)&uart4_irq;
149+
break;
132150
#endif
133151
#if defined(UART5_BASE)
134-
if (obj_s->uart == UART_5) {
135-
irq_n = UART5_IRQn;
136-
vector = (uint32_t)&uart5_irq;
137-
}
152+
case UART_5:
153+
irq_n = UART5_IRQn;
154+
vector = (uint32_t)&uart5_irq;
155+
break;
138156
#endif
157+
}
139158

140159
if (enable) {
141160
if (irq == RxIrq) {
@@ -286,40 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
286305
/**
287306
* Get index of serial object TX IRQ, relating it to the physical peripheral.
288307
*
289-
* @param obj pointer to serial object
308+
* @param uart_name i.e. UART_1, UART_2, ...
290309
* @return internal NVIC TX IRQ index of U(S)ART peripheral
291310
*/
292-
static IRQn_Type serial_get_irq_n(serial_t *obj)
311+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
293312
{
294-
struct serial_s *obj_s = SERIAL_S(obj);
295313
IRQn_Type irq_n;
296314

297-
switch (obj_s->index) {
298-
case 0:
315+
switch (uart_name) {
316+
#if defined(USART1_BASE)
317+
case UART_1:
299318
irq_n = USART1_IRQn;
300319
break;
301-
302-
case 1:
320+
#endif
321+
#if defined(USART2_BASE)
322+
case UART_2:
303323
irq_n = USART2_IRQn;
304324
break;
305-
306-
case 2:
325+
#endif
326+
#if defined(USART3_BASE)
327+
case UART_3:
307328
irq_n = USART3_IRQn;
308329
break;
330+
#endif
309331
#if defined(UART4_BASE)
310-
case 3:
332+
case UART_4:
311333
irq_n = UART4_IRQn;
312334
break;
313335
#endif
314336
#if defined(UART5_BASE)
315-
case 4:
337+
case UART_5:
316338
irq_n = UART5_IRQn;
317339
break;
318340
#endif
319341
default:
320342
irq_n = (IRQn_Type)0;
321343
}
322-
344+
323345
return irq_n;
324346
}
325347

@@ -364,7 +386,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
364386
serial_enable_event(obj, event, 1); // Set only the wanted events
365387

366388
// Enable interrupt
367-
IRQn_Type irq_n = serial_get_irq_n(obj);
389+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
368390
NVIC_ClearPendingIRQ(irq_n);
369391
NVIC_DisableIRQ(irq_n);
370392
NVIC_SetPriority(irq_n, 1);
@@ -414,7 +436,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
414436

415437
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
416438

417-
IRQn_Type irq_n = serial_get_irq_n(obj);
439+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
418440
NVIC_ClearPendingIRQ(irq_n);
419441
NVIC_DisableIRQ(irq_n);
420442
NVIC_SetPriority(irq_n, 0);

0 commit comments

Comments
 (0)