Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 2

The first part goes here


Now that I have understood the concept of threads, I will move on to something called ProtoThreads. Here is the basic code.



//To use protothreads (PT)
//first are foremost you need the header file, 

#include "pt.h"

//Now we have to initialize a PT, which is simply:

struct pt pt; 


//-------------------------------------------------------
//-------------------------------------------------------

//Last time we used PROCESS_THREAD(basic, ev, data) 
//but now we change to PT_THREAD
//'Example' is the name of process being declared
PT_THREAD(example(struct pt *pt)) 
{
PT_BEGIN(pt);
    
    /*
    Have to include code here what we want to do
    This portion lets the protothread wait for events indefinitely.
    It uses an infinite loop
    */

PT_END(pt);
}


/*
In memory constrained systems, traditional multi-threading may 
have a too large memory overhead. Each thread requires its own 
stack that may use large parts of the available memory. 
*/


/*That is where the ProtoThread solution proposed by the creator of Contiki is helpful. ProtoThreads 
 do not require their own stack. 
Each protothread takes two bytes of memory.
One thing to be careful is to limit use of local variables here.
And do not use SWITCH() statement ever.
*/




What I did was save this code as a .c file and dumped it in the Z1 examples folder and tried to compile it on a z1 mote. An error came up:

(Output truncated)
obj_z1/contiki-z1-main.o: In function `print_processes':
/home/user/contiki-2.6/examples/z1/../../platform/z1/./contiki-z1-main.c:169: undefined reference to `autostart_processes'
obj_z1/contiki-z1-main.o: In function `main':
/home/user/contiki-2.6/examples/z1/../../platform/z1/./contiki-z1-main.c:415: undefined reference to `autostart_processes'
collect2: ld returned 1 exit status
rm obj_z1/contiki-z1-main.o basicTemplate_ProtoThreads.co
Process returned error code 2
make: *** [basicTemplate_ProtoThreads.z1] Error 1


I dont know (yet) the working of the internals of Contiki, but I got the code working (compiling incorrectly but still ).

I just added this towards the end

AUTOSTART_PROCESSES(&basic);  

And it worked.. I need to research more why this happens, or if there is a suitable function for protoThreads that can be used to replace, or if I am just missing something in the big picture.


Will update it here once I find out.



Part Three can be found here.
comments