Created
June 2, 2023 18:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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