Skip to content
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

[wiring] acquireSerialXBuffer() support #2375

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion hal/inc/hal_dynalib_usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ DYNALIB_FN(BASE_IDX2 + 1, hal_usart, hal_usart_write_nine_bits, uint32_t(hal_usa
DYNALIB_FN(BASE_IDX2 + 2, hal_usart, hal_usart_send_break, void(hal_usart_interface_t, void*))
DYNALIB_FN(BASE_IDX2 + 3, hal_usart, hal_usart_break_detected, uint8_t(hal_usart_interface_t))
DYNALIB_FN(BASE_IDX2 + 4, hal_usart, hal_usart_sleep, int(hal_usart_interface_t serial, bool, void*))

DYNALIB_FN(BASE_IDX2 + 5, hal_usart, hal_usart_init_ex, int(hal_usart_interface_t, const hal_usart_buffer_config_t*, void*))

DYNALIB_END(hal_usart)

Expand Down
4 changes: 4 additions & 0 deletions hal/inc/hal_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,8 @@
#define HAL_PLATFORM_INTERNAL_LOW_SPEED_CLOCK (0)
#endif // HAL_PLATFORM_INTERNAL_LOW_SPEED_CLOCK

#ifndef HAL_PLATFORM_USART_9BIT_SUPPORTED
#define HAL_PLATFORM_USART_9BIT_SUPPORTED (0)
#endif // HAL_PLATFORM_USART_9BIT_SUPPORTED

#endif /* HAL_PLATFORM_H */
4 changes: 4 additions & 0 deletions hal/inc/hal_platform_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include "platforms.h"

#ifndef HAL_PLATFORM_USART_9BIT_SUPPORTED
#define HAL_PLATFORM_USART_9BIT_SUPPORTED (1)
#endif // HAL_PLATFORM_USART_9BIT_SUPPORTED

#if PLATFORM_ID <= PLATFORM_P1 || PLATFORM_ID == PLATFORM_NEWHAL
#define HAL_PLATFORM_WIFI 1
#define HAL_PLATFORM_WIFI_COMPAT 1
Expand Down
65 changes: 45 additions & 20 deletions hal/src/gcc/usart_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
#include "usart_hal.h"
#include "socket_hal.h"

struct UsartRingBuffer {
uint8_t* buffer;
uint16_t size;
volatile uint16_t head;
volatile uint16_t tail;
};

struct Usart {
virtual void init(hal_usart_ring_buffer_t *rx_buffer, hal_usart_ring_buffer_t *tx_buffer)=0;
virtual void init(const hal_usart_buffer_config_t* conf)=0;
virtual void begin(uint32_t baud)=0;
virtual void end()=0;
virtual int32_t available()=0;
Expand All @@ -21,8 +28,8 @@ const sock_handle_t SOCKET_INVALID = sock_handle_t(-1);
class SocketUsartBase : public Usart
{
private:
hal_usart_ring_buffer_t* rx;
hal_usart_ring_buffer_t* tx;
UsartRingBuffer rx;
UsartRingBuffer tx;

protected:
sock_handle_t socket;
Expand All @@ -34,44 +41,48 @@ class SocketUsartBase : public Usart

inline int32_t read_char(bool peek=false)
{
if (rx->tail==rx->head)
if (rx.tail==rx.head)
return -1;

int32_t c = rx->buffer[rx->tail];
int32_t c = rx.buffer[rx.tail];
if (!peek)
rx->tail = (rx->tail+1) % SERIAL_BUFFER_SIZE;
rx.tail = (rx.tail+1) % rx.size;
return c;
}

inline void write_char(unsigned char c)
{
unsigned i = (unsigned int)(tx->head + 1) % SERIAL_BUFFER_SIZE;
if (i != tx->tail)
unsigned i = (unsigned int)(tx.head + 1) % tx.size;
if (i != tx.tail)
{
tx->buffer[tx->head] = c;
tx->head = i;
tx.buffer[tx.head] = c;
tx.head = i;
}
}

void fillFromSocketIfNeeded() {
int space;
if (rx->head>rx->tail) { // head after tail, so can fill up to end of buffer
space = SERIAL_BUFFER_SIZE-rx->tail;
if (rx.head>rx.tail) { // head after tail, so can fill up to end of buffer
space = rx.size-rx.tail;
}
else {
space = rx->tail-rx->head; // may be 0
space = rx.tail-rx.head; // may be 0
}
if (socket!=SOCKET_INVALID && space>0) {
socket_receive(socket, rx->buffer+rx->head, space, 0);
socket_receive(socket, rx.buffer+rx.head, space, 0);
}
}


public:
virtual void init(hal_usart_ring_buffer_t *rx_buffer, hal_usart_ring_buffer_t *tx_buffer) override
virtual void init(const hal_usart_buffer_config_t* conf) override
{
this->rx = rx_buffer;
this->tx = tx_buffer;
this->rx.buffer = (uint8_t*)conf->rx_buffer;
this->rx.size = conf->rx_buffer_size;
this->rx.head = this->rx.tail = 0;
this->tx.buffer = (uint8_t*)conf->tx_buffer;
this->tx.size = conf->tx_buffer_size;
this->tx.head = this->tx.tail = 0;
}

virtual void end() override {
Expand All @@ -83,10 +94,10 @@ class SocketUsartBase : public Usart

virtual int32_t available() override {
fillFromSocketIfNeeded();
return (rx->head-rx->tail) % SERIAL_BUFFER_SIZE;
return (rx.head-rx.tail) % rx.size;
}
virtual int32_t availableForWrite() override {
return (SERIAL_BUFFER_SIZE + tx->head - tx->tail) % SERIAL_BUFFER_SIZE;
return (rx.size + tx.head - tx.tail) % rx.size;
}
virtual int32_t read() override {
fillFromSocketIfNeeded();
Expand Down Expand Up @@ -178,9 +189,23 @@ static SocketUsartClient usart2 = SocketUsartClient();

}

int hal_usart_init_ex(hal_usart_interface_t serial, const hal_usart_buffer_config_t* config, void*)
{
usartMap(serial).init(config);
return 0;
}

void hal_usart_init(hal_usart_interface_t serial, hal_usart_ring_buffer_t *rx_buffer, hal_usart_ring_buffer_t *tx_buffer)
{
usartMap(serial).init(rx_buffer, tx_buffer);
hal_usart_buffer_config_t conf = {
.size = sizeof(hal_usart_buffer_config_t),
.rx_buffer = rx_buffer->buffer,
.rx_buffer_size = sizeof(rx_buffer->buffer),
.tx_buffer = tx_buffer->buffer,
.tx_buffer_size = sizeof(tx_buffer->buffer)
};

hal_usart_init_ex(serial, &conf, nullptr);
}

void hal_usart_begin(hal_usart_interface_t serial, uint32_t baud)
Expand Down
Loading