STM32 serial: improve irq index management for L1 devices

pull/5962/head
bcostm 2018-01-26 11:18:26 +01:00
parent 689e15cf29
commit 665d4a8003
1 changed files with 79 additions and 57 deletions

View File

@ -39,59 +39,72 @@ UART_HandleTypeDef uart_handlers[UART_NUM];
static uart_irq_handler irq_handler; static uart_irq_handler irq_handler;
// Defined in serial_api.c
inline int8_t get_uart_index(UARTName uart_name);
/****************************************************************************** /******************************************************************************
* INTERRUPTS HANDLING * INTERRUPTS HANDLING
******************************************************************************/ ******************************************************************************/
static void uart_irq(int id) static void uart_irq(UARTName uart_name)
{ {
UART_HandleTypeDef * huart = &uart_handlers[id]; int8_t id = get_uart_index(uart_name);
if (serial_irq_ids[id] != 0) { if (id >= 0) {
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { UART_HandleTypeDef * huart = &uart_handlers[id];
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { if (serial_irq_ids[id] != 0) {
irq_handler(serial_irq_ids[id], TxIrq); if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) {
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) {
irq_handler(serial_irq_ids[id], TxIrq);
}
} }
} if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) {
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) {
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { irq_handler(serial_irq_ids[id], RxIrq);
irq_handler(serial_irq_ids[id], RxIrq); /* Flag has been cleared when reading the content */
/* Flag has been cleared when reading the content */ }
} }
} if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) {
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET) { volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag
volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag }
} }
} }
} }
} }
#if defined(USART1_BASE)
static void uart1_irq(void) static void uart1_irq(void)
{ {
uart_irq(0); uart_irq(UART_1);
} }
#endif
#if defined(USART2_BASE)
static void uart2_irq(void) static void uart2_irq(void)
{ {
uart_irq(1); uart_irq(UART_2);
} }
#endif
#if defined(USART3_BASE)
static void uart3_irq(void) static void uart3_irq(void)
{ {
uart_irq(2); uart_irq(UART_3);
} }
#endif
#if defined(UART4_BASE) #if defined(UART4_BASE)
static void uart4_irq(void) static void uart4_irq(void)
{ {
uart_irq(3); uart_irq(UART_4);
} }
#endif #endif
#if defined(UART5_BASE) #if defined(UART5_BASE)
static void uart5_irq(void) static void uart5_irq(void)
{ {
uart_irq(4); uart_irq(UART_5);
} }
#endif #endif
@ -110,32 +123,38 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
IRQn_Type irq_n = (IRQn_Type)0; IRQn_Type irq_n = (IRQn_Type)0;
uint32_t vector = 0; uint32_t vector = 0;
if (obj_s->uart == UART_1) { switch (obj_s->uart) {
irq_n = USART1_IRQn; #if defined(USART1_BASE)
vector = (uint32_t)&uart1_irq; case UART_1:
} irq_n = USART1_IRQn;
vector = (uint32_t)&uart1_irq;
if (obj_s->uart == UART_2) { break;
irq_n = USART2_IRQn; #endif
vector = (uint32_t)&uart2_irq; #if defined(USART2_BASE)
} case UART_2:
irq_n = USART2_IRQn;
if (obj_s->uart == UART_3) { vector = (uint32_t)&uart2_irq;
irq_n = USART3_IRQn; break;
vector = (uint32_t)&uart3_irq; #endif
} #if defined(USART3_BASE)
case UART_3:
irq_n = USART3_IRQn;
vector = (uint32_t)&uart3_irq;
break;
#endif
#if defined(UART4_BASE) #if defined(UART4_BASE)
if (obj_s->uart == UART_4) { case UART_4:
irq_n = UART4_IRQn; irq_n = UART4_IRQn;
vector = (uint32_t)&uart4_irq; vector = (uint32_t)&uart4_irq;
} break;
#endif #endif
#if defined(UART5_BASE) #if defined(UART5_BASE)
if (obj_s->uart == UART_5) { case UART_5:
irq_n = UART5_IRQn; irq_n = UART5_IRQn;
vector = (uint32_t)&uart5_irq; vector = (uint32_t)&uart5_irq;
} break;
#endif #endif
}
if (enable) { if (enable) {
if (irq == RxIrq) { if (irq == RxIrq) {
@ -286,40 +305,43 @@ static void serial_enable_event(serial_t *obj, int event, uint8_t enable)
/** /**
* Get index of serial object TX IRQ, relating it to the physical peripheral. * Get index of serial object TX IRQ, relating it to the physical peripheral.
* *
* @param obj pointer to serial object * @param uart_name i.e. UART_1, UART_2, ...
* @return internal NVIC TX IRQ index of U(S)ART peripheral * @return internal NVIC TX IRQ index of U(S)ART peripheral
*/ */
static IRQn_Type serial_get_irq_n(serial_t *obj) static IRQn_Type serial_get_irq_n(UARTName uart_name)
{ {
struct serial_s *obj_s = SERIAL_S(obj);
IRQn_Type irq_n; IRQn_Type irq_n;
switch (obj_s->index) { switch (uart_name) {
case 0: #if defined(USART1_BASE)
case UART_1:
irq_n = USART1_IRQn; irq_n = USART1_IRQn;
break; break;
#endif
case 1: #if defined(USART2_BASE)
case UART_2:
irq_n = USART2_IRQn; irq_n = USART2_IRQn;
break; break;
#endif
case 2: #if defined(USART3_BASE)
case UART_3:
irq_n = USART3_IRQn; irq_n = USART3_IRQn;
break; break;
#endif
#if defined(UART4_BASE) #if defined(UART4_BASE)
case 3: case UART_4:
irq_n = UART4_IRQn; irq_n = UART4_IRQn;
break; break;
#endif #endif
#if defined(UART5_BASE) #if defined(UART5_BASE)
case 4: case UART_5:
irq_n = UART5_IRQn; irq_n = UART5_IRQn;
break; break;
#endif #endif
default: default:
irq_n = (IRQn_Type)0; irq_n = (IRQn_Type)0;
} }
return irq_n; return irq_n;
} }
@ -364,7 +386,7 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
serial_enable_event(obj, event, 1); // Set only the wanted events serial_enable_event(obj, event, 1); // Set only the wanted events
// Enable interrupt // Enable interrupt
IRQn_Type irq_n = serial_get_irq_n(obj); IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
NVIC_ClearPendingIRQ(irq_n); NVIC_ClearPendingIRQ(irq_n);
NVIC_DisableIRQ(irq_n); NVIC_DisableIRQ(irq_n);
NVIC_SetPriority(irq_n, 1); 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
serial_rx_buffer_set(obj, rx, rx_length, rx_width); serial_rx_buffer_set(obj, rx, rx_length, rx_width);
IRQn_Type irq_n = serial_get_irq_n(obj); IRQn_Type irq_n = serial_get_irq_n(obj_s->uart);
NVIC_ClearPendingIRQ(irq_n); NVIC_ClearPendingIRQ(irq_n);
NVIC_DisableIRQ(irq_n); NVIC_DisableIRQ(irq_n);
NVIC_SetPriority(irq_n, 0); NVIC_SetPriority(irq_n, 0);