Hi there,
I’m going to simulate a simple freeRTOS Arduino program in Proteus. It should run two LEDs and ON/OFF a DC motor with different timing and also printing the execution of the tasks on Virtual Terminal, but after few seconds and one cycle of running the motor, the simulate stops. When I comment the “runMotor” task lines, the program keeps the simulation going.
I’d appreciate it if anyone gives an idea to solve.
Because I’m new in the forum and can’t upload the files according to the site policy, I copied the code below this message.
Kind Regrads,
Depending on the Serial.println implementation the task stack of 128 is probably way too small because printf family functions are usually stack hungry.
You really should define configASSERT and also enable stack overflow checking for development/debugging. It’s very helpful !
Hi Hartmut,
Thanks for your reply. I followed your instruction and it was very helpful. Also, to ensure of stack’s issue of printing task, I commented it and checked. The result is the same. For your information, before adding the “TaskRunMotor” the simulation was working properly. It seems the bug occurs in the “TaskRunMotor”.
Can you break in the debugger and see what it is doing? Couple other things that you can try:
Try comment all the Serial.println calls.
Try increasing all task’s the stack sizes.
Check that you are not running out heap and failing to start the scheduler. You can put a print and an infinite loop after the call to vTaskStartScheduler to catch this:
void setup() {
.
.
.
vTaskStartScheduler();
/* This line should never be reached. */
Serial.println("Failed to start the scheduler!");
for(;;) {}
}
Hi Aggarg,
Thanks for your attention.
According to your recommendation, I commented all the print tasks and increased the stacks as much as possible. But the result is the same in the simulation. I think maybe the Proteus as a simulator is not able to keep the process going. So, I’m going to use an Arduino developer board and check it with real components.
I tried to copy the code based on your suggestion to know your comments, but unfortunately, the site policy prevented it.
Thanks,
Don’t use serial prints on Arduino UNO. It will fail even in simpler programs because the underlying implementation was not intended to be used with multithread programming.
The serial output buffer is a single circular array that gets corrupted whenever two or more tasks want to print out at the same time.
A way to prevent such errors is encapsulating the writings into a monitor so that all writings are mutually exclusive. However this solution makes harder to print out but it’s done at the user level, instead of messing around the Arduino’s low levels.
Hi Xavier,
I tried to compile the code to provide it for Arduino UNO, but the Arduino IDE returns an error message “Error compiling for board Arduino Uno”.
In the details writes: “Sketch uses 83046 bytes (257%) of program storage space. Maximum is 32256 bytes.
text section exceeds available space in boardGlobal variables use 1399 bytes (68%) of dynamic memory, leaving 649 bytes for local variables. Maximum is 2048 bytes.”
So, I have to get back to Arduino Mega 2560.
Although I reduced the stacks as much as possible and commented some parts of the code, the same error appears.
Could you please share with me your experience in compiling the code for the Arduino UNO board?
Thanks,
It seems that your program is either repeating code or it is using big fat libraries, and if you wrote such program originally for the atmega2560 it’s unlikely that you can load it into the humble atmega328.
If the problem is the code that repeats and repeats, the best you can do is reusing it through functions.
The programs I code for the atmega328 (Arduino UNO) are tailored to its limits, but as I said, you’ll need to reengineer your application to fit the 32KB flash limit.
Once I met a couple of guys that were hired by the same client that hired me, so we both were working in the same app, without knowing anything about each other. When we met I saw their application was nearly 10000 lines of code, of course, repeating and repeating (e.g., they didn’t use functions), while my app was, without blank lines, 700 lines, and too easy for adding new functionality.
Are you using a third party library that is eating the flash?
I don’t trust simulations =)
Have you tried a very simple “hello world” in your platform? Maybe it’s not your program but the tools the offending ones.
Finally, I’ve integrated a series of applications (Arduino, Arduino-mk, FreeRTOS) into a tool called Molcajete, that is tailored to the UNO boards (those with the atmega328 chip). You might want to give it a try so you can isolate your problem. (Don’t install it, just run it, so it does not interfere with your actual Arduino installation).