Pages: [1] 2 3 4   Go Down
Author Topic: NilRTOS - A Fast Tiny Preemptive RTOS  (Read 16615 times)
0 Members and 1 Guest are viewing this topic.
0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: January 29, 2013, 12:07:03 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying the NilFifoDataLogger demo, but getting this when typing a char to begin, DATA.CSV is emtpy:
Code:
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..  smiley-razz
« Last Edit: January 25, 2013, 10:48:46 am by pito » Logged

0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 // 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:
 uint16_t value;
 ...
  file.printField(value, ',');
is three times faster than this code
Code:
 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.
« Last Edit: January 25, 2013, 11:24:58 am by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IDE 1.5.1r2, @115k2, ide terminal:
Code:
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
« Last Edit: January 25, 2013, 12:49:54 pm by pito » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pito,

I can't reproduce your problem.  Please try the attached NilRTOS and SdFat libraries.

What board are you using?  I tried an Uno and a 2560 Mega.

* pito.zip (157.6 KB - downloaded 49 times.)
« Last Edit: January 25, 2013, 01:30:43 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using my own UNO, BTooth serial, sdfatlib20121217.zip and latest nilrtos from your repo.
Your pito.zip shows
Code:
!   C:\Documents and Settings\igi\Desktop\pito.zip: Unexpected end of archive
Logged

0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I downloaded pito.zip and extracted the libraries with 7-zip on a Windows 7 machine.

The contents diff OK.

Edit: I tried the Windows extractor and it works too.
« Last Edit: January 25, 2013, 01:54:12 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

firefox under xp has a problem to download it, IE8 did it .. smiley-confuse
but still the same issue..
I'll try with usb serial dongle..
..the same..

fyi - this is the bench from the sdfat:
Code:
Type any character to start
Free RAM: 1019
Type is FAT32
File size 5MB
Buffer size 100 bytes
Starting write test.  Please wait up to a minute
Write 135.23 KB/sec
Maximum latency: 54924 usec, Minimum Latency: 88 usec, Avg Latency: 733 usec

Starting read test.  Please wait up to a minute
Read 284.79 KB/sec
Maximum latency: 4656 usec, Minimum Latency: 84 usec, Avg Latency: 345 usec

Done

Type any character to start
« Last Edit: January 25, 2013, 02:19:21 pm by pito » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I opened the example from your lib and did only this change:
Serial.begin(115200);
..no luck..

Again - I took vanilla demo and run @9k6 (usb dongle, your libs from pito.zip, IDE 1.5.1r2, UNO 328p @16MHz):
Code:
type any character to begin
type any character to endþtype any character to begin
type any character to endþtype any character to begin
Code:
../nilFifoDataLogger.cpp.hex
Binary sketch size: 13,546 bytes (of a 32,256 byte maximum)


* ser.jpg (22.96 KB, 532x183 - viewed 92 times.)
« Last Edit: January 25, 2013, 02:45:52 pm by pito » Logged

0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pito,

I can't reproduce your problem so I don't know what to do.   I only do the Serial.begin(115200) change with 1.5.1r2 on an Uno and get.
Quote
type any character to begin
type any character to end
Done
Maximum SD write latency: 65008 usec
Unused Stack: 57 454

I get the same result with all line endings.

To see the performance of printField(), run the PrintBenchmark example with the new SdFat.  You should get something like this:
Quote
Test of println(uint16_t)
Time 10.09 sec
File size 128.89 KB
Write 12.78 KB/sec
Maximum latency: 62460 usec, Minimum Latency: 176 usec, Avg Latency: 497 usec

Test of printField(uint16_t, char)
Time 3.26 sec
File size 128.89 KB
Write 39.60 KB/sec
Maximum latency: 67240 usec, Minimum Latency: 60 usec, Avg Latency: 156 usec
The two files are identical but printField() runs three times faster.


Edit: Try making the stack for thread 1 larger like this:
Code:
NIL_WORKING_AREA(waThread1, 100);
« Last Edit: January 25, 2013, 02:51:24 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Type any character to start
Free RAM: 1109
Type is FAT32
Starting print test.  Please wait.

Test of println(uint16_t)
Time 9.97 sec
File size 128.89 KB
Write 12.93 KB/sec
Maximum latency: 20828 usec, Minimum Latency: 176 usec, Avg Latency: 491 usec

Test of printField(uint16_t, char)
Time 3.15 sec
File size 128.89 KB
Write 40.92 KB/sec
Maximum latency: 18748 usec, Minimum Latency: 60 usec, Avg Latency: 150 usec

Test of println(double)
Time 15.06 sec
File size 149.00 KB
Write 9.89 KB/sec
Maximum latency: 19160 usec, Minimum Latency: 572 usec, Avg Latency: 746 usec

Done!

Type any character to start
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Edit: Try making the stack for thread 1 larger like this:
Code:
NIL_WORKING_AREA(waThread1, 100);
..no luck..
Logged

0
Online Online
Edison Member
*
Karma: 67
Posts: 1652
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do the other NilRTOS examples work?

I can't do anything more since everything I try works fine.  I don't have a working xp system.

Edit: Here is one more idea.  Change nilSysBegin() to nilSysBeginNoFill().  This will prevent filling stacks when NilRTOS starts  so the final message will be wrong.
« Last Edit: January 25, 2013, 03:22:51 pm by fat16lib » Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2086
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Change nilSysBegin() to nilSysBeginNoFill().
..the same issue..

other stuff ie.:
nilblink blinks

nilBlinkPrint:
Code:
Count: 628299, Unused Stack: 57 28 1663
Count: 628306, Unused Stack: 57 28 1663
Count: 628304, Unused Stack: 57 28 1663
Count: 628302, Unused Stack: 57 28 1663
nilSemTest:
Code:
micros overhead = 4
sem switch tasks micros = 28
Thd1 Done
sem immediate timeout = 4
sem immediate OK = 4
Thd2 Done
« Last Edit: January 25, 2013, 03:36:23 pm by pito » Logged

Pages: [1] 2 3 4   Go Up
Jump to: