Skip to content

Commit 107c192

Browse files
committed
Added general yield()-hook for cooperative scheduling development
1 parent cf4d72c commit 107c192

File tree

9 files changed

+49
-30
lines changed

9 files changed

+49
-30
lines changed

build/shared/lib/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pulseIn KEYWORD2 PulseIn
161161
shiftIn KEYWORD2 ShiftIn
162162
shiftOut KEYWORD2 ShiftOut
163163
tone KEYWORD2 Tone
164+
yield KEYWORD2 Yield
164165

165166
Serial KEYWORD3 Serial
166167
Serial1 KEYWORD3 Serial

hardware/arduino/avr/cores/arduino/Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
extern "C"{
1616
#endif
1717

18+
#define yield()
19+
1820
#define HIGH 0x1
1921
#define LOW 0x0
2022

hardware/arduino/sam/cores/arduino/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern "C"{
3939
#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (SystemCoreClock / 1000L) )
4040
#define microsecondsToClockCycles(a) ( (a) * (SystemCoreClock / 1000000L) )
4141

42+
void yield(void);
4243

4344
#include "wiring.h"
4445
#include "wiring_digital.h"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright (c) 2012 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
/**
20+
* Empty yield() hook.
21+
*
22+
* This function is intended to be used by library writers to build
23+
* libraries or sketches that supports cooperative threads.
24+
*
25+
* Its defined as a weak symbol and it can be redefined to implement a
26+
* real cooperative scheduler.
27+
*/
28+
static void __empty() {
29+
// Empty
30+
}
31+
void yield(void) __attribute__ ((weak, alias("__empty")));

hardware/arduino/sam/cores/arduino/wiring.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,18 @@ uint32_t micros( void )
4343
return count * 1000 + (SysTick->LOAD + 1 - ticks) / (SystemCoreClock/1000000) ;
4444
}
4545

46-
void delay( uint32_t dwMs )
46+
void delay( uint32_t ms )
4747
{
48-
Wait( dwMs ) ;
48+
uint32_t end = GetTickCount() + ms;
49+
while (GetTickCount() < end)
50+
yield();
4951
}
5052

51-
void delayMicroseconds( uint32_t dwUs )
53+
void delayMicroseconds( uint32_t us )
5254
{
53-
uint32_t dwStartMicros=micros() ;
54-
55-
while ( (micros() - dwStartMicros) < dwUs )
56-
{
57-
// do nothing
58-
}
55+
uint32_t start = micros();
56+
while ((micros() - start) < us)
57+
yield();
5958
}
6059

6160
/*

hardware/arduino/sam/libraries/Scheduler/Scheduler.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ void yield(void) {
122122
coopDoYield(cur);
123123
}
124124

125-
void wait(uint32_t ms) {
126-
uint32_t start = millis();
127-
while (millis() - start < ms)
128-
yield();
129-
}
130-
131125
}; // extern "C"
132126

133127
SchedulerClass::SchedulerClass() {

hardware/arduino/sam/libraries/Scheduler/Scheduler.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
extern "C" {
2323
typedef void (*SchedulerTask)(void);
2424
typedef void (*SchedulerParametricTask)(void *);
25-
26-
void wait(uint32_t ms);
27-
void yield();
2825
}
2926

3027
class SchedulerClass {
@@ -34,7 +31,6 @@ class SchedulerClass {
3431
static void start(SchedulerTask task, uint32_t stackSize = 1024);
3532
static void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024);
3633

37-
static void wait(uint32_t ms) { ::wait(ms); };
3834
static void yield() { ::yield(); };
3935
};
4036

hardware/arduino/sam/libraries/Scheduler/examples/MultipleBlinks/MultipleBlinks.ino

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
This example code is in the public domain
1515
1616
http://arduino.cc/en/Tutorial/MultipleBlinks
17-
1817
*/
1918

20-
2119
// Include Scheduler since we want to manage multiple tasks.
2220
#include <Scheduler.h>
2321

@@ -44,21 +42,20 @@ void loop() {
4442
digitalWrite(led1, HIGH);
4543

4644
// IMPORTANT:
47-
// We must use 'wait' instead of 'delay' to guarantee
48-
// that the other tasks get executed.
49-
// ('wait' passes control to other tasks while waiting)
50-
wait(1000);
45+
// When multiple tasks are running 'delay' passes control to
46+
// other tasks while waiting and guarantees they get executed.
47+
delay(1000);
5148

5249
digitalWrite(led1, LOW);
53-
wait(1000);
50+
delay(1000);
5451
}
5552

5653
// Task no.2: blink LED with 0.1 second delay.
5754
void loop2() {
5855
digitalWrite(led2, HIGH);
59-
wait(100);
56+
delay(100);
6057
digitalWrite(led2, LOW);
61-
wait(100);
58+
delay(100);
6259
}
6360

6461
// Task no.3: accept commands from Serial port

hardware/arduino/sam/libraries/Scheduler/keywords.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ Scheduler KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414

15-
yield KEYWORD2
16-
wait KEYWORD2
1715
startLoop KEYWORD2
1816

1917
#######################################

0 commit comments

Comments
 (0)