├── .mbedignore ├── ArduinoCore-API.lib ├── ArduinoCore-nRF528x-mbedos.lib ├── ChainableLED.lib ├── LICENSE ├── README.md ├── mbed-os.lib ├── mbed_app.json └── source ├── pins_arduino.h ├── sketch.cpp ├── sketch.cpp.multithreaded └── variant.cpp /.mbedignore: -------------------------------------------------------------------------------- 1 | ArduinoCore-nRF528x-mbedos/cores/arduino/api/ 2 | ArduinoCore-nRF528x-mbedos/cores/arduino/USB/ 3 | ArduinoCore-nRF528x-mbedos/libraries/PDM/ 4 | ArduinoCore-nRF528x-mbedos/libraries/USBHID/ 5 | ArduinoCore-nRF528x-mbedos/libraries/USBMSD/ 6 | ArduinoCore-nRF528x-mbedos/variants/ 7 | mbed-os/usb/ -------------------------------------------------------------------------------- /ArduinoCore-API.lib: -------------------------------------------------------------------------------- 1 | https://github.com/arduino/ArduinoCore-API/#b5afe537cb7d72b69db65794a1f552bfcf5f2d55 2 | -------------------------------------------------------------------------------- /ArduinoCore-nRF528x-mbedos.lib: -------------------------------------------------------------------------------- 1 | https://github.com/janjongboom/ArduinoCore-nRF528x-mbedos/#9593a51e44b15b2ef4599178c32cb359146b321b 2 | -------------------------------------------------------------------------------- /ChainableLED.lib: -------------------------------------------------------------------------------- 1 | https://github.com/pjpmarques/ChainableLED/#6b26978f5d02964b0a13032de7f7f6264d9b3d6b 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2019 Jan Jongboom 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Running Arduino sketches on Mbed OS 2 | 3 | The new [Arduino Nano 33 BLE](https://blog.arduino.cc/2019/07/31/why-we-chose-to-build-the-arduino-nano-33-ble-core-on-mbed-os/) board is a development board that runs both Arduino and Mbed OS. This is done by implementing Arduino core on top of Mbed OS, giving users the opportunity to write both Arduino sketches and Mbed OS applications at the same time. This project also allows us to add the Arduino core as a library to any Mbed OS application, thus adding support for Arduino sketches and libraries to any Mbed OS project. 4 | 5 | This repository contains an example of running the [Arduino ChainableLED sketch](https://github.com/pjpmarques/ChainableLED) on Mbed OS boards. It can be used as a basis to run any Arduino sketch on any Mbed OS board. 6 | 7 | ## Building with Mbed CLI 8 | 9 | **Note:** You can only build this repository on a **case-sensitive** file system (due to naming issues around `String.cpp` / `string.cpp`). On macOS follow [these instructions](https://coderwall.com/p/mgi8ja/case-sensitive-git-in-mac-os-x-like-a-pro) to create a case-sensitive mount point. On Windows, follow [these instructions](https://superuser.com/questions/266110/how-do-you-make-windows-7-fully-case-sensitive-with-respect-to-the-filesystem) to enable case-sensitivity on your file system. 10 | 11 | 1. Connect a Grove Chainable LED to pin D4/D5 on your development board. 12 | 1. Import this application: 13 | 14 | ``` 15 | $ mbed import https://github.com/janjongboom/mbed-arduino-mashup 16 | ``` 17 | 18 | 1. Build and flash the application: 19 | 20 | ``` 21 | $ mbed compile -m auto -t GCC_ARM -f 22 | ``` 23 | 24 | 1. Done! 25 | 26 | ## Building with the Online Compiler 27 | 28 | Not supported. The Arduino core cannot currently be compiled under under the ARMCC compiler. 29 | 30 | ## The sketch and bootstrapping 31 | 32 | The sketch is located in [source/sketch.cpp](source/sketch.cpp). 33 | 34 | Arduino uses a `setup` and a `loop` function, which are unknown to Mbed OS. Thus, you'll need to call these functions manually. You'll see this at the bottom of the `sketch.cpp` file: 35 | 36 | ```cpp 37 | // Mbed OS bootstrap code 38 | int main() { 39 | setup(); 40 | 41 | while (1) loop(); 42 | } 43 | ``` 44 | 45 | Here you'll have the full power of Mbed OS on your hands, so you can start new threads, use the file system, or use one of the network drivers. See [source/sketch.cpp.multithreaded](source/sketch.cpp.multithreaded) for an example. 46 | 47 | ## Help 48 | 49 | * Q: I receive errors that `strlen`, `memmove` or other functions are not found. 50 | * A: This project can only be built on case-sensitive file systems. See the note under 'Building with Mbed CLI'. 51 | * Q: I receive errors that the `D0` symbol cannot be found. 52 | * A: Your development board doesn't support Arduino pins (in PinNames.h in Mbed OS). You need to create a custom pin map in `variant.cpp`. 53 | -------------------------------------------------------------------------------- /mbed-os.lib: -------------------------------------------------------------------------------- 1 | https://github.com/ARMmbed/mbed-os/#b81aeff1a3e171c6421984faa2cc18d0e35746c0 2 | -------------------------------------------------------------------------------- /mbed_app.json: -------------------------------------------------------------------------------- 1 | { 2 | "target_overrides": { 3 | "*": { 4 | "target.macros_remove": [ "SERIAL_CDC" ] 5 | } 6 | } 7 | } -------------------------------------------------------------------------------- /source/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "PinNames.h" 3 | #include 4 | #include 5 | 6 | #ifndef __PINS_ARDUINO__ 7 | #define __PINS_ARDUINO__ 8 | 9 | // // Frequency of the board main oscillator 10 | // #define VARIANT_MAINOSC (32768ul) 11 | 12 | // // Master clock frequency 13 | // #define VARIANT_MCK (64000000ul) 14 | 15 | // Pins 16 | // ---- 17 | 18 | // Number of pins defined in PinDescription array 19 | #ifdef __cplusplus 20 | extern "C" unsigned int PINCOUNT_fn(); 21 | #endif 22 | #define PINS_COUNT (PINCOUNT_fn()) 23 | #define NUM_DIGITAL_PINS (21u) 24 | #define NUM_ANALOG_INPUTS (7u) 25 | #define NUM_ANALOG_OUTPUTS (0u) 26 | 27 | // LEDs 28 | // ---- 29 | #define PIN_LED LED1 30 | #define LED_BUILTIN PIN_LED 31 | 32 | // Analog pins 33 | // ----------- 34 | #define PIN_A0 A0 35 | #define PIN_A1 A1 36 | #define PIN_A2 A2 37 | #define PIN_A3 A3 38 | #define PIN_A4 A4 39 | #define PIN_A5 A5 40 | #define ADC_RESOLUTION 12 41 | 42 | /* 43 | * Serial interfaces 44 | */ 45 | // Serial (EDBG) 46 | #define PIN_SERIAL_RX SERIAL_RX 47 | #define PIN_SERIAL_TX SERIAL_TX 48 | 49 | // SPI 50 | #define PIN_SPI_MISO SPI_MISO 51 | #define PIN_SPI_MOSI SPI_MOSI 52 | #define PIN_SPI_SCK SPI_SCK 53 | #define PIN_SPI_SS SPI_CS 54 | 55 | static const uint8_t SS = PIN_SPI_SS; // SPI Slave SS not used. Set here only for reference. 56 | static const uint8_t MOSI = PIN_SPI_MOSI; 57 | static const uint8_t MISO = PIN_SPI_MISO; 58 | static const uint8_t SCK = PIN_SPI_SCK; 59 | 60 | // Wire 61 | #define PIN_WIRE_SDA I2C_SDA 62 | #define PIN_WIRE_SCL I2C_SCL 63 | 64 | // #define PIN_WIRE_SDA1 (30u) 65 | // #define PIN_WIRE_SCL1 (31u) 66 | 67 | #define PIN_ENABLE_SENSORS_3V3 NC 68 | #define PIN_ENABLE_I2C_PULLUP NC 69 | 70 | #define PIN_INT_APDS (26u) 71 | 72 | // PDM Interfaces 73 | // --------------- 74 | // #define PIN_PDM_PWR (27) 75 | // #define PIN_PDM_CLK (28) 76 | // #define PIN_PDM_DIN (29) 77 | 78 | // These serial port names are intended to allow libraries and architecture-neutral 79 | // sketches to automatically default to the correct port name for a particular type 80 | // of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN, 81 | // the first hardware serial port whose RX/TX pins are not dedicated to another use. 82 | // 83 | // SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor 84 | // 85 | // SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial 86 | // 87 | // SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library 88 | // 89 | // SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins. 90 | // 91 | // SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX 92 | // pins are NOT connected to anything by default. 93 | #define SERIAL_PORT_USBVIRTUAL SerialUSB 94 | #define SERIAL_PORT_MONITOR SerialUSB 95 | #define SERIAL_PORT_HARDWARE Serial1 96 | #define SERIAL_PORT_HARDWARE_OPEN Serial1 97 | 98 | 99 | // Mbed specific defines 100 | #define SERIAL_HOWMANY 1 101 | #define SERIAL1_TX PIN_SERIAL_TX 102 | #define SERIAL1_RX PIN_SERIAL_RX 103 | 104 | #define BOARD_NAME "GENERIC-MBED-OS" 105 | 106 | #define DFU_MAGIC_SERIAL_ONLY_RESET 0xb0 107 | 108 | #define digitalPinToPort(P) (digitalPinToPinName(P)/32) 109 | 110 | uint8_t getUniqueSerialNumber(uint8_t* name); 111 | void _ontouch1200bps_(); 112 | 113 | #endif //__PINS_ARDUINO__ 114 | -------------------------------------------------------------------------------- /source/sketch.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include 3 | 4 | // demo from https://github.com/pjpmarques/ChainableLED 5 | #define NUM_LEDS 1 6 | 7 | ChainableLED leds(4, 5, NUM_LEDS); 8 | 9 | void setup() { 10 | leds.init(); 11 | pinMode(LED_BUILTIN, OUTPUT); 12 | } 13 | 14 | float hue = 0.0; 15 | boolean up = true; 16 | 17 | void loop() { 18 | for (byte i=0; i= 1.0 && up) 29 | up = false; 30 | else if (hue <= 0.0 && !up) 31 | up = true; 32 | } 33 | 34 | // Mbed OS bootstrap code 35 | int main() { 36 | setup(); 37 | 38 | while (1) loop(); 39 | } 40 | -------------------------------------------------------------------------------- /source/sketch.cpp.multithreaded: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include 3 | 4 | // demo from https://github.com/pjpmarques/ChainableLED 5 | #define NUM_LEDS 1 6 | 7 | ChainableLED leds(4, 5, NUM_LEDS); 8 | 9 | void setup() { 10 | Serial.begin(115200); 11 | leds.init(); 12 | pinMode(LED_BUILTIN, OUTPUT); 13 | Serial.println("Welcome to Arduino on Mbed OS"); 14 | } 15 | 16 | float hue = 0.0; 17 | boolean up = true; 18 | 19 | void loop() { 20 | for (byte i=0; i= 1.0 && up) 31 | up = false; 32 | else if (hue <= 0.0 && !up) 33 | up = true; 34 | } 35 | 36 | void loop_builtin_led() { 37 | digitalWrite(LED_BUILTIN, HIGH); 38 | Serial.println("LED is now on!"); 39 | delay(1000); 40 | digitalWrite(LED_BUILTIN, LOW); 41 | Serial.println("LED is now off!"); 42 | delay(1000); 43 | } 44 | 45 | void builtin_thread_main() { 46 | while (1) loop_builtin_led(); 47 | } 48 | 49 | // bootstrap code 50 | int main() { 51 | setup(); 52 | 53 | Thread builtin_thread; 54 | builtin_thread.start(&builtin_thread_main); 55 | 56 | while (1) loop(); 57 | } 58 | -------------------------------------------------------------------------------- /source/variant.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "PinNames.h" 3 | 4 | PinDescription g_APinDescription[] = { 5 | // D0 - D7 6 | D0, NULL, NULL, // D0/TX 7 | D1, NULL, NULL, // D1/RX 8 | D2, NULL, NULL, // D2 9 | D3, NULL, NULL, // D3 10 | D4, NULL, NULL, // D4 11 | D5, NULL, NULL, // D5 12 | D6, NULL, NULL, // D6 13 | D7, NULL, NULL, // D7 14 | 15 | // D8 - D13 16 | D8, NULL, NULL, // D8 17 | D9, NULL, NULL, // D9 18 | D10, NULL, NULL, // D10 19 | D11, NULL, NULL, // D11/MOSI 20 | D12, NULL, NULL, // D12/MISO 21 | D13, NULL, NULL, // D13/SCK/LED 22 | 23 | // A0 - A7 24 | A0, NULL, NULL, // A0 25 | A1, NULL, NULL, // A1 26 | A2, NULL, NULL, // A2 27 | A3, NULL, NULL, // A3 28 | A4, NULL, NULL, // A4/SDA 29 | A5, NULL, NULL, // A5/SCL 30 | NC, NULL, NULL, // A6 31 | NC, NULL, NULL, // A7 32 | 33 | // LEDs 34 | LED1, NULL, NULL, // LED R 35 | LED2, NULL, NULL, // LED G 36 | LED3, NULL, NULL, // LED B 37 | LED4, NULL, NULL, // LED PWR 38 | 39 | NC, NULL, NULL, // INT APDS 40 | 41 | // PDM 42 | NC, NULL, NULL, // PDM PWR 43 | NC, NULL, NULL, // PDM CLK 44 | NC, NULL, NULL, // PDM DIN 45 | 46 | // Internal I2C 47 | I2C_SDA, NULL, NULL, // SDA2 48 | I2C_SCL, NULL, NULL, // SCL2 49 | 50 | // Internal I2C 51 | NC, NULL, NULL, // I2C_PULL 52 | NC, NULL, NULL, // VDD_ENV_ENABLE 53 | }; 54 | 55 | extern "C" { 56 | unsigned int PINCOUNT_fn() { 57 | return (sizeof(g_APinDescription) / sizeof(g_APinDescription[0])); 58 | } 59 | } 60 | 61 | void initVariant() { 62 | // noop 63 | } 64 | 65 | uint8_t getUniqueSerialNumber(uint8_t* name) { 66 | // noop 67 | return 32; 68 | } 69 | 70 | void _ontouch1200bps_() { 71 | NVIC_SystemReset(); 72 | } 73 | --------------------------------------------------------------------------------