SYSTEM_THREAD - can it be disabled at runtime

Hi,

My question is can the system thread enabled mode be disabled during critical sections of code ??

I have a set of code which drives one-wire temperature sensors (my own code not the library version).

Thi worked perfectly well for some considerable time, but then I started using ‘SYSTEM_THREAD(enabled)’. Now I seem to get spurious failures when talking to my 1W devices.
The reason is probably quite simple - in that the bit-banged code uses critical timing in the code.

So - is there any way to disable the thread switching behaviour for critical sections of code ??? (as you would often do with Interrupts.

I can overcome this issue simply by NOT enabling the threading mode, but it seems to me like the ability to prevent my thread from being switched out would be advantageous for this and other requirements ???. As far as I recall - Free-RTOS supports this functionality ?? - but then its been a while since I used it (directly).

Many Thanks
BR
Graham

For now you can use CriticalSection. Note that all interrupts will be disabled during the bit banging, so you should make sure this code is very short.

For example:

void sendCommand(uint8_t command) {
  CriticalSection cs; // interrupts are disabled when this is object is created

  // bang each bit from command

  // interrupts are enabled when the cs object is destroyed before the function returns
}

A better solution here would be to temporarily raise the priority of the current thread to prevent it being swapped out by the system thread. That’s not currently implemented in the system firmware.

1 Like

Hi,

Many thanks for the reply.

I saw this defined in another forum posting but as its not even mentioned in the Ref docs - wasn’t sure how it was used. So its just defined and never actually used then :-O, and goes out of scope at the end of the function ???.

So can I use it like

function()
{
CriticalSection cs;
{
do this.....
}
do something non-critical
}

or even 
foo()
{
startup....
{
criticalsection cs;
do critical stuff.....
}
do non-critical stuff....
{
criticalsection cs;
do more critical stuff
}

}

Or is function-wide ???.

Is there any documentation for this - somewhere ???.

Many Thanks

BR

Graham

I don’t know where to find any docs about it, but as it is declared and instanciated as a local (automatic) object, it will be destroyed once the code leaves the scope of that instanciating function resulting in the freeing of any sync objects.

An easy way to test the scope of a variable for yourself is to declare it and then try tor access it whereever you are not sure if you can or not.
e.g.

void foo()
{
  String strOutside;
  strOutside = "surely";
  {
    String strIndside;
    strInside = "surely too";
    strOutside = "yup, still visible";
  }
  strInside = "not sure";
  strOutside = "guess what"; 
}

void bar()
{
  strOutside = "sure not";
  strInside = "even less likely";
}

@GrahamS, the THREAD stuff is still considered beta and documentation is very lite at this time. What is really needed is a set of guidelines and functions/macros for using FreeRTOS on the Particle platform without causing conflicts with WICED and the system firmware IMO. Perhaps @jvanier could start a new section in the docs to document some of this? :wink:

1 Like

I can at least open a GitHub issue for it...

1 Like