Skip to content

Remove global variable used for uart Rx #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,21 @@ void HardwareSerial::init(void)

void HardwareSerial::_rx_complete_irq(serial_t* obj)
{
// No Parity error, read byte and store it in the buffer if there is
// room
unsigned char c = uart_getc(obj);

rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != obj->rx_tail) {
obj->rx_buff[obj->rx_head] = c;
obj->rx_head = i;
// No Parity error, read byte and store it in the buffer if there is room
unsigned char c;

if (uart_getc(obj, &c) == 0) {

rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != obj->rx_tail) {
obj->rx_buff[obj->rx_head] = c;
obj->rx_head = i;
}
}
}

Expand Down
17 changes: 10 additions & 7 deletions cores/arduino/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,19 @@
#define UART_NUM (8)
#elif defined(STM32F2xx)
#define UART_NUM (6)
#else // STM32F1xx || STM32F3xx || STM32L0xx || STM32L1xx || STM32L4xx
#elif defined(STM32F1xx) || defined(STM32F3xx) ||\
defined(STM32L0xx) || defined(STM32L1xx) || defined(STM32L4xx)
#define UART_NUM (5)
#else
#error "Unknown Family - unknown UART_NUM"
#endif

static UART_HandleTypeDef *uart_handlers[UART_NUM] = {NULL};
static void (*rx_callback[UART_NUM])(serial_t*);
static serial_t *rx_callback_obj[UART_NUM];
static int (*tx_callback[UART_NUM])(serial_t*);
static serial_t *tx_callback_obj[UART_NUM];

static uint8_t rx_buffer[1] = {0};

/**
* @brief Function called to initialize the uart interface
* @param obj : pointer to serial_t structure
Expand Down Expand Up @@ -457,7 +459,7 @@ uint8_t serial_tx_active(serial_t *obj)
* @param obj : pointer to serial_t structure
* @retval last character received
*/
int uart_getc(serial_t *obj)
int uart_getc(serial_t *obj, unsigned char* c)
{
if(obj == NULL) {
return -1;
Expand All @@ -467,11 +469,12 @@ int uart_getc(serial_t *obj)
return -1; // transaction ongoing
}

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

return rx_buffer[0];
return 0;
}

/**
Expand All @@ -498,7 +501,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*))
HAL_NVIC_SetPriority(obj->irq, 0, 1);
HAL_NVIC_EnableIRQ(obj->irq);

if(HAL_UART_Receive_IT(uart_handlers[obj->index], rx_buffer, 1) != HAL_OK) {
if(HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1) != HAL_OK) {
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions cores/arduino/stm32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ typedef struct serial_s serial_t;
struct serial_s {
USART_TypeDef *uart;
UART_HandleTypeDef handle;
int index;
uint8_t index;
uint8_t recv;
uint32_t baudrate;
uint32_t databits;
uint32_t stopbits;
Expand Down Expand Up @@ -279,7 +280,7 @@ struct serial_s {
void uart_init(serial_t *obj);
void uart_deinit(serial_t *obj);
size_t uart_write(serial_t *obj, uint8_t data, uint16_t size);
int uart_getc(serial_t *obj);
int uart_getc(serial_t *obj, unsigned char* c);
void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t*));
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t*));

Expand Down