stm32 FreeRTOS Interrupt cannot run smooth as i think

51 views Asked by At

I have two tasks, T1 and T2. I want to switch between these two by invoking an ISR, yet, T1 and T2 both have their private vTaskDelay. How to let them run smoother when delay is in the code?

void T1(void){

    while(1){
         
         if (tsk == 1) {

///          doSth
             vTaskDelay(2000) // 2seconds
//           doSth
         }
    }
}
void T2(void){

    while(1){
         
         if (tsk == 2) {

///          doSth
             vTaskDelay(1000) // 1seconds
//           doSth
         }
    }
}

void ISR(){
  if (READGPIO){
   // change tsk between 1 and 2
   
}
}

I tried spliting vTaskDelay into small pieces like this

for (int i = 0; i < 200; i++) {
                vTaskDelay(10);
                if (tsk == 1) break;;
            }

however, it doesnot help with the smoothness i except

2

There are 2 answers

0
gulpr On

It would help if you learned a bit more about inter-task communication primitives before starting programming using RTOS.

Use for example task notifications and give the notification to the particular task from the ISR.

1
JoshuaLee On
void LED_T2( void ){
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
    for(;;){
        if (currentTask == 1) {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET );
            vTaskDelay(1000);
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET );
             

        }
    vTaskDelay(1000);// outside of if ...
    }
}
// after
void LED_T2( void ){
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
    for(;;){
        if (currentTask == 1) {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET );
            vTaskDelay(1000);
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET );
            vTaskDelay(1000);

        }
    
    }
}

I fixed the code that bugs me... It seems like I need to study harder on the behavior of interrupt...

Anyways, thanks for ya'alls' replies