Pages: 1 [2] 3   Go Down
Author Topic: Updated versions of ChibiOS/RT, NilRTOS, and FreeRTOS  (Read 10328 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

fat16lib,

Got another question for you... 

One of the functions (apps?) that I want to run on the RTOS is a web server.  However, I think I read one of your posts that said the Ethernet lib isn't RTOS friendly.  I can't find the post again but do I have that right?

Thanks
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't recall a post about the Ethernet library. 

Many Arduino libraries are not RTOS friendly but that doesn't mean you can't use them.

Here is an example from the Ethernet library:
Code:
    // Wait for a response packet
    while(iUdp.parsePacket() <= 0)
    {
        if((millis() - startTime) > aTimeout)
            return TIMED_OUT;
        delay(50);
    }

This loop has a delay(50) call that will block lower priority threads.  If the delay were replaced by a chThdSleep(50) call, lower priority threads could use the CPU time.

I think a web server is an ideal candidate for RTOS use.  I would love to have a well designed web server as an example.

I hope to write a "How To" for making minimal changes to libraries that will improve their performance with an RTOS.
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pito,

I found my BMP085 and it seems to work with the Adafruit library by just changing Wire.h to TwiMaster.h.

I noticed this call in your post:
Code:
  bmp.begin(I2C_100KHZ);

The argument should be a BMP085 mode, not an I2C speed.  I don't know if that would make a difference.

I tested on an Uno and a 2560 Mega using the Adafruit example sketch.  I don't have a 1284.
Logged

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

The argument should be a BMP085 mode, not an I2C speed.  I don't know if that would make a difference.
This is how I set up the bmp now:
Code:
//#include <Wire.h>
#include <TwiMaster.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
..
void setup() {
  Serial.begin(115200);
  bmp.begin(BMP085_ULTRAHIGHRES);
..
Still hangs at bmp.begin()..

Enclosed the sketch you may try with ie Mega (you want to decrease the buffer size).

* nilSdLogger_4.zip (2.36 KB - downloaded 16 times.)
« Last Edit: February 20, 2013, 02:09:42 pm by pito » Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strange!  On an Uno this sketch:

Code:
#include <TwiMaster.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(115200);
  if (!bmp.begin(BMP085_ULTRAHIGHRES)) {
    Serial.println("bmp.begin failed");
    while(1);
  }
  Serial.print("Altitude: ");
  Serial.println(bmp.readAltitude());
}
void loop() {}

Prints:
Quote
Altitude: 82.33

The only change to the library is this in Adafruit_BMP085.h:
Code:
#include "TwiMaster.h"
//#include "Wire.h"
« Last Edit: February 20, 2013, 02:25:40 pm by fat16lib » Logged

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

With 1.5.1r2 and mighty 1284p:
Code:
#include <TwiMaster.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(115200);
 // if (!bmp.begin(BMP085_ULTRAHIGHRES)) {
 //   Serial.println("bmp.begin failed");
 //   while(1); }
 
  bmp.begin(BMP085_ULTRAHIGHRES);
  
  
  Serial.print("Altitude: ");
  Serial.println(bmp.readAltitude(99500, 101350));
}
void loop() {}
Code:
Altitude: 155.14
With your original sketch I get errors during build..
PS: It seems my BMP lib is older one, with the latest I get with your original sketch:
Code:
Altitude: 200.40
« Last Edit: February 20, 2013, 02:48:34 pm by pito » Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand the problem.  TwiMaster uses a semaphore when linked with Nil RTOS so Nil RTOS must be started to do I2C I/O.

Move bmp.begin() here:

Code:
// Declare thread function for thread 1.
NIL_THREAD(Thread1, arg) {
  
  bmp.begin(BMP085_HIGHRES);
  // Start timer 1 with a period of PERIOD_USEC.
  nilTimer1Start(PERIOD_USEC);

bmp.begin() does lots of I/O.
« Last Edit: February 20, 2013, 02:41:03 pm by fat16lib » Logged

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

Quote
I understand the problem.  TwiMaster uses a semaphore when linked with Nil RTOS so Nil RTOS must be started to do I2C I/O.

Move bmp.begin() here:
Yep, it works now.
Bmp stuff takes 22ms now (35ms with wire).

A high time to replace _delay_ms() with chThdSleep() in the bmp driver..
Not easy, though.. smiley-roll
« Last Edit: February 20, 2013, 03:10:00 pm by pito » Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will put problems like these on my to-think-about list.

I am experimenting with using a weak symbol for delay. The weak symbol would use the standard Arduino delay. 

When you link a library with an RTOS, the weak symbol would be replaced by the appropriate sleep or delay function in the RTOS.

This way you can have a single library and it will work with standard Arduino sketches or any RTOS that defines the override for the weak delay.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to use the RTOS you have ported with Atmel Studio?
If it is possible do you have any guide of how to do it? Or I just can copy the folder and place it in my project.
What I want to do is use Arduino Due with Atmel Studio.
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think it would be better to use the unmodified versions of FreeRTOS or ChibiOS with Atmel Studio.  Both of theses systems have AVR ports and examples. 

I know FreeRTOS and Atmel are cooperating to make their products work together.

I really like ChibiOS/RT on Due.  It is fast, takes a bit less memory than FreeRTOS but has more features. 

Nill RTOS is great when you need minimal memory use and simple features are adequate.

Nil RTOS is experimental and not really supported yet by the author.  I am personally supporting Nill RTOS mods for the Arduino core.

I assume you are not using the Arduino core software.  My mods were to allow use of an RTOS with the Arduino core and Arduino libraries.


 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, fat16lib,


Thanks a LOT for so useful contribution!
I just come across your work. May I ask you few quick questions?

1) Which Arduino libraries known NOT to work with FreeRTOS, or have timing issues?
2) Which Arduino would you recommend to handle 3 step motors (at speed up to 2000 rpm), 2 rotary encoders and 2 magnetic hall sensors with any of RTOS? Probably Arduino Due is a minimum?

Thanks in advance for any suggestion(s).
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have a list of libraries that don't work.

Any library that calls delay() or has a wait loop for something to complete will block all lower priority threads.

The ideal way to handle devices with an RTOS is to start an operation in a thread then wait on a semaphore.  When the operation completes, an interrupt occurs and the device ISR signals with the semaphore to wake the waiting thread.  This means many Arduino libraries need some mods for optimum performance.

The malloc() included with new versions of the Arduino IDE will not work in threads.  This is O.K. since dynamic memory is not a good idea in real-time systems after setup.

FreeRTOS uses the most RAM and flash, ChibiOS/RT is smaller, faster, and has more features.  Nil RTOS is the smallest but has few features and probably is not appropriate for your system.

Certainly Due is the minimum but I can't guess what you will need.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, fat16lib,

I'm currently stuck with something damn simple on Due, your latest FreeRTOS ARM, and 1.52 IDE.

I have an early draft for a software which should control 2 step motors, display data on TM1639, and exchange data via Firmata with Linux PC (3rd feature is not written yet). Currently I have only 1 step motor and TM1638 LED connected, and both work wit conventional Arduino setup.

I took a FreeRTOS example which basically spawns 2 threads, and in my case call of vTaskStartScheduler() hangs Due. I even removed all TM1639 calls, result is still the same.

void setup(void) {
  portBASE_TYPE s1, s2;
  Serial.begin(9600);
 
  pinMode(MOTORCLK, OUTPUT);
 
  s1 = xTaskCreate(vNumericLED, (signed char*)"Task 1", 200, NULL, 1, NULL);
  s2 = xTaskCreate(vRunStepMotor1, (signed char*)"Task 2", 200, NULL, 2, NULL);
 
  if (s1 != pdPASS || s2 != pdPASS ) {
    Serial.println(F("Creation problem"));
    // while(1);
  }
  else
    Serial.println(F("Creation OK"));;

  vTaskStartScheduler();
 
  for (;smiley-wink;
}

static void vNumericLED(void *pvParameters)
{}

static void vRunStepMotor1(void *pvParameters)
{
  digitalWrite(MOTORCLK, HIGH);
  vTaskDelay(20000 * 250 / portTICK_RATE_MS);
  digitalWrite(MOTORCLK, LOW);
  vTaskDelay(20000 * 250 / portTICK_RATE_MS);
}

extern "C"{
  void vApplicationIdleHook(void) {}
}

Anything after  vTaskStartScheduler() is not executed, so I assume its a system freeze. Please correct if I'm wrong here.

Any idea what might get wrong?

Thanks in advance.
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

vTaskStartScheduler() never returns.  Please see the FreeRTOS documentation for details of API functions.

Here is a link to vTaskStartScheduler() http://www.freertos.org/a00132.html.
Logged

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