FreeRTOS 任务的挂起与恢复

任务的挂起与恢复的API函数

挂起 - vTaskSuspend

挂起任务类似暂停,可恢复; 删除任务,无法恢复,类似“人死两清” 

使用

必须将 INCLUDE_vTaskSuspend 定义为 1 才能使用此函数

void vTaskSuspend( TaskHandle_t xTaskToSuspend );
//xTaskToSuspend 	被挂起的任务句柄。传递空句柄将导致调用任务被暂停

对 vTaskSuspend 的调用不会累积次数,例如:若在同一任务上调用 vTaskSuspend () 两次,将仍然仅需调用一次 vTaskResume (),即可准备完毕暂停的任务。 

恢复 - vTaskResume

恢复被挂起的任务

使用

必须将 INCLUDE_vTaskSuspend 定义为 1 才能使用此函数。

 void vTaskResume( TaskHandle_t xTaskToResume );
//xTaskToResume 	要恢复的任务句柄

由一次或多次调用 vTaskSuspend () 而挂起的任务可通过单次调用 vTaskResume () 重新运行。 

 代码

因为只有部分改动,所以只使用了改动的部分,完整代码请看我上一篇

FreeRTOS - 任务的创建与删除-CSDN博客

  • start_task 任务,用于创建其他3 个任务。
  • KEY1 被按下,调用函数vTaskSuspend()挂起任务1。
  • KEY2 被按下,调用函数vTaskResume()恢复任务1 的运行。
  • 任务1 的任务函数,用于观察任务挂起和恢复的过程。
     
//LED0闪烁500ms
void task1(void * pvParameters)
{
	uint32_t task1_num = 0;
	while(1)
	{
		printf("task1_num:%d\r\n",++task1_num);
		LED0_TOGGLE();
		vTaskDelay(500);
	}
	
}
 
//LED1闪烁500ms
void task2(void * pvParameters)
{
	uint32_t task2_num = 0;
	while(1)
	{
		printf("task2_num:%d\r\n",++task2_num);
		LED1_TOGGLE();
		vTaskDelay(500);
	}
	
}
 
//按下KEY0挂起LED0任务,按下KEY1恢复LED0任务
void task3(void * pvParameters)
{
	uint8_t key = 0;
	while(1)
	{
		
		key = key_scan(0);
		if      (key == KEY0_PRES)
		{
			printf("挂起task1\r\n");
			vTaskSuspend(task1_handler);
			
		}
        else if (key == KEY1_PRES)
		{
			vTaskResume(task1_handler);
			printf("在任务中恢复task1\r\n");
	
		vTaskDelay(10);
	}
}
 

在中断中恢复 - xTaskResumeFromISR

带FromISR后缀是在中断函数中专用的API函数

使用

必须将 INCLUDE_vTaskSuspend INCLUDE_xTaskResumeFromISR 定义为 1 才能使用此函数。

由多次调用 vTaskSuspend() 中的一次调用挂起的任务可通过单次调用 xTaskResumeFromISR() 重新运行

 xTaskResumeFromISR() 通常被视为危险函数,因为其 操作未被锁定。 因此,如果中断可能在任务被挂起之前到达, 从而中断丢失, 则绝对不应使用该函数 来同步任务与中断。 可使用信号量, 或者最好是直达任务通知,来避免这种可能性。

BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume );
//xTaskToResume 	要恢复的任务句柄。
//注意有返回值

 返回:

如果恢复任务导致上下文切换,则返回 pdTRUE,否则返回 pdFALSE

ISR 使用此信息来确定 ISR 之后是否需要上下文切换。

代码

/************************************************************************/

中断EXTI.c中

/************************************************************************/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    delay_ms(20);      /* 消抖 */
    switch(GPIO_Pin)
    {
		BaseType_t A;     //定义一个变量接收返回值
        case WKUP_INT_GPIO_PIN:
            if (WK_UP == 1)
            {
                A = xTaskResumeFromISR(task1_handler);
				printf("在中断中恢复task1\r\n");
            }
			if (A == pdTRUE)
			{
				portYIELD_FROM_ISR(A);
			}
            break;
    }
}

中断恢复注意事项 

一、抢占优先级

 /* Priority grouping:  The interrupt controller (NVIC) allows the bits
         * that define each interrupt's priority to be split between bits that
         * define the interrupt's pre-emption priority bits and bits that define
         * the interrupt's sub-priority.  For simplicity all bits must be defined
         * to be pre-emption priority bits.  The following assertion will fail if
         * this is not the case (if some bits represent a sub-priority).
         *
         * If the application only uses CMSIS libraries for interrupt
         * configuration then the correct setting can be achieved on all Cortex-M
         * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
         * scheduler.  Note however that some vendor specific peripheral libraries
         * assume a non-zero priority group setting, in which cases using a value
         * of zero will result in unpredictable behaviour. 
*/

 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );

上面的意思是: 优先级分组要求所有位用作抢占式,而不能用作子优先级

使用 RTOS 时的相关性

建议将所有优先级位都指定为抢占优先级位, 不保留任何优先级位作为子优先级位。 任何其他配置都会 使 configMAX_SYSCALL_interrupt_PRIORITY 设置与 分配给各个外设中断之间的直接关系复杂化。

大多数系统的默认配置都是所需要的, STM32 驱动器库除外。 如果您使用 STM32 和 STM32 驱动器库, 请通过 调用 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ) 来确保所有优先级位都被指定为抢占优先级位,这一步需要 在启动 RTOS 前完成。

二、中断优先级

/* Is the interrupt number a user defined interrupt? */
    if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
      {
            //Look up the interrupt's priority. 

            ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];

/*             The following assertion will fail if a service routine (ISR) for
             * an interrupt that has been assigned a priority above
             * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
             * function.  ISR safe FreeRTOS API functions must *only* be called
             * from interrupts that have been assigned a priority at or below
             * configMAX_SYSCALL_INTERRUPT_PRIORITY.
             *
             * Numerically low interrupt priority numbers represent logically high
             * interrupt priorities, therefore the priority of the interrupt must
             * be set to a value equal to or numerically *higher* than
             * configMAX_SYSCALL_INTERRUPT_PRIORITY.
             *
             * Interrupts that	use the FreeRTOS API must not be left at their
             * default priority of	zero as that is the highest possible priority,
             * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
             * and	therefore also guaranteed to be invalid.
             *
             * FreeRTOS maintains separate thread and ISR API functions to ensure
             * interrupt entry is as fast and simple as possible.
             *
             * The following links provide detailed information:
             * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
             * https://www.FreeRTOS.org/FAQHelp.html
*/
            configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
       }
使用 RTOS 时的相关性

以 "FromISR" 结尾的 FreeRTOS 函数是中断安全的,但前提是 调用这些函数的中断的逻辑优先级不高于 configMAX_SYSCALL_INTERRUPT_PRIORITY 定义的优先级(configMAX_SYSCALL_INTERRUPT_PRIORITY 在 FreeRTOSConfig.h 头文件中定义)。 因此,对于任何使用一个 RTOS API 函数的中断服务程序, 必须为其手动设置为一个数值优先级, 这个值必须等于或大于 configMAX_SYSCALL_INTERRUPT_PRIORITY 设定 的值。 这确保了中断的逻辑优先级等于或小于 configMAX_SYSCALL_INTRUPT_PRIORITY 设置。

Cortex-M 中断的默认数值优先级为 0。 0 是最高的 优先级。 因此,切勿将使用中断安全 RTOS API 的中断的优先级 保留为其默认值

因此中断优先级的范围就应该是 5 - 15

 

FreeRTOS官方文档

相关推荐

  1. FreeRtos任务恢复实验示例(后续)

    2024-07-22 07:52:04       50 阅读
  2. 【3】STM32·FreeRTOS·任务恢复

    2024-07-22 07:52:04       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 07:52:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 07:52:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 07:52:04       45 阅读
  4. Python语言-面向对象

    2024-07-22 07:52:04       55 阅读

热门阅读

  1. PO设计模式

    2024-07-22 07:52:04       16 阅读
  2. 【Python】探索 Python 中的 slice 方法

    2024-07-22 07:52:04       14 阅读
  3. WEB渗透信息收集篇--IP和端口信息

    2024-07-22 07:52:04       18 阅读
  4. rabbitmq笔记

    2024-07-22 07:52:04       18 阅读
  5. DFS从入门到精通

    2024-07-22 07:52:04       13 阅读
  6. Robot Operating System——借用内存型消息

    2024-07-22 07:52:04       13 阅读
  7. B树(B-Tree)详解

    2024-07-22 07:52:04       17 阅读
  8. IPython与Pandas:数据分析的动态组

    2024-07-22 07:52:04       17 阅读
  9. SSR和SPA渲染模式

    2024-07-22 07:52:04       14 阅读
  10. 《流程引擎原理与实践》开源电子书

    2024-07-22 07:52:04       16 阅读