Skip to content

Commit b00bd29

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

File tree

2 files changed

+19
-33
lines changed

2 files changed

+19
-33
lines changed

cores/arduino/stm32/uart.c

+17-33
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
}
@@ -681,8 +677,7 @@ int uart_getc(serial_t *obj, unsigned char *c)
681677

682678
*c = (unsigned char)(obj->recv);
683679
/* Restart RX irq */
684-
UART_HandleTypeDef *huart = uart_handlers[obj->index];
685-
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
680+
HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);
686681

687682
return 0;
688683
}
@@ -704,9 +699,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *))
704699
if (serial_rx_active(obj)) {
705700
return;
706701
}
707-
708-
rx_callback[obj->index] = callback;
709-
rx_callback_obj[obj->index] = obj;
702+
obj->rx_callback = callback;
710703

711704
/* Must disable interrupt to prevent handle lock contention */
712705
HAL_NVIC_DisableIRQ(obj->irq);
@@ -730,9 +723,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
730723
if (obj == NULL) {
731724
return;
732725
}
733-
734-
tx_callback[obj->index] = callback;
735-
tx_callback_obj[obj->index] = obj;
726+
obj->tx_callback = callback;
736727

737728
/* Must disable interrupt to prevent handle lock contention */
738729
HAL_NVIC_DisableIRQ(obj->irq);
@@ -750,6 +741,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
750741
* @param UartHandle pointer on the uart reference
751742
* @retval index
752743
*/
744+
/*
753745
uint8_t uart_index(UART_HandleTypeDef *huart)
754746
{
755747
uint8_t i = 0;
@@ -765,6 +757,7 @@ uint8_t uart_index(UART_HandleTypeDef *huart)
765757
766758
return i;
767759
}
760+
*/
768761

769762
/**
770763
* @brief Rx Transfer completed callback
@@ -773,10 +766,9 @@ uint8_t uart_index(UART_HandleTypeDef *huart)
773766
*/
774767
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
775768
{
776-
uint8_t index = uart_index(huart);
777-
778-
if (index < UART_NUM) {
779-
rx_callback[index](rx_callback_obj[index]);
769+
serial_t *obj = get_serial_obj(huart);
770+
if (obj) {
771+
obj->rx_callback(obj);
780772
}
781773
}
782774

@@ -787,14 +779,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
787779
*/
788780
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
789781
{
790-
uint8_t index = uart_index(huart);
791-
serial_t *obj = tx_callback_obj[index];
782+
serial_t *obj = get_serial_obj(huart);
792783

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-
}
784+
if (obj && obj->tx_callback(obj) != -1) {
785+
if (HAL_UART_Transmit_IT(huart, &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
786+
return;
798787
}
799788
}
800789
}
@@ -828,12 +817,9 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
828817
}
829818
#endif
830819
/* 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-
}
820+
serial_t *obj = get_serial_obj(huart);
821+
if (obj && !serial_rx_active(obj)) {
822+
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
837823
}
838824
}
839825

@@ -1044,9 +1030,7 @@ void UART10_IRQHandler(void)
10441030
*/
10451031
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
10461032
{
1047-
uint8_t index = uart_index(huart);
1048-
serial_t *obj = rx_callback_obj[index];
1049-
1033+
serial_t *obj = get_serial_obj(huart);
10501034
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
10511035
}
10521036
#endif /* HAL_UART_MODULE_ENABLED */

cores/arduino/stm32/uart.h

+2
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)