Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cversek/e2163f91611e4aed814492470c1fd71a to your computer and use it in GitHub Desktop.
Save cversek/e2163f91611e4aed814492470c1fd71a to your computer and use it in GitHub Desktop.
Demo code for issue reproduction, see: https://github.com/stm32duino/STM32FreeRTOS/issues/63
#include <STM32FreeRTOS.h>
//NOTE the DRIVER_PIN is connected to the INTERRUPT_PIN via a wire
const int INTERRUPT_PIN = 11;
const int DRIVER_PIN = 13;
const int DRIVER_TASK_DELAY_MILLIS = 2;
const int DRIVER_TASK_RANDOM_BLOCKING_MICROS_MAX = 2000;
const int COMPETING_TASK_DELAY_MILLIS = 10; //decreasing this brings fault on faster
volatile unsigned long interrupt_counter = 0;
void pin_ISR(){
interrupt_counter++;
}
// this task drives a pin periodically with random added blocking
static void vTask1(void* arg) {
UNUSED(arg);
digitalWrite(DRIVER_PIN,HIGH);
while (1) {
// toggle the pin
vTaskDelay(pdMS_TO_TICKS(DRIVER_TASK_DELAY_MILLIS));
//random delay to simulate work
delayMicroseconds(random(DRIVER_TASK_RANDOM_BLOCKING_MICROS_MAX));
// this transition to low state should trigger the interrupt
digitalWrite(DRIVER_PIN,LOW);
vTaskDelay(pdMS_TO_TICKS(DRIVER_TASK_DELAY_MILLIS));
digitalWrite(DRIVER_PIN,HIGH);
}
}
// this task competes with the driver task vTask1 for context
static void vTask2(void* arg) {
UNUSED(arg);
while (1) {
// Sleep for tiny bit
vTaskDelay(pdMS_TO_TICKS(COMPETING_TASK_DELAY_MILLIS));
}
}
// this task shows the interrupt counter
static void vTask3(void* arg) {
UNUSED(arg);
while (1) {
// Sleep for five seconds
vTaskDelay(pdMS_TO_TICKS(5000));
Serial.println(interrupt_counter);
}
}
void setup() {
pinMode(DRIVER_PIN, OUTPUT);
digitalWrite(DRIVER_PIN,HIGH);
Serial.begin(115200);
while (!Serial){}; //WAIT FOR SERIAL CONNECTION TO OPEN, DEBUG ONLY!
Serial.print("USBD_IRQ_PRIO: ");Serial.println(USBD_IRQ_PRIO);
Serial.print("configMEMMANG_HEAP_NB: ");Serial.println(configMEMMANG_HEAP_NB);
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
int intNum = digitalPinToInterrupt(INTERRUPT_PIN);
attachInterrupt(intNum, pin_ISR, FALLING);
xTaskCreate(vTask1,NULL,configMINIMAL_STACK_SIZE+50,NULL,1,NULL);
xTaskCreate(vTask2,NULL,configMINIMAL_STACK_SIZE+50,NULL,1,NULL);
xTaskCreate(vTask3,NULL,configMINIMAL_STACK_SIZE+500,NULL,1,NULL);
// start FreeRTOS
vTaskStartScheduler();
// should never return
Serial.println(F("Die"));
assert_param(false);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment