2
votes

I am using MPU9250-breakout board with Arduino Uno.
The library I used is from here.
Below is my code.

#include <Arduino_FreeRTOS.h>

#include "mpu9250.h"

MPU9250 IMU(Wire,0x68);
int status;

void task_1(void *pvParameters)
{
  (void) pvParameters;

  for (;;)
  {
  }
}

void task_2(void *pvParameters)
{
  (void) pvParameters;

  for (;;)
  {
  }
}

void setup() {
  Serial.begin(115200);
  while(!Serial) {}

  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }

  xTaskCreate(
  task_2,
  (const portCHAR *)"task2", // A name just for humans
  128, // Stack size
  NULL,
  1, // priority
  NULL);

  xTaskCreate(
  task_1,
  (const portCHAR *)"task1", // A name just for humans
  128, // Stack size
  NULL,
  1, // priority
  NULL);

}

void loop()
{

}

The problem is that when there are two tasks defined, the program will be restarted automatically. But when I comment out task_1, the program works fine. The result value of xTaskCreate is correct.

I guess the problem might be the stack or heap size is too small, but I've increased stack and heap size and it's still doesn't work.

Can anyone tell me where the problem might be from?

3
Do you use cooperative or preemptive scheduler? Not enough memory? Do you know that 128 for stack type is of size StackType_t which may be 4bytes, thus effectively allocating 512B of stack? I also don't see where is your scheduler started.tilz0R
Neither, in arduino freeRTOS example, it didn't call such thing. Should I call that in my example?Lion Lai

3 Answers

0
votes

At the end of your setup(), you need to start the scheduler:

// Now the task scheduler, which takes over control of scheduling individual tasks, //is automatically started.

vTaskStartScheduler();

That is all I see different between my project which works and yours.

0
votes

Once you have created your tasks, you have to start scheduler.

Call vTaskStartScheduler() before exiting setup function and after you have created your tasks.

https://www.freertos.org/a00132.html

Just for the info, Arduino-UNO (with ATMega-328P) has very limited RAM and it may happen some tasks won't be created. Check return value of xTaskCreate functions.

0
votes

When I run into this problem, it's usually been that my interrupt vectors weren't pointing to appropriate handlers. So when the RTOS needed to do a context switch, for example, it jumped off into la-la land. Since you say that taking out the tasks allows it to run to the library call, but fails in the same way, it is possible none of your handlers are setup correctly.

The FreeRTOS website has an FAQ about getting new projects to run that I would suggest reading through to help troubleshoot this kind of problem: https://www.freertos.org/FAQHelp.html#faq

You might also look at some of the other AVR examples included with FreeRTOS to see how they have interrupts setup: https://www.freertos.org/a00090.html#ATMEL