Skip to content

Commit

Permalink
sys/arduino: support serial with feather-m0 (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavid committed Dec 10, 2021
1 parent af1bb73 commit b2f9289
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
32 changes: 18 additions & 14 deletions sys/arduino/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern "C" {

#include "fmt.h"
#include "irq.h"
#include "stdio_base.h"
}

#include "serialport.hpp"
Expand All @@ -41,19 +42,21 @@ SerialPort::SerialPort(uart_t dev)

int SerialPort::available(void)
{
return (int)rx_buf.avail;
return stdio_avail();
}

void SerialPort::begin(long baudrate)
{
stdio_init();
/* this clears the contents of the ringbuffer... */
ringbuffer_init(&rx_buf, rx_mem, SERIAL_RX_BUFSIZE);
uart_init(dev, (uint32_t)baudrate, rx_cb, (void *)&rx_buf);
stdio_clear();
(void)baudrate; // FIXME Should we use the baudrate, somehow?
}

void SerialPort::end(void)
{
uart_poweroff(dev);
// XXX Should we switch off the stdio backend?
//uart_poweroff(dev);
}

size_t SerialPort::print(int val)
Expand Down Expand Up @@ -249,31 +252,32 @@ size_t SerialPort::println(void)

int SerialPort::read(void)
{
int res = -1;

irq_disable();
if (rx_buf.avail > 0) {
res = ringbuffer_get_one(&rx_buf);
if (stdio_avail()) {
char c;
ssize_t n = stdio_read((void*)&c, 1);
if (n != 1) {
return -1;
}
return c;
}
irq_enable();

return res;
return -1;
}

int SerialPort::write(int val)
{
uart_write(dev, (uint8_t *)&val, 1);
stdio_write((const void*)&val, 1);
return 1;
}

int SerialPort::write(const char *str)
{
uart_write(dev, (uint8_t *)str, strlen(str));
stdio_write((const void*)str, strlen(str));
return strlen(str);
}

int SerialPort::write(char *buf, int len)
{
uart_write(dev, (uint8_t *)buf, len);
stdio_write((const void*)buf, len);
return len;
}
12 changes: 12 additions & 0 deletions sys/include/stdio_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ extern "C" {
*/
void stdio_init(void);

/**
* @brief Get number of bytes available for reading.
*
* @return nr of available bytes
*/
int stdio_avail(void);

/**
* @brief Clear the input buffer
*/
void stdio_clear(void);

/**
* @brief read @p len bytes from stdio uart into @p buffer
*
Expand Down
10 changes: 10 additions & 0 deletions sys/usb/usbus/cdc/acm/cdc_acm_stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ void stdio_init(void)
#endif
}

int stdio_avail(void)
{
return tsrb_avail(&_cdc_stdio_isrpipe.tsrb);
}

void stdio_clear(void)
{
tsrb_clear(&_cdc_stdio_isrpipe.tsrb);
}

ssize_t stdio_read(void* buffer, size_t len)
{
(void)buffer;
Expand Down

0 comments on commit b2f9289

Please sign in to comment.