In this tutorial you will learn how to write your first BeRTOS application. We'll be using one of the cpu's currently support by BeRTOS, the ATMega1281. We will use the wizard to set up the project, then we'll set up debugging facilities and we'll print some output messages to the debug port.
Project setup
Download BeRTOS and launch the wizard. From cfg select debug. This will automatically include debug support in BeRTOS. Set the correct output port for your cpu and then create the project. Let's call our project hello_world.
The wizard creates the directory hello_world/, in which you see:
- bertos/: the whole bertos source tree;
- Makefile: the makefile for the whole project. To build the project, simply launch make;
- hello_world/: our project directory
- cfg/: directory with all the configuration files
- hw/: hardware specific files
- main.c: our application entry point
Coding
Open main.c and input:
#include "buildrev.h" #include <cfg/debug.h> int main(void) { IRQ_ENABLE; kdbg_init(); kprintf("Program build: %d\n", VERS_BUILD); kputs("Hello world!\n"); return 0; }
then compile the program; you will see some warnings but for now you can safely ignore them. Flash your board and reset. On the debugging serial you'll see these messages:
*** BeRTOS DBG START *** Program build: 17 Hello world!
Congratulations! You have built your first BeRTOS program!
Line by line tutorial
#include <cfg/debug.h>
The first line includes function prototypes. Since this is often included by other files, you can omit it in larger projects.
IRQ_ENABLE;
This line enables IRQs on your CPU. We don't need it for this simple example, but you will need for almost every other module in BeRTOS, so it's a good habit to learn as soon as possible.
kdbg_init();
This line initializes the debug subsystem, opening a serial port for debugging. The parameters for this port are in hello_world/cfg/cfg_debug.h.
kprintf("Program build: %d\n", VERS_BUILD);
kputs("Hello world!\n");
Writes a debug string on the output. You can also use a printf-like function to format the output. Be aware, though, that the delay introduced by printf is high, so use it sparingly.
To reduce footprint and cpu usage, BeRTOS implements different types of formatting for printf. You can select your formatting options modifying hello_world/cfg/cfg_formatwr.h. Remember the warnings we got on the first build? These were due to using full formatting on an underpowered cpu. To avoid the warnings you can change printf format option to PRINTF_NOFLOAT.
Flashing the image
Compiled files will be placed in the directory images/ of your project directory. You will find lots of formats, for example .elf, .s19, .hex. If you're flashing a firmware, the one you need to look for is .bin. There are various techniques to flash the firmware and these vary from cpu to cpu. Here we'll see how to flash a fairly common cpu, the AT91SAM7.
Example: flashing an AT91SAM7
By default, this cpu has a bootloader in rom that allows you to flash it either from usb o serial port. We don't need this facility, so we will switch it off. We need the openocd tool (which you can download from http://openocd.berlios.de/web/). Make the correct scripts (look at the examples in bertos/cpu/arm/scripts/), then use the command:
$ openocd -f [script]
when firmware is flashed, openocd will become a telnet-like server which you can connect to. Give these commands to set the protection off:
$ telnet localhost 4444 # this command will connect to openocd server > at91sam7 gpnvm 0 2 set > flash protect 0 0 1 off
To check if the cpu was correctly unlocked, use the following command:
> mdb 0xFFFFFF68 4 0xffffff68: 01 04 00 00
If the output (little endian) is the same as above, your cpu is correctly unlocked. Now re-flash the firmware and enjoy!
![(please configure the [header_logo] section in trac.ini)](/chrome/site/bertos_logo.png)