Skip to content

Commit 927fdc5

Browse files
authored
Merge pull request #139 from fpistm/rev_uart
Remove global variable used for uart Rx
2 parents bfd0d0a + fa9f9a3 commit 927fdc5

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
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

+10-7
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@
7070
#define UART_NUM (8)
7171
#elif defined(STM32F2xx)
7272
#define UART_NUM (6)
73-
#else // STM32F1xx || STM32F3xx || STM32L0xx || STM32L1xx || STM32L4xx
73+
#elif defined(STM32F1xx) || defined(STM32F3xx) ||\
74+
defined(STM32L0xx) || defined(STM32L1xx) || defined(STM32L4xx)
7475
#define UART_NUM (5)
76+
#else
77+
#error "Unknown Family - unknown UART_NUM"
7578
#endif
79+
7680
static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL};
7781
static void (*rx_callback[UART_NUM])(serial_t*);
7882
static serial_t *rx_callback_obj[UART_NUM];
7983
static int (*tx_callback[UART_NUM])(serial_t*);
8084
static serial_t *tx_callback_obj[UART_NUM];
8185

82-
static uint8_t rx_buffer[1] = {0};
83-
8486
/**
8587
* @brief Function called to initialize the uart interface
8688
* @param obj : pointer to serial_t structure
@@ -457,7 +459,7 @@ uint8_t serial_tx_active(serial_t *obj)
457459
* @param obj : pointer to serial_t structure
458460
* @retval last character received
459461
*/
460-
int uart_getc(serial_t *obj)
462+
int uart_getc(serial_t *obj, unsigned char* c)
461463
{
462464
if(obj == NULL) {
463465
return -1;
@@ -467,11 +469,12 @@ int uart_getc(serial_t *obj)
467469
return -1; // transaction ongoing
468470
}
469471

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

474-
return rx_buffer[0];
477+
return 0;
475478
}
476479

477480
/**
@@ -498,7 +501,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*))
498501
HAL_NVIC_SetPriority(obj->irq, 0, 1);
499502
HAL_NVIC_EnableIRQ(obj->irq);
500503

501-
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) {
502505
return;
503506
}
504507
}

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)