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

boards/feather-m0: add arduino feature #17335

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions boards/feather-m0/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ config BOARD_FEATHER_M0
select HAS_PERIPH_TIMER
select HAS_PERIPH_UART
select HAS_PERIPH_USBDEV
select HAS_ARDUINO
select HAS_HIGHLEVEL_STDIO
1 change: 1 addition & 0 deletions boards/feather-m0/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ FEATURES_PROVIDED += periph_uart
FEATURES_PROVIDED += periph_usbdev

# Put other features for this board (in alphabetical order)
FEATURES_PROVIDED += arduino
FEATURES_PROVIDED += highlevel_stdio
84 changes: 84 additions & 0 deletions boards/feather-m0/include/arduino_board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2021 J. David Ibáñez <jdavid.ibp@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup boards_feather-m0
* @{
*
* @file
* @brief Board specific configuration for the Arduino API
*
* @author J. David Ibáñez <jdavid.ibp@gmail.com>
*/

#ifndef ARDUINO_BOARD_H
#define ARDUINO_BOARD_H

#include "arduino_pinmap.h"
#include "periph/pwm.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The on-board LED is connected to pin 13 on this board
*/
#define ARDUINO_LED (13)

/**
* @brief Look-up table for the Arduino's digital pins
*/
static const gpio_t arduino_pinmap[] = {
ARDUINO_PIN_0,
ARDUINO_PIN_1,
ARDUINO_PIN_2,
ARDUINO_PIN_3,
ARDUINO_PIN_4,
ARDUINO_PIN_5,
ARDUINO_PIN_6,
ARDUINO_PIN_7,
ARDUINO_PIN_8,
ARDUINO_PIN_9,
ARDUINO_PIN_10,
ARDUINO_PIN_11,
ARDUINO_PIN_12,
ARDUINO_PIN_13,
ARDUINO_PIN_14,
ARDUINO_PIN_15,
ARDUINO_PIN_16,
ARDUINO_PIN_17,
ARDUINO_PIN_18,
ARDUINO_PIN_19,
ARDUINO_PIN_20,
ARDUINO_PIN_21,
ARDUINO_PIN_22,
ARDUINO_PIN_23,
ARDUINO_PIN_24,
};

/**
* @brief Look-up table for the Arduino's analog pins
*/
static const adc_t arduino_analog_map[] = {
ARDUINO_A0,
ARDUINO_A1,
ARDUINO_A2,
ARDUINO_A3,
ARDUINO_A4,
ARDUINO_A5,
ADC_UNDEF,
ARDUINO_A7,
};

#ifdef __cplusplus
}
#endif

#endif /* ARDUINO_BOARD_H */
/** @} */
91 changes: 91 additions & 0 deletions boards/feather-m0/include/arduino_pinmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2021 J. David Ibáñez <jdavid.ibp@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup boards_feather-m0
* @{
*
* @file
* @brief Mapping from MCU pins to Arduino pins
*
* You can use the defines in this file for simplified interaction with the
* Arduino specific pin numbers.
*
* @author J. David Ibáñez <jdavid.ibp@gmail.com>
*/

#ifndef ARDUINO_PINMAP_H
#define ARDUINO_PINMAP_H

#include "periph/gpio.h"
#include "periph/adc.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Mapping of MCU pins to Arduino pins
*
* @{
*/

#define ARDUINO_PIN_0 GPIO_PIN(PA, 11) /**< D0 (RX) */
#define ARDUINO_PIN_1 GPIO_PIN(PA, 10) /**< D1 (TX) */
#define ARDUINO_PIN_2 GPIO_UNDEF /**< D2 */
#define ARDUINO_PIN_3 GPIO_UNDEF /**< D3 */
#define ARDUINO_PIN_4 GPIO_UNDEF /**< D4 */
#define ARDUINO_PIN_5 GPIO_PIN(PA, 15) /**< D5 */
#define ARDUINO_PIN_6 GPIO_PIN(PA, 20) /**< D6 */
#define ARDUINO_PIN_7 GPIO_UNDEF /**< D7 */
#define ARDUINO_PIN_8 GPIO_UNDEF /**< D8 */
#define ARDUINO_PIN_9 GPIO_PIN(PA, 7) /**< D9 */
#define ARDUINO_PIN_10 GPIO_PIN(PA, 18) /**< D10 */
#define ARDUINO_PIN_11 GPIO_PIN(PA, 16) /**< D11 */
#define ARDUINO_PIN_12 GPIO_PIN(PA, 19) /**< D12 */
#define ARDUINO_PIN_13 GPIO_PIN(PA, 17) /**< D13 (LED) */
#define ARDUINO_PIN_14 GPIO_PIN(PA, 2) /**< D14 (A0) */
#define ARDUINO_PIN_15 GPIO_PIN(PB, 8) /**< D15 (A1) */
#define ARDUINO_PIN_16 GPIO_PIN(PB, 9) /**< D16 (A2) */
#define ARDUINO_PIN_17 GPIO_PIN(PA, 4) /**< D17 (A3) */
#define ARDUINO_PIN_18 GPIO_PIN(PA, 5) /**< D18 (A4) */
#define ARDUINO_PIN_19 GPIO_PIN(PB, 2) /**< D19 (A5) */
#define ARDUINO_PIN_20 GPIO_PIN(PA, 22) /**< D20 (I2C SDA) */
#define ARDUINO_PIN_21 GPIO_PIN(PA, 23) /**< D21 (I2C SCL) */
#define ARDUINO_PIN_22 GPIO_PIN(PA, 12) /**< D22 (SPI MISO) */
#define ARDUINO_PIN_23 GPIO_PIN(PB, 10) /**< D23 (SPI MOSI) */
#define ARDUINO_PIN_24 GPIO_PIN(PB, 11) /**< D24 (SPI SCK) */

#define ARDUINO_PIN_A0 ARDUINO_PIN_14 /**< A0 */
#define ARDUINO_PIN_A1 ARDUINO_PIN_15 /**< A1 */
#define ARDUINO_PIN_A2 ARDUINO_PIN_16 /**< A2 */
#define ARDUINO_PIN_A3 ARDUINO_PIN_17 /**< A3 */
#define ARDUINO_PIN_A4 ARDUINO_PIN_18 /**< A4 */
#define ARDUINO_PIN_A5 ARDUINO_PIN_19 /**< A5 */
#define ARDUINO_PIN_A7 ARDUINO_PIN_9 /**< A7 */
/** @} */

/**
* @name Mapping of Arduino analog pins to RIOT ADC lines
* @{
*/
#define ARDUINO_A0 ADC_LINE(0) /**< ADC 0 */
#define ARDUINO_A1 ADC_LINE(2) /**< ADC 1 */
#define ARDUINO_A2 ADC_LINE(3) /**< ADC 2 */
#define ARDUINO_A3 ADC_LINE(4) /**< ADC 3 */
#define ARDUINO_A4 ADC_LINE(5) /**< ADC 4 */
#define ARDUINO_A5 ADC_LINE(10) /**< ADC 5 */
#define ARDUINO_A7 ADC_LINE(7) /**< ADC 7 */
/** @} */

#ifdef __cplusplus
}
#endif

#endif /* ARDUINO_PINMAP_H */
/** @} */
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be realized without extending the stdio_base API by using a ringbuffer as for direct used of periph_uart. In that case Serial::begin would clear the ringbuffer and stdio_base wouldn't be necessary.

}

void SerialPort::begin(long baudrate)
{
stdio_init();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdio_init is called during CPU initialization and should only be called once. Calling it again here might lead to problems. It should be sufficient to initialize members of the SerialPort object or variables of the module here if there are any.

/* 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of stdio_uart, it might be enough to call stdio_init. It depends on periph_uart implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether it is necessary to clear buffers here at all.

(void)baudrate; // FIXME Should we use the baudrate, somehow?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of std_uart backend, the baudrate is fixed and defined by STDIO_UART_BAUDRATE.

}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of stdio_uart backend, this requires to enable module stdio_uart_rx to work.

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);
Copy link
Contributor

@gschorcht gschorcht Dec 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works only, if all stdio_* backends implement this function. std_uart does not at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this would be an API change and should at least be done in a separate PR. An API change should only be made if and when absolutely necessary. It would require that all stdio_* backends implement it.


/**
* @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