Skip to content

Commit 64433f3

Browse files
committed
Restructure core bindings
More clever port of arduino/ArduinoCore-nRF528x-mbedos#31
1 parent d7540a6 commit 64433f3

File tree

9 files changed

+205
-183
lines changed

9 files changed

+205
-183
lines changed

cores/arduino/Arduino.h

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "mbed_config.h"
3333
#include "mbed/drivers/InterruptIn.h"
3434
#include "mbed/drivers/PwmOut.h"
35+
#include "mbed/drivers/AnalogIn.h"
3536
#undef PinMode
3637
#undef F
3738
#endif //__cplusplus
@@ -89,8 +90,12 @@ typedef struct _PinDescription
8990
PinName name;
9091
mbed::InterruptIn* irq;
9192
mbed::PwmOut* pwm;
93+
mbed::AnalogIn* analog;
94+
gpio_t* gpio;
9295
} PinDescription ;
9396

97+
int PinNameToIndex(PinName P);
98+
9499
// Pins table to be instantiated into variant.cpp
95100
extern PinDescription g_APinDescription[];
96101

cores/arduino/Interrupts.cpp

+16-29
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,49 @@
1818

1919
#include "Arduino.h"
2020

21-
#ifdef digitalPinToInterruptObj
22-
static mbed::InterruptIn* PinNameToInterruptObj(PinName P) {
23-
// reverse search for pinName in g_APinDescription[P].name fields
24-
for (pin_size_t i=0; i < PINS_COUNT; i++) {
25-
if (g_APinDescription[i].name == P) {
26-
return g_APinDescription[i].irq;
27-
}
28-
}
29-
return NULL;
30-
}
31-
#endif
32-
3321
void detachInterrupt(PinName interruptNum) {
34-
#ifdef digitalPinToInterruptObj
35-
if (PinNameToInterruptObj(interruptNum) != NULL) {
36-
delete PinNameToInterruptObj(interruptNum);
22+
pin_size_t idx = PinNameToIndex(interruptNum);
23+
if (idx != NOT_A_PIN) {
24+
detachInterrupt(idx);
3725
}
38-
#endif
3926
}
4027

4128
void detachInterrupt(pin_size_t interruptNum) {
42-
#ifdef digitalPinToInterruptObj
43-
if (digitalPinToInterruptObj(interruptNum) != NULL) {
29+
if ((interruptNum < PINS_COUNT) && (digitalPinToInterruptObj(interruptNum) != NULL)) {
4430
delete digitalPinToInterruptObj(interruptNum);
4531
}
46-
#endif
4732
}
4833

4934
void attachInterruptParam(PinName interruptNum, voidFuncPtrParam func, PinStatus mode, void* param) {
50-
detachInterrupt(interruptNum);
51-
mbed::InterruptIn* irq = new mbed::InterruptIn(interruptNum);
52-
if (mode == FALLING) {
53-
irq->fall(mbed::callback(func, param));
35+
pin_size_t idx = PinNameToIndex(interruptNum);
36+
if (idx != NOT_A_PIN) {
37+
attachInterruptParam(PinNameToIndex(interruptNum), func, mode, param);
5438
} else {
55-
irq->rise(mbed::callback(func, param));
39+
mbed::InterruptIn* irq = new mbed::InterruptIn(interruptNum);
40+
if (mode == FALLING) {
41+
irq->fall(mbed::callback(func, param));
42+
} else {
43+
irq->rise(mbed::callback(func, param));
44+
}
5645
}
57-
#ifdef digitalPinToInterruptObj
58-
digitalPinToInterruptObj(interruptNum) = irq;
59-
#endif
6046
}
6147

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

6652
void attachInterruptParam(pin_size_t interruptNum, voidFuncPtrParam func, PinStatus mode, void* param) {
53+
if (interruptNum >= PINS_COUNT) {
54+
return;
55+
}
6756
detachInterrupt(interruptNum);
6857
mbed::InterruptIn* irq = new mbed::InterruptIn(digitalPinToPinName(interruptNum));
6958
if (mode == FALLING) {
7059
irq->fall(mbed::callback(func, param));
7160
} else {
7261
irq->rise(mbed::callback(func, param));
7362
}
74-
#ifdef digitalPinToInterruptObj
7563
digitalPinToInterruptObj(interruptNum) = irq;
76-
#endif
7764
}
7865

7966
void attachInterrupt(pin_size_t interruptNum, voidFuncPtr func, PinStatus mode) {

cores/arduino/macros.h

+20-16
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,29 @@
2323
#pragma once
2424
#ifdef USE_ARDUINO_PINOUT
2525

26-
#define analogPinToPinName(P) (P < A0 ? g_APinDescription[P+A0].name : g_APinDescription[P].name)
27-
#define digitalPinToPinName(P) (g_APinDescription[P].name)
28-
#define digitalPinToInterrupt(P) (P)
29-
#define digitalPinToInterruptObj(P) (g_APinDescription[P].irq)
30-
#define digitalPinToPwmObj(P) (g_APinDescription[P].pwm)
26+
#define analogPinToPinName(P) (P >= PINS_COUNT ? NC : P < A0 ? g_APinDescription[P+A0].name : g_APinDescription[P].name)
27+
#define digitalPinToPinName(P) (P >= PINS_COUNT ? NC : g_APinDescription[P].name)
28+
#define digitalPinToInterruptObj(P) (g_APinDescription[P].irq)
29+
#define digitalPinToPwm(P) (g_APinDescription[P].pwm)
30+
#define digitalPinToGpio(P) (g_APinDescription[P].gpio)
31+
#define analogPinToAnalogObj(P) (P < A0 ? g_APinDescription[P+A0].analog : g_APinDescription[P].analog)
32+
33+
// this is needed for backwards compatibility
34+
#define digitalPinToInterrupt(P) (P)
3135

3236
#else
3337

34-
#define analogPinToPinName(P) ((PinName)P)
35-
#define digitalPinToPinName(P) ((PinName)P)
36-
#define digitalPinToInterrupt(P) ((PinName)P)
38+
#define analogPinToPinName(P) ((PinName)P)
39+
#define digitalPinToPinName(P) ((PinName)P)
40+
#define digitalPinToInterrupt(P) ((PinName)P)
3741

3842
#endif
3943

40-
#define REDIRECT_STDOUT_TO(stream) namespace mbed { \
41-
FileHandle *mbed_override_console(int fd) { \
42-
return &stream; \
43-
} \
44-
FileHandle *mbed_target_override_console(int fd) { \
45-
return &stream; \
46-
} \
47-
}
44+
#define REDIRECT_STDOUT_TO(stream) namespace mbed { \
45+
FileHandle *mbed_override_console(int fd) { \
46+
return &stream; \
47+
} \
48+
FileHandle *mbed_target_override_console(int fd) { \
49+
return &stream; \
50+
} \
51+
}

cores/arduino/pinToIndex.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Arduino.h"
2+
3+
int PinNameToIndex(PinName P) {
4+
for (pin_size_t i=0; i < PINS_COUNT; i++) {
5+
if (g_APinDescription[i].name == P) {
6+
return i;
7+
}
8+
}
9+
return NOT_A_PIN;
10+
}

cores/arduino/wiring_analog.cpp

+26-37
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,32 @@
2727
static int write_resolution = 8;
2828
static int read_resolution = 10;
2929

30-
#ifdef digitalPinToPwmObj
31-
static mbed::PwmOut* PinNameToPwmObj(PinName P) {
32-
// reverse search for pinName in g_APinDescription[P].name fields
33-
for (pin_size_t i=0; i < PINS_COUNT; i++) {
34-
if (g_APinDescription[i].name == P) {
35-
return g_APinDescription[i].pwm;
36-
}
37-
}
38-
return NULL;
39-
}
40-
#endif
41-
4230
void analogWrite(PinName pin, int val)
4331
{
44-
float percent = (float)val/(float)(1 << write_resolution);
45-
#ifdef digitalPinToPwmObj
46-
mbed::PwmOut* pwm = PinNameToPwmObj(pin);
47-
if (pwm == NULL) {
48-
pwm = new mbed::PwmOut(pin);
49-
digitalPinToPwmObj(pin) = pwm;
32+
pin_size_t idx = PinNameToIndex(pin);
33+
if (idx != NOT_A_PIN) {
34+
analogWrite(idx, val);
35+
} else {
36+
mbed::PwmOut* pwm = new mbed::PwmOut(pin);
5037
pwm->period_ms(2); //500Hz
38+
float percent = (float)val/(float)(1 << write_resolution);
39+
pwm->write(percent);
5140
}
52-
#else
53-
// attention: this leaks badly
54-
mbed::PwmOut* pwm = new mbed::PwmOut(digitalPinToPinName(pin));
55-
#endif
56-
pwm->write(percent);
5741
}
5842

5943
void analogWrite(pin_size_t pin, int val)
6044
{
45+
if (pin >= PINS_COUNT) {
46+
return;
47+
}
6148
float percent = (float)val/(float)(1 << write_resolution);
62-
#ifdef digitalPinToPwmObj
63-
mbed::PwmOut* pwm = digitalPinToPwmObj(pin);
49+
mbed::PwmOut* pwm = digitalPinToPwm(pin);
6450
if (pwm == NULL) {
6551
pwm = new mbed::PwmOut(digitalPinToPinName(pin));
66-
digitalPinToPwmObj(pin) = pwm;
52+
digitalPinToPwm(pin) = pwm;
6753
pwm->period_ms(2); //500Hz
6854
}
6955
pwm->write(percent);
70-
#endif
7156
}
7257

7358
void analogWriteResolution(int bits)
@@ -77,20 +62,24 @@ void analogWriteResolution(int bits)
7762

7863
int analogRead(PinName pin)
7964
{
80-
int multiply_factor = 1;
81-
#ifdef ANALOG_BUG_MBED
82-
multiply_factor = 4;
83-
#endif
84-
return (mbed::AnalogIn(pin).read_u16() >> (16 - read_resolution)) * multiply_factor;
65+
return (mbed::AnalogIn(pin).read_u16() >> (16 - read_resolution));
8566
}
8667

8768
int analogRead(pin_size_t pin)
8869
{
89-
int multiply_factor = 1;
90-
#ifdef ANALOG_BUG_MBED
91-
multiply_factor = 4;
92-
#endif
93-
return (mbed::AnalogIn(analogPinToPinName(pin)).read_u16() >> (16 - read_resolution)) * multiply_factor;
70+
if (pin >= PINS_COUNT) {
71+
return -1;
72+
}
73+
PinName name = analogPinToPinName(pin);
74+
if (name == NC) {
75+
return -1;
76+
}
77+
mbed::AnalogIn* obj = analogPinToAnalogObj(pin);
78+
if (obj == NULL) {
79+
obj = new mbed::AnalogIn(name);
80+
analogPinToAnalogObj(pin) = obj;
81+
}
82+
return (obj->read_u16() >> (16 - read_resolution));
9483
}
9584

9685
void analogReadResolution(int bits)

cores/arduino/wiring_digital.cpp

+48-21
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,83 @@
2626

2727
void pinMode(PinName pin, PinMode mode)
2828
{
29-
switch (mode) {
30-
case INPUT:
31-
mbed::DigitalIn(pin, PullNone);
32-
break;
33-
case OUTPUT:
34-
mbed::DigitalOut(pin, 0);
35-
break;
36-
case INPUT_PULLUP:
37-
mbed::DigitalIn(pin, PullUp);
38-
break;
39-
case INPUT_PULLDOWN:
40-
mbed::DigitalIn(pin, PullDown);
41-
break;
29+
pin_size_t idx = PinNameToIndex(pin);
30+
if (idx != NOT_A_PIN) {
31+
pinMode(idx, mode);
32+
} else {
33+
switch (mode) {
34+
case INPUT:
35+
mbed::DigitalIn(pin, PullNone);
36+
break;
37+
case OUTPUT:
38+
mbed::DigitalOut(pin, 0);
39+
break;
40+
case INPUT_PULLUP:
41+
mbed::DigitalIn(pin, PullUp);
42+
break;
43+
case INPUT_PULLDOWN:
44+
mbed::DigitalIn(pin, PullDown);
45+
break;
46+
}
4247
}
4348
}
4449

4550
void pinMode(pin_size_t pin, PinMode mode)
4651
{
52+
gpio_t* gpio = digitalPinToGpio(pin);
53+
if (gpio == NULL) {
54+
gpio = new gpio_t();
55+
digitalPinToGpio(pin) = gpio;
56+
}
57+
4758
switch (mode) {
4859
case INPUT:
49-
mbed::DigitalIn(digitalPinToPinName(pin), PullNone);
60+
gpio_init_in_ex(gpio, digitalPinToPinName(pin), PullNone);
5061
break;
5162
case OUTPUT:
52-
mbed::DigitalOut(digitalPinToPinName(pin));
63+
gpio_init_out(gpio, digitalPinToPinName(pin));
5364
break;
5465
case INPUT_PULLUP:
55-
mbed::DigitalIn(digitalPinToPinName(pin), PullUp);
66+
gpio_init_in_ex(gpio, digitalPinToPinName(pin), PullUp);
5667
break;
5768
case INPUT_PULLDOWN:
58-
mbed::DigitalIn(digitalPinToPinName(pin), PullDown);
69+
gpio_init_in_ex(gpio, digitalPinToPinName(pin), PullDown);
5970
break;
6071
}
6172
}
6273

6374
void digitalWrite(PinName pin, PinStatus val)
6475
{
65-
mbed::DigitalOut(pin).write((int)val);
76+
pin_size_t idx = PinNameToIndex(pin);
77+
if (idx != NOT_A_PIN) {
78+
digitalWrite(idx, val);
79+
} else {
80+
mbed::DigitalOut(pin).write((int)val);
81+
}
6682
}
6783

6884
void digitalWrite(pin_size_t pin, PinStatus val)
6985
{
70-
mbed::DigitalOut(digitalPinToPinName(pin)).write((int)val);
86+
if (pin >= PINS_COUNT) {
87+
return;
88+
}
89+
gpio_write(digitalPinToGpio(pin), (int)val);
7190
}
7291

7392
PinStatus digitalRead(PinName pin)
7493
{
75-
return (PinStatus)mbed::DigitalIn(pin).read();
94+
pin_size_t idx = PinNameToIndex(pin);
95+
if (idx != NOT_A_PIN) {
96+
return digitalRead(idx);
97+
} else {
98+
return (PinStatus)mbed::DigitalIn(pin).read();
99+
}
76100
}
77101

78102
PinStatus digitalRead(pin_size_t pin)
79103
{
80-
return (PinStatus)mbed::DigitalIn(digitalPinToPinName(pin)).read();
104+
if (pin >= PINS_COUNT) {
105+
return LOW;
106+
}
107+
return (PinStatus)gpio_read(digitalPinToGpio(pin));
81108
}

0 commit comments

Comments
 (0)