Signification on LEd blinking on Teensy 4.0

Hello,

I’m using FreeRTOS v10.4.5 on an Arduino Teensy 4.0 (TeensDuino 1.57, IDE 2), with the PWMServo library (find it on github) .

Everytime I call Servo.write, the task freezes and the led is blinking 4 times every 4 s.

I guess it could be a kernel status…
Where can I find it?
What do you think about this problem?

My code:

/****************************************************************
 * 
 * ** This example is based on InvenSense's _confidential_ Application Note "Programming Sequence for DMP Hardware Functions".
 * ** We are grateful to InvenSense for sharing this with us.
 * 
 * ** Important note: by default the DMP functionality is disabled in the library
 * ** as the DMP firmware takes up 14301 Bytes of program memory.
 * ** To use the DMP, you will need to:
 * ** Edit ICM_20948_C.h
 * ** Uncomment line 29: #define ICM_20948_USE_DMP
 * ** Save changes
 * ** If you are using Windows, you can find ICM_20948_C.h in:
 * ** Documents\Arduino\libraries\SparkFun_ICM-20948_ArduinoLibrary\src\util
 *
 * Please see License.md for the license information.
 *
 * Distributed as-is; no warranty is given.
 ***************************************************************/
/***************************************************************
 * V1.1 : 

 */

#include <Arduino.h>
#include "arduino_freertos.h"
#include <PWMServo.h>

#define DEBUG
#define Serial Serial
#define AD0_VAL 1      // The value of the last bit of the I2C address.                \
                       // On the SparkFun 9DoF IMU breakout the default is 1, and when \
                       // the ADR jumper is closed the value becomes 0

// PINOUT 

#define ICM_SDA                   18
#define ICM_SCL                   19
#define OLED_SDA                  17
#define OLED_SCL                  16
#define SERVO_PWM                 2
#define CYCLE_TIME                3
//#define CORRECTION_ACTIVE         4

PWMServo myServo;
int8_t blinkState, cycle;
// Logging timer interrupt

static void task_ICM( void *) {

  for (;;) {
    cycle = !cycle;
    digitalWriteFast(CYCLE_TIME, cycle);
    myServo.write(90);
    ::vTaskDelay(10);
  } // For ever   

}
void setup()
{

  pinMode(CYCLE_TIME,arduino::OUTPUT);  //cycle time measure
  pinMode(SERVO_PWM,arduino::OUTPUT);  //Servo PWM
  
  Serial.begin(115200); // Start the serial console
  while (Serial.available()) // Make sure the serial RX buffer is empty
    Serial.read();

  delay(100);

  // Initialize Servo
  myServo.attach(SERVO_PWM);
  myServo.write(90);
  
  Serial.println();
  Serial.println(PSTR("Configuration complete!"));

  arduino::Serial.println(PSTR("\r\nrunning FreeRTOS kernel " tskKERNEL_VERSION_NUMBER "."));
  ::xTaskCreate(task_ICM, "task_ICM", 2048, nullptr, 3, nullptr);
  ::vTaskStartScheduler();
}

void loop()
{
}

Can you break the code in debugger and see what is it doing? Are you able to use this library without FreeRTOS?

There is no such status code in the kernel. I’d suggest to check with the maintainers of this driver or on Arduino Teensy related forums to find out about this status code.

Hello @aggarg ,
Thanks for your reply.

I’m going to see if I can trace something with the debugger.
It perfectly works when I use it without FreeRTOS and even with FreeRTOS, it works in setup() function.
The problem only occurs in a task.

So ok, I’m going to see also with Teensy maintainers.
Thanks.

One thing to note, in the “Setup” function, FreeRTOS hasn’t been started yet, so that is still very much like without FreeRTOS.

The big question is does the Servo code in the library use resources that FreeRTOS thinks it has for itself. (Like a timer).

Teensy unfortunately doesn’t support debug.
Anyway, PWMServo.write() code is very simple:

static void task_ICM( void *) {

  for (;;) {
    cycle = !cycle;
    digitalWriteFast(CYCLE_TIME, cycle);

    // -----------Code of PWMServo.write()
    uint32_t oldres = analogWriteResolution(12);
    analogWrite(2, 301);
    analogWriteResolution(oldres);
   //-----------
    ::vTaskDelay(10);
  } // For ever   

}

In that case, the task seems to be freezed, digitalWriteFast() doesn’t update the output and the PWM is stopped.
It seems that the problem comes from analogWriteResolution(12)call inside a task !?!

Other tasks are still running.

Well, then just comment it out and retry to track it down if you unfortunately can’t make use of a debugger.

As hs2@ suggested, comment out this call and adjust the analogWrite accordingly and see if it works.

Yes sorry, I didn’t give this info, If I don’t call analogWriteResolution(12) analogWrite works as expected, but with a 8 bits resolution of course.

Sounds strange … I wouldn’t expect that analogWriteResolution is something sophisticated potentially causing any problem… but I don’t know the code.
Or is it possible that something completely different happens just caused by the PWM reconfiguration ?

I don’t but that would be surprising that I’m the first one to encounter this problem.
Maybe something in Teensy’s FreeRTOS implementation…