FreeRTOS Function call Help.

Hey everyone I am Mick, hope someone can help me.

I am just starting to explorer FreeRTOS for arduino (STM32).
This is a stripped back version of my sketch.

// ----------------------------------------
// Pin assignments and global variables
unsigned long startT;
unsigned long previousMillis_1 = 0;
unsigned long previousMillis_2 = 0;

const int AQ1_IN = 7;
const int AQ2_IN = 6;

const int DQ1_OUT = 7;
const int DQ2_OUT = 6;
// ----------------------------------------

void setup() {
  // pinMode and serial setup
  pinMode(AQ1_IN, INPUT_ANALOG);
  pinMode(AQ2_IN, INPUT_ANALOG);

  pinMode(DQ1_OUT, OUTPUT);
  pinMode(DQ2_OUT, OUTPUT);


void loop() {

  // Get current time
  startT = micros();

  if (startT - previousMillis_1 >= 1000000) {
    previousMillis_1 = micros();

    //int q_pin, int a_pin, int hold_t
    StartC(DQ1_OUT, AQ1_IN, 500000);


  if (startT - previousMillis_2 >= 1100000) {
    previousMillis_2 = micros();

    //int q_pin, int a_pin, int hold_t
    StartC(DQ2_OUT, AQ2_IN, 250000);


void StartC(int q_pin, int a_pin, int hold_t) {

  // q_pin ON
  digitalWrite(q_pin, HIGH);

  unsigned long stamp_t = micros();
  unsigned long loop_t;
  int a_val; 

  int z = 0;
  while (z == 0)  {

    // Reset a_val at start of each loop
    a_val = 0;

    // Set loop_t time
    loop_t = micros();

    // Wait for 250uS to read ADC
    if (micros() - loop_t >= 250) {

      // Read ADC
      a_val + analogRead(a_pin);

      // Check for stop value (timeout or ADC > 2000)
      if (micros() - stamp_t >= hold_t || a_val >= 2000) {

        // q_pin OFF
        digitalWrite(q_pin, LOW);

        // end while loop
        z = 1;


The calls to StartC in the main loop are random at the moment just for testing, but will be triggered externally in the final version. As expected previousMillis_1 and 2 run one after another in turn. The StartC function could be called upto 16 times in 1 second so what I need is for them to run simultaneously.

I have been playing with MapleFreeRTO and got this sketch working beautifully.

#include <MapleFreeRTOS900.h>

void setup() {
    // initialize the digital pin as an output:
    pinMode(PB11, OUTPUT);
    pinMode(PB10, OUTPUT);

    pinMode(PC13, OUTPUT);

    xTaskCreate(vLEDFlashTask_1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
    xTaskCreate(vLEDFlashTask_2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);


static void vLEDFlashTask_1(void *pvParameters) {
    for (;;) {
        digitalWrite(PB11, HIGH);
        digitalWrite(PB11, LOW);

static void vLEDFlashTask_2(void *pvParameters) {
    for (;;) {
        digitalWrite(PB10, HIGH);
        digitalWrite(PB10, LOW);

void loop() {
    // Insert background code here

Now how do I go about calling the StartC function and keep it running and start another instance at the same time? My understanding is static void vLEDFlashTask_1 and 2 are in a loop but my StartC function is passed different variables and only needs to run until a condition is met.

I have been testing and reading alot over the last few days but cant seem to pinpoint what I need, If you can point me in the right direction that would be most helpful.

Thanks for looking over it.


For the STM32, I use

#include <STM32FreeRTOS.h>

I prefer not to have function parameters.

I create a task like this

 xTaskCreate(v_getIMU, "getIMU", 2100, NULL, tskIDLE_PRIORITY + 2, NULL);

I use structures as containers to pass information from one task to the other:

struct stuPosit
float Lat = 0.0;
float Lon = 0.0;
float Alti = 0.0;
float MPH = 0.0;
float KPH = 0.0;
float Hdg = 0.0;
} xPosit;

I use queues to pass copies of the stu or pointers to the stu. I generally pass copies of queues.

QueueHandle_t xQ_Posit;

I use semaphores to protect the resource

SemaphoreHandle_t sema_GetIMU;[/quote]

I use events to trigger the tasks
#define evtGetIMU                  ( 1 << 1 ) // 10

A event wait inside a task

 for (;;)
    xEventGroupWaitBits (eg, evtGetIMU, pdTRUE, pdTRUE, portMAX_DELAY);

This is a useful freeRTOS guide:

I only use the STM32’s for single taskings; such as reading a MMU and sending its data to another unit for processing. The BluePill has only 20K of RAM and will just stop functioning when that RAM limit is hit, not to be confused with the 128K of flash.

I have switched to mainly using ESP32’s with freeRTOS for the brute force; such as doing the MahonyQuaternionUpdate, converting to Euler angles, and converting to servo torque values takes 290uS per calculation with an ESP32.

Thank you very much for taking the time to help me and write that helpful reply. I had a play around this morning before work and I see what you mean

The BluePill has only 20K of RAM and will just stop functioning when that RAM limit is hit

glad you pointed that out or I may have got stuck again. Definitely something to keep in mind when starting and stopping xTaskCreate. I am starting to get my head around it somewhat, it takes a different way of thinking compared to writing a single task. :o

Should vTaskDelete not free up the RAM used in the task it is ending?

Once again your input was much appreciated. Thank you.