1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Added general yield()-hook for cooperative scheduling development

This commit is contained in:
Cristian Maglie 2012-10-31 01:37:37 +01:00
parent cf4d72c043
commit 107c1929bd
9 changed files with 49 additions and 30 deletions

View File

@ -161,6 +161,7 @@ pulseIn KEYWORD2 PulseIn
shiftIn KEYWORD2 ShiftIn shiftIn KEYWORD2 ShiftIn
shiftOut KEYWORD2 ShiftOut shiftOut KEYWORD2 ShiftOut
tone KEYWORD2 Tone tone KEYWORD2 Tone
yield KEYWORD2 Yield
Serial KEYWORD3 Serial Serial KEYWORD3 Serial
Serial1 KEYWORD3 Serial Serial1 KEYWORD3 Serial

View File

@ -15,6 +15,8 @@
extern "C"{ extern "C"{
#endif #endif
#define yield()
#define HIGH 0x1 #define HIGH 0x1
#define LOW 0x0 #define LOW 0x0

View File

@ -39,6 +39,7 @@ extern "C"{
#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (SystemCoreClock / 1000L) ) #define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (SystemCoreClock / 1000L) )
#define microsecondsToClockCycles(a) ( (a) * (SystemCoreClock / 1000000L) ) #define microsecondsToClockCycles(a) ( (a) * (SystemCoreClock / 1000000L) )
void yield(void);
#include "wiring.h" #include "wiring.h"
#include "wiring_digital.h" #include "wiring_digital.h"

View File

@ -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")));

View File

@ -43,19 +43,18 @@ uint32_t micros( void )
return count * 1000 + (SysTick->LOAD + 1 - ticks) / (SystemCoreClock/1000000) ; 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() ; uint32_t start = micros();
while ((micros() - start) < us)
while ( (micros() - dwStartMicros) < dwUs ) yield();
{
// do nothing
}
} }
/* /*

View File

@ -122,12 +122,6 @@ void yield(void) {
coopDoYield(cur); coopDoYield(cur);
} }
void wait(uint32_t ms) {
uint32_t start = millis();
while (millis() - start < ms)
yield();
}
}; // extern "C" }; // extern "C"
SchedulerClass::SchedulerClass() { SchedulerClass::SchedulerClass() {

View File

@ -22,9 +22,6 @@
extern "C" { extern "C" {
typedef void (*SchedulerTask)(void); typedef void (*SchedulerTask)(void);
typedef void (*SchedulerParametricTask)(void *); typedef void (*SchedulerParametricTask)(void *);
void wait(uint32_t ms);
void yield();
} }
class SchedulerClass { class SchedulerClass {
@ -34,7 +31,6 @@ public:
static void start(SchedulerTask task, uint32_t stackSize = 1024); static void start(SchedulerTask task, uint32_t stackSize = 1024);
static void start(SchedulerParametricTask task, void *data, 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(); }; static void yield() { ::yield(); };
}; };

View File

@ -14,10 +14,8 @@
This example code is in the public domain This example code is in the public domain
http://arduino.cc/en/Tutorial/MultipleBlinks http://arduino.cc/en/Tutorial/MultipleBlinks
*/ */
// Include Scheduler since we want to manage multiple tasks. // Include Scheduler since we want to manage multiple tasks.
#include <Scheduler.h> #include <Scheduler.h>
@ -44,21 +42,20 @@ void loop() {
digitalWrite(led1, HIGH); digitalWrite(led1, HIGH);
// IMPORTANT: // IMPORTANT:
// We must use 'wait' instead of 'delay' to guarantee // When multiple tasks are running 'delay' passes control to
// that the other tasks get executed. // other tasks while waiting and guarantees they get executed.
// ('wait' passes control to other tasks while waiting) delay(1000);
wait(1000);
digitalWrite(led1, LOW); digitalWrite(led1, LOW);
wait(1000); delay(1000);
} }
// Task no.2: blink LED with 0.1 second delay. // Task no.2: blink LED with 0.1 second delay.
void loop2() { void loop2() {
digitalWrite(led2, HIGH); digitalWrite(led2, HIGH);
wait(100); delay(100);
digitalWrite(led2, LOW); digitalWrite(led2, LOW);
wait(100); delay(100);
} }
// Task no.3: accept commands from Serial port // Task no.3: accept commands from Serial port

View File

@ -12,8 +12,6 @@ Scheduler KEYWORD1
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
####################################### #######################################
yield KEYWORD2
wait KEYWORD2
startLoop KEYWORD2 startLoop KEYWORD2
####################################### #######################################