Go Down

Topic: NilRTOS - A Fast Tiny Preemptive RTOS (Read 17 times) previous topic - next topic

fat16lib

Jan 24, 2013, 05:13 pm Last Edit: Jan 29, 2013, 06:07 pm by fat16lib Reason: 1
Giovanni Di Sirio, the author of ChibiOS/RT, has written an experimental system called Nil RTOS.

Giovanni describes Nil RTOS as "Smaller than ChibiOS/RT, so small it's almost nil".

I decided to try Nil RTOS on an Arduino Uno. The latest version of NilRTOS is here http://code.google.com/p/rtoslibs/.

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 requires about 2150 bytes of flash on an Uno.

Code: [Select]

/*
* 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.
}


pito

#1
Jan 25, 2013, 04:09 pm Last Edit: Jan 25, 2013, 04:48 pm by pito Reason: 1
I am trying the NilFifoDataLogger demo, but getting this when typing a char to begin, DATA.CSV is emtpy:
Code: [Select]

type any character to begin
type any character to end
type any character to begin


PS: maybe it is a high time to introduce a new item into the "Topics", ie. " RTOS" or something like that..  :P

fat16lib

#2
Jan 25, 2013, 05:18 pm Last Edit: Jan 25, 2013, 05:24 pm by fat16lib Reason: 1
What line ending are you using in the serial monitor?

I have a bug in flushing all input.  I used nilThdSleep(10) in setup() before NilRTOS was started:
Code: [Select]

 // throw away input
 while (Serial.read() >= 0) {
   nilThdSleep(10);
 }



Try "No line ending" or replace the nilThdSleep(10) with delay(10).

I will quickly post an update for this and another bug in NilRTOS.

Also I will post a version of SdFat with a new printField(uint16_t value, char terminator)  so this code
Code: [Select]

 uint16_t value;
...
 file.printField(value, ',');

is three times faster than this code
Code: [Select]

 uint16_t value;
...
 file.print(value);
 file.print(',');


This will allow nilFifoDataLogger to write more fields at 1024 usec intervals.

Arduino Print is too slow and nilFifoDataLogger will suffer overrun errors currently.

pito

#3
Jan 25, 2013, 06:43 pm Last Edit: Jan 25, 2013, 06:49 pm by pito Reason: 1
IDE 1.5.1r2, @115k2, ide terminal:
Code: [Select]

with nilThdSleep(10);
No line ending:
type any character to begin
type any character to end
type any character to begin
Newline:
type any character to begin
type any character to begin
CR:
type any character to begin
type any character to begin
Both NL&CR:
type any character to begin
type any character to begin

with delay(10);
any settings
type any character to begin
type any character to end
type any character to begin

DATA.CSV 0kB

pito

with:
//#define Serial NilSerial
and No line ending:
type any character ├┤type any character to begin
type any charactertype any character to begin
type any character to eîtype any character to begin
type any character totype any character to begin

Go Up