diff --git a/Readme.md b/Readme.md index 88104cd..6c1b0a8 100644 --- a/Readme.md +++ b/Readme.md @@ -63,6 +63,11 @@ Version History =============== ``` 2.3 Release (xx.xx.2015) +* Updated Libraries +* Updated Arduino Core +* Added Minor Consumer definitions +* Fixed platforms.txt +* SERIAL_RX_BUFFER_SIZE reverted to 16 (TODO add -D to build option) 2.2 Release (12.04.2015) * added experimental, not finished nor documented HID-Bridge between 16u2 and 328/2560 diff --git a/avr/cores/hid/Arduino.h b/avr/cores/hid/Arduino.h index e8575a3..df7990a 100644 --- a/avr/cores/hid/Arduino.h +++ b/avr/cores/hid/Arduino.h @@ -134,7 +134,7 @@ typedef unsigned int word; #define bit(b) (1UL << (b)) -typedef uint8_t boolean; +typedef bool boolean; typedef uint8_t byte; void init(void); diff --git a/avr/cores/hid/HardwareSerial.h b/avr/cores/hid/HardwareSerial.h index 71ad331..7f4f162 100644 --- a/avr/cores/hid/HardwareSerial.h +++ b/avr/cores/hid/HardwareSerial.h @@ -43,7 +43,7 @@ #endif #if !defined(SERIAL_RX_BUFFER_SIZE) #if (RAMEND < 1000) -#define SERIAL_RX_BUFFER_SIZE 32 +#define SERIAL_RX_BUFFER_SIZE 16 #else #define SERIAL_RX_BUFFER_SIZE 64 #endif diff --git a/avr/cores/hid/Stream.cpp b/avr/cores/hid/Stream.cpp index 9c581be..b31942f 100644 --- a/avr/cores/hid/Stream.cpp +++ b/avr/cores/hid/Stream.cpp @@ -18,6 +18,8 @@ Created July 2011 parsing functions based on TextFinder library by Michael Margolis + + findMulti/findUntil routines written by Jim Leonard/Xuth */ #include "Arduino.h" @@ -75,7 +77,7 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi // find returns true if the target string is found bool Stream::find(char *target) { - return findUntil(target, (char*)""); + return findUntil(target, strlen(target), NULL, 0); } // reads data from the stream until the target string of given length is found @@ -96,32 +98,13 @@ bool Stream::findUntil(char *target, char *terminator) // returns true if target string is found, false if terminated or timed out bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) { - size_t index = 0; // maximum target string length is 64k bytes! - size_t termIndex = 0; - int c; - - if( *target == 0) - return true; // return true if target is a null string - while( (c = timedRead()) > 0){ - - if(c != target[index]) - index = 0; // reset index if any char does not match - - if( c == target[index]){ - //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1); - if(++index >= targetLen){ // return true if all chars in the target match - return true; - } - } - - if(termLen > 0 && c == terminator[termIndex]){ - if(++termIndex >= termLen) - return false; // return false if terminate string found before target string - } - else - termIndex = 0; + if (terminator == NULL) { + MultiTarget t[1] = {{target, targetLen, 0}}; + return findMulti(t, 1) == 0 ? true : false; + } else { + MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; + return findMulti(t, 2) == 0 ? true : false; } - return false; } @@ -137,7 +120,7 @@ long Stream::parseInt() // this allows format characters (typically commas) in values to be ignored long Stream::parseInt(char skipChar) { - boolean isNegative = false; + bool isNegative = false; long value = 0; int c; @@ -173,10 +156,10 @@ float Stream::parseFloat() // as above but the given skipChar is ignored // this allows format characters (typically commas) in values to be ignored float Stream::parseFloat(char skipChar){ - boolean isNegative = false; - boolean isFraction = false; + bool isNegative = false; + bool isFraction = false; long value = 0; - int c; + char c; float fraction = 1.0; c = peekNextDigit(); @@ -268,3 +251,67 @@ String Stream::readStringUntil(char terminator) return ret; } +int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { + // any zero length target string automatically matches and would make + // a mess of the rest of the algorithm. + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + if (t->len <= 0) + return t - targets; + } + + while (1) { + int c = timedRead(); + if (c < 0) + return -1; + + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + // the simple case is if we match, deal with that first. + if (c == t->str[t->index]) { + if (++t->index == t->len) + return t - targets; + else + continue; + } + + // if not we need to walk back and see if we could have matched further + // down the stream (ie '1112' doesn't match the first position in '11112' + // but it will match the second position so we can't just reset the current + // index to 0 when we find a mismatch. + if (t->index == 0) + continue; + + int origIndex = t->index; + do { + --t->index; + // first check if current char works against the new current index + if (c != t->str[t->index]) + continue; + + // if it's the only char then we're good, nothing more to check + if (t->index == 0) { + t->index++; + break; + } + + // otherwise we need to check the rest of the found string + int diff = origIndex - t->index; + size_t i; + for (i = 0; i < t->index; ++i) { + if (t->str[i] != t->str[i + diff]) + break; + } + + // if we successfully got through the previous loop then our current + // index is good. + if (i == t->index) { + t->index++; + break; + } + + // otherwise we just try the next index + } while (t->index); + } + } + // unreachable + return -1; +} diff --git a/avr/cores/hid/Stream.h b/avr/cores/hid/Stream.h index 5cf5ddf..a810132 100644 --- a/avr/cores/hid/Stream.h +++ b/avr/cores/hid/Stream.h @@ -97,6 +97,17 @@ class Stream : public Print // this allows format characters (typically commas) in values to be ignored float parseFloat(char skipChar); // as above but the given skipChar is ignored + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); }; + #endif diff --git a/avr/cores/hid/Tone.cpp b/avr/cores/hid/Tone.cpp index 9bb6fe7..7216219 100644 --- a/avr/cores/hid/Tone.cpp +++ b/avr/cores/hid/Tone.cpp @@ -30,6 +30,8 @@ Version Modified By Date Comments 0006 D Mellis 09/12/29 Replaced objects with functions 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register 0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY +0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62) +0010 jipp 15/04/13 added additional define check #2923 *************************************************/ #include @@ -151,7 +153,7 @@ static int8_t toneBegin(uint8_t _pin) // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar switch (_timer) { - #if defined(TCCR0A) && defined(TCCR0B) + #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01) case 0: // 8 bit timer TCCR0A = 0; @@ -296,13 +298,13 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) #if defined(TCCR0B) if (_timer == 0) { - TCCR0B = prescalarbits; + TCCR0B = (TCCR0B & 0b11111000) | prescalarbits; } else #endif #if defined(TCCR2B) { - TCCR2B = prescalarbits; + TCCR2B = (TCCR2B & 0b11111000) | prescalarbits; } #else { @@ -389,7 +391,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) break; #endif -#if defined(TIMSK3) +#if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A) case 3: OCR3A = ocr; timer3_toggle_count = toggle_count; @@ -397,7 +399,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) break; #endif -#if defined(TIMSK4) +#if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A) case 4: OCR4A = ocr; timer4_toggle_count = toggle_count; @@ -454,21 +456,21 @@ void disableTimer(uint8_t _timer) #endif break; -#if defined(TIMSK3) +#if defined(TIMSK3) && defined(OCIE3A) case 3: - TIMSK3 = 0; + bitWrite(TIMSK3, OCIE3A, 0); break; #endif -#if defined(TIMSK4) +#if defined(TIMSK4) && defined(OCIE4A) case 4: - TIMSK4 = 0; + bitWrite(TIMSK4, OCIE4A, 0); break; #endif -#if defined(TIMSK5) +#if defined(TIMSK5) && defined(OCIE5A) case 5: - TIMSK5 = 0; + bitWrite(TIMSK5, OCIE5A, 0); break; #endif } diff --git a/avr/cores/hid/wiring.c b/avr/cores/hid/wiring.c index 5cbe241..6cb22c0 100644 --- a/avr/cores/hid/wiring.c +++ b/avr/cores/hid/wiring.c @@ -92,7 +92,6 @@ unsigned long micros() { #error TIMER 0 not defined #endif - #ifdef TIFR0 if ((TIFR0 & _BV(TOV0)) && (t < 255)) m++; @@ -119,65 +118,118 @@ void delay(unsigned long ms) } } -/* 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, - // 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; +#elif F_CPU >= 12000000L + // for the 12 MHz clock if somebody is working with USB - // the following loop takes half of a microsecond (4 cycles) + // 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 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; - - // 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--; + 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 + + #endif // busy wait @@ -185,6 +237,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() @@ -199,7 +252,7 @@ void init() #if defined(TCCR0A) && defined(WGM01) sbi(TCCR0A, WGM01); sbi(TCCR0A, WGM00); -#endif +#endif // set timer 0 prescale factor to 64 #if defined(__AVR_ATmega128__) @@ -302,14 +355,32 @@ void init() #endif #if defined(ADCSRA) - // set a2d prescale factor to 128 - // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. - // XXX: this will not work properly for other clock speeds, and - // this code should use F_CPU to determine the prescale factor. - sbi(ADCSRA, ADPS2); - sbi(ADCSRA, ADPS1); - sbi(ADCSRA, ADPS0); - + // set a2d prescaler so we are inside the desired 50-200 KHz range. + #if F_CPU >= 16000000 // 16 MHz / 128 = 125 KHz + sbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #elif F_CPU >= 8000000 // 8 MHz / 64 = 125 KHz + sbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + cbi(ADCSRA, ADPS0); + #elif F_CPU >= 4000000 // 4 MHz / 32 = 125 KHz + sbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #elif F_CPU >= 2000000 // 2 MHz / 16 = 125 KHz + sbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + cbi(ADCSRA, ADPS0); + #elif F_CPU >= 1000000 // 1 MHz / 8 = 125 KHz + cbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #else // 128 kHz / 2 = 64 KHz -> This is the closest you can get, the prescaler is 2 + cbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #endif // enable a2d conversions sbi(ADCSRA, ADEN); #endif diff --git a/avr/cores/hid/wiring_private.h b/avr/cores/hid/wiring_private.h index 35f644b..e37ec4d 100644 --- a/avr/cores/hid/wiring_private.h +++ b/avr/cores/hid/wiring_private.h @@ -84,7 +84,7 @@ extern "C"{ #define EXTERNAL_NUM_INTERRUPTS 2 #endif - typedef void(*voidFuncPtr)(void); +typedef void (*voidFuncPtr)(void); #ifdef __cplusplus } // extern "C" diff --git a/avr/cores/hid/wiring_pulse.c b/avr/cores/hid/wiring_pulse.c index 0d96886..830c454 100644 --- a/avr/cores/hid/wiring_pulse.c +++ b/avr/cores/hid/wiring_pulse.c @@ -61,9 +61,25 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) width++; } - // convert the reading to microseconds. The loop has been determined - // to be 20 clock cycles long and have about 16 clocks between the edge - // and the start of the loop. There will be some error introduced by + // convert the reading to microseconds. There will be some error introduced by // the interrupt handlers. - return clockCyclesToMicroseconds(width * 21 + 16); + + // Conversion constants are compiler-dependent, different compiler versions + // have different levels of optimization. +#if __GNUC__==4 && __GNUC_MINOR__==3 && __GNUC_PATCHLEVEL__==2 + // avr-gcc 4.3.2 + return clockCyclesToMicroseconds(width * 21 + 16); +#elif __GNUC__==4 && __GNUC_MINOR__==8 && __GNUC_PATCHLEVEL__==1 + // avr-gcc 4.8.1 + return clockCyclesToMicroseconds(width * 24 + 16); +#elif __GNUC__<=4 && __GNUC_MINOR__<=3 + // avr-gcc <=4.3.x + #warning "pulseIn() results may not be accurate" + return clockCyclesToMicroseconds(width * 21 + 16); +#else + // avr-gcc >4.3.x + #warning "pulseIn() results may not be accurate" + return clockCyclesToMicroseconds(width * 24 + 16); +#endif + }