-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
491612f
f177a61
af1bb73
b2f9289
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 */ | ||
/** @} */ |
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 */ | ||
/** @} */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ extern "C" { | |
|
||
#include "fmt.h" | ||
#include "irq.h" | ||
#include "stdio_base.h" | ||
} | ||
|
||
#include "serialport.hpp" | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/* 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of |
||
} | ||
|
||
void SerialPort::end(void) | ||
{ | ||
uart_poweroff(dev); | ||
// XXX Should we switch off the stdio backend? | ||
//uart_poweroff(dev); | ||
} | ||
|
||
size_t SerialPort::print(int val) | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of |
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works only, if all There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
/** | ||
* @brief Clear the input buffer | ||
*/ | ||
void stdio_clear(void); | ||
|
||
/** | ||
* @brief read @p len bytes from stdio uart into @p buffer | ||
* | ||
|
There was a problem hiding this comment.
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 ofperiph_uart
. In that caseSerial::begin
would clear the ringbuffer andstdio_base
wouldn't be necessary.