Skip to content

Commit 689e15c

Browse files
committed
STM32 serial: improve irq index management for F7 devices
1 parent eeb4d2c commit 689e15c

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

targets/TARGET_STM/TARGET_STM32F7/serial_device.c

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,81 +38,93 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
3838

3939
static uart_irq_handler irq_handler;
4040

41+
// Defined in serial_api.c
42+
inline int8_t get_uart_index(UARTName uart_name);
43+
4144
/******************************************************************************
4245
* INTERRUPTS HANDLING
4346
******************************************************************************/
4447

45-
static void uart_irq(int id)
48+
static void uart_irq(UARTName uart_name)
4649
{
47-
UART_HandleTypeDef * huart = &uart_handlers[id];
48-
49-
if (serial_irq_ids[id] != 0) {
50-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
51-
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
52-
irq_handler(serial_irq_ids[id], TxIrq);
50+
int8_t id = get_uart_index(uart_name);
51+
52+
if (id >= 0) {
53+
UART_HandleTypeDef * huart = &uart_handlers[id];
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+
}
5359
}
54-
}
55-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
56-
if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) {
57-
irq_handler(serial_irq_ids[id], RxIrq);
58-
/* Flag has been cleared when reading the content */
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 */
64+
}
5965
}
60-
}
61-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
62-
if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) {
63-
__HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
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_IT(huart, UART_CLEAR_OREF);
69+
}
6470
}
6571
}
6672
}
6773
}
6874

75+
#if defined(USART1_BASE)
6976
static void uart1_irq(void)
7077
{
71-
uart_irq(0);
78+
uart_irq(UART_1);
7279
}
80+
#endif
7381

82+
#if defined(USART2_BASE)
7483
static void uart2_irq(void)
7584
{
76-
uart_irq(1);
85+
uart_irq(UART_2);
7786
}
87+
#endif
7888

7989
#if defined(USART3_BASE)
8090
static void uart3_irq(void)
8191
{
82-
uart_irq(2);
92+
uart_irq(UART_3);
8393
}
8494
#endif
8595

8696
#if defined(UART4_BASE)
8797
static void uart4_irq(void)
8898
{
89-
uart_irq(3);
99+
uart_irq(UART_4);
90100
}
91101
#endif
92102

93103
#if defined(UART5_BASE)
94104
static void uart5_irq(void)
95105
{
96-
uart_irq(4);
106+
uart_irq(UART_5);
97107
}
98108
#endif
99109

110+
#if defined(USART6_BASE)
100111
static void uart6_irq(void)
101112
{
102-
uart_irq(5);
113+
uart_irq(UART_6);
103114
}
115+
#endif
104116

105117
#if defined(UART7_BASE)
106118
static void uart7_irq(void)
107119
{
108-
uart_irq(6);
120+
uart_irq(UART_7);
109121
}
110122
#endif
111123

112124
#if defined(UART8_BASE)
113125
static void uart8_irq(void)
114126
{
115-
uart_irq(7);
127+
uart_irq(UART_8);
116128
}
117129
#endif
118130

@@ -132,15 +144,18 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
132144
uint32_t vector = 0;
133145

134146
switch (obj_s->uart) {
147+
#if defined(USART1_BASE)
135148
case UART_1:
136149
irq_n = USART1_IRQn;
137150
vector = (uint32_t)&uart1_irq;
138151
break;
139-
152+
#endif
153+
#if defined(USART2_BASE)
140154
case UART_2:
141155
irq_n = USART2_IRQn;
142156
vector = (uint32_t)&uart2_irq;
143157
break;
158+
#endif
144159
#if defined(USART3_BASE)
145160
case UART_3:
146161
irq_n = USART3_IRQn;
@@ -159,10 +174,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
159174
vector = (uint32_t)&uart5_irq;
160175
break;
161176
#endif
177+
#if defined(USART6_BASE)
162178
case UART_6:
163179
irq_n = USART6_IRQn;
164180
vector = (uint32_t)&uart6_irq;
165181
break;
182+
#endif
166183
#if defined(UART7_BASE)
167184
case UART_7:
168185
irq_n = UART7_IRQn;
@@ -318,55 +335,58 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
318335
/**
319336
* Get index of serial object TX IRQ, relating it to the physical peripheral.
320337
*
321-
* @param obj pointer to serial object
338+
* @param uart_name i.e. UART_1, UART_2, ...
322339
* @return internal NVIC TX IRQ index of U(S)ART peripheral
323340
*/
324-
static IRQn_Type serial_get_irq_n(serial_t *obj)
341+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
325342
{
326-
struct serial_s *obj_s = SERIAL_S(obj);
327343
IRQn_Type irq_n;
328344

329-
switch (obj_s->index) {
330-
331-
case 0:
345+
switch (uart_name) {
346+
#if defined(USART1_BASE)
347+
case UART_1:
332348
irq_n = USART1_IRQn;
333349
break;
334-
335-
case 1:
350+
#endif
351+
#if defined(USART2_BASE)
352+
case UART_2:
336353
irq_n = USART2_IRQn;
337354
break;
355+
#endif
338356
#if defined(USART3_BASE)
339-
case 2:
357+
case UART_3:
340358
irq_n = USART3_IRQn;
341359
break;
342360
#endif
343361
#if defined(UART4_BASE)
344-
case 3:
362+
case UART_4:
345363
irq_n = UART4_IRQn;
346364
break;
347365
#endif
348366
#if defined(UART5_BASE)
349-
case 4:
367+
case UART_5:
350368
irq_n = UART5_IRQn;
351369
break;
352370
#endif
353-
case 5:
371+
#if defined(USART6_BASE)
372+
case UART_6:
354373
irq_n = USART6_IRQn;
355374
break;
375+
#endif
356376
#if defined(UART7_BASE)
357-
case 6:
377+
case UART_7:
358378
irq_n = UART7_IRQn;
359379
break;
360380
#endif
361381
#if defined(UART8_BASE)
362-
case 7:
382+
case UART_8:
363383
irq_n = UART8_IRQn;
364384
break;
365385
#endif
366386
default:
367387
irq_n = (IRQn_Type)0;
368388
}
369-
389+
370390
return irq_n;
371391
}
372392

@@ -411,7 +431,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
411431
serial_enable_event(obj, event, 1); // Set only the wanted events
412432

413433
// Enable interrupt
414-
IRQn_Type irq_n = serial_get_irq_n(obj);
434+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
415435
NVIC_ClearPendingIRQ(irq_n);
416436
NVIC_DisableIRQ(irq_n);
417437
NVIC_SetPriority(irq_n, 1);
@@ -461,7 +481,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
461481

462482
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
463483

464-
IRQn_Type irq_n = serial_get_irq_n(obj);
484+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
465485
NVIC_ClearPendingIRQ(irq_n);
466486
NVIC_DisableIRQ(irq_n);
467487
NVIC_SetPriority(irq_n, 0);

0 commit comments

Comments
 (0)