Wiki

Clone wiki

ArdOS / build

#Building ArdOS Applications

ArdOS is written primarily for use within the AVR-GCC and Atmel Studio environments but can also be used within the Arduino IDE.

Atmel Studio is however a much richer and more powerful environment than the Arduino IDE, providing an integrated debugger, hardware simulator, disassembly view, watches, breakpoints, etc. It is a far superior environment for developing Arduino applications than the Arduino IDE.

In this section we will look at:

  • How ArdOS files are organized.
  • Setting up ArdOS within the Arduino IDE.
  • Setting up ArdOS within Atmel Studio.
  • Setting up and using the Arduino libraries within Atmel Studio

ArdOS is currently only available for Arduino boards based on the ATmega128 and ATmega328.

How ArdOS files are Organized

The main ArdOS kernel is distributed across two directories: source and include.

The Source Directory

The source directory contains the main C files for all the parts of the kernel. Not all of these are required in your project.

The Main Files

These are the files that you will need for all projects

  • ardio.c: Contains all the routines needed for analog and digital I/O.
  • task.c: Contains task management routines.
  • kernel.c: Contains the main core of ArdOS, including the schedulers and the system clock handler.

Optional Features

These files are needed only if you use their respecive features. See the Configuring ArdOS section to see how to configure ArdOS to use these services.

  • mutex.c: Provides mutex lock and conditional variable services.
  • queue.c: Provides FIFO and priority message queues.
  • sema.c: Provides semaphores.

The Include Directory

These contain the include files required by ArdOS. The include file ArdOS.h automatically brings in all of the *.h files in this directory.

The Demo Directory

This directory contains 3 files:

  • testSwap.c : Demonstrates how to use semaphores to coordinate tasks.
  • testMutex.c : Demonstrates mutex locks and conditional variables.
  • testISRSema.c : Demonstrates how to correctly use unblocking calls within interrupt service routines, by suspending and resuming the scheduler.

Setting up ArdOS within the Arduino IDE.

Keying in the Arduino Sketch

Start the Arduino IDE and key in the following code. this program flashes the LED at pin 6 five times, then the LED at pin 9 five times, and repeats. It uses two semaphores to coordinate the tasks to achieve this.

extern "C"
{
#include "ArdOS.h"
}

TOSSema sema1, sema2;

void task1(void *p)
{
    while(1)
    {
        for(int i=0; i<5; i++)
        {
            digitalWrite(6, HIGH);
            OSSleep(125);
            digitalWrite(6, LOW);
            OSSleep(125);
        }

        // Release sema2
        OSGiveSema(&sema2);

        // Wait on sema1
        OSTakeSema(&sema1);
    }
}

void task2(void *p)
{
    while(1)
    {
        // Wait on sema2
        OSTakeSema(&sema2);

        for(int i=0; i<5; i++)
        {
            digitalWrite(9, HIGH);
            OSSleep(250);
            digitalWrite(9, LOW);
            OSSleep(250);
        }

        // Release sema1
        OSGiveSema(&sema1);
    }
}

unsigned long t1s[30], t2s[30];

void setup()
{
    OSInit();

    OSInitSema(&sema1, 0, 1);
    OSInitSema(&sema2, 0, 1);

    pinMode(6, OUTPUT);
    pinMode(9, OUTPUT);

    OSCreateTask(0, &t1s[29], task1, NULL);
    OSCreateTask(1, &t2s[29], task2, NULL);

    // Start the OS
    OSRun();
}

void loop()
{
    // Empty
}

Adding in the ArdOS Source Files

The Arduino IDE requires you to add the header (.h) and source (.c) files that are used by your application into the Arduino sketch.

To do this, click Sketch->Add File, then navigate to the directory containing the ArdOS kernel source (e.g. C:\ArdOS\source), and add in the following files one at a time:

  • kernel.c
  • task.c

These are the two core files in ArdOS and are used in every project created in the Arduino IDE. There is no need to add ardio.c as Arduino provides its own library to read/write the digital and analog pins.

Our current sketch uses semaphores, so you must also add:

  • sema.c

If your project uses mutex locks and/or queues, you must add mutex.c and/or queue.c using Sketch->Add File.

All the added files will appear in your Arduino IDE as separate tabs.

Adding in the ArdOS Header Files

The Arduino IDE requires you to add in all the header files into the sketch. To do so, click Sketch->Add File, then navigate to the directory containing the ArdOS header files (e.g. C:\ArdOS\include), then add in all the *.h files in this directory:

  • ardio.h
  • ArdOS.h
  • ArdOSConfig.h
  • kernel.h
  • mutex.h
  • queue.h
  • sema.h
  • task.h

Disabling Arduino Routines in ArdOS

For convenience ArdOS provides its own pinMode, digitalRead, digitalWrite, analogRead and analogWrite functions. When used within the Arduino IDE these will conflict with the routines provided by Arduino. To prevent this, click on the tab that says "ArdConfig.h", and set the USEIO paramter to 0, and as mentioned earlier, do not include the ardio.c module in the Arduino sketch.

Compiling the Sketch

Once you have added all the Arduino source and header files, you can save your sketch as "semademo". Your Arduino IDE will look like this:

Arduino IDE

Now click on the Upload button (the one that looks like -> ). If all goes well the sketch will get compiled and uploaded to the Arduino.

Setting up ArdOS within Atmel Studio.

If you do not currently use Atmel Studio, now is a good time to start. The current version is Atmel Studio 6, which you can obtain from the Atmel site. Detailed instructions are provided here on how to set up ArdOS in Atmel Studio 6.

You are encouraged to use the analog and digital input/output routines provided by ArdOS. However if you want to compile the Arduino libraries for Atmel Studio 6, please see here. If you use Arduino libraries be sure to set USEIO in ArdOSConfig.h to 0.

We will assume that you are using an Arduino UNO board with an Atmel ATmega328P microcontroller on it. In addition it is assumed that you have unpacked ArdOS into a directory called C:\ArdOS.

You should wire up a red LED to digital pin 6 and a yellow LED to digital pin 9, taking care to use 330 ohm resistors on the GND lead to prevent burning out your LEDs.

Create an Atmega Project

Start up AS6. Click File->New->Project. This brings up the dialog box shown below.

Create new project

Under "Name" enter "ardemo".

Click OK. This will bring up the Device Selection Box shown below:

Under "Device Family" choose megaAVR, 8-bit, then in the list below choose ATmega328P.

Choose Device

This will bring up an empty project like the one shown here:

Empty Project

Adding the ArdOS source files and Include Directories

In the Solution Explorer window on the right, right click on the "ardemo" icon, and choose Add->Existing item.

When the Add Existing Item dialog box comes up, navigate to c:\ArdOS\source and add in ardio.c, kernel.c, sema.c and task.c.

Add to Project

We now need to add in the include directory so that Atmel Studio can find all the header files.

To do this, select Project->ardemo Properties. This brings up the following dialog box. Click "Toolchain" the tabs on the left.

Project Properties

Now under AVR/GNU C Compilter, click on Directories (you might have to click on AVR/GNU C Compiler first to expand the option tree if it is folded up).

Arduino Includes

Above the "Include Paths (-I)" text box click on the icon with the green + sign. This will bring up the "Add Include Paths (-I)" dialog. Enter "c:\ArdOS\include" as shown below. You can uncheck the "Relative Path" checkbox.

Arduino Include Dialog

Click OK.

Configuring ArdOS

Navigate to the include directory (in our example it is c:\ArdOS\include) and open the ArdOSConfig.h file. Set the following parameters to the values indicated:

  • OS_DEBUG 0
  • OS_PREEMPTIVE 1
  • OSSCHED_TYPE OS_PRIORITY
  • OSTASK_COUNT 2
  • OSUSE_SLEEP 1
  • OSUSE_SEMA 1
  • USEIO 1
  • USEGPIO 1
  • USEARDGPIO 1

The following features are not needed and can be set to 0 to save memory.

  • OSUSE_QUEUES 0
  • OSUSE_PRIOQUEUES 0
  • OSUSE_MUTEXES 0
  • OSUSE_CONDITIONALS 0
  • USEPWM 0
  • USEARDPWM 0
  • PIN5PWM 0
  • PIN6PWM 0
  • PIN9PWM 0
  • PIN10PWM 0
  • PIN12PWM 0

A section of ArdOSConfig.h is shown here:

ArdOS Configuration

Keying in the Application Program

Get back to your source code by clicking on the "ardemo.c" tab. Erase any code that's already there. Enter the following code, which will use semaphores to cause LEDs connected to pins 6 and 9 to take turns to flash 5 times each repeatedly.

#include <stdlib.h>
#include <ArdOS.h>

TOSSema sema1, sema2;

void task1(void *p)
{
    while(1)
    {
        for(int i=0; i<5; i++)
        {
            digitalWrite(6, HIGH);
            OSSleep(125);
            digitalWrite(6, LOW);
            OSSleep(125);
        }

        // Release sema2
        OSGiveSema(&sema2);

        // Wait on sema1
        OSTakeSema(&sema1);
    }
}

void task2(void *p)
{
    while(1)
    {
        // Wait on sema2
        OSTakeSema(&sema2);

        for(int i=0; i<5; i++)
        {
            digitalWrite(9, HIGH);
            OSSleep(250);
            digitalWrite(9, LOW);
            OSSleep(250);
        }

        // Release sema1
        OSGiveSema(&sema1);
    }
}

unsigned long t1s[30], t2s[30];

int main()
{
    OSInit();

    OSInitSema(&sema1, 0, 1);
    OSInitSema(&sema2, 0, 1);

    pinMode(6, OUTPUT);
    pinMode(9, OUTPUT);

    OSCreateTask(0, &t1s[29], task1, NULL);
    OSCreateTask(1, &t2s[29], task2, NULL);

    // Start the OS
    OSRun();

    // Never reach here
    return 0;
}

Now press F7 or Build->Build Solution to compile, fixing any errors. You may get warnings that say "cast from pointer to integer of different size [-Wpointer-to-int-cast]", but ignore these.

Compiling Ardemo

Uploading to the Arduino

You can upload using avrdude, but if you don't want to muck about with a command-line interface, you can also use XLoader, an excellent tool that simplifies uploading greatly.

After downloading XLoader start it up and you will get a screen that looks like this:

The XLoader Screen

Click on the "..." button, and navigate to the file containing the compiled "hex" code to be uploaded. In my case it was on Documents\Atmel Studio\6.1\ardemo\ardemo\debug". Click on the file that says ardemo.hex:

Loading Hex File

Click Open. Under "Device" choose the appropriate type of Arduino board that you have (in my case it is an Uno), and choose the correct serial port under COM port and a baud rate of 115200. Now click the Upload button and watch the magic work!

Setting Up and Using the Arduino Libraries in Atmel Studio.

We recommend that you use the hardware access layers that are provided in ArdOS. However if you wish to use routines in the standard Arduino library from within Atmel Studio 6, you can find detailed instructions here.

You must set USEARDGPIO, USEARDADC and USEARDPWM within ArdOSConfig.h to 0.


Go home

Configuring ArdOS

ArdOS I/O Access Calls

ArdOS Library Calls

Updated