Page 1 of 1
STM32F103C8T6 how to Thread?
Posted: Tue Jun 27, 2023 6:39 am
by myksj1105
STM32F103C8T6 how to Thread?
- MCU: STM32F103C8T6
Nice to meet you. I want to use 'Thread' function.
Q1) Is 'Thread' function possible in 'STM32F103C8T6'?
Q2) Is there any way to do it?
- 'STM32Duino' example file or source code is required.
help. I need your help.
Re: STM32F103C8T6 how to Thread?
Posted: Tue Jun 27, 2023 4:26 pm
by GonzoG
There are no threads without a RTOS that supports threads.
Re: STM32F103C8T6 how to Thread?
Posted: Tue Jun 27, 2023 6:51 pm
by myksj1105
@GonzoG
@GonzoG
Thank you for your kind reply.
1. 'STM32F103C8T6' means that there is no 'thread' due to hardware characteristics.
2. If so, is there a 'thread'(Parallelism Thread) in the 'F4' chip?
Re: STM32F103C8T6 how to Thread?
Posted: Wed Jun 28, 2023 9:41 pm
by GonzoG
@myksj1105
Same thing. Parallel threads can be achieved only on multi core MCUs.
Concurrent threads can be achieved using RTOS. There is a FreeRTOS for STM32duino:
https://github.com/stm32duino/STM32FreeRTOS
But concurrent multithreading should be used only if there is no other way. Jumping between thread is time and resource consuming.
Re: STM32F103C8T6 how to Thread?
Posted: Mon Jul 03, 2023 6:53 am
by ag123
In a simple sketch, it is basically setup() and loop()
and a common implementation is
Code: Select all
void main() {
setup();
while(1)
loop();
}
where main() is the entry point, so loop runs indefinitely. Among the ways that you can *multitask* can be codes like
Code: Select all
uint32_t wait = 100; //ms
uint32_t end;
void setup() {
end = millis() + wait;
}
void loop() {
if( millis() > end ) {
// do my work
end = millis() + wait;
}
}
that would make
runs approximately every 100 ms.
and you can have multiple set of those clauses to do different
there are even simpler 'dumb' implementations like such
Code: Select all
void loop() {
task1();
task2();
task3();
}
it works well if the tasks() don't hold things up
of course, then there are interrupts, hardware timers etc, they are there in the wiki
https://github.com/stm32duino/Arduino_Core_STM32/wiki
Re: STM32F103C8T6 how to Thread?
Posted: Tue Jul 04, 2023 10:31 pm
by dannyf
Q1) Is 'Thread' function possible in 'STM32F103C8T6'?
it depends on your definition of "thread".
as the chip has only one execution unit (cpu or core), true concurrency (aka multi-thread execution) is not possible.
but that's not the end of the world: if you run multiple tasks fast enough, you can create the perception of "concurrency".
one way to achieve that is to use an OS. many different OSs exist for that, with varying degrees of complexity.
The simplest would be to run them serially - assuming that they are individually short enough.
using interrupts is a way of doing that as well.
or some form of a state machine:
Code: Select all
switch (state) {
case state 1: execute task1; break;
case state 2: execute task2; break;
...
}
or its "if" equivalents.
A more structured way of doin that is protothreads.
http://dunkels.com/adam/pt/
or my "tiny scheduler":
Code: Select all
//tiny scheduler macro
#define TS_RUN_WHILE(cs) if (cs) //tiny scheduler macro
you can use it this way:
Code: Select all
TS_RUN_WHILE(condition1) task1(); //run task1 if condition1 is true
TS_RUN_WHILE(condition2) task2(); //run task2 if condition2 is true
...
it is nothing but the "if" version of the state machine approach mentioned above.
the simplicity comes at a huge limitation: those tasks cannot hang. That limitation actually can be quite effectively addressed on a mcu, via a watchdog.
Re: STM32F103C8T6 how to Thread?
Posted: Tue Jul 04, 2023 10:35 pm
by dannyf
Code: Select all
void loop() {
if( millis() > end ) {
// do my work
end = millis() + wait;
}
}
there is a bug in that: as "end" rolls over, "do my work" will be executed multiple times, until "millis()" catches up with "end".
the way I do it is slightly different:
Code: Select all
void loop() {
if( millis() - millis0 > DURATION ) {
// do my work
millis0 += DURATION; //advance millis
}
}
it relies on millis() / millis0 being a "uint32_t" type.