Skip to content

Commit f907012

Browse files
authored
Merge pull request #5962 from bcostm/fix_usart_irq_index
STM32: Fix usart irq index
2 parents 097966b + eb4b339 commit f907012

File tree

10 files changed

+768
-504
lines changed

10 files changed

+768
-504
lines changed

targets/TARGET_STM/TARGET_STM32F0/serial_device.c

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,51 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
4747

4848
static uart_irq_handler irq_handler;
4949

50+
// Defined in serial_api.c
51+
inline int8_t get_uart_index(UARTName uart_name);
52+
5053
/******************************************************************************
5154
* INTERRUPTS HANDLING
5255
******************************************************************************/
5356

54-
static void uart_irq(int id)
57+
static void uart_irq(UARTName uart_name)
5558
{
56-
UART_HandleTypeDef * huart = &uart_handlers[id];
57-
if (serial_irq_ids[id] != 0) {
58-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
59-
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
60-
irq_handler(serial_irq_ids[id], TxIrq);
59+
int8_t id = get_uart_index(uart_name);
60+
61+
if (id >= 0) {
62+
UART_HandleTypeDef * huart = &uart_handlers[id];
63+
if (serial_irq_ids[id] != 0) {
64+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
65+
if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) {
66+
irq_handler(serial_irq_ids[id], TxIrq);
67+
}
6168
}
62-
}
63-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
64-
if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) {
65-
irq_handler(serial_irq_ids[id], RxIrq);
66-
/* Flag has been cleared when reading the content */
69+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
70+
if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) {
71+
irq_handler(serial_irq_ids[id], RxIrq);
72+
/* Flag has been cleared when reading the content */
73+
}
6774
}
68-
}
69-
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
70-
if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) {
71-
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
75+
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
76+
if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) {
77+
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
78+
}
7279
}
7380
}
7481
}
7582
}
7683

84+
#if defined(USART1_BASE)
7785
static void uart1_irq(void)
7886
{
79-
uart_irq(0);
87+
uart_irq(UART_1);
8088
}
89+
#endif
8190

8291
#if defined(USART2_BASE)
8392
static void uart2_irq(void)
8493
{
85-
uart_irq(1);
94+
uart_irq(UART_2);
8695
}
8796
#endif
8897

@@ -92,43 +101,43 @@ static void uart3_8_irq(void)
92101
#if defined(TARGET_STM32F091RC)
93102
#if defined(USART3_BASE)
94103
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART3) != RESET) {
95-
uart_irq(2);
104+
uart_irq(UART_3);
96105
}
97106
#endif
98107
#if defined(USART4_BASE)
99108
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART4) != RESET) {
100-
uart_irq(3);
109+
uart_irq(UART_4);
101110
}
102111
#endif
103112
#if defined(USART5_BASE)
104113
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART5) != RESET) {
105-
uart_irq(4);
114+
uart_irq(UART_5);
106115
}
107116
#endif
108117
#if defined(USART6_BASE)
109118
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART6) != RESET) {
110-
uart_irq(5);
119+
uart_irq(UART_6);
111120
}
112121
#endif
113122
#if defined(USART7_BASE)
114123
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART7) != RESET) {
115-
uart_irq(6);
124+
uart_irq(UART_7);
116125
}
117126
#endif
118127
#if defined(USART8_BASE)
119128
if (__HAL_GET_PENDING_IT(HAL_ITLINE_USART8) != RESET) {
120-
uart_irq(7);
129+
uart_irq(UART_8);
121130
}
122131
#endif
123132
#else // TARGET_STM32F070RB, TARGET_STM32F072RB
124133
#if defined(USART3_BASE)
125134
if (USART3->ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE)) {
126-
uart_irq(2);
135+
uart_irq(UART_3);
127136
}
128137
#endif
129138
#if defined(USART4_BASE)
130139
if (USART4->ISR & (UART_FLAG_TXE | UART_FLAG_RXNE | UART_FLAG_ORE)) {
131-
uart_irq(3);
140+
uart_irq(UART_4);
132141
}
133142
#endif
134143
#endif
@@ -149,10 +158,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
149158
IRQn_Type irq_n = (IRQn_Type)0;
150159
uint32_t vector = 0;
151160

161+
#if defined(USART1_BASE)
152162
if (obj_s->uart == UART_1) {
153163
irq_n = USART1_IRQn;
154164
vector = (uint32_t)&uart1_irq;
155165
}
166+
#endif
156167

157168
#if defined(USART2_BASE)
158169
if (obj_s->uart == UART_2) {
@@ -238,7 +249,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
238249
}
239250

240251
if (all_disabled) {
241-
NVIC_DisableIRQ(irq_n);
252+
NVIC_DisableIRQ(irq_n);
242253
}
243254
}
244255
}
@@ -351,44 +362,75 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
351362
/**
352363
* Get index of serial object TX IRQ, relating it to the physical peripheral.
353364
*
354-
* @param obj pointer to serial object
365+
* @param uart_name i.e. UART_1, UART_2, ...
355366
* @return internal NVIC TX IRQ index of U(S)ART peripheral
356367
*/
357-
static IRQn_Type serial_get_irq_n(serial_t *obj)
368+
static IRQn_Type serial_get_irq_n(UARTName uart_name)
358369
{
359-
struct serial_s *obj_s = SERIAL_S(obj);
360370
IRQn_Type irq_n;
361371

362-
switch (obj_s->index) {
372+
switch (uart_name) {
373+
363374
#if defined(USART1_BASE)
364-
case 0:
375+
case UART_1:
365376
irq_n = USART1_IRQn;
366377
break;
367378
#endif
379+
368380
#if defined(USART2_BASE)
369-
case 1:
381+
case UART_2:
370382
irq_n = USART2_IRQn;
371383
break;
372384
#endif
385+
386+
#if defined(USART3_BASE)
387+
case UART_3:
373388
#if defined (TARGET_STM32F091RC)
374-
case 2:
375-
case 3:
376-
case 4:
377-
case 5:
378-
case 6:
379-
case 7:
380389
irq_n = USART3_8_IRQn;
390+
#else
391+
irq_n = USART3_4_IRQn;
392+
#endif
381393
break;
382-
#elif !defined (TARGET_STM32F030R8) && !defined (TARGET_STM32F051R8)
383-
case 2:
384-
case 3:
394+
#endif
395+
396+
#if defined(USART4_BASE)
397+
case UART_4:
398+
#if defined (TARGET_STM32F091RC)
399+
irq_n = USART3_8_IRQn;
400+
#else
385401
irq_n = USART3_4_IRQn;
402+
#endif
386403
break;
387404
#endif
405+
406+
#if defined(USART5_BASE)
407+
case UART_5:
408+
irq_n = USART3_8_IRQn;
409+
break;
410+
#endif
411+
412+
#if defined(USART6_BASE)
413+
case UART_6:
414+
irq_n = USART3_8_IRQn;
415+
break;
416+
#endif
417+
418+
#if defined(USART7_BASE)
419+
case UART_7:
420+
irq_n = USART3_8_IRQn;
421+
break;
422+
#endif
423+
424+
#if defined(USART8_BASE)
425+
case UART_8:
426+
irq_n = USART3_8_IRQn;
427+
break;
428+
#endif
429+
388430
default:
389431
irq_n = (IRQn_Type)0;
390432
}
391-
433+
392434
return irq_n;
393435
}
394436

@@ -434,7 +476,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
434476
serial_enable_event(obj, event, 1); // Set only the wanted events
435477

436478
// Enable interrupt
437-
IRQn_Type irq_n = serial_get_irq_n(obj);
479+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
438480
NVIC_ClearPendingIRQ(irq_n);
439481
NVIC_DisableIRQ(irq_n);
440482
NVIC_SetPriority(irq_n, 1);
@@ -484,7 +526,7 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
484526

485527
serial_rx_buffer_set(obj, rx, rx_length, rx_width);
486528

487-
IRQn_Type irq_n = serial_get_irq_n(obj);
529+
IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
488530
NVIC_ClearPendingIRQ(irq_n);
489531
NVIC_DisableIRQ(irq_n);
490532
NVIC_SetPriority(irq_n, 0);
@@ -642,7 +684,7 @@ void serial_tx_abort_asynch(serial_t *obj)
642684

643685
// clear flags
644686
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
645-
687+
646688
// reset states
647689
huart->TxXferCount = 0;
648690
// update handle state

0 commit comments

Comments
 (0)