-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtoThreads.cpp
252 lines (213 loc) · 8.23 KB
/
ProtoThreads.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Author: Michael Lloyd (micl.dev)
* Version: 1.0.0
* Variant: A
* Date: 21/09/22
*
* __ ___.--'_`.
* ( _`.'. - 'o` )
* _\.'_' _.-'
* ( `. ) //`
* \_`-'`---'\\__,
* \\` \`--
*
*/
/* ============================================================================================
* This file contains an extraction and refactoring of the BRL4 additions to the ProtoThreads
* header, from Cornell university.
*
* The original source files implemented everything primarily in the header file that is
* distributed on the ECE4760 website, which does not allow for reliable abstraction or
* modification.
*
* As it stands, this is not a modification, but there may be one here in the future.
* - micl
* ============================================================================================
*/
#include "ProtoThreads.h"
//=====================================================================
//=== BRL4 additions for rp2040 =======================================
//=====================================================================
//====================================================================
// IMPROVED SCHEDULER
// === thread structures ===
// thread control structs
// === extended structure for scheduler ===============
// an array of task structures
// see https://github.com/edartuz/c-ptx/tree/master/src
// and the license above
// add an entry to the thread list
//struct ptx *pt_add( char (*pf)(struct pt *pt), int rate) {
int pt_add( char (*pf)(struct pt *pt)) {
if (pt_task_count < (MAX_THREADS)) {
// get the current thread table entry
struct ptx *ptx = &pt_thread_list[pt_task_count];
// enter the tak data into the thread table
ptx->num = pt_task_count;
// function pointer
ptx->pf = pf;
//
PT_INIT( &ptx->pt );
// count of number of defined threads
pt_task_count++;
// return current entry
return pt_task_count-1;
}
return 0;
}
// core 1 -- add an entry to the thread list
//struct ptx *pt_add( char (*pf)(struct pt *pt), int rate) {
int pt_add1( char (*pf)(struct pt *pt)) {
if (pt_task_count1 < (MAX_THREADS)) {
// get the current thread table entry
struct ptx *ptx = &pt_thread_list1[pt_task_count1];
// enter the tak data into the thread table
ptx->num = pt_task_count1;
// function pointer
ptx->pf = pf;
//
PT_INIT( &ptx->pt );
// count of number of defined threads
pt_task_count1++;
// return current entry
return pt_task_count1-1;
}
return 0;
}
/* Scheduler
Copyright (c) 2014 edartuz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// === Scheduler Thread =================================================
// update a 1 second tick counter
// schedulser code was almost copied from
// https://github.com/edartuz/c-ptx
// see license above
// choose schedule method
static PT_THREAD (protothread_sched(struct pt *pt)) {
PT_BEGIN(pt);
static int i, rate;
if (pt_sched_method==SCHED_ROUND_ROBIN) {
while(1) {
// test stupid round-robin
// on all defined threads
struct ptx *ptx = &pt_thread_list[0];
// step thru all defined threads
// -- loop can have more than one initialization or increment/decrement,
// -- separated using comma operator. But it can have only one condition.
for (i=0; i<pt_task_count; i++, ptx++ ) {
// call thread function
(pt_thread_list[i].pf)(&ptx->pt);
}
// Never yields!
// NEVER exit while!
} // END WHILE(1)
} //end if (pt_sched_method==RR)
PT_END(pt);
} // scheduler thread
// ================================================
// === second core scheduler
static PT_THREAD (protothread_sched1(struct pt *pt)) {
PT_BEGIN(pt);
static int i, rate;
if (pt_sched_method==SCHED_ROUND_ROBIN ) {
while(1) {
// test stupid round-robin
// on all defined threads
struct ptx *ptx = &pt_thread_list1[0];
// step thru all defined threads
// -- loop can have more than one initialization or increment/decrement,
// -- separated using comma operator. But it can have only one condition.
for (i=0; i<pt_task_count1; i++, ptx++ ) {
// call thread function
(pt_thread_list1[i].pf)(&ptx->pt);
}
// Never yields!
// NEVER exit while!
} // END WHILE(1)
} // end if(pt_sched_method==SCHED_ROUND_ROBIN)
PT_END(pt);
} // scheduler1 thread
// === serial input thread ================================
// serial buffers
//
static PT_THREAD (pt_serialin_polled(struct pt *pt)) {
PT_BEGIN(pt);
static uint8_t ch;
static int pt_current_char_count;
// clear the string
memset(pt_serial_in_buffer, 0, pt_buffer_size);
pt_current_char_count = 0;
// clear uart fifo
while(uart_is_readable(UART_ID)) {
uart_getc(UART_ID);
}
// build the output string
while(pt_current_char_count < pt_buffer_size) {
PT_YIELD_UNTIL(pt, (int)uart_is_readable(UART_ID));
//get the character and echo it back to terminal
// NOTE this assumes a human is typing!!
ch = uart_getc(UART_ID);
PT_YIELD_UNTIL(pt, (int)uart_is_writable(UART_ID));
uart_putc(UART_ID, ch);
// check for <enter> or <backspace>
if (ch == '\r' ) {
// <enter>> character terminates string,
// advances the cursor to the next line, then exits
pt_serial_in_buffer[pt_current_char_count] = 0;
PT_YIELD_UNTIL(pt, (int)uart_is_writable(UART_ID));
uart_putc(UART_ID, '\n');
break;
}
// check fo ,backspace>
else if (ch == pt_backspace) {
PT_YIELD_UNTIL(pt, (int)uart_is_writable(UART_ID));
uart_putc(UART_ID, ' ');
PT_YIELD_UNTIL(pt, (int)uart_is_writable(UART_ID));
uart_putc(UART_ID, pt_backspace);
//uart_putc(UART_ID, ' ');
// wipe a character from the output
pt_current_char_count--;
if (pt_current_char_count<0) {pt_current_char_count = 0;
}
}
// must be a real character
else {
// build the output string
pt_serial_in_buffer[pt_current_char_count++] = ch;
}
} // END WHILe
// kill this input thread, to allow spawning thread to execute
PT_EXIT(pt);
PT_END(pt);
} // serial input thread
// ================================================================
// === serial output thread
int pt_serialout_polled(struct pt *pt) {
static int num_send_chars;
PT_BEGIN(pt);
num_send_chars = 0;
while (pt_serial_out_buffer[num_send_chars] != 0) {
PT_YIELD_UNTIL(pt, (int)uart_is_writable(UART_ID));
uart_putc(UART_ID, pt_serial_out_buffer[num_send_chars]);
num_send_chars++;
}
// kill this output thread, to allow spawning thread to execute
PT_EXIT(pt);
// and indicate the end of the thread
PT_END(pt);
}