Tuesday, February 7, 2012

Adding a custom device driver to Contiki OS

While working on various project works on sensor networks, we had a requirement to attach our own devices with sensor motes for different purposes. To do it we have to write low level drivers which will access the pins of the MCU and perform various tasks. If our applications are running on Contiki operating system, then we have to add our drivers to Contiki. In this post, I write the methodology we followed to do it.        
    This may not be the proper way of adding device drivers for Contiki. However this is the way we did it. Lets say we have two driver files named as my_driver.c and my_driver.h which are some drivers for Tmote Sky platform. The C file contains some functions of the driver and the header file contains the prototypes of the functions in C file. The contents of these files may be like follows. (please note that this example function calls the LED driver functions without doing something its own. Depending on your application it may contain some low level codes)

my_driver.c content

1:  #include "dev/leds.h"  
2:  void driver_function(){  
3:       leds_on(LEDS_RED);  
4:  }  

my_driver.h content

1:  void driver_function();  

 First we should copy these two files to Contiki/core/dev/ directory. Then open the Makefile.sky file which resides in the Contiki/platform/sky directory. There's an entry in this file which specify some C file names separated by spaces. Append our drivers C file name to this entry.

    Now open the file contiki-sky-main.c which resides in the same directory (Contiki/platform/sky) and add the following header file inclusion to it.

1:  #include "dev/my_driver.h"  

Now our driver is correctly placed inside the Contiki source code and therefore we can use it in our applications. To test this functionality, call the driver_function() within the Hello-World example application in Contiki/examples/hello-world directory and run it on MSPSIM simulator by issuing the following command.

make TARGET=sky hello-world.mspsim

    If every things went fine, the simulators red LED should be switched on by our drivers driver_function().



No comments:

Post a Comment