Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Backport patches from arduino/ArduinoCore-mbed #81

Merged
merged 20 commits into from
Jul 14, 2020
Merged
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
2 changes: 2 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ nano33ble.build.variant=ARDUINO_NANO33BLE
nano33ble.build.mcu=cortex-m4
nano33ble.build.extra_flags=
nano33ble.build.architecture=cortex-m4
nano33ble.build.fpu=fpv4-sp-d16
nano33ble.build.float-abi=softfp
nano33ble.build.board=ARDUINO_NANO33BLE
nano33ble.build.ldscript=linker_script.ld
nano33ble.compiler.mbed.arch.define=-DARDUINO_ARCH_NRF52840
Expand Down
45 changes: 39 additions & 6 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef Arduino_h
#if !defined(Arduino_h) && !defined(ARDUINO_LIB_DISCOVERY_PHASE)
#define Arduino_h

#if defined(__cplusplus)
#if !defined(ARDUINO_AS_MBED_LIBRARY)
#define PinMode MbedPinMode

#include "pinmode_arduino.h"

#ifdef F
#define Arduino_F F
#undef F
#endif // F (mbed included after arduino.h)
#define F Mbed_F
#endif // !ARDUINO_AS_MBED_LIBRARY
#include "mbed.h"
#undef PinMode
#include "mbed_config.h"
#include "mbed/drivers/InterruptIn.h"
#include "mbed/drivers/PwmOut.h"
#include "mbed/drivers/AnalogIn.h"
#include "mbed/drivers/DigitalInOut.h"
#undef F
#endif //__cplusplus

Expand Down Expand Up @@ -80,16 +85,42 @@ void analogWriteResolution(int bits);

#include "pins_arduino.h"

/* Types used for the table below */
#ifdef __cplusplus
// Types used for the table below
typedef struct _PinDescription
{
PinName name;
mbed::InterruptIn* irq;
mbed::PwmOut* pwm;
mbed::DigitalInOut* gpio;
} PinDescription ;

/* Pins table to be instantiated into variant.cpp */
typedef struct _AnalogPinDescription
{
PinName name;
mbed::AnalogIn* adc;
} AnalogPinDescription ;

int PinNameToIndex(PinName P);

// Pins table to be instantiated into variant.cpp
extern PinDescription g_APinDescription[];
extern AnalogPinDescription g_AAnalogPinDescription[];

#ifdef ANALOG_CONFIG

typedef enum _AnalogReferenceMode AnalogReferenceMode;
void analogReference(uint8_t mode);
/* nRF specific function to change analog acquisition time */
typedef enum _AnalogAcquisitionTime AnalogAcquisitionTime;
void analogAcquisitionTime(uint8_t time);

/* Function to reconfigure already active ADC channels */
void analogUpdate();
extern bool isAdcConfigChanged;
extern analogin_config_t adcCurrentConfig;

#endif

#include "Serial.h"
#if defined(SERIAL_CDC)
Expand All @@ -105,6 +136,8 @@ extern PinDescription g_APinDescription[];
#endif

#include "overloads.h"
#endif

#include "macros.h"

#endif
63 changes: 30 additions & 33 deletions cores/arduino/Interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,44 @@

#include "Arduino.h"

#ifdef digitalPinToInterruptObj
static mbed::InterruptIn* PinNameToInterruptObj(PinName P) {
// reverse search for pinName in g_APinDescription[P].name fields
for (pin_size_t i=0; i < PINS_COUNT; i++) {
if (g_APinDescription[i].name == P) {
return g_APinDescription[i].irq;
}
}
return NULL;
}
#endif

void detachInterrupt(PinName interruptNum) {
#ifdef digitalPinToInterruptObj
if (PinNameToInterruptObj(interruptNum) != NULL) {
delete PinNameToInterruptObj(interruptNum);
pin_size_t idx = PinNameToIndex(interruptNum);
if (idx != NOT_A_PIN) {
detachInterrupt(idx);
}
#endif
}

void detachInterrupt(pin_size_t interruptNum) {
#ifdef digitalPinToInterruptObj
if (digitalPinToInterruptObj(interruptNum) != NULL) {
if ((interruptNum < PINS_COUNT) && (digitalPinToInterruptObj(interruptNum) != NULL)) {
delete digitalPinToInterruptObj(interruptNum);
}
#endif
}

void attachInterruptParam(PinName interruptNum, voidFuncPtrParam func, PinStatus mode, void* param) {
detachInterrupt(interruptNum);
mbed::InterruptIn* irq = new mbed::InterruptIn(interruptNum);
if (mode == CHANGE) {
irq->rise(mbed::callback(func, param));
irq->fall(mbed::callback(func, param));
} else if (mode == FALLING) {
irq->fall(mbed::callback(func, param));
pin_size_t idx = PinNameToIndex(interruptNum);
if (idx != NOT_A_PIN) {
attachInterruptParam(PinNameToIndex(interruptNum), func, mode, param);
} else {
irq->rise(mbed::callback(func, param));
mbed::InterruptIn* irq = new mbed::InterruptIn(interruptNum);
if (mode == CHANGE) {
irq->rise(mbed::callback(func, param));
irq->fall(mbed::callback(func, param));
} else if (mode == FALLING) {
irq->fall(mbed::callback(func, param));
} else {
irq->rise(mbed::callback(func, param));
}
}
#ifdef digitalPinToInterruptObj
digitalPinToInterruptObj(interruptNum) = irq;
#endif
}

void attachInterrupt(PinName interruptNum, voidFuncPtr func, PinStatus mode) {
attachInterruptParam(interruptNum, (voidFuncPtrParam)func, mode, NULL);
}

void attachInterruptParam(pin_size_t interruptNum, voidFuncPtrParam func, PinStatus mode, void* param) {
if (interruptNum >= PINS_COUNT) {
return;
}
detachInterrupt(interruptNum);
mbed::InterruptIn* irq = new mbed::InterruptIn(digitalPinToPinName(interruptNum));
if (mode == CHANGE) {
Expand All @@ -77,11 +66,19 @@ void attachInterruptParam(pin_size_t interruptNum, voidFuncPtrParam func, PinSta
} else {
irq->rise(mbed::callback(func, param));
}
#ifdef digitalPinToInterruptObj
digitalPinToInterruptObj(interruptNum) = irq;
#endif
// Give a default pullup for the pin, since calling InterruptIn with PinMode is impossible
if (digitalPinToGpio(interruptNum) == NULL) {
if (mode == FALLING) {
pinMode(interruptNum, INPUT_PULLUP);
} else if (mode == RISING) {
pinMode(interruptNum, INPUT_PULLDOWN);
} else {
pinMode(interruptNum, INPUT);
}
}
}

void attachInterrupt(pin_size_t interruptNum, voidFuncPtr func, PinStatus mode) {
attachInterruptParam(interruptNum, (voidFuncPtrParam)func, mode, NULL);
}
}
1 change: 1 addition & 0 deletions cores/arduino/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "api/RingBuffer.h"
#include "Arduino.h"
#include "drivers/RawSerial.h"

#ifdef __cplusplus

Expand Down
1 change: 1 addition & 0 deletions cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Arduino.h"
#include "mbed.h"

class Tone {
mbed::DigitalOut *pin;
Expand Down
5 changes: 5 additions & 0 deletions cores/arduino/USB/PluggableUSBDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/

#include "Arduino.h"

#if defined(DEVICE_USBDEVICE) && defined(SERIAL_CDC)

#include "stdint.h"
#include "PluggableUSBDevice.h"
#include "EndpointResolver.h"
Expand Down Expand Up @@ -289,3 +292,5 @@ arduino::PluggableUSBDevice& PluggableUSBD()
static arduino::PluggableUSBDevice obj(BOARD_VENDORID, BOARD_PRODUCTID);
return obj;
}

#endif
1 change: 1 addition & 0 deletions cores/arduino/USB/PluggableUSBSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Arduino.h"
#include "USBCDC.h"
#include "platform/Stream.h"
#include "mbed/rtos/rtos.h"
#include "Callback.h"

/**
Expand Down
8 changes: 8 additions & 0 deletions cores/arduino/USB/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
* limitations under the License.
*/


#include "Arduino.h"
#include "PeripheralPins.h"

#if DEVICE_USBDEVICE && defined(SERIAL_CDC)

#include "stdint.h"
#include "USBCDC.h"
#include "EndpointResolver.h"
Expand Down Expand Up @@ -611,3 +617,5 @@ const uint8_t *USBCDC::configuration_desc(uint8_t index)
return NULL;
}
}

#endif
9 changes: 8 additions & 1 deletion cores/arduino/USB/USBSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
* limitations under the License.
*/

#include "Arduino.h"

#if DEVICE_USBDEVICE && defined(SERIAL_CDC)

#include "stdint.h"
#include "PluggableUSBSerial.h"
#include "usb_phy_api.h"
#include "mbed.h"

using namespace arduino;

Expand Down Expand Up @@ -113,4 +118,6 @@ bool USBSerial::connected()
return _terminal_connected;
}

USBSerial SerialUSB(false);
USBSerial SerialUSB(false);

#endif
36 changes: 20 additions & 16 deletions cores/arduino/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,29 @@
#pragma once
#ifdef USE_ARDUINO_PINOUT

#define analogPinToPinName(P) (P < A0 ? g_APinDescription[P+A0].name : g_APinDescription[P].name)
#define digitalPinToPinName(P) (g_APinDescription[P].name)
#define digitalPinToInterrupt(P) (P)
#define digitalPinToInterruptObj(P) (g_APinDescription[P].irq)
#define digitalPinToPwmObj(P) (g_APinDescription[P].pwm)
#define analogPinToPinName(P) (P >= PINS_COUNT ? NC : P < A0 ? g_APinDescription[P+A0].name : g_APinDescription[P].name)
#define analogPinToAdcObj(P) (P < A0 ? g_AAnalogPinDescription[P].adc : g_AAnalogPinDescription[P-A0].adc)
#define digitalPinToPinName(P) (P >= PINS_COUNT ? NC : g_APinDescription[P].name)
#define digitalPinToInterruptObj(P) (g_APinDescription[P].irq)
#define digitalPinToPwm(P) (g_APinDescription[P].pwm)
#define digitalPinToGpio(P) (g_APinDescription[P].gpio)

// this is needed for backwards compatibility
#define digitalPinToInterrupt(P) (P)

#else

#define analogPinToPinName(P) ((PinName)P)
#define digitalPinToPinName(P) ((PinName)P)
#define digitalPinToInterrupt(P) ((PinName)P)
#define analogPinToPinName(P) ((PinName)P)
#define digitalPinToPinName(P) ((PinName)P)
#define digitalPinToInterrupt(P) ((PinName)P)

#endif

#define REDIRECT_STDOUT_TO(stream) namespace mbed { \
FileHandle *mbed_override_console(int fd) { \
return &stream; \
} \
FileHandle *mbed_target_override_console(int fd) { \
return &stream; \
} \
}
#define REDIRECT_STDOUT_TO(stream) namespace mbed { \
FileHandle *mbed_override_console(int fd) { \
return &stream; \
} \
FileHandle *mbed_target_override_console(int fd) { \
return &stream; \
} \
}
18 changes: 18 additions & 0 deletions cores/arduino/mbed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _MBED_WRAP_H_
#define _MBED_WRAP_H_

#include "Arduino.h"

#if defined(__cplusplus)
#if !defined(ARDUINO_AS_MBED_LIBRARY)
#ifdef F
#define Arduino_F F
#undef F
#endif // F (mbed included after arduino.h)
#define F Mbed_F
#endif // !ARDUINO_AS_MBED_LIBRARY
#include "mbed/mbed.h"
#undef F
#endif //__cplusplus

#endif //_MBED_WRAP_H_
Loading