An Arduino Library for Nil RTOS

Discussions and support about ChibiOS/NIL, the almost nil RTOS.
wgreiman
Posts: 33
Joined: Sun Oct 23, 2011 3:21 pm
Been thanked: 2 times

An Arduino Library for Nil RTOS

Postby wgreiman » Thu Jan 24, 2013 5:21 pm

I have posted an Arduino Library for Nil RTOS as NilRTOS_Preview20130124.zip http://code.google.com/p/rtoslibs/downloads/list.

This system seems ideal for small chips like the ATmega328. Nil RTOS is extremely simple so it is great for new users.

I have written a number of examples and a guide, "Understanding the Examples", to help new users.

Here is the first example new users should try. It is included in the above download and requires about 2150 bytes of flash on an Uno.

Code: Select all

/*
 * Example to demonstrate thread definition, semaphores, and thread sleep.
 */
#include <NilRTOS.h>

// The LED is attached to pin 13 on Arduino.
const uint8_t LED_PIN = 13;

// Declare a semaphore with an inital counter value of zero.
SEMAPHORE_DECL(sem, 0);
//------------------------------------------------------------------------------
/*
 * Thread 1, turn the LED off when signaled by thread 2.
 */
// Declare a stack with 128 bytes beyond context switch and interrupt needs.
NIL_WORKING_AREA(waThread1, 128);

// Declare the thread function for thread 1.
NIL_THREAD(Thread1, arg) {
  while (TRUE) {
   
    // Wait for signal from thread 2.
    nilSemWait(&sem);
   
    // Turn LED off.
    digitalWrite(LED_PIN, LOW);
  }
}
//------------------------------------------------------------------------------
/*
 * Thread 2, turn the LED on and signal thread 1 to turn the LED off.
 */
// Declare a stack with 128 bytes beyond context switch and interrupt needs.
NIL_WORKING_AREA(waThread2, 128);

// Declare the thread function for thread 2.
NIL_THREAD(Thread2, arg) {

  pinMode(LED_PIN, OUTPUT);
 
  while (TRUE) {
    // Turn LED on.
    digitalWrite(LED_PIN, HIGH);
   
    // Sleep for 200 milliseconds.
    nilThdSleepMilliseconds(200);
   
    // Signal thread 1 to turn LED off.
    nilSemSignal(&sem);
   
    // Sleep for 200 milliseconds.   
    nilThdSleepMilliseconds(200);
  }
}
//------------------------------------------------------------------------------
/*
 * Threads static table, one entry per thread.  A thread's priority is
 * determined by its position in the table with highest priority first.
 *
 * These threads start with a null argument.  A thread's name may also
 * be null to save RAM since the name is currently not used.
 */
NIL_THREADS_TABLE_BEGIN()
NIL_THREADS_TABLE_ENTRY("thread1", Thread1, NULL, waThread1, sizeof(waThread1))
NIL_THREADS_TABLE_ENTRY("thread2", Thread2, NULL, waThread2, sizeof(waThread2))
NIL_THREADS_TABLE_END()
//------------------------------------------------------------------------------
void setup() {
  // Start Nil RTOS.
  nilSysBegin();
}
//------------------------------------------------------------------------------
// Loop is the idle thread.  The idle thread must not invoke any
// kernel primitive able to change its state to not runnable.
void loop() {
  // Not used.
}


Mars
Posts: 5
Joined: Mon May 13, 2013 11:46 am

Re: An Arduino Library for Nil RTOS

Postby Mars » Mon May 13, 2013 12:19 pm

I just stumbled by accident on NilRTOS and this wonderfull Arduino port :o

Looking at the examples I truly hope that the development of this OS for Arduino continues!

I did try some sketches using Quantum Leap's State Framework (QF), but got lost in complexity and rework of very simple existing software / sketches.
What you examples show is that you bring back the simplicity Arduino was made for:
  • Simply blinking a LED is again just a few sequential statements, instead of a statemachine!
  • I can re-use most of the (again of course sequential) sketches / libraries I have. Of course some modifications are required (replace the delay() for instance), but that's all!

The only thing I miss is some kind of messaging/event handling, but I guess that isn't that hard to solve by implementing a simple publisch/subscribe mechanism that can 'queue' commands to a thread using the simple FIFO class. In that way a thread waits for the command semaphore, and then reads its commandqueue in order to to the specific work...

And last but not least: a question.
I'm using ITead IBoard Ex as platform (Atmega 32U4). These got XBee/NRF24L01/SDCard support (headers) onboard, including an integrated WS5100 LAN interface.
The examples show a working sdfat library which uses SPI. Does this mean that a NRF24L01+ tranceiver should also work with the current libraries using hardware SPI?
And what about the XBee (softwareserial?) and the WS5100 (softwarespi?) ??

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: An Arduino Library for Nil RTOS

Postby Giovanni » Mon May 13, 2013 1:50 pm

Hi,

The development on Nil will restart really soon now (tm), the general idea is to enhance it until its size reaches around 1KB max, I have to decide to which features give priority.

A mailbox mechanism like the one present in ChibiOS is one of the extensions I am considering, it would allow to send data between ISRs and Threads.

Giovanni

Mars
Posts: 5
Joined: Mon May 13, 2013 11:46 am

Re: An Arduino Library for Nil RTOS

Postby Mars » Mon May 13, 2013 3:39 pm

Thanx for that update, Giovanni!

As for the other questions (device/library compatibility), are you able to answer those questions, or should Bill Greiman do that 8-)

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: An Arduino Library for Nil RTOS

Postby Giovanni » Mon May 13, 2013 4:00 pm

Hi,

I don't know the specifics, I think Bill could answer, in general there should be no problems, as you write an RTOS allows to simply use existing code from a thread.

Giovanni

Mars
Posts: 5
Joined: Mon May 13, 2013 11:46 am

Re: An Arduino Library for Nil RTOS

Postby Mars » Wed May 15, 2013 1:44 pm

I've read some of the ChibiOS documentation and see that this OS has a lot of sync/event possibilities.
The event mechanism (single/multiple events) is very nice, but if I look at the code, it also looks rather a 'huge' addon considering ROM/RAM...

Is it possible that nilRTOS will support adding/removing functionality, so one can choose the options required?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: An Arduino Library for Nil RTOS

Postby Giovanni » Wed May 15, 2013 2:00 pm

Hi,

ChibiOS already does that being a more complex OS, relatively speaking it is 7KB large... while Nil is around 650bytes :)

Core space reduction is implicitly done by the linker, functions not called are removed by the garbage collector and do not take space in the final image. The only thing that could be saved would be optional fields in the OS structures but so far thee are no fields that are specific of a subsystem. This could change and #ifdefs will be added where appropriate like in ChibiOS.

Giovanni

Mars
Posts: 5
Joined: Mon May 13, 2013 11:46 am

Re: An Arduino Library for Nil RTOS

Postby Mars » Tue May 21, 2013 1:09 pm

Small update :)

I did some small / simple experiments on a 32u4 and Arduino IDE 1.04 / 1.05 and must say that I like NilRTOS alot!
At first I used the february libraries Bill made for AVR. This morning I saw that Bill already made some changes to the AVR port due to changes in IDE 1.05. Nice!

I do hope that the combined effort of you guys will make NilRTOS the ultimate small RTOS for these 8-bit microcontrollers. It is really fun!

If I look at what I would like to be included in NilRTOS, I see a couple of things (that you might consider :lol: ):
  • Waiting for one or more events, including passing eventdata:
    I already have had several occasions were waiting on multiple events would make programming a lot easier; Now I'm using multiple threads to be able to wait for more than one event as I don't want to use timed polling (say a loop which checks multiple things every 10msec).
  • Some kind of 1:n event notification:
    I have no idea if this should be, or is in any RTOS, but considering very loosely coupled threads/functionality it would be nice to be able to Notify an event (with data), which is received by n for that event registered threads.
    Given the current functionality of NilRTOS, it is perfectly doable by (ab)using the FIFO class to pass data back & forth between threads, and some extra layer which handles the register/unregister/notification of events, but out-of-the-box would be even nicer... This all makes it very easy during development to write dummy or testharness threads that register for an event, and just write the event to the serial output or return predefined (series of) data.

If there are other possibilities to accomplish what I want, don't hesitate to bring them up, as it has been some time that I used some kind of RTOS 8-)
Bill's examples already helped a lot btw.

Last question: where to report bugs / problems with the AVR port (I can't get NilSerial to work, the examples with NilSerial don't compile)?

(Hmm, competition? -> ArdOS - The Arduino Operating System)

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: An Arduino Library for Nil RTOS

Postby Giovanni » Tue May 21, 2013 6:46 pm

Hi,

NilRTOS as distributed by Bill contains several fixes not yet backported in the "official" version, well, there is no official version yet, is has been just a nice experiment so far, the concept proved successful.
You may report problems here but also make sure Bill notices them, I don't know how often he visits this board, he is very active on the Arduino forum.

I will look into events but they would have to be necessarily a simplified mechanism compared with ChibiOS events.

Giovanni

Mars
Posts: 5
Joined: Mon May 13, 2013 11:46 am

Re: An Arduino Library for Nil RTOS

Postby Mars » Tue May 21, 2013 9:36 pm

Ok. Thanx for the info.
I will check the arduino forum. AFAIK he is fat16lib over there...


Return to “ChibiOS/NIL”

Who is online

Users browsing this forum: No registered users and 17 guests