STM32F103C8T6 how to Thread?

Post here first, or if you can't find a relevant section!
Post Reply
myksj1105
Posts: 54
Joined: Sun Jun 18, 2023 11:35 am

STM32F103C8T6 how to Thread?

Post 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.
by GonzoG » Wed Jun 28, 2023 9:41 pm
@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.
Go to full post
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: STM32F103C8T6 how to Thread?

Post by GonzoG »

There are no threads without a RTOS that supports threads.
myksj1105
Posts: 54
Joined: Sun Jun 18, 2023 11:35 am

Re: STM32F103C8T6 how to Thread?

Post by myksj1105 »

@GonzoG

Image

@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?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: STM32F103C8T6 how to Thread?

Post 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.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32F103C8T6 how to Thread?

Post 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

Code: Select all

//do my work
runs approximately every 100 ms.

and you can have multiple set of those clauses to do different

Code: Select all

//do my work
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
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32F103C8T6 how to Thread?

Post 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.
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32F103C8T6 how to Thread?

Post 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.
Post Reply

Return to “General discussion”