Skip to content

Commit 34531b8

Browse files
committed
[UART] Simplify code using get_serial_obj()
Remove of 4 array which are no more needed. Add rx/tx_callaback in the serial_t. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 5c3fb05 commit 34531b8

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

cores/arduino/stm32/uart.c

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ typedef enum {
9797
} uart_index_t;
9898

9999
static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL};
100-
static void (*rx_callback[UART_NUM])(serial_t *);
101-
static serial_t *rx_callback_obj[UART_NUM];
102-
static int (*tx_callback[UART_NUM])(serial_t *);
103-
static serial_t *tx_callback_obj[UART_NUM];
104100

105101
static serial_t serial_debug = { .uart = NP, .index = UART_NUM };
106102

@@ -621,7 +617,7 @@ size_t uart_debug_write(uint8_t *data, uint32_t size)
621617
return 0;
622618
}
623619
} else {
624-
serial_t *obj = rx_callback_obj[serial_debug.index];
620+
serial_t *obj = get_serial_obj(uart_handlers[serial_debug.index]);
625621
if (obj) {
626622
serial_debug.irq = obj->irq;
627623
}
@@ -704,9 +700,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *))
704700
if (serial_rx_active(obj)) {
705701
return;
706702
}
707-
708-
rx_callback[obj->index] = callback;
709-
rx_callback_obj[obj->index] = obj;
703+
obj->rx_callback = callback;
710704

711705
/* Must disable interrupt to prevent handle lock contention */
712706
HAL_NVIC_DisableIRQ(obj->irq);
@@ -730,9 +724,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
730724
if (obj == NULL) {
731725
return;
732726
}
733-
734-
tx_callback[obj->index] = callback;
735-
tx_callback_obj[obj->index] = obj;
727+
obj->tx_callback = callback;
736728

737729
/* Must disable interrupt to prevent handle lock contention */
738730
HAL_NVIC_DisableIRQ(obj->irq);
@@ -750,6 +742,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
750742
* @param UartHandle pointer on the uart reference
751743
* @retval index
752744
*/
745+
/*
753746
uint8_t uart_index(UART_HandleTypeDef *huart)
754747
{
755748
uint8_t i = 0;
@@ -765,6 +758,7 @@ uint8_t uart_index(UART_HandleTypeDef *huart)
765758
766759
return i;
767760
}
761+
*/
768762

769763
/**
770764
* @brief Rx Transfer completed callback
@@ -773,10 +767,9 @@ uint8_t uart_index(UART_HandleTypeDef *huart)
773767
*/
774768
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
775769
{
776-
uint8_t index = uart_index(huart);
777-
778-
if (index < UART_NUM) {
779-
rx_callback[index](rx_callback_obj[index]);
770+
serial_t *obj = get_serial_obj(huart);
771+
if (obj) {
772+
obj->rx_callback(obj);
780773
}
781774
}
782775

@@ -787,14 +780,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
787780
*/
788781
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
789782
{
790-
uint8_t index = uart_index(huart);
791-
serial_t *obj = tx_callback_obj[index];
783+
serial_t *obj = get_serial_obj(huart);
792784

793-
if (index < UART_NUM) {
794-
if (tx_callback[index](obj) != -1) {
795-
if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
796-
return;
797-
}
785+
if (obj && obj->tx_callback(obj) != -1) {
786+
if (HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
787+
return;
798788
}
799789
}
800790
}
@@ -828,12 +818,9 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
828818
}
829819
#endif
830820
/* Restart receive interrupt after any error */
831-
uint8_t index = uart_index(huart);
832-
if (index < UART_NUM) {
833-
serial_t *obj = rx_callback_obj[index];
834-
if (obj && !serial_rx_active(obj)) {
835-
HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);
836-
}
821+
serial_t *obj = get_serial_obj(huart);
822+
if (obj && !serial_rx_active(obj)) {
823+
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
837824
}
838825
}
839826

@@ -1044,9 +1031,7 @@ void UART10_IRQHandler(void)
10441031
*/
10451032
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
10461033
{
1047-
uint8_t index = uart_index(huart);
1048-
serial_t *obj = rx_callback_obj[index];
1049-
1034+
serial_t *obj = get_serial_obj(huart);
10501035
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
10511036
}
10521037
#endif /* HAL_UART_MODULE_ENABLED */

cores/arduino/stm32/uart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct serial_s {
6060
*/
6161
USART_TypeDef *uart;
6262
UART_HandleTypeDef handle;
63+
void (*rx_callback)(serial_t *);
64+
int (*tx_callback)(serial_t *);
6365
uint32_t baudrate;
6466
uint32_t databits;
6567
uint32_t stopbits;

0 commit comments

Comments
 (0)