Skip to content

Commit b8e9e34

Browse files
authored
Namespace update (#25)
* Improve delay_ns accuracy * namesapce update
1 parent 414863f commit b8e9e34

23 files changed

+478
-464
lines changed

cores/arduino/Arduino.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <LPC17xx.h>
3131

3232
#include <binary.h>
33+
34+
#include <time.h>
3335
#include <const_functions.h>
3436
#include <pinmapping.h>
3537
#include <adc.h>
@@ -56,9 +58,9 @@ typedef uint8_t byte;
5658
#define PSTR(v) (v)
5759
#define PGM_P const char *
5860

59-
using util::min;
60-
using util::max;
61-
using util::abs;
61+
using LPC176x::util::min;
62+
using LPC176x::util::max;
63+
using LPC176x::util::abs;
6264
using std::isnan;
6365
using std::isinf;
6466

@@ -114,26 +116,36 @@ void analogReference(uint8_t);
114116
void analogReadResolution(uint8_t resolution);
115117
uint8_t analogReadResolution();
116118

119+
constexpr uint16_t NUM_DIGITAL_PINS = 160;
120+
constexpr uint16_t NUM_ANALOG_INPUTS = LPC176x::analog_input_count;
121+
122+
// Get the digital pin for an analog index
123+
constexpr pin_t analogInputToDigitalPin(const int8_t channel) {
124+
return LPC176x::pin_type::index_from_adc_channnel(channel);
125+
}
126+
127+
constexpr pin_t digitalPinToInterrupt(const pin_t pin) { return pin; }
128+
117129

118130
[[gnu::always_inline, gnu::optimize("O3")]] inline void digitalWrite(const pin_t pin, const uint8_t pin_status) {
119-
if (!pin_is_valid(pin)) return;
131+
if (!LPC176x::pin_is_valid(pin)) return;
120132

121-
gpio_set(pin, pin_status);
133+
LPC176x::gpio_set(pin, pin_status);
122134
// Set pin mode on every write (Arduino version does this)
123-
pin_enable_function(pin, LPC176x::Function::GPIO);
124-
gpio_set_output(pin);
135+
LPC176x::pin_enable_function(pin, LPC176x::Function::GPIO);
136+
LPC176x::gpio_set_output(pin);
125137
}
126138
[[gnu::always_inline, gnu::optimize("O3")]] inline bool digitalRead(const pin_t pin) {
127-
if (!pin_is_valid(pin)) return false;
128-
return gpio_get(pin);
139+
if (!LPC176x::pin_is_valid(pin)) return false;
140+
return LPC176x::gpio_get(pin);
129141
}
130142
[[gnu::always_inline, gnu::optimize("O3")]] inline uint16_t analogRead(pin_t pin) {
131-
if (!pin_is_valid(pin) || !pin_has_adc(pin)) return 0;
143+
if (!LPC176x::pin_is_valid(pin) || !LPC176x::pin_has_adc(pin)) return 0;
132144

133-
if (!pin_adc_enabled(pin)) {
134-
pin_enable_adc(pin);
135-
LPC176x::adc_hardware.select(pin_get_adc_channel(pin));
136-
while(!LPC176x::adc_hardware.done(pin_get_adc_channel(pin)));
145+
if (!LPC176x::pin_adc_enabled(pin)) {
146+
LPC176x::pin_enable_adc(pin);
147+
LPC176x::adc_hardware.select(LPC176x::pin_get_adc_channel(pin));
148+
while(!LPC176x::adc_hardware.done(LPC176x::pin_get_adc_channel(pin)));
137149
}
138150

139151
int8_t bs = 16 - analogReadResolution();
@@ -153,7 +165,7 @@ void randomSeed(uint32_t);
153165

154166
char *dtostrf (double __val, signed char __width, unsigned char __prec, char *__s);
155167

156-
using util::map;
168+
using LPC176x::util::map;
157169

158170
void tone(const pin_t _pin, const uint32_t frequency, const uint32_t duration = 0);
159171
void noTone(const pin_t _pin);

cores/arduino/CDCSerial.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class CDCSerial: public Stream {
146146
size_t readBytes(char* dst, size_t length) {
147147
size_t buffered = receive_buffer.read((uint8_t*)dst, length);
148148
const uint32_t usb_rx_timeout = millis() + USBCDCTIMEOUT;
149-
while (buffered != length && util::pending(millis(), usb_rx_timeout)) {
149+
while (buffered != length && LPC176x::util::pending(millis(), usb_rx_timeout)) {
150150
if (!host_connected) return 0;
151151
CDC_FillBuffer(receive_buffer.free());
152152
buffered += receive_buffer.read((uint8_t*)dst + buffered, length - buffered);
@@ -158,7 +158,7 @@ class CDCSerial: public Stream {
158158
size_t write(char* src, size_t length) {
159159
size_t buffered = transmit_buffer.write((uint8_t*)src, length);
160160
const uint32_t usb_tx_timeout = millis() + USBCDCTIMEOUT;
161-
while (buffered != length && util::pending(millis(), usb_tx_timeout)) {
161+
while (buffered != length && LPC176x::util::pending(millis(), usb_tx_timeout)) {
162162
if (!host_connected) return 0;
163163
buffered += transmit_buffer.write((uint8_t*)src + buffered, length - buffered);
164164
CDC_FlushBuffer();
@@ -170,7 +170,7 @@ class CDCSerial: public Stream {
170170
size_t write(const uint8_t c) {
171171
if (!host_connected) return 0; // Do not fill buffer when host disconnected
172172
const uint32_t usb_tx_timeout = millis() + USBCDCTIMEOUT;
173-
while (transmit_buffer.write(c) == 0 && util::pending(millis(), usb_tx_timeout)) { // Block until there is free room in buffer
173+
while (transmit_buffer.write(c) == 0 && LPC176x::util::pending(millis(), usb_tx_timeout)) { // Block until there is free room in buffer
174174
if (!host_connected) return 0; // Break infinite loop on host disconect
175175
CDC_FlushBuffer();
176176
}
@@ -194,7 +194,7 @@ class CDCSerial: public Stream {
194194

195195
void flushTX(void) {
196196
const uint32_t usb_tx_timeout = millis() + USBCDCTIMEOUT;
197-
while (transmit_buffer.available() && host_connected && util::pending(millis(), usb_tx_timeout)) {
197+
while (transmit_buffer.available() && host_connected && LPC176x::util::pending(millis(), usb_tx_timeout)) {
198198
CDC_FlushBuffer();
199199
}
200200
}
@@ -208,7 +208,7 @@ class CDCSerial: public Stream {
208208
size_t i = 0;
209209
if (length > 0 && length < 256) {
210210
uint32_t usb_tx_timeout = millis() + USBCDCTIMEOUT;
211-
while (i < (size_t)length && host_connected && util::pending(millis(), usb_tx_timeout)) {
211+
while (i < (size_t)length && host_connected && LPC176x::util::pending(millis(), usb_tx_timeout)) {
212212
i += transmit_buffer.write(buffer[i]);
213213
CDC_FlushBuffer();
214214
}

cores/arduino/SoftwareSPI.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,38 @@
4949
uint8_t swSpiTransfer(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin) {
5050
for (uint8_t i = 0; i < 8; i++) {
5151
if (spi_speed == 0) {
52-
gpio_set(mosi_pin, !!(b & 0x80));
53-
gpio_set(sck_pin, HIGH);
52+
LPC176x::gpio_set(mosi_pin, !!(b & 0x80));
53+
LPC176x::gpio_set(sck_pin, HIGH);
5454
b <<= 1;
55-
if (miso_pin >= 0 && gpio_get(miso_pin)) b |= 1;
56-
gpio_set(sck_pin, LOW);
55+
if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;
56+
LPC176x::gpio_set(sck_pin, LOW);
5757
}
5858
else {
5959
const uint8_t state = (b & 0x80) ? HIGH : LOW;
6060
for (uint8_t j = 0; j < spi_speed; j++)
61-
gpio_set(mosi_pin, state);
61+
LPC176x::gpio_set(mosi_pin, state);
6262

6363
for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
64-
gpio_set(sck_pin, HIGH);
64+
LPC176x::gpio_set(sck_pin, HIGH);
6565

6666
b <<= 1;
67-
if (miso_pin >= 0 && gpio_get(miso_pin)) b |= 1;
67+
if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;
6868

6969
for (uint8_t j = 0; j < spi_speed; j++)
70-
gpio_set(sck_pin, LOW);
70+
LPC176x::gpio_set(sck_pin, LOW);
7171
}
7272
}
7373
return b;
7474
}
7575

7676
void swSpiBegin(const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin) {
7777
pinMode(sck_pin, OUTPUT);
78-
if (pin_is_valid(miso_pin)) pinMode(miso_pin, INPUT);
78+
if (LPC176x::pin_is_valid(miso_pin)) pinMode(miso_pin, INPUT);
7979
pinMode(mosi_pin, OUTPUT);
8080
}
8181

8282
uint8_t swSpiInit(const uint8_t spiRate, const pin_t sck_pin, const pin_t mosi_pin) {
83-
gpio_set(mosi_pin, HIGH);
84-
gpio_set(sck_pin, LOW);
83+
LPC176x::gpio_set(mosi_pin, HIGH);
84+
LPC176x::gpio_set(sck_pin, LOW);
8585
return (SystemCoreClock == 120000000 ? 44 : 38) / std::pow(2, 6 - std::min(spiRate, (uint8_t)6));
8686
}

cores/arduino/SoftwareSerial.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ inline void SoftwareSerial::setTX() {
122122
// output hihg. Now, it is input with pullup for a short while, which
123123
// is fine. With inverse logic, either order is fine.
124124

125-
gpio_set(_transmitPin, _inverse_logic ? LOW : HIGH);
125+
LPC176x::gpio_set(_transmitPin, _inverse_logic ? LOW : HIGH);
126126
pinMode(_transmitPin, OUTPUT);
127127
}
128128

@@ -157,7 +157,7 @@ inline void SoftwareSerial::setRXTX(bool input) {
157157
if (--tx_tick_cnt <= 0) {
158158
if (tx_bit_cnt++ < 10 ) {
159159
// send data (including start and stop bits)
160-
gpio_set(_transmitPin, tx_buffer & 1);
160+
LPC176x::gpio_set(_transmitPin, tx_buffer & 1);
161161
tx_buffer >>= 1;
162162
tx_tick_cnt = OVERSAMPLE;
163163
}
@@ -185,7 +185,7 @@ inline void SoftwareSerial::setRXTX(bool input) {
185185
//
186186
[[gnu::always_inline, gnu::optimize("O3")]] inline void SoftwareSerial::recv() {
187187
if (--rx_tick_cnt <= 0) {
188-
uint8_t inbit = gpio_get(_receivePin);
188+
uint8_t inbit = LPC176x::gpio_get(_receivePin);
189189
if (rx_bit_cnt == -1) {
190190
// waiting for start bit
191191
if (!inbit) {

cores/arduino/Tone.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ void tone(const pin_t _pin, const uint32_t frequency, const uint32_t duration =
1414
toggles = duration ? 2 * frequency * duration / 1000 : -1;
1515

1616
if (!initialised_tone_timer) {
17-
util::bit_set(LPC_SC->PCONP, 22); // Power ON Timer 2
18-
LPC_TIM2->TCR = util::bit_value(1); // Put Timer in reset state
17+
LPC176x::util::bit_set(LPC_SC->PCONP, 22); // Power ON Timer 2
18+
LPC_TIM2->TCR = LPC176x::util::bit_value(1); // Put Timer in reset state
1919
LPC_TIM2->PR = ((SystemCoreClock / 4) / 1000000) - 1; // Use prescaler to set frequency
20-
LPC_TIM2->MCR = util::bitset_value(0, 1); // Match on MR0, reset on MR0, interrupts when NVIC enables them
20+
LPC_TIM2->MCR = LPC176x::util::bitset_value(0, 1); // Match on MR0, reset on MR0, interrupts when NVIC enables them
2121
NVIC_SetPriority(TIMER2_IRQn, NVIC_EncodePriority(0, 2, 0));
2222
initialised_tone_timer = true;
2323
}
2424

25-
LPC_TIM2->TCR = util::bit_value(1); // Put Timer in reset state
25+
LPC_TIM2->TCR = LPC176x::util::bit_value(1); // Put Timer in reset state
2626
LPC_TIM2->MR0 = (1000000 / (2 * frequency)) - 1; // Match value (period) to set frequency
27-
LPC_TIM2->TCR = util::bit_value(0); // Counter Enable
27+
LPC_TIM2->TCR = LPC176x::util::bit_value(0); // Counter Enable
2828

29-
pin_enable_function(_pin, LPC176x::Function::GPIO);
30-
gpio_set_output(_pin);
31-
gpio_clear(_pin);
29+
LPC176x::pin_enable_function(_pin, LPC176x::Function::GPIO);
30+
LPC176x::gpio_set_output(_pin);
31+
LPC176x::gpio_clear(_pin);
3232

3333
NVIC_EnableIRQ(TIMER2_IRQn);
3434
}
3535

3636
void noTone(const pin_t _pin) {
37-
LPC_TIM2->TCR = util::bit_value(1); // Put Timer in reset state
37+
LPC_TIM2->TCR = LPC176x::util::bit_value(1); // Put Timer in reset state
3838
NVIC_DisableIRQ(TIMER2_IRQn);
39-
gpio_clear(_pin);
39+
LPC176x::gpio_clear(_pin);
4040
tone_pin = P_NC;
4141
}
4242

@@ -45,6 +45,6 @@ extern "C" [[gnu::optimize("O3")]] void TIMER2_IRQHandler(void) {
4545
LPC_TIM2->IR = interrupts; // clear all interrupts
4646
if (toggles != 0) {
4747
if(toggles > 0) toggles--;
48-
gpio_toggle(tone_pin);
48+
LPC176x::gpio_toggle(tone_pin);
4949
} else noTone(tone_pin);
5050
}

cores/arduino/WInterrupts.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ static void __initialize() {
4040
void attachInterrupt(const pin_t pin, void (*callback)(void), uint32_t mode) {
4141
static int enabled = 0;
4242

43-
if (!gpio_interrupt_capable(pin)) return;
43+
if (!LPC176x::gpio_interrupt_capable(pin)) return;
4444

4545
if (!enabled) {
4646
__initialize();
4747
++enabled;
4848
}
4949

50-
uint8_t myport = pin_port(pin),
51-
mypin = pin_bit(pin);
50+
uint8_t myport = LPC176x::pin_port(pin),
51+
mypin = LPC176x::pin_bit(pin);
5252

5353
if (myport == 0)
5454
callbacksP0[mypin] = callback;
@@ -60,10 +60,10 @@ void attachInterrupt(const pin_t pin, void (*callback)(void), uint32_t mode) {
6060
}
6161

6262
void detachInterrupt(const pin_t pin) {
63-
if (!gpio_interrupt_capable(pin)) return;
63+
if (!LPC176x::gpio_interrupt_capable(pin)) return;
6464

65-
const uint8_t myport = pin_port(pin),
66-
mypin = pin_bit(pin);
65+
const uint8_t myport = LPC176x::pin_port(pin),
66+
mypin = LPC176x::pin_bit(pin);
6767

6868
// Disable interrupt
6969
GpioDisableInt(myport, mypin);
@@ -79,47 +79,47 @@ void detachInterrupt(const pin_t pin) {
7979
extern "C" void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode) {
8080
//pin here is the processor pin, not logical pin
8181
if (port == 0) {
82-
LPC_GPIOINT->IO0IntClr = util::bit_value(pin);
82+
LPC_GPIOINT->IO0IntClr = LPC176x::util::bit_value(pin);
8383
if (mode == RISING) {
84-
util::bit_set(LPC_GPIOINT->IO0IntEnR, pin);
85-
util::bit_clear(LPC_GPIOINT->IO0IntEnF, pin);
84+
LPC176x::util::bit_set(LPC_GPIOINT->IO0IntEnR, pin);
85+
LPC176x::util::bit_clear(LPC_GPIOINT->IO0IntEnF, pin);
8686
}
8787
else if (mode == FALLING) {
88-
util::bit_set(LPC_GPIOINT->IO0IntEnF, pin);
89-
util::bit_clear(LPC_GPIOINT->IO0IntEnR, pin);
88+
LPC176x::util::bit_set(LPC_GPIOINT->IO0IntEnF, pin);
89+
LPC176x::util::bit_clear(LPC_GPIOINT->IO0IntEnR, pin);
9090
}
9191
else if (mode == CHANGE) {
92-
util::bit_set(LPC_GPIOINT->IO0IntEnR, pin);
93-
util::bit_set(LPC_GPIOINT->IO0IntEnF, pin);
92+
LPC176x::util::bit_set(LPC_GPIOINT->IO0IntEnR, pin);
93+
LPC176x::util::bit_set(LPC_GPIOINT->IO0IntEnF, pin);
9494
}
9595
}
9696
else {
97-
LPC_GPIOINT->IO2IntClr = util::bit_value(pin);
97+
LPC_GPIOINT->IO2IntClr = LPC176x::util::bit_value(pin);
9898
if (mode == RISING) {
99-
util::bit_set(LPC_GPIOINT->IO2IntEnR, pin);
100-
util::bit_clear(LPC_GPIOINT->IO2IntEnF, pin);
99+
LPC176x::util::bit_set(LPC_GPIOINT->IO2IntEnR, pin);
100+
LPC176x::util::bit_clear(LPC_GPIOINT->IO2IntEnF, pin);
101101
}
102102
else if (mode == FALLING) {
103-
util::bit_set(LPC_GPIOINT->IO2IntEnF, pin);
104-
util::bit_clear(LPC_GPIOINT->IO2IntEnR, pin);
103+
LPC176x::util::bit_set(LPC_GPIOINT->IO2IntEnF, pin);
104+
LPC176x::util::bit_clear(LPC_GPIOINT->IO2IntEnR, pin);
105105
}
106106
else if (mode == CHANGE) {
107-
util::bit_set(LPC_GPIOINT->IO2IntEnR, pin);
108-
util::bit_set(LPC_GPIOINT->IO2IntEnF, pin);
107+
LPC176x::util::bit_set(LPC_GPIOINT->IO2IntEnR, pin);
108+
LPC176x::util::bit_set(LPC_GPIOINT->IO2IntEnF, pin);
109109
}
110110
}
111111
}
112112

113113
extern "C" void GpioDisableInt(const uint32_t port, const uint32_t pin) {
114114
if (port == 0) {
115-
util::bit_clear(LPC_GPIOINT->IO0IntEnR, pin);
116-
util::bit_clear(LPC_GPIOINT->IO0IntEnF, pin);
117-
LPC_GPIOINT->IO0IntClr = util::bit_value(pin);
115+
LPC176x::util::bit_clear(LPC_GPIOINT->IO0IntEnR, pin);
116+
LPC176x::util::bit_clear(LPC_GPIOINT->IO0IntEnF, pin);
117+
LPC_GPIOINT->IO0IntClr = LPC176x::util::bit_value(pin);
118118
}
119119
else {
120-
util::bit_clear(LPC_GPIOINT->IO2IntEnR, pin);
121-
util::bit_clear(LPC_GPIOINT->IO2IntEnF, pin);
122-
LPC_GPIOINT->IO2IntClr = util::bit_value(pin);
120+
LPC176x::util::bit_clear(LPC_GPIOINT->IO2IntEnR, pin);
121+
LPC176x::util::bit_clear(LPC_GPIOINT->IO2IntEnF, pin);
122+
LPC_GPIOINT->IO2IntClr = LPC176x::util::bit_value(pin);
123123
}
124124
}
125125

@@ -138,24 +138,24 @@ extern "C" void EINT3_IRQHandler(void) {
138138
while (rise0 > 0) { // If multiple pins changes happened continue as long as there are interrupts pending
139139
const uint8_t bitloc = 31 - __CLZ(rise0); // CLZ returns number of leading zeros, 31 minus that is location of first pending interrupt
140140
if (callbacksP0[bitloc] != NULL) callbacksP0[bitloc]();
141-
rise0 -= util::bit_value(bitloc);
141+
rise0 -= LPC176x::util::bit_value(bitloc);
142142
}
143143

144144
while (fall0 > 0) {
145145
const uint8_t bitloc = 31 - __CLZ(fall0);
146146
if (callbacksP0[bitloc] != NULL) callbacksP0[bitloc]();
147-
fall0 -= util::bit_value(bitloc);
147+
fall0 -= LPC176x::util::bit_value(bitloc);
148148
}
149149

150150
while(rise2 > 0) {
151151
const uint8_t bitloc = 31 - __CLZ(rise2);
152152
if (callbacksP2[bitloc] != NULL) callbacksP2[bitloc]();
153-
rise2 -= util::bit_value(bitloc);
153+
rise2 -= LPC176x::util::bit_value(bitloc);
154154
}
155155

156156
while (fall2 > 0) {
157157
const uint8_t bitloc = 31 - __CLZ(fall2);
158158
if (callbacksP2[bitloc] != NULL) callbacksP2[bitloc]();
159-
fall2 -= util::bit_value(bitloc);
159+
fall2 -= LPC176x::util::bit_value(bitloc);
160160
}
161161
}

0 commit comments

Comments
 (0)