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