From 3b1d67badd215edcb9bf399a5743130a3af60423 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Mon, 24 Oct 2016 11:01:48 +0200 Subject: [PATCH] sys/arduino: make arduino sketch works on avr based boards --- boards/arduino-common/Makefile.features | 1 + examples/arduino_hello-world/hello-world.sketch | 14 ++++++++++++-- sys/arduino/Makefile.include | 2 +- sys/arduino/base.cpp | 2 +- sys/arduino/include/arduino.hpp | 2 +- sys/arduino/include/serialport.hpp | 2 +- sys/arduino/pre.snip | 1 + sys/arduino/serialport.cpp | 4 ++-- 8 files changed, 20 insertions(+), 8 deletions(-) diff --git a/boards/arduino-common/Makefile.features b/boards/arduino-common/Makefile.features index 8131d940b..4a18a5a3c 100644 --- a/boards/arduino-common/Makefile.features +++ b/boards/arduino-common/Makefile.features @@ -5,6 +5,7 @@ FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_uart # Various other features (if any) +FEATURES_PROVIDED += arduino # The board MPU family (used for grouping by the CI system) FEATURES_MCU_GROUP = avr8 diff --git a/examples/arduino_hello-world/hello-world.sketch b/examples/arduino_hello-world/hello-world.sketch index 4247e352d..a6fd74d97 100644 --- a/examples/arduino_hello-world/hello-world.sketch +++ b/examples/arduino_hello-world/hello-world.sketch @@ -13,6 +13,15 @@ #define ARDUINO_LED (0) #endif +// For some boards RIOT defines a macro assigning the required baudrate of the +// serial link. If this macro is not set, the default baudrate is set to +// 115200. +#ifdef UART_STDIO_BAUDRATE +#define SERIAL_BAUDRATE UART_STDIO_BAUDRATE +#else +#define SERIAL_BAUDRATE 115200 +#endif + // Assign the default LED pin int ledPin = ARDUINO_LED; @@ -26,8 +35,9 @@ void setup(void) { // configure the LED pin to be output pinMode(ledPin, OUTPUT); - // configure the first serial port to run with a baudrate of 115200 - Serial.begin(115200); + // configure the first serial port to run with the previously defined + // baudrate + Serial.begin(SERIAL_BAUDRATE); // say hello Serial.println("Hello Arduino!"); } diff --git a/sys/arduino/Makefile.include b/sys/arduino/Makefile.include index 7e87c6f1c..a19ad212e 100644 --- a/sys/arduino/Makefile.include +++ b/sys/arduino/Makefile.include @@ -6,4 +6,4 @@ SRCDIR = $(RIOTBASE)/sys/arduino $(shell $(RIOTBASE)/dist/tools/arduino/pre_build.sh $(SRCDIR) $(APPDIR) $(SKETCHES)) # include the Arduino headers -export INCLUDES += -I$(RIOTBASE)/sys/arduino/include +INCLUDES += -I$(RIOTBASE)/sys/arduino/include diff --git a/sys/arduino/base.cpp b/sys/arduino/base.cpp index 6c9ea49f8..4f61a81c4 100644 --- a/sys/arduino/base.cpp +++ b/sys/arduino/base.cpp @@ -54,7 +54,7 @@ int digitalRead(int pin) } } -void delay(int msec) +void delay(unsigned long msec) { xtimer_usleep(1000 * msec); } diff --git a/sys/arduino/include/arduino.hpp b/sys/arduino/include/arduino.hpp index 883564e1f..5323012ad 100644 --- a/sys/arduino/include/arduino.hpp +++ b/sys/arduino/include/arduino.hpp @@ -80,7 +80,7 @@ int digitalRead(int pin); * * @param[in] msec number of milliseconds to sleep */ -void delay(int msec); +void delay(unsigned long msec); #endif /* ARDUINO_H */ /** @} */ diff --git a/sys/arduino/include/serialport.hpp b/sys/arduino/include/serialport.hpp index 36cc7167a..c373afbbb 100644 --- a/sys/arduino/include/serialport.hpp +++ b/sys/arduino/include/serialport.hpp @@ -84,7 +84,7 @@ public: * * @param[in] speed speed in bits per second (baud) */ - void begin(int speed); + void begin(long speed); /** * @brief Disables serial communication, allowing the RX and TX pins to be diff --git a/sys/arduino/pre.snip b/sys/arduino/pre.snip index 48d08c557..ea75bec12 100644 --- a/sys/arduino/pre.snip +++ b/sys/arduino/pre.snip @@ -1 +1,2 @@ #include "arduino.hpp" +#include "board.h" diff --git a/sys/arduino/serialport.cpp b/sys/arduino/serialport.cpp index 8d5732169..29828f43f 100644 --- a/sys/arduino/serialport.cpp +++ b/sys/arduino/serialport.cpp @@ -43,7 +43,7 @@ int SerialPort::available(void) return (int)rx_buf.avail; } -void SerialPort::begin(int baudrate) +void SerialPort::begin(long baudrate) { /* this clears the contents of the ringbuffer... */ ringbuffer_init(&rx_buf, rx_mem, SERIAL_RX_BUFSIZE); @@ -92,7 +92,7 @@ size_t SerialPort::print(float val) size_t SerialPort::print(float val, int format) { char buf[64]; - size_t len = sprintf(buf, "%.*f", format, val); + size_t len = sprintf(buf, "%.*f", format, (double)val); write(buf, len); return len; }