Bizarre behavior with C6, Arduino and RTC_NOINIT_ATTR variable

slaboure
Posts: 5
Joined: Thu Oct 10, 2024 5:19 pm

Bizarre behavior with C6, Arduino and RTC_NOINIT_ATTR variable

Postby slaboure » Fri Nov 08, 2024 10:27 am

Hello,

I'm programming a C6 in Arduino mode with pioarduino. My code has a boolean value that has to swap back-n-forth to properly operate an electrical motor (one time in one direction, one time in the other, etc.) Yet, from time to time, I need to soft reboot my device and when doing so, it will always reset to the default value of the variable (true) rather than the "right" value.

So I thought about tagging my value with RTC_NOINIT_ATTR:
  1. RTC_NOINIT_ATTR boolean engineBalanceDirection = true;
Then, my code is pretty basic (simplified here):
  1. void wallclock_force_forward()
  2. {
  3.   if (engineBalanceDirection)
  4.   {
  5.     digitalWrite(ENGINE_BALANCE_1, HIGH); // activate A+ on L293D
  6.     digitalWrite(ENGINE_BALANCE_2, LOW);  // de-activate A- on L293D
  7.   }
  8.   else
  9.   {
  10.     digitalWrite(ENGINE_BALANCE_1, LOW);  // de-activate A+ on L293D
  11.     digitalWrite(ENGINE_BALANCE_2, HIGH); // activate A- on L293D
  12.   }
  13.   engineBalanceDirection = !engineBalanceDirection;
  14.   delay(IMPULSE_DURATION_MS); // wait 1/2 second
  15.  
  16.   // stop it all - de-activate A+ and A- on L293D
  17.   digitalWrite(ENGINE_BALANCE_1, LOW);
  18.   digitalWrite(ENGINE_BALANCE_2, LOW);
  19. }
(I know I could simplify this but I truly want to make sure I don't have two signals HIGH at the same time for some reason)

Now, when I do this... nothing happens! Which is very odd. As if my variable could possibly be read, but any write doesn't go back to memory or is not read back as modified. Is that even possible? What am I doing wrong? Ideas are welcomed!

slaboure
Posts: 5
Joined: Thu Oct 10, 2024 5:19 pm

Re: Bizarre behavior with C6, Arduino and RTC_NOINIT_ATTR variable

Postby slaboure » Fri Nov 08, 2024 2:07 pm

More on this: I started adding debugging information within the block and my boolean value (also tried to change its type to basic bool) prints as ... 47! Then, when negating the boolean, it converts to 46, then back to 47, etc. so it seems using RTC_NOINIT_ATTR seemingly changes its data type as well?!?

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Bizarre behavior with C6, Arduino and RTC_NOINIT_ATTR variable

Postby MicroController » Fri Nov 08, 2024 10:52 pm

Well, noinit means not initialized, so you have to expect to see random data in the noinit section if you don't initialize it yourself.
And each bool is stored as one byte in memory so it may read as any value from 0...255, if not initialized otherwise.

Who is online

Users browsing this forum: No registered users and 125 guests