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

Merged Namespaced api #135

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extern const uint8_t digital_pin_to_port[];
extern const uint8_t digital_pin_to_bit_mask[];
extern const uint8_t digital_pin_to_bit_position[];
extern const uint8_t digital_pin_to_timer[];
extern const uint8_t analog_pin_to_channel[];

// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.
Expand Down Expand Up @@ -155,5 +156,12 @@ bool isDoubleBondedActive(uint8_t pin);

#endif

#ifdef __cplusplus
extern "C" {
#endif
#include "pins_arduino.h"
#ifdef __cplusplus
} // extern "C"
#endif

#endif
2 changes: 2 additions & 0 deletions cores/arduino/UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "api/HardwareSerial.h"
#include "pins_arduino.h"

using namespace arduino;

// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which head is the index of the location
// to which to write the next incoming character and tail is the index of the
Expand Down
24 changes: 16 additions & 8 deletions cores/arduino/WInterrupts.c → cores/arduino/WInterrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@

#include "wiring_private.h"

static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
static volatile voidFuncPtrParam intFunc[EXTERNAL_NUM_INTERRUPTS];
static void* args[EXTERNAL_NUM_INTERRUPTS];

void attachInterrupt(uint8_t pin, void (*userFunc)(void), PinStatus mode) {
void attachInterruptParam(pin_size_t pin, void (*userFunc)(void*), PinStatus mode, void* params) {

/* Get bit position and check pin validity */
uint8_t bit_pos = digitalPinToBitPosition(pin);
Expand All @@ -46,24 +47,27 @@ void attachInterrupt(uint8_t pin, void (*userFunc)(void), PinStatus mode) {
/* Check interrupt number and apply function pointer to correct array index */
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
intFunc[interruptNum] = userFunc;
args[interruptNum] = params;

// Configure the interrupt mode (trigger on low input, any change, rising
// edge, or falling edge). The mode constants were chosen to correspond
// to the configuration bits in the hardware register, so we simply apply
// the setting in the pin control register

int isc_mode;

switch (mode) {
case CHANGE:
mode = PORT_ISC_BOTHEDGES_gc;
isc_mode = PORT_ISC_BOTHEDGES_gc;
break;
case FALLING:
mode = PORT_ISC_FALLING_gc;
isc_mode = PORT_ISC_FALLING_gc;
break;
case RISING:
mode = PORT_ISC_RISING_gc;
isc_mode = PORT_ISC_RISING_gc;
break;
case LOW:
mode = PORT_ISC_LEVEL_gc;
isc_mode = PORT_ISC_LEVEL_gc;
break;
default:
// AVR doesn't support level triggered interrupts
Expand All @@ -80,10 +84,14 @@ void attachInterrupt(uint8_t pin, void (*userFunc)(void), PinStatus mode) {
*pin_ctrl_reg &= ~(PORT_ISC_gm);

/* Apply ISC setting */
*pin_ctrl_reg |= mode;
*pin_ctrl_reg |= isc_mode;
}
}

void attachInterrupt(uint8_t pin, void (*userFunc)(void), PinStatus mode) {
attachInterruptParam(pin, (voidFuncPtrParam)userFunc, mode, NULL);
}

void detachInterrupt(uint8_t pin) {
/* Get bit position and check pin validity */
uint8_t bit_pos = digitalPinToBitPosition(pin);
Expand Down Expand Up @@ -127,7 +135,7 @@ static void port_interrupt_handler(uint8_t port) {
if(intFunc[interrupt_num] != 0){

/* Call function */
intFunc[interrupt_num]();
intFunc[interrupt_num](args[interrupt_num]);
}
}
bit_pos++;
Expand Down
1 change: 0 additions & 1 deletion cores/arduino/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#include "wiring_private.h"
#include "pins_arduino.h"
#include "Arduino.h"

uint8_t analog_reference = DEFAULT;
Expand Down
6 changes: 2 additions & 4 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#define SPI_IMODE_EXTINT 1
#define SPI_IMODE_GLOBAL 2

const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();

SPIClass::SPIClass(uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, uint8_t uc_pinSS, uint8_t uc_mux)
{
initialized = false;
Expand Down Expand Up @@ -66,7 +64,7 @@ void SPIClass::init()
initialized = true;
}

void SPIClass::config(SPISettings settings)
void SPIClass::config(SPISettingsMegaAVR settings)
{
SPI0.CTRLA = settings.ctrla;
SPI0.CTRLB = settings.ctrlb;
Expand Down Expand Up @@ -173,7 +171,7 @@ void SPIClass::reattachMaskedInterrupts() {
}
}

void SPIClass::beginTransaction(SPISettings settings)
void SPIClass::beginTransaction(SPISettingsMegaAVR settings)
{
if (interruptMode != SPI_IMODE_NONE)
{
Expand Down
31 changes: 20 additions & 11 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// - endTransaction()
// - usingInterrupt()
// - SPISetting(clock, bitOrder, dataMode)
#define SPI_HAS_TRANSACTION 1
// #define SPI_HAS_TRANSACTION 1

// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
#define SPI_HAS_NOTUSINGINTERRUPT 1
Expand All @@ -52,9 +52,9 @@
#define EXTERNAL_NUM_INTERRUPTS NUM_TOTAL_PINS
#endif

class SPISettings {
class SPISettingsMegaAVR : public arduino::SPISettings {
public:
SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
SPISettingsMegaAVR(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
if (__builtin_constant_p(clock)) {
init_AlwaysInline(clock, bitOrder, dataMode);
} else {
Expand All @@ -63,7 +63,9 @@ class SPISettings {
}

// Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first.
SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); }
SPISettingsMegaAVR() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); }

SPISettingsMegaAVR(SPISettings& x) { SPISettingsMegaAVR(x.getClockFreq(), x.getBitOrder(), x.getDataMode()); }

private:
void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
Expand Down Expand Up @@ -142,12 +144,12 @@ class SPISettings {
/* member variables containing the desired SPI settings */
uint8_t ctrla;
uint8_t ctrlb;
friend class SPIClass;
friend class SPIClassMegaAVR;
};

class SPIClass {
class SPIClassMegaAVR : public arduino::HardwareSPI {
public:
SPIClass(uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, uint8_t uc_pinSS, uint8_t uc_mux);
SPIClassMegaAVR(uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, uint8_t uc_pinSS, uint8_t uc_mux);

byte transfer(uint8_t data);
uint16_t transfer16(uint16_t data);
Expand All @@ -156,7 +158,10 @@ class SPIClass {
// Transaction Functions
void usingInterrupt(int interruptNumber);
void notUsingInterrupt(int interruptNumber);
void beginTransaction(SPISettings settings);
void beginTransaction(SPISettingsMegaAVR settings);
void beginTransaction(SPISettings settings) {
beginTransaction(SPISettingsMegaAVR(settings));
}
void endTransaction(void);

void begin();
Expand All @@ -169,13 +174,16 @@ class SPIClass {
private:

void init();
void config(SPISettings settings);
void config(SPISettingsMegaAVR settings);
void config(SPISettings settings) {
config(SPISettingsMegaAVR(settings));
}

// These undocumented functions should not be used. SPI.transfer()
// polls the hardware flag which is automatically cleared as the
// AVR responds to SPI's interrupt
inline static void attachInterrupt() { SPI0.INTCTRL |= (SPI_IE_bm); }
inline static void detachInterrupt() { SPI0.INTCTRL &= ~(SPI_IE_bm); }
inline void attachInterrupt() { SPI0.INTCTRL |= (SPI_IE_bm); }
inline void detachInterrupt() { SPI0.INTCTRL &= ~(SPI_IE_bm); }

void detachMaskedInterrupts();
void reattachMaskedInterrupts();
Expand All @@ -199,6 +207,7 @@ class SPIClass {
#endif
};

#define SPIClass SPIClassMegaAVR

#if SPI_INTERFACES_COUNT > 0
extern SPIClass SPI;
Expand Down
1 change: 0 additions & 1 deletion variants/nona4809/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ const uint8_t analog_pin_to_channel[] = {

#endif

extern const uint8_t analog_pin_to_channel[];
#define digitalPinToAnalogInput(p) ((p < ANALOG_INPUT_OFFSET) ? analog_pin_to_channel[p] : analog_pin_to_channel[p - ANALOG_INPUT_OFFSET] )

// These serial port names are intended to allow libraries and architecture-neutral
Expand Down
Loading