Characteristics of ChibiOS' main thread

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

refcodes
Posts: 3
Joined: Tue Jun 29, 2021 1:16 pm

Characteristics of ChibiOS' main thread

Postby refcodes » Tue Jun 29, 2021 2:32 pm

Dear ChibiOS team, I am a newbie to ChibiOS and microcontroller programming. I have got a question regarding ChibiOS's main thread on an Arduino mega. As of my understanding, after having initialized all threads, ChibiOS's main thread ends up in the "loop()" function (similar to Arduino's super-loop). Now I wonder where I am being wrong: I am using a NextionDisplay connected via serial port to an Ardunio mega. The NextionDisplay is controlled by the Arduino over a serial port and signals the Arduino upon any touch events triggered on the NextionDisplay over the serial port. When polling the NextionDisplay touch events in a dedicated ChibiOS thread, no signals coming from the NextionDisplay are caught by the code in the thread, though when I poll for NextionDisplay touch events in the "loop()" function (as of my best & humble knowledge it is the main thread) I get the touch signals correctly while the other threads still being processed. For my use case this is a feasible approach, though I would like to know if it is good practice to place legacy serial communication processing (as of the NextionDisplay library) in the main thread ("loop()") or are there configuration means to use a dedicated thread when doing such communication (what have I missed)?

(NextionDisplay: https://www.exp-tech.de/displays/tft/76 ... lay?c=1074)
(NextionLibrary: https://github.com/itead/ITEADLIB_Arduino_Nextion)

Below a snippet on how I created the threads:

Code: Select all

#include "NextionChibiOS.h"

#include <Arduino.h>
#include ...
#include <ChRt.h>

#define THREAD_MEMORY 512

...

void displayThread() {
   while( true ) {
      // nexLoop(touchListeners); // NextionDisplay query touch events does not function here
   }
}

THD_WORKING_AREA(displayArea, THREAD_MEMORY);
THD_FUNCTION(displayStart, arg) {
   displayThread();
}

void startThreads() {
   ch_thread *displayThread = chThdCreateStatic(displayArea, sizeof(displayArea), HIGHPRIO, displayStart, NULL);
}

void setup() {
   chBegin(startThreads);
}

void loop() {
   nexLoop(touchListeners); // NextionDisplay query touch events does function here
}


Thank you beforehand & best regards,
Siegfried

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

Re: Characteristics of ChibiOS' main thread

Postby Giovanni » Tue Jun 29, 2021 6:46 pm

Hi,

In ChibiOS/RT the main function can be an "idle" thread or a normal thread depending on the CH_CFG_NO_IDLE_THREAD setting, the default is "normal" (CH_CFG_NO_IDLE_THREAD=FALSE). This means that the main function is a thread like all others placed at priority NORMALPRIO, if the main thread never sleeps then threads placed at priority NORMALPRIO or lower are never scheduled, you need to verify if this is your case.

About threads, place priorities "around" NORMALPRIO, for example NORMALPRIO+5, NORMALPRIO-1 etc, do not use absolute values.

Note that normally threads should not execute loops without waiting for some kind of event or sleeping, not sure if this is your case.

Not sure about which version are you using so other details may vary, your code looks "customized", it is not what I would expect in a ChibiOS application.

Giovanni

refcodes
Posts: 3
Joined: Tue Jun 29, 2021 1:16 pm

Re: Characteristics of ChibiOS' main thread

Postby refcodes » Wed Jun 30, 2021 2:38 pm

Hello,

thank you very much for your reply! I am using the ChibiOS version with Tag 1.3.0 as of https://github.com/greiman/ChRt/releases/tag/1.3.0 and added delays and thread priorities as you suggested. What I still do not understand is why this code works polling NextionDisplay's touch display (should be the main thread):

Code: Select all

void loop() {
   nexLoop(touchListeners);  // NextionDisplay query touch events functions here
   chThdSleepMilliseconds(100);
}

Though the same code in a dedicatedly created thread (now working with NORMALPRIO as well) does not seem to be able to get data from the serial port (I also added the sleep period):

Code: Select all

void displayThread() {
   while( true ) {
      nexLoop(touchListeners); // NextionDisplay query touch events does not function here
      chThdSleepMilliseconds(100);
   }
}

I understood (hopefully correctly) that ChibiOS uses custom functionality to handle serial communication (the serial driver) in a multi threaded environment though the NextionDisplay library uses Arduino's legacy library for serial communication. May it be the case that the main thread has other settings regarding the thread memory or handling I/O or the like which makes it possible to run the NextionDisplay's "nexLoop" function correctly in the main thread whereas there are constraints making it to fail to read from the serial port in a dedicated thread (as of "... chThdCreateStatic(displayArea, sizeof(displayArea), NORMALPRIO, displayFunction, NULL); ...")?

Maybe you have an idea?

Thank you beforehand & best regards,
Siegfried

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

Re: Characteristics of ChibiOS' main thread

Postby Giovanni » Wed Jun 30, 2021 2:43 pm

Sorry, those bits of code tell me nothing, all those functions are not part of ChibiOS and I cannot go guessing what those do, those are just function declarations with no info on how those are glued together or used.

The only functions I recognize there are chThdCreateStatic() and chThdSleepMilliseconds() which are part of the RT API but I suspect the problem is not there.

Perhaps you should direct the question to greiman who is maintaining that customization on arduino. Here we can just help with ChibiOS-related topics and you are welcome to ask about those.

Moving this topic in the "AVR support" forum because this one is not for support.

Giovanni

refcodes
Posts: 3
Joined: Tue Jun 29, 2021 1:16 pm

Re: Characteristics of ChibiOS' main thread

Postby refcodes » Fri Jul 02, 2021 8:41 am

(for anyone interested, the Arduino specifics for this questions regard the Stack, see https://github.com/greiman/ChRt/issues/14 for more details)


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 11 guests