diff --git a/build/shared/lib/keywords.txt b/build/shared/lib/keywords.txt index 10477805a..bc71ed96f 100644 --- a/build/shared/lib/keywords.txt +++ b/build/shared/lib/keywords.txt @@ -161,6 +161,7 @@ pulseIn KEYWORD2 PulseIn shiftIn KEYWORD2 ShiftIn shiftOut KEYWORD2 ShiftOut tone KEYWORD2 Tone +yield KEYWORD2 Yield Serial KEYWORD3 Serial Serial1 KEYWORD3 Serial diff --git a/hardware/arduino/avr/cores/arduino/Arduino.h b/hardware/arduino/avr/cores/arduino/Arduino.h index b26582589..908c66e12 100755 --- a/hardware/arduino/avr/cores/arduino/Arduino.h +++ b/hardware/arduino/avr/cores/arduino/Arduino.h @@ -15,6 +15,8 @@ extern "C"{ #endif +#define yield() + #define HIGH 0x1 #define LOW 0x0 diff --git a/hardware/arduino/sam/cores/arduino/Arduino.h b/hardware/arduino/sam/cores/arduino/Arduino.h index 4b94acf18..198d86c31 100644 --- a/hardware/arduino/sam/cores/arduino/Arduino.h +++ b/hardware/arduino/sam/cores/arduino/Arduino.h @@ -39,6 +39,7 @@ extern "C"{ #define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (SystemCoreClock / 1000L) ) #define microsecondsToClockCycles(a) ( (a) * (SystemCoreClock / 1000000L) ) +void yield(void); #include "wiring.h" #include "wiring_digital.h" diff --git a/hardware/arduino/sam/cores/arduino/hooks.c b/hardware/arduino/sam/cores/arduino/hooks.c new file mode 100644 index 000000000..641eabc73 --- /dev/null +++ b/hardware/arduino/sam/cores/arduino/hooks.c @@ -0,0 +1,31 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * 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"))); diff --git a/hardware/arduino/sam/cores/arduino/wiring.c b/hardware/arduino/sam/cores/arduino/wiring.c index ce3f798ac..b75425e58 100644 --- a/hardware/arduino/sam/cores/arduino/wiring.c +++ b/hardware/arduino/sam/cores/arduino/wiring.c @@ -43,19 +43,18 @@ uint32_t micros( void ) return count * 1000 + (SysTick->LOAD + 1 - ticks) / (SystemCoreClock/1000000) ; } -void delay( uint32_t dwMs ) +void delay( uint32_t ms ) { - Wait( dwMs ) ; + uint32_t end = GetTickCount() + ms; + while (GetTickCount() < end) + yield(); } -void delayMicroseconds( uint32_t dwUs ) +void delayMicroseconds( uint32_t us ) { - uint32_t dwStartMicros=micros() ; - - while ( (micros() - dwStartMicros) < dwUs ) - { - // do nothing - } + uint32_t start = micros(); + while ((micros() - start) < us) + yield(); } /* diff --git a/hardware/arduino/sam/libraries/Scheduler/Scheduler.cpp b/hardware/arduino/sam/libraries/Scheduler/Scheduler.cpp index e2ea3c879..2be2d8206 100644 --- a/hardware/arduino/sam/libraries/Scheduler/Scheduler.cpp +++ b/hardware/arduino/sam/libraries/Scheduler/Scheduler.cpp @@ -122,12 +122,6 @@ void yield(void) { coopDoYield(cur); } -void wait(uint32_t ms) { - uint32_t start = millis(); - while (millis() - start < ms) - yield(); -} - }; // extern "C" SchedulerClass::SchedulerClass() { diff --git a/hardware/arduino/sam/libraries/Scheduler/Scheduler.h b/hardware/arduino/sam/libraries/Scheduler/Scheduler.h index e4b910225..1b972a386 100644 --- a/hardware/arduino/sam/libraries/Scheduler/Scheduler.h +++ b/hardware/arduino/sam/libraries/Scheduler/Scheduler.h @@ -22,9 +22,6 @@ extern "C" { typedef void (*SchedulerTask)(void); typedef void (*SchedulerParametricTask)(void *); - - void wait(uint32_t ms); - void yield(); } class SchedulerClass { @@ -34,7 +31,6 @@ public: static void start(SchedulerTask task, uint32_t stackSize = 1024); static void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024); - static void wait(uint32_t ms) { ::wait(ms); }; static void yield() { ::yield(); }; }; diff --git a/hardware/arduino/sam/libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino b/hardware/arduino/sam/libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino index a05491e54..f48bbf6f5 100644 --- a/hardware/arduino/sam/libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino +++ b/hardware/arduino/sam/libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino @@ -14,10 +14,8 @@ This example code is in the public domain http://arduino.cc/en/Tutorial/MultipleBlinks - */ - // Include Scheduler since we want to manage multiple tasks. #include @@ -44,21 +42,20 @@ void loop() { digitalWrite(led1, HIGH); // IMPORTANT: - // We must use 'wait' instead of 'delay' to guarantee - // that the other tasks get executed. - // ('wait' passes control to other tasks while waiting) - wait(1000); + // When multiple tasks are running 'delay' passes control to + // other tasks while waiting and guarantees they get executed. + delay(1000); digitalWrite(led1, LOW); - wait(1000); + delay(1000); } // Task no.2: blink LED with 0.1 second delay. void loop2() { digitalWrite(led2, HIGH); - wait(100); + delay(100); digitalWrite(led2, LOW); - wait(100); + delay(100); } // Task no.3: accept commands from Serial port diff --git a/hardware/arduino/sam/libraries/Scheduler/keywords.txt b/hardware/arduino/sam/libraries/Scheduler/keywords.txt index 0ad806f88..87313b8fc 100644 --- a/hardware/arduino/sam/libraries/Scheduler/keywords.txt +++ b/hardware/arduino/sam/libraries/Scheduler/keywords.txt @@ -12,8 +12,6 @@ Scheduler KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -yield KEYWORD2 -wait KEYWORD2 startLoop KEYWORD2 #######################################