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
}
相关推荐
Brilliantwxx3 分钟前
【STM32】 深度详解总线架构 哈佛总线结构
stm32·嵌入式硬件·架构
恶魔泡泡糖6 分钟前
stm32F103C8T6标准库DWT应用DHT11温湿度传感器数据手册说明
stm32·单片机·嵌入式硬件
深圳市恒锐丰科技杨生8 分钟前
EG1192S 10‑100V 高压集成上管降压 DC‑DC|零功耗使能 Buck 电源芯片
嵌入式硬件·硬件工程
单片机仿真设计10 分钟前
【Proteus 仿真】基于 STM32 单片机鸡舍环境监控系统设计(仿真 + 源码)
stm32·单片机·嵌入式硬件·proteus·毕设
李永奉1 小时前
中科蓝讯SDK开发-提示音音频文件格式转换和压缩教程
网络·单片机·嵌入式硬件·mcu·物联网
春风解人意1 小时前
从零开始学习嵌入式P35----数据库
数据库·嵌入式硬件·学习
单片机仿真设计2 小时前
【Proteus 仿真】基于 STM32 单片机智能楼宇照明控制系统设计(定制仿真咨询)
stm32·单片机·嵌入式硬件·proteus·毕设
ACP广源盛139246256732 小时前
M6/M5 Pro Mac mini 端侧 AI 爆发@ACP#YLB3118 存储扩展芯片在本地 AI 服务中的机会与落地场景
大数据·网络·数据库·人工智能·嵌入式硬件·macos
HUI-4743 小时前
EG1198|ESOP‑8 高性价比内置 MOS 降压 DC‑DC,10‑120V 小功率车载仪表优选
嵌入式硬件·硬件工程
智购科技自动贩卖机14 小时前
自动售货机嵌入式状态机设计实战:从45个事件源到层次型状态机的工程重构
大数据·人工智能·stm32·物联网·重构·硬件架构