mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
Added general yield()-hook for cooperative scheduling development
This commit is contained in:
parent
cf4d72c043
commit
107c1929bd
@ -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
|
||||
|
@ -15,6 +15,8 @@
|
||||
extern "C"{
|
||||
#endif
|
||||
|
||||
#define yield()
|
||||
|
||||
#define HIGH 0x1
|
||||
#define LOW 0x0
|
||||
|
||||
|
@ -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"
|
||||
|
31
hardware/arduino/sam/cores/arduino/hooks.c
Normal file
31
hardware/arduino/sam/cores/arduino/hooks.c
Normal 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")));
|
@ -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();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -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() {
|
||||
|
@ -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(); };
|
||||
};
|
||||
|
||||
|
@ -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 <Scheduler.h>
|
||||
|
||||
@ -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
|
||||
|
@ -12,8 +12,6 @@ Scheduler KEYWORD1
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
yield KEYWORD2
|
||||
wait KEYWORD2
|
||||
startLoop KEYWORD2
|
||||
|
||||
#######################################
|
||||
|
Loading…
x
Reference in New Issue
Block a user