MOS Applications live in the src/apps/ directory inside your mantis source folder (mantis-1.0-beta or mantis-unstable). The very first step is to create a folder to hold your new application. We'll call our new application 'myblink'.
cd ~/mantis-unstable/src/apps/; mkdir myblink
At this point, we're ready to start coding. Create a file called 'myblink.c' in your myblink directory, and open it with your favorite editor.
touch myblink.c ; emacs myblink.c
myblink.c:
#include "mos.h" // always include the mos header
#include "msched.h" // include the mantis scheduler
#include "led.h" // include functions to access the LEDs.
void blink_thread(void)
{
while(1)
{
mos_led_toggle(0); // toggle the value of the 1st LED
mos_thread_sleep(1000); // sleep for one second
}
}
// this is the entry point of our application.
void start(void)
{
// create a new blink_thread with 128 bytes of stack space and normal priority.
mos_thread_new(blink_thread, 128, PRIORITY_NORMAL);
}The next step is to create an optsconfig.h file which specifies which parts of MOS you can leave out (if you want to save code space).
optsconfig.h:
// build configuration options // uncomment/comment to enable/disable support for a device, // radio, or interface #include "build_opts.h" /* By default all devices and interfaces are defined in src/lib/include/build_opts.h. You can check this file for the list of possible build options. To disable a build option, use #undef Example: #undef MICA2_ACCEL #undef CC2420 #undef MAXSTREAM will disable the accelerometer, the cc2420 radio, and the maxstream radio drivers. An exception is changing CC1000 from CSMA to another protocol, where another must be defined. Ex: #undef CC1000_CSMA #define CC1000_TDMA */ #undef CC1000 #undef CC1000_CSMA #undef CC2420
In this example, we only disable support for the CC1000 and CC2420 radios.
Once you've entered the code above, it's time to move on to compiling and installing the application.
...page...
The first thing to do is to create an SConscript file in your application directory (we're using 'myblink') to indicate what files need to be compiled, and what to call the resulting program. If you're using an application that already has an SConscript file, you can skip creating it.
SConscript:
import glob
import os
import string
from scripts.build_support import *
Import ('*')
# This is a comment
# what to call the resulting program(s).
app_names_list = ~np~['myblink.elf']~/np~
# the list of the source files that need to be compiled to produce myblink
myblink_sources = ~np~['myblink.c']~/np~
# list all the source lists here
app_sources_list = ~np~[myblink_sources]~/np~
# app build methods
loadstat = ARGUMENTS.get('load', '0')
build_app_function(env, app_names_list, app_sources_list, loadstat)
Generally you can just copy this example SConscript and modify the three lists — you shouldn't have to change the three lines at the end or the import directives.
Now that SCons knows how to build our program, let's go ahead and compile it. From your application directory, issue the command:
scons -D platform=myplatform
where myplatform can be telosb, micaz, mica2, depending on the platform you'd like to build for.
Everything should compile (hopefully!) and you should see the message:
scons: done building targets.
Now we can move on to installing the program image.
...page...
The programs that scons built for us can be found in the build-platform directory in your application directory. For example, myblink/build-telosb/.
Here, the steps differ depending on which platform you built for in the previous step.
If you built for micaz or mica2, you should attach your node to the programming board, and issue the following command:
mos_shell -p myblink.srec
and reset the node. If the shell is stuck on "Searching for a sensor node..." you might need to experiment with the --sdev option to pass the correct serial port to the shell:
mos_shell -p myblink.srec --sdev /dev/ttyS2
Tip:
The Mica2/MicaZ Nodes need to be set up with the mos bootloader before you can program them. Use the "set_mos" script (passing it the serial device) to do this. You should only need to do this once.
If you built for telosb, the command is different; attach your telosb node to a USB port, and issue the command:
bsl.py --telosb -e -c /dev/ttyUSB0 -p myblink.elf
Again, your USB port number might be different. The node should erase and reprogram with your myblink program. Reset the node with the reset button, and myblink should run.
Tip:
The command to program a telosb node is kind of long. You might want to create an alias for it in your ~/.bashrc, like
alias tinstall='bsl.py --telosb -e -c /dev/ttyUSB0 -p '
Now you can use the command
tinstall myblink.elf
