Skip to content

Commit 6974da2

Browse files
committed
STM32 serial: improve irq index management for F1 devices
1 parent 2305db6 commit 6974da2

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

targets/TARGET_STM/TARGET_STM32F1/serial_device.c

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,60 @@ 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
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
8698
{
@@ -97,20 +109,26 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
97109
IRQn_Type irq_n = (IRQn_Type)0;
98110
uint32_t vector = 0;
99111

112+
#if defined(USART1_BASE)
100113
if (obj_s->uart == UART_1) {
101114
irq_n = USART1_IRQn;
102115
vector = (uint32_t)&uart1_irq;
103116
}
117+
#endif
104118

119+
#if defined(USART2_BASE)
105120
if (obj_s->uart == UART_2) {
106121
irq_n = USART2_IRQn;
107122
vector = (uint32_t)&uart2_irq;
108123
}
124+
#endif
109125

126+
#if defined(USART3_BASE)
110127
if (obj_s->uart == UART_3) {
111128
irq_n = USART3_IRQn;
112129
vector = (uint32_t)&uart3_irq;
113130
}
131+
#endif
114132

115133
if (enable) {
116134
if (irq == RxIrq) {
@@ -261,31 +279,33 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
261279
/**
262280
* Get index of serial object TX IRQ, relating it to the physical peripheral.
263281
*
264-
* @param obj pointer to serial object
282+
* @param uart_name i.e. UART_1, UART_2, ...
265283
* @return internal NVIC TX IRQ index of U(S)ART peripheral
266284
*/
267-
static IRQn_Type serial_get_irq_n(serial_t *obj)
285+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
268286
{
269-
struct serial_s *obj_s = SERIAL_S(obj);
270287
IRQn_Type irq_n;
271288

272-
switch (obj_s->index) {
273-
case 0:
289+
switch (uart_name) {
290+
#if defined(USART1_BASE)
291+
case UART_1:
274292
irq_n = USART1_IRQn;
275293
break;
276-
277-
case 1:
294+
#endif
295+
#if defined(USART2_BASE)
296+
case UART_2:
278297
irq_n = USART2_IRQn;
279298
break;
280-
281-
case 2:
299+
#endif
300+
#if defined(USART3_BASE)
301+
case UART_3:
282302
irq_n = USART3_IRQn;
283303
break;
284-
304+
#endif
285305
default:
286306
irq_n = (IRQn_Type)0;
287307
}
288-
308+
289309
return irq_n;
290310
}
291311

@@ -330,7 +350,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
330350
serial_enable_event(obj, event, 1); // Set only the wanted events
331351

332352
// Enable interrupt
333-
IRQn_Type irq_n = serial_get_irq_n(obj);
353+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
334354
NVIC_ClearPendingIRQ(irq_n);
335355
NVIC_DisableIRQ(irq_n);
336356
NVIC_SetPriority(irq_n, 1);
@@ -380,7 +400,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
380400

381401
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
382402

383-
IRQn_Type irq_n = serial_get_irq_n(obj);
403+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
384404
NVIC_ClearPendingIRQ(irq_n);
385405
NVIC_DisableIRQ(irq_n);
386406
NVIC_SetPriority(irq_n, 0);
@@ -471,8 +491,8 @@ int serial_irq_handler_asynch(serial_t *obj)
471491
if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) {
472492
return_event |= (SERIAL_EVENT_RX_PARITY_ERROR & obj_s->events);
473493
}
474-
}
475-
494+
}
495+
476496
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) {
477497
if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) {
478498
return_event |= (SERIAL_EVENT_RX_FRAMING_ERROR & obj_s->events);
@@ -535,7 +555,7 @@ void serial_tx_abort_asynch(serial_t *obj)
535555

536556
// clear flags
537557
__HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
538-
558+
539559
// reset states
540560
huart->TxXferCount = 0;
541561
// update handle state

0 commit comments

Comments
 (0)