Some problems about freeRTOS

Post here first, or if you can't find a relevant section!
Post Reply
KiraVerSace
Posts: 1
Joined: Tue Oct 13, 2020 1:09 pm

Some problems about freeRTOS

Post by KiraVerSace »

Hi Guys, I use the newest stm32duino-freeRTOS in my project, but I meet some serious problems, and I am from China, and not good at English.
MCU: STM32L476RG 96K/1024K
PlatformIO/ Frame=Stm32duino

1.Most programs we are using the heap4 in STM32, but in your
lib/STM32duino FreeRTOS/src/FreeRTOSConfig_Default.h
you use the heap_useNewlib, and I can not find more references, how about it? I doubted the reason that my program run to error is it. Should I change it to the
heap4?

2. I have serval tasks in my program like this,

Code: Select all

xTaskCreate(periphRunTask, 	(const portCHAR *)"PeriphRun", 		512,    	 NULL, 	6,	NULL);
xTaskCreate(sensorSampleTask, 	(const portCHAR *)"Sensor", 		256, 	      NULL, 	5, 	NULL);
xTaskCreate(dataHandleTask, 	      (const portCHAR *)"DataHandle", 	512, 	       NULL, 	4, 	NULL);
xTaskCreate(socketLTERunTask, 	(const portCHAR *)"LTESocket",		1024, 	NULL, 	3, 	NULL);
xTaskCreate(socketWaterRunTask,    (const portCHAR *)"WaterSocket",	1024, 	NULL, 	2, 	NULL);
In the ldscript.ld, I change the stack and heap value

Code: Select all

_Min_Heap_Size  = 0x200;;       /* 0x400 1KB 一般由程序员分配和释放,分配方式类似于数据结构中的链表     */
_Min_Stack_Size = 0x800;;       /* 0x800 2KB 由编译器自动分配和释放,如存放函数的参数值,局部变量的值等	 */
but usually, my program is run to death, and print the task list to see the stack use is a failure.
So I want to know [_Min_Heap_Size = 0x200; ] is the size for the freeRTOS, is it too small for the RTOS,
or Could you give me some tips to optimize my code?

Thank you very much!
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Some problems about freeRTOS

Post by ag123 »

welcome, hope you have reviewed some of the faq
viewtopic.php?f=2&t=3
viewtopic.php?f=2&t=301

as you mentioned the mcu is STM32L476RG , can we assume you are using the official core? or which core are you using?
note that strictly speaking freeRTOS is not literally 'supported' here, just that some of the members here uses it and they may respond. however, the response is voluntary.

note also that it seem some 'forks' or version of the cores libmaple (e.g. rogers) or official seemed to be bundled from elsewhere. literally the authors of those forks should be the real ones answering them. however, if you could figure out the 'flavor' of the core e.g. libmaple or official stm, it would help others give more intelligent answers.

note that freeRTOS is not a 'beginners' topic, hence it may be assumed that you are already familiar with features of the core, blinked a led etc.
MGeo
Posts: 38
Joined: Thu Dec 19, 2019 10:29 am
Answers: 2

Re: Some problems about freeRTOS

Post by MGeo »

Some form of RTOS is essential to manage the complexity of these increasingly complex Arm cores, hardware debuggers too IMHO.

This guy Dave Nadler does a deep dive on newlib issues with FreeRTOS https://nadler.com/embedded/newlibAndFreeRTOS.html Not a pretty sight.

Fix was documented here https://github.com/DRNadler/FreeRTOS_helpers
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Some problems about freeRTOS

Post by ag123 »

there are other ways beyond systick driven context switching rtos
e.g. an event loop
https://github.com/ag88/stm32duino-eventloop
i've used the event loop successfully to modularize access to various pheriperials e.g. an LCD Adafruit ILI9341 lib, monitor key presses, manage an sd card and still run an adc to collect data and that is on the small stm32f103c8 20k sram pill boards.
the main thing about context switching rtos is it is *memory hungry* the stack (and maybe heap) is fragmented so you run out of memory quickly on 20k sram stm32f103c8. And in addition, most functions in the core including Arduino are not *re-entrant*, if 2 threads digitalWrite() to the same pin, the out come depends on chance and randomness of that race condition. and try to imagine that being a chip select pin say for SPI. so you'd need to use a mutex or lock in core api calls.

then there are other means such as this
Demonstration code for several things at the same time
https://forum.arduino.cc/index.php?topic=223286.0

and there are no less schedulers
https://github.com/arduino-libraries/Scheduler
https://github.com/mikaelpatel/Arduino-Scheduler
https://github.com/arkhipenko/TaskScheduler

the challenge with even rtos is that now mcus are *multi-core*, esp32 and raspberry pico has started the snowball rolling
https://www.raspberrypi.org/products/raspberry-pi-pico/
rtos alone would not solve that 'multi-core' problem. in effect for a real smp, you have one os (e.g. arduino core or rtos) thread running on each core of the mcu. it is a 'brave new world', it may not be surprising if we'd ever see 8 core or more duinos
MGeo
Posts: 38
Joined: Thu Dec 19, 2019 10:29 am
Answers: 2

Re: Some problems about freeRTOS

Post by MGeo »

ag123 wrote: Sat Mar 27, 2021 9:03 am rtos alone would not solve that 'multi-core' problem. in effect for a real smp, you have one os (e.g. arduino core or rtos) thread running on each core of the mcu. it is a 'brave new world', it may not be surprising if we'd ever see 8 core or more duinos
A start?:
https://www.freertos.org/STM32H7_Dual_C ... _demo.html
https://www.freertos.org/2020/02/simple ... ffers.html
Attachments
dual_core_AMP_topology.png
dual_core_AMP_topology.png (59.83 KiB) Viewed 3685 times
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Some problems about freeRTOS

Post by ag123 »

well, imho the closest for now is nuttx
https://www.hackster.io/news/the-raspbe ... b07a04060c
https://nuttx.apache.org/
but i'd guess many working on freeRTOS would attempt to make it full SMP. SMP is 'difficult' as it implies the os thread runs on each core, so it has to do synchronization and locking. this is very different from a single core systick driven context switching.
i.e. it is easier to imagine that say we are running stm32 duino, and you have 2 set of setup() and loop() running on each core.

one 'easy' way is mutual exclusion, i.e. each set of setup() and loop() use its own stack and heap, but that still doesn't solve the IO parts such as all the gpio, and literally all hardware spi, uart, i2c, usb, adc, dma etc. some kind of locking will be needed to co-ord all that accesses.

for that message buffer way, in a sense it serialize the events,
in my event loop implementation i used an event queue
https://github.com/ag88/stm32duino-eventloop
so in a sense all the events (or messages) that are added to the queue are forced to be sequential or serialized. that in a way is how i solved the locking / sync problem
javascript has a good illustration of the event loop architecture
https://o7planning.org/11951/nodejs-event-loop
Image
that event loop methodology sticks around even in java today, in a world where high end processors run with some 8 cores 16 'hyper' threads and more
https://docs.oracle.com/javase/tutorial ... patch.html
The Event Dispatch Thread
Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe": invoking them from multiple threads risks thread interference or memory consistency errors.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Some problems about freeRTOS

Post by fpiSTM »

MGeo wrote: Sat Mar 27, 2021 8:23 am Some form of RTOS is essential to manage the complexity of these increasingly complex Arm cores, hardware debuggers too IMHO.

This guy Dave Nadler does a deep dive on newlib issues with FreeRTOS https://nadler.com/embedded/newlibAndFreeRTOS.html Not a pretty sight.

Fix was documented here https://github.com/DRNadler/FreeRTOS_helpers
STM32FreeRTOS library used the Heap allocation schemes from Nadler as stated in the README.md:
https://github.com/stm32duino/STM32Free ... figuration

Anyway, I saw that he made an update for the newlib V3. I will have to check if all is fine ;)
Post Reply

Return to “General discussion”