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