stm32duino FreeRTOS and adafruit stm32 feather

Post here first, or if you can't find a relevant section!
Post Reply
OllyLi
Posts: 2
Joined: Mon Dec 12, 2022 11:33 pm

stm32duino FreeRTOS and adafruit stm32 feather

Post by OllyLi »

Well, things were pretty interesting. Generally speaking STM32DUINO FreeRTOS works on this board, but not when USB is connected.
After flashing any STM32DUINO FreeRTOS example to the board, it seems it got stuck in the "main task" and not switching to any of the tasks created. However, in an accident I found that if my USB was not plugged in properly it works.

Here is what I observed:
1. flash the blink example of STM32DUINO FreeRTOS to it.
2. If the USB is connected to a Windows PC, the LED will NOT blink. (I did not run any app on the PC to open the port)
3. If the USB is connected to a power adapter, not a PC, the LED will blink.
4. With a power wire only USB cable, connecting to PC, the LED blinks as well.
5. I did try to use a STLink to trace the code, it seems it is running the "main task" forever, just not switching to the other tasks.

I am wondering if the USB connection made the timer tick wrong or something else?

Any suggestions?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: stm32duino FreeRTOS and adafruit stm32 feather

Post by ag123 »

vTasks in FreeRTOS isn't literally the same as a normal function in sketches. Memory is fragmented so that it has its own stack and heap.
One also needs to be careful that the allocated memory and stack do not overlap the 'global' stack which is needed for 'drivers'.
i.e. in principle, it isn't possible to call out to *duino 'drivers', the 'drivers' need to be re-adapted so that the reside in 'global' memory.

I've an approach which has worked rather well for me which is to use an event loop

https://github.com/ag88/stm32duino-eventloop

This I developed during the old 'roger's (libmaple) core' (https://github.com/rogerclarkmelbourne/Arduino_STM32) days, but I've not tested it in 'official' stm core https://github.com/stm32duino/Arduino_Core_STM32.
I think it should still work as the eventloop codes has no dependency on the core codes.
(edit: there is a dependency
systick_attach_callback(systick_handler);
I think a similar hook exist in the STM core, this needs to be patched so that the 1ms systick calls the systick handler.)

The event loop codes in itself is 'the sketch', so that to use it, copy all the codes in src
https://github.com/ag88/stm32duino-even ... master/src
into the sketch folder.

mainloop.cpp
https://github.com/ag88/stm32duino-even ... inloop.cpp
is actually the main sketch so that you can rename that mainloop.ino if you prefer

in the 'sketch' there are 2 'tasks' which are actually event handlers, these are in the "tasks" folder
https://github.com/ag88/stm32duino-even ... erTask.cpp
CKeySenderTask.cpp
^ this task read keystrokes 's' = slower, 'f' = faster from Serial (which can be usb) and posts EventID::LedTaskSlower or EventID::LedTaskFaster to the event queue

then in CLedTask.cpp
bool CLedTask::handleEvent(Event& event)
it handles the events and changes the duration accordingly
https://github.com/ag88/stm32duino-even ... sk.cpp#L42

thus you can make the led blink faster or slower from the serial key commands 's' slower, 'f' faster

---
to add your own event handler
inherit from the class CEventHandler
preferably put them in tasks folder and implement bool handleEvent(Event& event);

for your events add them to the enum class EventID : uint16_t { in Event.h

name your event handlers in with tasks IDs
enum class EHandleID : uint8_t {

then in void setup() register your event handler classes
e.g.
https://github.com/ag88/stm32duino-even ... op.cpp#L38

Code: Select all

EventLoop.registerhandler(EHandleID::KeySenderTask, KeySenderTask);
EventLoop.registerhandler(EHandleID::LedTask, LedTask);
to post events to the event queue it looks like
https://github.com/ag88/stm32duino-even ... sk.cpp#L97

Code: Select all

void CKeySenderTask::faster() {
	Event event;
	event.handle_id = EHandleID::LedTask;
	event.event = EventID::LedTaskFaster;
	EventLoop.post(event);
the event loop delivers EventID::Systick events (every 1 ms) to the event handlers so that you can handle that in your event handler

It has worked pretty well for me.
The idea is that use the stack for local variables (i.e. in your functions) and fixed global variables for things that need to stay across function calls.
the stack is unwound when the function returns, and i've been able to 'multi task' in a blue pill stm32f103c8 with only 20k sram.

for more info review the readme.md in the repository
https://github.com/ag88/stm32duino-eventloop
Last edited by ag123 on Tue Dec 13, 2022 6:01 am, edited 7 times in total.
OllyLi
Posts: 2
Joined: Mon Dec 12, 2022 11:33 pm

Re: stm32duino FreeRTOS and adafruit stm32 feather

Post by OllyLi »

Thanks! I will think about the stack issue.
And thanks for the hint about the eventloop thing. But we already have code running on FreeRTOS on other boards, and have to stick to it.
Post Reply

Return to “General discussion”