summaryrefslogtreecommitdiff
path: root/lib/arduino-stub.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arduino-stub.cpp')
-rw-r--r--lib/arduino-stub.cpp594
1 files changed, 559 insertions, 35 deletions
diff --git a/lib/arduino-stub.cpp b/lib/arduino-stub.cpp
index 237074c..b98946d 100644
--- a/lib/arduino-stub.cpp
+++ b/lib/arduino-stub.cpp
@@ -20,10 +20,13 @@
* along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
*/
+#define ARDUINO_MAIN
#include "Arduino.h"
+int atexit(void (* /*func*/ )()) { return 0; }
extern "C" {
+#include "stdlib.h"
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
@@ -52,11 +55,8 @@ extern "C" {
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
-
- $Id$
*/
-//#include "wiring_private.h"
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
@@ -132,7 +132,6 @@ unsigned long micros() {
#error TIMER 0 not defined
#endif
-
#ifdef TIFR0
if ((TIFR0 & _BV(TOV0)) && (t < 255))
m++;
@@ -148,75 +147,128 @@ unsigned long micros() {
void delay(unsigned long ms)
{
- uint16_t start = (uint16_t)micros();
+ uint32_t start = micros();
while (ms > 0) {
- if (((uint16_t)micros() - start) >= 1000) {
+ yield();
+ while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
-/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
+/* Delay for the given number of microseconds. Assumes a 1, 8, 12, 16, 20 or 24 MHz clock. */
void delayMicroseconds(unsigned int us)
{
+ // call = 4 cycles + 2 to 4 cycles to init us(2 for constant delay, 4 for variable)
+
// calling avrlib's delay_us() function with low values (e.g. 1 or
// 2 microseconds) gives delays longer than desired.
//delay_us(us);
-#if F_CPU >= 20000000L
+#if F_CPU >= 24000000L
+ // for the 24 MHz clock for the aventurous ones, trying to overclock
+
+ // zero delay fix
+ if (!us) return; // = 3 cycles, (4 when true)
+
+ // the following loop takes a 1/6 of a microsecond (4 cycles)
+ // per iteration, so execute it six times for each microsecond of
+ // delay requested.
+ us *= 6; // x6 us, = 7 cycles
+
+ // account for the time taken in the preceeding commands.
+ // we just burned 22 (24) cycles above, remove 5, (5*4=20)
+ // us is at least 6 so we can substract 5
+ us -= 5; //=2 cycles
+
+#elif F_CPU >= 20000000L
// for the 20 MHz clock on rare Arduino boards
- // for a one-microsecond delay, simply wait 2 cycle and return. The overhead
- // of the function call yields a delay of exactly a one microsecond.
+ // for a one-microsecond delay, simply return. the overhead
+ // of the function call takes 18 (20) cycles, which is 1us
__asm__ __volatile__ (
"nop" "\n\t"
- "nop"); //just waiting 2 cycle
- if (--us == 0)
- return;
+ "nop" "\n\t"
+ "nop" "\n\t"
+ "nop"); //just waiting 4 cycles
+ if (us <= 1) return; // = 3 cycles, (4 when true)
// the following loop takes a 1/5 of a microsecond (4 cycles)
// per iteration, so execute it five times for each microsecond of
// delay requested.
- us = (us<<2) + us; // x5 us
+ us = (us << 2) + us; // x5 us, = 7 cycles
// account for the time taken in the preceeding commands.
- us -= 2;
+ // we just burned 26 (28) cycles above, remove 7, (7*4=28)
+ // us is at least 10 so we can substract 7
+ us -= 7; // 2 cycles
#elif F_CPU >= 16000000L
// for the 16 MHz clock on most Arduino boards
// for a one-microsecond delay, simply return. the overhead
- // of the function call yields a delay of approximately 1 1/8 us.
- if (--us == 0)
- return;
+ // of the function call takes 14 (16) cycles, which is 1us
+ if (us <= 1) return; // = 3 cycles, (4 when true)
- // the following loop takes a quarter of a microsecond (4 cycles)
+ // the following loop takes 1/4 of a microsecond (4 cycles)
// per iteration, so execute it four times for each microsecond of
// delay requested.
- us <<= 2;
+ us <<= 2; // x4 us, = 4 cycles
// account for the time taken in the preceeding commands.
- us -= 2;
-#else
- // for the 8 MHz internal clock on the ATmega168
+ // we just burned 19 (21) cycles above, remove 5, (5*4=20)
+ // us is at least 8 so we can substract 5
+ us -= 5; // = 2 cycles,
+
+#elif F_CPU >= 12000000L
+ // for the 12 MHz clock if somebody is working with USB
- // for a one- or two-microsecond delay, simply return. the overhead of
- // the function calls takes more than two microseconds. can't just
- // subtract two, since us is unsigned; we'd overflow.
- if (--us == 0)
- return;
- if (--us == 0)
- return;
+ // for a 1 microsecond delay, simply return. the overhead
+ // of the function call takes 14 (16) cycles, which is 1.5us
+ if (us <= 1) return; // = 3 cycles, (4 when true)
- // the following loop takes half of a microsecond (4 cycles)
+ // the following loop takes 1/3 of a microsecond (4 cycles)
+ // per iteration, so execute it three times for each microsecond of
+ // delay requested.
+ us = (us << 1) + us; // x3 us, = 5 cycles
+
+ // account for the time taken in the preceeding commands.
+ // we just burned 20 (22) cycles above, remove 5, (5*4=20)
+ // us is at least 6 so we can substract 5
+ us -= 5; //2 cycles
+
+#elif F_CPU >= 8000000L
+ // for the 8 MHz internal clock
+
+ // for a 1 and 2 microsecond delay, simply return. the overhead
+ // of the function call takes 14 (16) cycles, which is 2us
+ if (us <= 2) return; // = 3 cycles, (4 when true)
+
+ // the following loop takes 1/2 of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
- us <<= 1;
+ us <<= 1; //x2 us, = 2 cycles
+
+ // account for the time taken in the preceeding commands.
+ // we just burned 17 (19) cycles above, remove 4, (4*4=16)
+ // us is at least 6 so we can substract 4
+ us -= 4; // = 2 cycles
+
+#else
+ // for the 1 MHz internal clock (default settings for common Atmega microcontrollers)
+
+ // the overhead of the function calls is 14 (16) cycles
+ if (us <= 16) return; //= 3 cycles, (4 when true)
+ if (us <= 25) return; //= 3 cycles, (4 when true), (must be at least 25 if we want to substract 22)
+
+ // compensate for the time taken by the preceeding and next commands (about 22 cycles)
+ us -= 22; // = 2 cycles
+ // the following loop takes 4 microseconds (4 cycles)
+ // per iteration, so execute it us/4 times
+ // us is at least 4, divided by 4 gives us 1 (no zero delay bug)
+ us >>= 2; // us div 4, = 4 cycles
- // partially compensate for the time taken by the preceeding commands.
- // we can't subtract any more than this or we'd overflow w/ small delays.
- us--;
#endif
// busy wait
@@ -224,6 +276,7 @@ void delayMicroseconds(unsigned int us)
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
+ // return = 4 cycles
}
void init()
@@ -264,8 +317,21 @@ void init()
#else
#error Timer 0 overflow interrupt not set correctly
#endif
+}
+/**
+ * Empty yield() hook.
+ *
+ * This function is intended to be used by library writers to build
+ * libraries or sketches that supports cooperative threads.
+ *
+ * Its defined as a weak symbol and it can be redefined to implement a
+ * real cooperative scheduler.
+ */
+static void __empty() {
+ // Empty
}
+void yield(void) __attribute__ ((weak, alias("__empty")));
} // extern "C"
@@ -273,3 +339,461 @@ void arduino_init(void)
{
init();
}
+
+// ******************
+// this is from Arduino's wiring_digital.cpp
+
+void pinMode(uint8_t pin, uint8_t mode)
+{
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+ volatile uint8_t *reg, *out;
+
+ if (port == NOT_A_PIN) return;
+
+ // JWS: can I let the optimizer do this?
+ reg = portModeRegister(port);
+ out = portOutputRegister(port);
+
+ if (mode == INPUT) {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg &= ~bit;
+ *out &= ~bit;
+ SREG = oldSREG;
+ } else if (mode == INPUT_PULLUP) {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg &= ~bit;
+ *out |= bit;
+ SREG = oldSREG;
+ } else {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg |= bit;
+ SREG = oldSREG;
+ }
+}
+
+// Forcing this inline keeps the callers from having to push their own stuff
+// on the stack. It is a good performance win and only takes 1 more byte per
+// user than calling. (It will take more bytes on the 168.)
+//
+// But shouldn't this be moved into pinMode? Seems silly to check and do on
+// each digitalread or write.
+//
+// Mark Sproul:
+// - Removed inline. Save 170 bytes on atmega1280
+// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
+// - Added more #ifdefs, now compiles for atmega645
+//
+//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
+//static inline void turnOffPWM(uint8_t timer)
+static void turnOffPWM(uint8_t timer)
+{
+ switch (timer)
+ {
+ #if defined(TCCR1A) && defined(COM1A1)
+ case TIMER1A: cbi(TCCR1A, COM1A1); break;
+ #endif
+ #if defined(TCCR1A) && defined(COM1B1)
+ case TIMER1B: cbi(TCCR1A, COM1B1); break;
+ #endif
+ #if defined(TCCR1A) && defined(COM1C1)
+ case TIMER1C: cbi(TCCR1A, COM1C1); break;
+ #endif
+
+ #if defined(TCCR2) && defined(COM21)
+ case TIMER2: cbi(TCCR2, COM21); break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0A1)
+ case TIMER0A: cbi(TCCR0A, COM0A1); break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0B1)
+ case TIMER0B: cbi(TCCR0A, COM0B1); break;
+ #endif
+ #if defined(TCCR2A) && defined(COM2A1)
+ case TIMER2A: cbi(TCCR2A, COM2A1); break;
+ #endif
+ #if defined(TCCR2A) && defined(COM2B1)
+ case TIMER2B: cbi(TCCR2A, COM2B1); break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3A1)
+ case TIMER3A: cbi(TCCR3A, COM3A1); break;
+ #endif
+ #if defined(TCCR3A) && defined(COM3B1)
+ case TIMER3B: cbi(TCCR3A, COM3B1); break;
+ #endif
+ #if defined(TCCR3A) && defined(COM3C1)
+ case TIMER3C: cbi(TCCR3A, COM3C1); break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4A1)
+ case TIMER4A: cbi(TCCR4A, COM4A1); break;
+ #endif
+ #if defined(TCCR4A) && defined(COM4B1)
+ case TIMER4B: cbi(TCCR4A, COM4B1); break;
+ #endif
+ #if defined(TCCR4A) && defined(COM4C1)
+ case TIMER4C: cbi(TCCR4A, COM4C1); break;
+ #endif
+ #if defined(TCCR4C) && defined(COM4D1)
+ case TIMER4D: cbi(TCCR4C, COM4D1); break;
+ #endif
+
+ #if defined(TCCR5A)
+ case TIMER5A: cbi(TCCR5A, COM5A1); break;
+ case TIMER5B: cbi(TCCR5A, COM5B1); break;
+ case TIMER5C: cbi(TCCR5A, COM5C1); break;
+ #endif
+ }
+}
+
+void digitalWrite(uint8_t pin, uint8_t val)
+{
+ uint8_t timer = digitalPinToTimer(pin);
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+ volatile uint8_t *out;
+
+ if (port == NOT_A_PIN) return;
+
+ // If the pin that support PWM output, we need to turn it off
+ // before doing a digital write.
+ if (timer != NOT_ON_TIMER) turnOffPWM(timer);
+
+ out = portOutputRegister(port);
+
+ uint8_t oldSREG = SREG;
+ cli();
+
+ if (val == LOW) {
+ *out &= ~bit;
+ } else {
+ *out |= bit;
+ }
+
+ SREG = oldSREG;
+}
+
+int digitalRead(uint8_t pin)
+{
+ uint8_t timer = digitalPinToTimer(pin);
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+
+ if (port == NOT_A_PIN) return LOW;
+
+ // If the pin that support PWM output, we need to turn it off
+ // before getting a digital reading.
+ if (timer != NOT_ON_TIMER) turnOffPWM(timer);
+
+ if (*portInputRegister(port) & bit) return HIGH;
+ return LOW;
+}
+
+// ******************
+// this is from Arduino's wiring_analog.cpp
+
+uint8_t analog_reference = DEFAULT;
+
+void analogReference(uint8_t mode)
+{
+ // can't actually set the register here because the default setting
+ // will connect AVCC and the AREF pin, which would cause a short if
+ // there's something connected to AREF.
+ analog_reference = mode;
+}
+
+int analogRead(uint8_t pin)
+{
+ uint8_t low, high;
+
+#if defined(analogPinToChannel)
+#if defined(__AVR_ATmega32U4__)
+ if (pin >= 18) pin -= 18; // allow for channel or pin numbers
+#endif
+ pin = analogPinToChannel(pin);
+#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+ if (pin >= 54) pin -= 54; // allow for channel or pin numbers
+#elif defined(__AVR_ATmega32U4__)
+ if (pin >= 18) pin -= 18; // allow for channel or pin numbers
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
+ if (pin >= 24) pin -= 24; // allow for channel or pin numbers
+#else
+ if (pin >= 14) pin -= 14; // allow for channel or pin numbers
+#endif
+
+#if defined(ADCSRB) && defined(MUX5)
+ // the MUX5 bit of ADCSRB selects whether we're reading from channels
+ // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
+ ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
+#endif
+
+ // set the analog reference (high two bits of ADMUX) and select the
+ // channel (low 4 bits). this also sets ADLAR (left-adjust result)
+ // to 0 (the default).
+#if defined(ADMUX)
+ ADMUX = (analog_reference << 6) | (pin & 0x07);
+#endif
+
+ // without a delay, we seem to read from the wrong channel
+ //delay(1);
+
+#if defined(ADCSRA) && defined(ADCL)
+ // start the conversion
+ sbi(ADCSRA, ADSC);
+
+ // ADSC is cleared when the conversion finishes
+ while (bit_is_set(ADCSRA, ADSC));
+
+ // we have to read ADCL first; doing so locks both ADCL
+ // and ADCH until ADCH is read. reading ADCL second would
+ // cause the results of each conversion to be discarded,
+ // as ADCL and ADCH would be locked when it completed.
+ low = ADCL;
+ high = ADCH;
+#else
+ // we dont have an ADC, return 0
+ low = 0;
+ high = 0;
+#endif
+
+ // combine the two bytes
+ return (high << 8) | low;
+}
+
+// Right now, PWM output only works on the pins with
+// hardware support. These are defined in the appropriate
+// pins_*.c file. For the rest of the pins, we default
+// to digital output.
+void analogWrite(uint8_t pin, int val)
+{
+ // We need to make sure the PWM output is enabled for those pins
+ // that support it, as we turn it off when digitally reading or
+ // writing with them. Also, make sure the pin is in output mode
+ // for consistenty with Wiring, which doesn't require a pinMode
+ // call for the analog output pins.
+ pinMode(pin, OUTPUT);
+ if (val == 0)
+ {
+ digitalWrite(pin, LOW);
+ }
+ else if (val == 255)
+ {
+ digitalWrite(pin, HIGH);
+ }
+ else
+ {
+ switch(digitalPinToTimer(pin))
+ {
+ // XXX fix needed for atmega8
+ #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
+ case TIMER0A:
+ // connect pwm to pin on timer 0
+ sbi(TCCR0, COM00);
+ OCR0 = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0A1)
+ case TIMER0A:
+ // connect pwm to pin on timer 0, channel A
+ sbi(TCCR0A, COM0A1);
+ OCR0A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0B1)
+ case TIMER0B:
+ // connect pwm to pin on timer 0, channel B
+ sbi(TCCR0A, COM0B1);
+ OCR0B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(COM1A1)
+ case TIMER1A:
+ // connect pwm to pin on timer 1, channel A
+ sbi(TCCR1A, COM1A1);
+ OCR1A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(COM1B1)
+ case TIMER1B:
+ // connect pwm to pin on timer 1, channel B
+ sbi(TCCR1A, COM1B1);
+ OCR1B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(COM1C1)
+ case TIMER1C:
+ // connect pwm to pin on timer 1, channel B
+ sbi(TCCR1A, COM1C1);
+ OCR1C = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2) && defined(COM21)
+ case TIMER2:
+ // connect pwm to pin on timer 2
+ sbi(TCCR2, COM21);
+ OCR2 = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2A) && defined(COM2A1)
+ case TIMER2A:
+ // connect pwm to pin on timer 2, channel A
+ sbi(TCCR2A, COM2A1);
+ OCR2A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2A) && defined(COM2B1)
+ case TIMER2B:
+ // connect pwm to pin on timer 2, channel B
+ sbi(TCCR2A, COM2B1);
+ OCR2B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3A1)
+ case TIMER3A:
+ // connect pwm to pin on timer 3, channel A
+ sbi(TCCR3A, COM3A1);
+ OCR3A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3B1)
+ case TIMER3B:
+ // connect pwm to pin on timer 3, channel B
+ sbi(TCCR3A, COM3B1);
+ OCR3B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3C1)
+ case TIMER3C:
+ // connect pwm to pin on timer 3, channel C
+ sbi(TCCR3A, COM3C1);
+ OCR3C = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A)
+ case TIMER4A:
+ //connect pwm to pin on timer 4, channel A
+ sbi(TCCR4A, COM4A1);
+ #if defined(COM4A0) // only used on 32U4
+ cbi(TCCR4A, COM4A0);
+ #endif
+ OCR4A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4B1)
+ case TIMER4B:
+ // connect pwm to pin on timer 4, channel B
+ sbi(TCCR4A, COM4B1);
+ OCR4B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4C1)
+ case TIMER4C:
+ // connect pwm to pin on timer 4, channel C
+ sbi(TCCR4A, COM4C1);
+ OCR4C = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4C) && defined(COM4D1)
+ case TIMER4D:
+ // connect pwm to pin on timer 4, channel D
+ sbi(TCCR4C, COM4D1);
+ #if defined(COM4D0) // only used on 32U4
+ cbi(TCCR4C, COM4D0);
+ #endif
+ OCR4D = val; // set pwm duty
+ break;
+ #endif
+
+
+ #if defined(TCCR5A) && defined(COM5A1)
+ case TIMER5A:
+ // connect pwm to pin on timer 5, channel A
+ sbi(TCCR5A, COM5A1);
+ OCR5A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR5A) && defined(COM5B1)
+ case TIMER5B:
+ // connect pwm to pin on timer 5, channel B
+ sbi(TCCR5A, COM5B1);
+ OCR5B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR5A) && defined(COM5C1)
+ case TIMER5C:
+ // connect pwm to pin on timer 5, channel C
+ sbi(TCCR5A, COM5C1);
+ OCR5C = val; // set pwm duty
+ break;
+ #endif
+
+ case NOT_ON_TIMER:
+ default:
+ if (val < 128) {
+ digitalWrite(pin, LOW);
+ } else {
+ digitalWrite(pin, HIGH);
+ }
+ }
+ }
+}
+
+// ******************
+// this is from Arduino's WMath.cpp
+
+void randomSeed(unsigned long seed)
+{
+ if (seed != 0) {
+ srandom(seed);
+ }
+}
+
+long random(long howbig)
+{
+ if (howbig == 0) {
+ return 0;
+ }
+ return random() % howbig;
+}
+
+long random(long howsmall, long howbig)
+{
+ if (howsmall >= howbig) {
+ return howsmall;
+ }
+ long diff = howbig - howsmall;
+ return random(diff) + howsmall;
+}
+
+long map(long x, long in_min, long in_max, long out_min, long out_max)
+{
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+unsigned int makeWord(unsigned int w) { return w; }
+unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }
+
+// end WMath.cpp