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
}
相关推荐
nuoxin1141 分钟前
CYUSB4024-FCAXI 是一款USB 20Gbps 控制器-富利威
网络·人工智能·嵌入式硬件·fpga开发·dsp开发
charlie1145141913 分钟前
嵌入式C++开发第17篇:C++23特性收尾 —— 属性、链接与零开销抽象的最终证明
开发语言·c++·stm32·学习·c++23
wwddgod13 分钟前
STM32L071 串口唤醒stop低功耗模式笔记
笔记·stm32·单片机·低功耗·串口唤醒
liuluyang53015 分钟前
DW I2C寄存器与使用简介
stm32·单片机·嵌入式硬件·dw i2c
Mr..Jackey15 分钟前
RA6809 的 HMI(人机交互) 开发:菜单逻辑架构设计与实现详解(4)
单片机·51单片机·人机交互·交互
笨笨饿28 分钟前
#65_反激电源
stm32·单片机·嵌入式硬件·算法·硬件工程·个人开发
汽车芯猿32 分钟前
嵌入式固件内存占用分析利器:Python实现S19/HEX地址空间可视化工具
python·单片机·嵌入式硬件
LCG元9 小时前
STM32实战:基于STM32F103的Bootloader设计与IAP在线升级
javascript·stm32·嵌入式硬件
不怕犯错,就怕不做10 小时前
Linux-Sensor驱动移植与调试(转载)
linux·驱动开发·嵌入式硬件
LCMICRO-1331084774611 小时前
长芯微LCMDC8584完全P2P替代ADS8584,是一款16位、4通道同步采样的逐次逼近型(SAR)模数转换器(ADC)
stm32·单片机·嵌入式硬件·fpga开发·硬件工程·模数转换器adc