FreeRTOS operating system -- interrupt management and testing

FreeRTOS operating system learning

preface

Interrupt is a very common feature of microcontroller. Interrupt is generated by hardware. When interrupt is generated, CPU will interrupt the current
The MCU of Cortex-M kernel provides a nested vector interrupt for interrupt management
Controller (NVIC). The NVIC of Cotex-M3 supports up to 240 IRQs (interrupt requests), 1 non maskable interrupt (NMI) and 1 systick (drop)
A) timer interrupt and multiple system exceptions.

1, Interrupt management


Critical zone:
Critical segment code, also known as critical area, refers to those code segments that must run completely and cannot be interrupted, such as some peripherals
The initialization of requires strict timing, and the initialization process cannot be interrupted. FreeRTOS needs to enter the critical section of code
Turn off the interrupt and turn on the interrupt after processing the critical segment code. The FreeRTOS system itself has many critical sections of code,
These codes are protected by critical sections. We also need to add critical sections in some places when writing our own user programs
Code protection.
FreeRTOS has four functions related to critical segment code protection: taskENTER_CRITICAL() ,taskEXIT_CRITICAL() , taskENTER_CRITICAL_FROM_ISR() and
taskEXIT_CRITICAL_FROM_ISR(), these four functions are actually macro definitions in task H is defined in the document. The difference between these four functions is that the first two are critical segment code protection at task level and the last two are critical segment code protection at interrupt level.

void taskcritical_test(void)
{
while(1)
{
taskENTER_CRITICAL(); (1)//Enter critical zone
total_num+=0.01f;
printf("total_num The value of is: %.4f\r\n",total_num);
taskEXIT_CRITICAL(); (2)//Exit critical zone
 vTaskDelay(1000);
} }

(1) . enter the critical zone.
(2) Exit the critical zone.
(1) The code in the middle of and (2) is the critical area code. Note that the critical area code must be simplified! Because entering the critical area will close the interrupt, which will cause the priority to be lower than configmax_ SYSCALL_ INTERRUPT_ The priority interrupt cannot be reached
Response when!
Only after all critical sections of code exit will the interrupt be enabled!

Minimum interrupt priority 15, maximum interrupt priority 5
0-4 is not managed by FreeRTOS, and 5-15 is managed by FreeRTOS

2, FreeRTOS interrupt test experiment

This experiment designed two tasks start_task() and interrupt_task(), the task functions of these two tasks are as follows:
start_task(): create another task. interrupt_task(): interrupt the test task. The interrupt function portdisable of FreeRTOS will be called in the task_ Interrupts() to close the interrupt for a period of time.

1. Code practice



#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#include "FreeRTOS.h"
#include "task.h"

//ÈÎÎñÓÅÏȼ¶
#define START_TASK_PRIO			1
//ÈÎÎñ¶ÑÕ»´óС	
#define START_STK_SIZE 			256  
//ÈÎÎñ¾ä±ú
TaskHandle_t StartTask_Handler;
//ÈÎÎñº¯Êý
void start_task(void *pvParameters);

//ÈÎÎñÓÅÏȼ¶
#define INTERRUPT_TASK_PRIO		2
//ÈÎÎñ¶ÑÕ»´óС	
#define INTERRUPT_STK_SIZE 		256  
//ÈÎÎñ¾ä±ú
TaskHandle_t INTERRUPTTask_Handler;
//ÈÎÎñº¯Êý
void interrupt_task(void *p_arg);

int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é4	 
	delay_init();	    				//ÑÓʱº¯Êý³õʼ»¯	 
	uart_init(115200);					//³õʼ»¯´®¿Ú
	LED_Init();		  					//³õʼ»¯LED
	TIM3_Int_Init(10000-1,7200-1);		//³õʼ»¯¶¨Ê±Æ÷3£¬¶¨Ê±Æ÷ÖÜÆÚ1S
	TIM5_Int_Init(10000-1,7200-1);		//³õʼ»¯¶¨Ê±Æ÷5£¬¶¨Ê±Æ÷ÖÜÆÚ1S
	
	//´´½¨¿ªÊ¼ÈÎÎñ
    xTaskCreate((TaskFunction_t )start_task,            //ÈÎÎñº¯Êý
                (const char*    )"start_task",          //ÈÎÎñÃû³Æ
                (uint16_t       )START_STK_SIZE,        //ÈÎÎñ¶ÑÕ»´óС
                (void*          )NULL,                  //´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý
                (UBaseType_t    )START_TASK_PRIO,       //ÈÎÎñÓÅÏȼ¶
                (TaskHandle_t*  )&StartTask_Handler);   //ÈÎÎñ¾ä±ú              
    vTaskStartScheduler();          //¿ªÆôÈÎÎñµ÷¶È
}

//¿ªÊ¼ÈÎÎñÈÎÎñº¯Êý
void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();           //½øÈëÁÙ½çÇø
    //´´½¨ÖжϲâÊÔÈÎÎñ
    xTaskCreate((TaskFunction_t )interrupt_task,  			//ÈÎÎñº¯Êý
                (const char*    )"interrupt_task", 			//ÈÎÎñÃû³Æ
                (uint16_t       )INTERRUPT_STK_SIZE,		//ÈÎÎñ¶ÑÕ»´óС
                (void*          )NULL,						//´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý
                (UBaseType_t    )INTERRUPT_TASK_PRIO,		//ÈÎÎñÓÅÏȼ¶
                (TaskHandle_t*  )&INTERRUPTTask_Handler); 	//ÈÎÎñ¾ä±ú
	vTaskDelete(StartTask_Handler); //ɾ³ý¿ªÊ¼ÈÎÎñ
    taskEXIT_CRITICAL();            //Í˳öÁÙ½çÇø
}

//ÖжϲâÊÔÈÎÎñº¯Êý 
void interrupt_task(void *pvParameters)
{
	static u32 total_num=0;
    while(1)
    {
		total_num+=1;
		if(total_num==5) 
		{
			portDISABLE_INTERRUPTS();				//¹Ø±ÕÖÐ¶Ï  
			delay_xms(5000);						//ÑÓʱ5s
			portENABLE_INTERRUPTS();
		}
        LED0=~LED0;
        vTaskDelay(1000);
    }
} 

summary

FreeRTOS interrupts. When used, 5-15 are FreeRTOS controllable interrupts. The higher the number, the lower the priority. Add critical area when using interrupt.

Keywords: Single-Chip Microcomputer IoT stm32

Added by Diggler on Mon, 17 Jan 2022 07:22:41 +0200