当前位置: 首页 > article >正文

STM32之HAL例程-FreeRTOS任务调度流程

FreeRTOS中的SysTick和PendSV中断优先级别

在函数xPortStartScheduler中设置PendSV和Systick中断为最低优先级中断,中断编号为15。

	    /* Make PendSV and SysTick the lowest priority interrupts. */
	portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;
	portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;

#define portNVIC_PENDSV_PRI					( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
#define portNVIC_SYSTICK_PRI				( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24U

#define configKERNEL_INTERRUPT_PRIORITY 		( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )

FreeRTOS调度

1、SYSTICK中断调度

        sysytick作为系统时钟基准,每1ms触发一次中断。分析中断程序,可知系统对各个状态链表进行查询,若需要进行任务切换,就使能PendSV位。

void xPortSysTickHandler( void )
{
	/* The SysTick runs at the lowest interrupt priority, so when this interrupt
	executes all interrupts must be unmasked.  There is therefore no need to
	save and then restore the interrupt mask value as its value is already
	known - therefore the slightly faster vPortRaiseBASEPRI() function is used
	in place of portSET_INTERRUPT_MASK_FROM_ISR(). */
	vPortRaiseBASEPRI();
	{
		/* Increment the RTOS tick. */
		if( xTaskIncrementTick() != pdFALSE )
		{
			/* A context switch is required.  Context switching is performed in
			the PendSV interrupt.  Pend the PendSV interrupt. */
			portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
		}
	}
	vPortClearBASEPRIFromISR();
}

        systick定时器全局监测变量xTickCount。

PRIVILEGED_DATA static volatile TickType_t xTickCount 				= ( TickType_t ) configINITIAL_TICK_COUNT;

2、taskYIELD()任务切换

#define taskYIELD()					portYIELD()
#define portYIELD()																\
{																				\
	/* Set a PendSV to request a context switch. */								\
	portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;								\
																				\
	/* Barriers are normally not required but do ensure the code is completely	\
	within the specified behaviour for the architecture. */						\
	__dsb( portSY_FULL_READ_WRITE );											\
	__isb( portSY_FULL_READ_WRITE );											\
}

3、PendSV中断中任务切换

        任务的切换都是在PendSV中断中进行。

__asm void xPortPendSVHandler( void )
{
	extern uxCriticalNesting;
	extern pxCurrentTCB;
	extern vTaskSwitchContext;

	PRESERVE8

	mrs r0, psp
	isb

	ldr	r3, =pxCurrentTCB		/* Get the location of the current TCB. */
	ldr	r2, [r3]

	stmdb r0!, {r4-r11}			/* Save the remaining registers. */
	str r0, [r2]				/* Save the new top of stack into the first member of the TCB. */

	stmdb sp!, {r3, r14}
	mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
	msr basepri, r0
	dsb
	isb
	bl vTaskSwitchContext
	mov r0, #0
	msr basepri, r0
	ldmia sp!, {r3, r14}

	ldr r1, [r3]
	ldr r0, [r1]				/* The first item in pxCurrentTCB is the task top of stack. */
	ldmia r0!, {r4-r11}			/* Pop the registers and the critical nesting count. */
	msr psp, r0
	isb
	bx r14
	nop
}


http://www.kler.cn/a/444787.html

相关文章:

  • ViEW生命周期
  • 数智化转型是什么?
  • FPGA:FPGA器件选型
  • 多协议视频监控汇聚/视频安防系统Liveweb搭建智慧园区视频管理平台
  • 基于SpringBoot+Vue实现的个人备忘录系统
  • 智能工厂的设计软件 三种处理单元(NPU/GPU/CPU)及其在深度学习框架中的作用 之4(百度文库答问 之2)
  • 罗德与施瓦茨ZN-Z129E网络分析仪校准套件具体参数
  • Zerotier + VSCode远程连接实验室的服务器、Xshell连接远程服务器
  • OpenEuler 22.03 安装 flink-1.17.2 集群
  • 相机雷达外参标定综述“Automatic targetless LiDAR–camera calibration: a survey“
  • Kafka集群篇
  • python paddle实现语音识别、语音合成
  • 《深入浅出Apache Spark》系列⑤:Spark SQL的表达式优化
  • F5中获取客户端ip地址(client ip)
  • ASP.NET Core 与 Blazor:现代 Web 开发技术的全新视角
  • 方正畅享全媒体新闻采编系统 screen.do SQL注入漏洞复现
  • Linux系统加固
  • macOS 显示或不显示隐藏文件
  • qlib优缺点
  • webdriver 反爬虫 (selenium反爬虫) 绕过
  • pnpm开发环境搭建
  • Knowledge Graph Prompting for Multi-Document Question Answering
  • 青少年编程与数学 02-004 Go语言Web编程 11课题、认证、授权与安全
  • Python中所有子图标签Legend显示详解
  • CSS 网页布局
  • 视频直播点播平台EasyDSS与无人机技术的森林防火融合应用