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官方文档

  • 30
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值