krnl 1
Loading...
Searching...
No Matches
k01myfirsttask.ino
Go to the documentation of this file.
1// 220216 -
2#include <krnl.h>
3
4struct k_t *pTask; // pointer to task descriptor
5
6// stak to be used by task
7#define STK 150
8unsigned char taskStak[STK];
9
10void t1() {
11 int cnt = 0;
12
13 while (1) // your task loop - forever
14 {
15 Serial.println(cnt++);
16 k_sleep(500);
17 }
18}
19
20void setup() {
21 int res;
22 Serial.begin(115200);
23 while (!Serial)
24 ;
25 Serial.println("k01myfirsttask");
26
27 pinMode(13, OUTPUT); // for debug
28
29 k_init(1, 0, 0); // init with space for one task
30 // ^------- number of message queues
31 // ^--------- number of semaphores
32 // ^------- number of tasks
33 // RETURNS 0 is all went ok
34
35 // priority low number higher priority than higher number
36 pTask =
37 k_crt_task(t1, 10, taskStak, STK); // t1 as task, priority 10, 100 B stak
38 // ^-- array used for stak
39 // ^--- stak size in bytes
40 // ^---------- task priority 1 is highest
41 // ^------------- function to be used as body for tas
42 // return NULL ptr if something went wrong
43
44 res = k_start(); // 1 milli sec tick
45 // you will never return from k_start
46 // if you come here is it bq krnl has not started due to an error
47 // ret vakue is negative and is a count of initalisation errors like
48 // no slot for tasks etc
49 // see krnl.c approx line ++1270
50
51 Serial.print("ups an error occured: ");
52 Serial.println(res);
53 while (1)
54 ;
55}
56
57void loop() {} // is NEVER used
58
59/*
60 * README README
61 * In task t1 we are waiting 500 msec in each loop by calling k_eat_msec
62 * This is busy waiting so t1 is running on CPU all time
63 * The debug k_break function below turn led13 on when dummy is running (dummey
64 * has pRun->nr == 0) But led 13 never light up - meaning 100% usage of cpu :-(
65 *
66 * Changing from k_eat_msec(500) to k_sleep(500) in t1 we still are waiting 500
67 * msec but now in passive mode So try do remove comment from k_sleep and
68 * comment out k_eat_msec Now you will observe that led13 is ON all time. Why?
69 * Because printing an integer takes nearly no time compared to sleeping in 500
70 * msec It you observe by a logic analyser on pin13 you will see it goes low for
71 * a very sjort amount of time and then high for 500 msec. So we dont use the
72 * CPU for just waiting -
73 *
74 * 2)
75 * Try to change k_eat_msec and k_sleep to (and no comment in from of no one ot
76 * them k_eat_msec(500); k_sleep(2000);
77 *
78 * Then will observe the led13 is off foo 500 msec (when printing and running
79 * k_eat_msec) and led13 is ON for 2000 msec(when we are sleeping in
80 * k_sleep(2000)
81 *
82 * You can see when your are printing becasue the led marked TX is ON when the
83 * uno is doing serial printing
84 */
85
86 /***
87
88extern "C" {
89
90void k_breakout(void) // called every task shift from dispatcher
91{
92
93 if (pRun->nr == 0) // 0 is dummy task - the eater of excessive CPU when all
94 // user tasks are idling
95 {
96 PORTB = PORTB | B00100000; // led13 (bit 5) on let the rest be untouched
97 } else {
98 PORTB = PORTB & B11011111; // led13 off uno
99 }
100}
101}
102
103// on MEGA led13 is on PORTB bit 7. so use B10000000 instead of B00100000 and
104// B011111111 instead of B11011111
105
106*/
unsigned char taskStak[150]
#define STK
struct k_t * pTask
void setup()
void t1()
void loop()
int k_sleep(int time)
let task sleep for a number of milliseconds
Definition krnl.c:445
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
create a task - only to be called before k_start creates a task and put it in the active Q
Definition krnl.c:328
int k_init(int nrTask, int nrSem, int nrMsg)
Definition krnl.c:1108
int k_start()
Definition krnl.c:1149
volatile int cnt
struct k_t * t1
Definition krnlgen.ino:3
Definition krnl.h:323