タスクqrhタイムスライスなしのシンプルなタスクライブラリ。yield()を実行したときだけタスク切替する2プライオリティのラウンドロビン型スケジューリングを行う。休止状態からの起床時に優先度が一時的に高くなる。
【avr8_config.h】
|
1 2 3 4 |
/* Taskクラスを使う設定 */ #define CONFIG_TASK_USE 1 /* Taskスタックサイズ指定省略時のスタックサイズ */ #define CONFIG_TASK_STACK_SIZE 256 |
【サンプルコード (Microchip Studio – Arduino風)】
|
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 |
/* * Task Sample Program * */ #include "avr8_task.h" void task0(void *arg) { (void)arg; uint32_t tick = Timer.us2tick(500000); while (1) { print("task0\n"); Task::sleep(tick); } } void task1(void *arg) { (void)arg; uint32_t tick = Timer.us2tick(500000); while (1) { print("task1\n"); Task::sleep(tick); } } void task2(void *arg) { (void)arg; uint32_t tick = Timer.us2tick(500000); while (1) { print("task2\n"); Task::sleep(tick); } } static char task1_stack[TASK_STACK_MEM_SIZE(CONFIG_TASK_STACK_SIZE)]; void setup(void) { /* 1) メインスタック領域を刻んでスタックに指定する */ Task::start(task0, 0, -CONFIG_TASK_STACK_SIZE); /* 2) スタティック配列領域をスタックに指定する */ Task::start(task1, 0, sizeof(task1_stack), task1_stack); /* 3) malloc()で割り当てた動的メモリ領域をスタックに指定する */ Task::start(task2, 0, CONFIG_TASK_STACK_SIZE); or Task::start(task2); } void loop(void) { } |
【修正履歴】
2025-12-25
msgboxなどに使えるavr8_fifo.hを追加。
2025-12-23
一時的にタスクスイッチングを止めるためのdisableAndSaveYields()/restoreYields()を追加。
2025-12-20
コードをよりシンプルにするためタスク生成時のスタック領域のレイアウトを変更。
「旧」
———– stack top (SP)
スタック領域
———–
(TCB)
———– stack bottom
「新」
———– stack top
(TCB)
———– (SP)
スタック領域
———– stack bottom
2025-12-19
メインタスク以外からからスタック領域を刻む方法でタスク生成すると誤動作するため修正。
【ライブラリ】
|
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 |
/* avr8_task.h - Task Library for Microchip AVR8 Series Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include <stddef.h> #include <stdint.h> #include <stdbool.h> #include <setjmp.h> #include "avr8_alarm.h" #if CONFIG_TASK_USE #define TASK_STACK_MEM_SIZE(n) ((n) + Task::TCB_SIZE + 4) class Task : public TimerClass { public: typedef void (*func_t)(void *arg); protected: typedef struct tcb { struct tcb *next; jmp_buf context; void *alloc; void *arg; func_t start; } tcb_t; typedef struct { Alarm alarm; state_t state; bool retval; } ctrl_t; static void enqueue0(tcb_t *tcb); static void enqueue1(tcb_t *tcb); static tcb_t *dequeue(void); static void switching(void); static void dispatch(void); public: static const int TCB_SIZE = sizeof(tcb_t); static void begin(size_t size = CONFIG_TASK_STACK_SIZE); static bool start(func_t func, void *arg = 0, int size = CONFIG_TASK_STACK_SIZE, void *stack = 0); static void stop(void); static bool yield(void); static size_t current(void); static void sleep(int32_t tick, uint8_t busyLoop = 0); static void delay(uint32_t tick, uint8_t busyLoop = 0); static bool disableAndSaveYields(void); static void restoreYields(bool state); private: static tcb_t *_disp; static tcb_t *_curr; static tcb_t *_head; static tcb_t *_tail[2]; static char *_stack; static bool _yield; }; class Sync : protected Task { public: Sync(void); bool sleep(int32_t timeout = -1); size_t wakeup(size_t task = 0); protected: bool sleep(ctrl_t& ctrl); static bool timeup(Alarm& alarm); tcb_t *_head; tcb_t *_tail; }; class Mutex : protected Sync { public: Mutex(void); bool lock(int32_t timeout = -1); void unlock(void); protected: size_t _owner; size_t _count; }; class Sem : protected Sync { public: Sem(size_t limit = (size_t)-1, size_t count = 0); bool wait(size_t count = 1, int32_t timeout = -1); void post(size_t count = 1); protected: size_t _limit; size_t _count; size_t _wait; }; class Event : protected Sem { protected: Event(void); bool wait(int32_t timeout = -1); void signal(void); }; #endif |
|
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
/* avr8_task.cpp - Task Library for Microchip AVR8 Series Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include <stdlib.h> #if !defined(alloca) #include <alloca.h> #endif #include "avr8_task.h" #if CONFIG_TASK_USE Task::tcb_t *Task::_disp; Task::tcb_t *Task::_curr; Task::tcb_t *Task::_head; Task::tcb_t *Task::_tail[]; char *Task::_stack; bool Task::_yield = true; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" #pragma GCC diagnostic ignored "-Wdangling-pointer" void Task::begin(size_t size) { static tcb_t tcb; char sp; /* Registration of main task */ _curr = &tcb; _stack = &sp - size; AlarmTimer.begin(); } #pragma GCC diagnostic pop bool Task::start(func_t func, void *arg, int size, void *stack) { void *alloc = 0; if (!_curr) return false; /* not initialized */ if (size < 0) { /* Set stack by cutting current stack area */ stack = _stack; _stack -= -size + sizeof(tcb_t) + 4; } else if (stack) { /* Set the stack in the variable area */ stack = (char *)stack + size; } else { /* Set the stack to the allocated memory area */ size = size + sizeof(tcb_t) + 4; if (!(alloc = malloc(size))) return false; stack = (char *)alloc + size; } stack = (char *)stack - sizeof(tcb_t) - 4; tcb_t *tcb = (tcb_t *)((char *)alloca((char *)&alloc - (char *)stack) + 4); tcb->alloc = alloc; tcb->arg = arg; tcb->start = func; enqueue0(tcb); if (setjmp(tcb->context)) { _curr = _disp; _curr->start(_curr->arg); stop(); } return true; } void Task::stop(void) { if (_curr->start) { if (_curr->alloc) free(_curr->alloc); dispatch(); } } void Task::enqueue0(tcb_t *tcb) { state_t state = disableAndSaveInterrupts(); if (_tail[0]) { tcb->next = _tail[0]->next; _tail[0] = (_tail[0]->next = tcb); } else { tcb->next = _head; _tail[0] = (_head = tcb); } restoreInterrupts(state); } void Task::enqueue1(tcb_t *tcb) { state_t state = disableAndSaveInterrupts(); (_tail[1] = *(_tail[1] ? &_tail[1]->next : (_tail[0] ? &_tail[0]->next : &_head)) = tcb)->next = 0; restoreInterrupts(state); } Task::tcb_t *Task::dequeue(void) { tcb_t *tcb; state_t state = disableAndSaveInterrupts(); if ((tcb = _head)) { if (_head == _tail[0]) _tail[0] = 0; else if (_head == _tail[1]) _tail[1] = 0; _head = _head->next; } restoreInterrupts(state); return tcb; } size_t Task::current(void) { return (size_t)_curr; } bool Task::yield(void) { AlarmTimer.poll(); if (_yield) { enqueue1(_curr); switching(); } return !_curr->start; } void Task::switching(void) { if (!setjmp(_curr->context)) dispatch(); _curr = _disp; } void Task::dispatch(void) { while (1) { if ((_disp = dequeue())) longjmp(_disp->context, 1); idle(); AlarmTimer.poll(); } } void Task::sleep(int32_t tick, uint8_t busyLoop) { uint32_t t = Timer.read(); if (tick > busyLoop) { Sync sync; sync.sleep(tick -= busyLoop); t += tick; } if (busyLoop) { while (Timer.read() - t < busyLoop) continue; } } void Task::delay(uint32_t tick, uint8_t busyLoop) { uint32_t t = Timer.read(); if (tick > busyLoop) { tick -= busyLoop; while (Timer.read() - t < tick) yield(); t += tick; } if (busyLoop) { while (Timer.read() - t < busyLoop) continue; } } bool Task::disableAndSaveYields(void) { bool state = _yield; _yield = false; return state; } void Task::restoreYields(bool state) { _yield = state; } Sync::Sync(void) : _head(0), _tail(0) { } bool Sync::timeup(Alarm& alarm) { if (((Sync *)alarm.param(0))->wakeup((size_t)alarm.param(1))) *(bool *)alarm.param(2) = false; return false; } bool Sync::sleep(ctrl_t& ctrl) { tcb_t *tcb = (tcb_t *)current(); (_tail = *(_tail ? &_tail->next : &_head) = tcb)->next = 0; restoreInterrupts(ctrl.state); ctrl.alarm.param(0, this); ctrl.alarm.param(1, tcb); ctrl.alarm.param(2, &ctrl.retval); ctrl.alarm.handler(timeup); AlarmTimer.start(ctrl.alarm); switching(); return ctrl.retval; } bool Sync::sleep(int32_t timeout) { if (timeout == 0) return false; ctrl_t ctrl; ctrl.alarm.interval(timeout); ctrl.retval = true; ctrl.state = disableAndSaveInterrupts(); return sleep(ctrl); } size_t Sync::wakeup(size_t task) { tcb_t *tcb = 0; state_t state = disableAndSaveInterrupts(); if (task) { for (tcb_t **p = &_head; *p; p = &(*p)->next) { if (*p == (tcb_t *)task) { if (!(*p = (*p)->next)) _tail = (p == &_head ? 0 : (tcb_t *)p); tcb = (tcb_t *)task; break; } } } else _head = ((tcb = _head) != _tail ? _head->next : _tail = 0); restoreInterrupts(state); if (tcb) { AlarmTimer.cancel(1, tcb); enqueue0(tcb); } return (size_t)tcb; } Mutex::Mutex(void) : _owner(0) { } bool Mutex::lock(int32_t timeout) { ctrl_t ctrl; ctrl.alarm.interval(timeout); ctrl.retval = true; ctrl.state = disableAndSaveInterrupts(); size_t task = current(); if (!_owner) { _owner = task; _count = 1; } else if (_owner == task) ++_count; else if (timeout == 0) ctrl.retval = false; else return sleep(ctrl); restoreInterrupts(ctrl.state); return ctrl.retval; } void Mutex::unlock(void) { state_t state = disableAndSaveInterrupts(); if (_owner == current()) { if (--_count == 0) { _owner = wakeup(); _count = 1; } } restoreInterrupts(state); } Sem::Sem(size_t limit, size_t count) : _limit(limit), _count(count), _wait(0) { } bool Sem::wait(size_t count, int32_t timeout) { ctrl_t ctrl; ctrl.alarm.interval(timeout); ctrl.retval = true; ctrl.state = disableAndSaveInterrupts(); if ((count == 0) || (count > _limit)) ctrl.retval = false; else if (!_wait && (_count >= count)) _count -= count; else if (timeout == 0) ctrl.retval = false; else { ++_wait; ((tcb_t *)current())->arg = (void *)count; sleep(ctrl); disableAndSaveInterrupts(); --_wait; while (_head) { count = (size_t)_head->arg; if (_count < count) break; _count -= count; wakeup(); } } restoreInterrupts(ctrl.state); return ctrl.retval; } void Sem::post(size_t count) { state_t state = disableAndSaveInterrupts(); if (count && (count + _count <= _limit)) { _count += count; if (_head) { count = (size_t)_head->arg; if (_count >= count) { _count -= count; wakeup(); } } } restoreInterrupts(state); } Event::Event(void) : Sem(1) { } bool Event::wait(int32_t timeout) { return Sem::wait(1, timeout); } void Event::signal(void) { Sem::post(); } #endif |
|
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 |
/* avr8_fifo.h - First-In First-Out Buffer Template Library Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include <string.h> #include "avr8_task.h" #define FIFO_NO_WAIT 0 #define FIFO_INFINITE_WAIT -1 #define FIFO_OVERWRITE -2 template<typename T, size_t SIZE> class Fifo : protected Sync { private: size_t _wridx; size_t _rdidx; size_t _count; size_t _losts; T _buffer[SIZE]; void read(T *buf, size_t count) { _count -= count; if (_rdidx + count > SIZE) { size_t n = SIZE - _rdidx; memcpy(buf, _buffer + _rdidx, n * sizeof(T)); buf += n; count -= n; _rdidx = 0; } memcpy(buf, _buffer + _rdidx, count * sizeof(T)); _rdidx += count; } void write(const T *buf, size_t count) { _count += count; if (_wridx + count > SIZE) { size_t n = SIZE - _wridx; memcpy(_buffer + _wridx, buf, n * sizeof(T)); buf += n; count -= n; _wridx = 0; } memcpy(_buffer + _wridx, buf, count * sizeof(T)); _wridx += count; } public: Fifo(void) : _wridx(0) , _rdidx(0) , _count(0) , _losts(0) { } size_t available(void) { state_t state = disableAndSaveInterrupts(); size_t rv = _count; restoreInterrupts(state); return rv; } size_t availableForWrite(void) { return SIZE - available(); } size_t losts(void) { state_t state = disableAndSaveInterrupts(); size_t rv = _losts; _losts = 0; restoreInterrupts(state); return rv; } bool get(T *item, size_t count = 1, int32_t timeout = FIFO_INFINITE_WAIT) { size_t n, rv = 0; ctrl_t ctrl; while (count) { ctrl.alarm.interval(timeout); ctrl.retval = true; ctrl.state = disableAndSaveInterrupts(); if (_count) { n = count < _count ? count : _count; read(item, n); restoreInterrupts(ctrl.state); item += n; count -= n; rv += n; } else { if (timeout == FIFO_NO_WAIT) restoreInterrupts(ctrl.state); else if (sleep(ctrl)) continue; break; } } return rv; } size_t put(const T *item, size_t count = 1, int32_t timeout = FIFO_INFINITE_WAIT) { size_t n, rv = 0; Alarm alarm; alarm.interval(timeout); while (count) { state_t state = disableAndSaveInterrupts(); n = SIZE - _count; if (n) { if (count < n) n = count; write(item, n); restoreInterrupts(state); wakeup(); item += n; count -= n; rv += n; alarm.interval(timeout); } else if (timeout == FIFO_OVERWRITE) { n = _count < count ? _count : count; _count -= n; _losts += n; if ((_rdidx += n) >= SIZE) _rdidx -= SIZE; restoreInterrupts(state); } else { restoreInterrupts(state); if (timeout == FIFO_NO_WAIT) break; else if ((timeout > FIFO_NO_WAIT) && alarm.expire()) break; yield(); } } return rv; } }; |
【関連投稿】
Microchip AVR8 用のライブラリを自作する。(GPIO)
Microchip AVR8 用のライブラリを自作する。(FUSE)
Microchip AVR8 用のライブラリを自作する。(CLOCK)
Microchip AVR8 用のライブラリを自作する。(RESET)
Microchip AVR8 用のライブラリを自作する。(PORTMUX)
Microchip AVR8 用のライブラリを自作する。(USART)
Microchip AVR8 用のライブラリを自作する。(RTC)
Microchip AVR8 用のライブラリを自作する。(TCA)
Microchip AVR8 用のライブラリを自作する。(TCB)
Microchip AVR8 用のライブラリを自作する。(VREF)
Microchip AVR8 用のライブラリを自作する。(DAC)
Microchip AVR8 用のライブラリを自作する。(AC)
Microchip AVR8 用のライブラリを自作する。(ADC)
Microchip AVR8 用のライブラリを自作する。(ZCD)
Microchip AVR8 用のライブラリを自作する。(SPI)
Microchip AVR8 用のライブラリを自作する。(TWI)
Microchip AVR8 用のライブラリを自作する。(MAIN)
Microchip AVR8 用のライブラリを自作する。(CONFIG)
Microchip AVR8 用のライブラリを自作する。(ALARM)
Microchip AVR8 用のライブラリを自作する。(TASK)
Microchip AVR8 用のライブラリを自作する。(DOWNLOAD)
