BriscoeTech / Arduino-FreeRTOS-SAMD21

A port of FreeRTOS that runs on Arduino Samd21 boards
63 stars 19 forks source link

Arduino-FreeRTOS-SAMD21 and AdaFruit Graphics Libraries and ArduinoJson #7

Closed lfg6000 closed 2 years ago

lfg6000 commented 5 years ago

This is more of an observation than an issue. It would be good to get some feedback about these observations. I am probably missing something....

BriscoeTech commented 5 years ago

Hello lfg6000,

Programming with a rtos definatley adds its own new sets of challenges and adds afew more moving cogs than people are used to have to think about when programming an Arduino. Sounds like you have an existing project and are merging it to use freertos? Without having seen your code (I would suggest posting it so we can take a look) I have afew possible things you can look into to help diagnose what is going on.

Based on the Malloc Failed blinking error code, I think I have afew things you can try out:

  1. Your project may have used up all the ram (heap) of the processor, look at the second rtos example project and try using the MemmoryFree function call in your non-rtos project's setup function to see if you are running close to using up all the ram. Making your project a rtos project could be putting you over the edge, especially if you are doing alot of string manipulation and haven't forced the strings to flash using the F() macro. For some reason, the MemmoryFree function does not work if put inside of a task, am still trying to figure that one out, try using it at the beginning and end of your setup function to see how much heap is being used up initializing everything.

  2. I have noticed that there are alot of classes out there (my experience neopixles, adafruit oled screens) that need to be declared as global variables outside of tasks so they work properly. By making them global, they get allocated onto the global heap and not on a rtos stack. Its not necessarily that they are not thread safe objects, but that there is some sort of assumption that the object has access to an Arduino dependency on the global heap. I think by putting them on a task stack, it causes it to not work properly.

  3. The malloc failure is triggered typically for me when you try and make a new rtos object and it cannot be made. The way the rtos is currently implemented is it does not allow dynamic allocation of rtos objects, they all need to be initialized in the setup function and before vTaskStartScheduler is called. Embedded systems don't do well with dynamic allocation since they can easily lead to heap fragmentation and crash the rtos, so that is why it is disabled. I would take a look and see if you are trying to do any dynamic allocation of tasks/semaphores/ques and make them allocated in the setup function before the rtos is started.

  4. Debugging the rtos can be hard, I agree lol. I would highly recommend starting with one of the blank examples and add one new piece of code at a time, and see if it causes a rtos crash. If you add too much at once, it will quickly become impossible to figure out what exactly about one line of code made it crash.

Hope that something in here is useful for you to diagnose what is happening. I recommend posting your code so we can take a look and see what is happening.

lfg6000 commented 5 years ago

briscoetech,

yep i have a existing project using the adafruit itsybitsy M0 (ATSAMD21G18 32-bit Cortex M0+ with 32k of ram). At the moment the rtos is removed. It is easy enough to add the rtos back to the project by using git to fetch the version using the rtos. Once i do that i will make a simple example that crashes and post the code. This may take me some time to get to because i am a bit short on free time for the time being.

1) ok i will look at MemoryFree at the end of setup.

2) all my objects are global and are not being created inside a task here is an example of a global object right below the includes in main.h // --- construct the tft class, it is use to send text to display Adafruit_ST7735 goTft = Adafruit_ST7735(PN_TFT_CS, PN_TFT_DC, PN_TFT_RST);

3) all my rtos objects are created in setup...no rtos objects are created inside a task

4) i agree adding one piece at a time until it crashes is a great approach to problems like these.

Thanks, Louie

BriscoeTech commented 5 years ago

I ran into a interesting problem last last week, and think that it could apply to you.

https://github.com/BriscoeTech/Arduino-FreeRTOS-SAMD21/issues/8

BriscoeTech commented 5 years ago

Hello @lfg6000

With the help of the community the current release now has the ability to enable FreeRtos to manage all the malloc/free/realloc/calloc calls of the microcontroller, this might help deal with unresolved strange memory issues.

I am interested if v1.0.0 solves your issue (it is now using heap_4), or if v1.0.0 with wrapped memory functions enabled would fix your problem.

Hope to hear from you soon!

lfg6000 commented 5 years ago

Ok thanks for the heads up. I had been following the conversation and was thinking the same thing. I will let you know how it goes.

Louie

On Sun, Jun 16, 2019 at 4:13 PM BriscoeTech notifications@github.com wrote:

Hello @lfg6000 https://github.com/lfg6000

With the help of the community the current release now has the ability to enable FreeRtos to manage all the malloc/free/realloc/calloc calls of the microcontroller, this might help deal with unresolved strange memory issues.

I am interested if v1.0.0 solves your issue (it is now using heap_4), or if v1.0.0 with wrapped memory functions enabled would fix your problem.

Hope to hear from you soon!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/BriscoeTech/Arduino-FreeRTOS-SAMD21/issues/7?email_source=notifications&email_token=ACBTRWPPQEFWHGIO5OEK5BLP22NILA5CNFSM4G6RU2ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXZUKYI#issuecomment-502482273, or mute the thread https://github.com/notifications/unsubscribe-auth/ACBTRWJL7LSMNH6VO5OFSE3P22NILANCNFSM4G6RU2ZA .

lfg6000 commented 5 years ago

test 1) fetched v1.0.0 that uses heap_4
result 1) still crashes

test 2) v1.0.0 using wrapped memory functions

observations

Thanks, Louie

BriscoeTech commented 3 years ago

Hello Louie,

It has been awhile, but I have added some new error serial printing functions to the rtos that might help diagnose the exact cause of your rtos project crashing.

If you download the new library (v2.3.0), try adding vSetErrorSerial(Serial Pointer Here); to your project to enable the feature. When the rtos crashes it will print the reason for the crash to the terminal. Also refer to the new RtosCrash_Test.ino file, where you can replicate the most common crashes and see which one best describes what you are seeing.

Let me know what you find :-)

lfg6000 commented 3 years ago

If and when i try to use Arduino-FreeRTOS-SAMD21 again i use vSetErrorSerial(Serial Pointer Here). thanks for pointing it out.