Skip to content

Commit fa9f9a3

Browse files
committed
Remove global variable used for uart Rx
In case of several Serial instances, this global variable was used for all received char. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 3722520 commit fa9f9a3

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

cores/arduino/HardwareSerial.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,21 @@ void HardwareSerial::init(void)
179179

180180
void HardwareSerial::_rx_complete_irq(serial_t* obj)
181181
{
182-
// No Parity error, read byte and store it in the buffer if there is
183-
// room
184-
unsigned char c = uart_getc(obj);
185-
186-
rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;
187-
188-
// if we should be storing the received character into the location
189-
// just before the tail (meaning that the head would advance to the
190-
// current location of the tail), we're about to overflow the buffer
191-
// and so we don't write the character or advance the head.
192-
if (i != obj->rx_tail) {
193-
obj->rx_buff[obj->rx_head] = c;
194-
obj->rx_head = i;
182+
// No Parity error, read byte and store it in the buffer if there is room
183+
unsigned char c;
184+
185+
if (uart_getc(obj, &c) == 0) {
186+
187+
rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;
188+
189+
// if we should be storing the received character into the location
190+
// just before the tail (meaning that the head would advance to the
191+
// current location of the tail), we're about to overflow the buffer
192+
// and so we don't write the character or advance the head.
193+
if (i != obj->rx_tail) {
194+
obj->rx_buff[obj->rx_head] = c;
195+
obj->rx_head = i;
196+
}
195197
}
196198
}
197199

cores/arduino/stm32/uart.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ static serial_t *rx_callback_obj[UART_NUM];
8383
static int (*tx_callback[UART_NUM])(serial_t*);
8484
static serial_t *tx_callback_obj[UART_NUM];
8585

86-
static uint8_t rx_buffer[1] = {0};
87-
8886
/**
8987
* @brief Function called to initialize the uart interface
9088
* @param obj : pointer to serial_t structure
@@ -461,7 +459,7 @@ uint8_t serial_tx_active(serial_t *obj)
461459
* @param obj : pointer to serial_t structure
462460
* @retval last character received
463461
*/
464-
int uart_getc(serial_t *obj)
462+
int uart_getc(serial_t *obj, unsigned char* c)
465463
{
466464
if(obj == NULL) {
467465
return -1;
@@ -471,11 +469,12 @@ int uart_getc(serial_t *obj)
471469
return -1; // transaction ongoing
472470
}
473471

472+
*c = (unsigned char)(obj->recv);
474473
// Restart RX irq
475474
UART_HandleTypeDef *huart = uart_handlers[obj->index];
476-
HAL_UART_Receive_IT(huart, rx_buffer, 1);
475+
HAL_UART_Receive_IT(huart, &(obj->recv), 1);
477476

478-
return rx_buffer[0];
477+
return 0;
479478
}
480479

481480
/**
@@ -502,7 +501,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*))
502501
HAL_NVIC_SetPriority(obj->irq, 0, 1);
503502
HAL_NVIC_EnableIRQ(obj->irq);
504503

505-
if(HAL_UART_Receive_IT(uart_handlers[obj->index], rx_buffer, 1) != HAL_OK) {
504+
if(HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1) != HAL_OK) {
506505
return;
507506
}
508507
}

cores/arduino/stm32/uart.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ typedef struct serial_s serial_t;
188188
struct serial_s {
189189
USART_TypeDef *uart;
190190
UART_HandleTypeDef handle;
191-
int index;
191+
uint8_t index;
192+
uint8_t recv;
192193
uint32_t baudrate;
193194
uint32_t databits;
194195
uint32_t stopbits;
@@ -279,7 +280,7 @@ struct serial_s {
279280
void uart_init(serial_t *obj);
280281
void uart_deinit(serial_t *obj);
281282
size_t uart_write(serial_t *obj, uint8_t data, uint16_t size);
282-
int uart_getc(serial_t *obj);
283+
int uart_getc(serial_t *obj, unsigned char* c);
283284
void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*));
284285
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t*));
285286

0 commit comments

Comments
 (0)