タイムスライスなしのシンプルなタスクライブラリ。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 |
/* * 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 setup(void) { Task::start(task0); Task::start(task1); } void loop(void) { } |
【ライブラリ】
|
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 |
/* 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_SIZE(n) ((n) + Task::TCB_SIZE) 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; sreg_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 = TASK_STACK_SIZE(CONFIG_TASK_STACK_SIZE)); static bool start(func_t func, void *arg = 0, int size = TASK_STACK_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); private: static tcb_t *_disp; static tcb_t *_curr; static tcb_t *_head; static tcb_t *_tail[2]; static size_t _used; }; 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 |
/* 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[]; size_t Task::_used; void Task::begin(size_t size) { /* Registration of main task */ static tcb_t tcb; _curr = &tcb; _used = size; AlarmTimer.begin(); } bool Task::start(func_t func, void *arg, int size, void *stack) { tcb_t *tcb; if (size < 0) { /* Use the current stack area */ size_t n = - size; size = _used; stack = 0; _used += n; } else if (stack) { /* Set the stack area to the variable area */ size = (char *)&tcb - (char *)stack - size + sizeof(*tcb); stack = 0; } else { /* Dynamically allocate stack area */ stack = malloc(size); if (!stack) return false; size = (char *)&tcb - (char *)stack - size + sizeof(*tcb); } tcb = (tcb_t *)alloca(size); tcb->alloc = stack; 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) { sreg_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) { sreg_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; sreg_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.handle(); 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.handle(); } } 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; } } 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; sreg_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) { sreg_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) { sreg_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 |
【関連投稿】
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)
