TASKS

k_crt_task

Ret values

  • ok - pointer which refers to krnl element

  • NULL - not ok - could NOT create task

#define STK 100
char s1[STK]; // your are stack supplier
struct k_t * p_t1;

void t1() {
  while (1) {
  }
}
...
p_t1 = k_crt_task(t1, 10, s1, STK); // code, 0 < prio < DMY_PRIO, s1: array of bytes, STK: 60 < nrofbytes

k_set_prio:

Ret values

  • pos value: my new priority

  • 1: not ok - priority outside legal interval: (prio <= 0) || (DMY_PRIO <= prio)

    • DMY_PRIO is 100

  • 2: not ok - krnl is not running yet

k_sleep:

Parm tp int - Legal 0 < val < 32000 Ret values

  • 0: ok

  • 1: not ok - no sleep - (time <=0)

.. K_sleep(34); // sleep for 34 ticks

k_round_robbin

  k_round_robbin();
  // reinsert caller so a round robbin situation is issued

k_release

In non preemptive setup k_release switch running to the task in front of activeQ

k_unused_stak

Ret values

  • returns nr of unused bytes on your or another tasks stak NB not check if reference to task is legal etc

int unusedStak;

unusedStak = k_unused_stak(NULL); // myself
or
 struct k_t *anotherTask;

unusedStak = k_unused_stak(anotherTask);

SEMAPHORES

k_crt_sem

Ret values

  • 0: ok

  • NULL: if (init_val < 0) || (SEM_MAX_VALUE < init_val) || (maxvalue < 0) || (SEM_MAX_VALUE < maxvalue)

struct k_t *sem1;
...
  sem1 = k_crt_sem(1, 10); // 1: start value, 10: max value (clipping)

k_signal, ki_signal

Ret values

  • 0: ok - signal delivered, a task was waiting for it

  • 1: ok - signal delivered, no body waiting for it

  • 1: not ok: no signal delivered to to clip (sem value is at max see k_sem_crt)

struct k_t *sem;

...
if (0 <= ksignal(sem) ) {
  // signal went ok
}

k_wait,ki_wait

Ret values0: ok - has been block bef signal arrived at semaphore

  • 1: ok - wait done - a signal was waiting so no blocking

  • 1: not ok - timeout occured when waiting on signal

  • 2: not ok - no wait done bq waiting by block not allowed - parm timeout <0

struct k_t *sem;
int res;

  res = k_wait(sem,0);  //0: we will wait forever until we get a signal
or
  res = k_wait(sem,10);  //0: we will wait max 10 krnl ticks for a signal
or
  res = k_wait(sem,-1);  //0: we will not wait if no signal is present

k_set_sem_timer:

Ret values

  • 0: ok

  • 1: not ok - (val < 0) (timer value)

struct k_t * sem;
  k_set_sem_timer(sem,10); // now krnl will signal every 10 krnl tick to semaphore
  // krnl must be running before it will work

k_sem_val, ki_sem_val

  • return semaphore value

  • ki version do not enable interrupt again

struct k_t * sem;
int semVal;

semVal = k_semval(sem);

k_wait_lost

Ret values

  • TODO 0<= int : number of missed signals on semaphore due to clipping. Counter is reset to 0

struct k_t * sem;
int nrLost;

nrLost = k_wait_lost(sem);

MESSAGE PASSING

k_crt_send_Q

Ret values

  • NULL : error

  • not NULL ptr , ref to message Q

struct k_msg_t *pMsg,*pMsg2;
char mar[10*2];
...
pMsg = k_crt_send_Q(10,2,mar);  // 10: nr elem, 2: size in bytes, mar 10*2 "bytes"

ki_send,k_send


struct k_msg_t *pMsg;
char mar[10*2];
... before k_start
 pMsg = k_crt_send_Q(10,2,mar); // crt a buffer with 10 elements each 2 byte size (std int in Arduino)

... after k_start

.. sender

int value=3
 k_send(pMsg,(void *)&value);

.. receiver
int destVar;
int lostMesg;
  if (0 <= k_receive(pMsg,&destVar,10,&lostMsg) ) { // third parm 10 = timeout which is forever
      //got it
      // lostMsg hold number of lost messages since last call with this parm NOT eq NULL. Counter is reset
    }

... or
  k_receive(pMsg,&destVar,0,NULL) ) { // third parm 0 = timeout which is forever
  // we know we will be in receive until data arrives. NULL says we dont want to have info about lost messages

ki_receive,k_receive

see above

k_msg_count, ki_msg_count

Returns nr of messages waiting

int count;
...
struct k_msg_t *m;
count = k_msg_count(m);

Ceiling Mutex

k_mut_ceil_set

Sets priority in semaphore for immediate ceiling protocol (critical region) returns

  • 0: ok

  • 1: ceiling value outside legal interval (prio <= 0) || (DMY_PRIO <= prio)

  • 2: krnl running - only legal before k_start

struct k_t *mutex;
 -- before k_start !
 mutex = k_crt_sem(1,10); // initalvalue(1) and max clip value
k_mut_ceil_set(sem,50); // semaphore and max prio for ceil protocol

k_mut_ceil_enter

pendant to k_wait just with immediate ceiling protocol

  • return values like k_wait

struct k_t * mut;

if ( 0 <= k_mut_ceil_enter(mut,10)) {  // max 10 tick wating in Q !
  // yes we are now inside
  // mutex region code
  k_mut_leave(mut);
}
else {
  // did not get mutex bq of timeout
}
...

k_mut_ceil_leave

pendant to signal

  • return values like k_signal

freeRam

return nr of unused bytes (between heap and orginal stack - they are growing towards each other)

int memoryleft

memoryleft = freeRam();