1. Interrupt service function
1. Interrupt: The process of terminating the current task in response to the request of an internal or external asynchronous event and processing the task required by the asynchronous event is called interruption.
Learn how to write interrupt service functions under UCOSIII!
If you use UCOIII, you will first perform conditional compilation, then execute the interrupt service routine, and finally exit the UCOIII interrupt. The interrupt function is as follows:
void USART1_IRQHandler(void) //Serial port 1 interrupt service routine
{
u8 Res;
#if SYSTEM_SUPPORT_OS //Use UCOS operating system
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //Receive interrupt (the received data must end with 0x0d 0x0a)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //Read the received data
if((USART_RX_STA&0x8000)==0)//receiving is not completed
{
if(USART_RX_STA&0x4000) //Received 0x0d
{
if(Res!=0x0a)USART_RX_STA=0; //Receive error, restart
else USART_RX_STA|=0x8000; //Receiving completed
}
else //Has not received 0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0; //Receive data error, restart receiving
}
}
}
}
#if SYSTEM_SUPPORT_OS
OSIntExit(); //Exit interrupt
#endif
}
Entering and exiting the interrupt service function
After entering the interrupt service function, use the function OSIntEnter()
void OSIntEnter (void)
{
if (OSRunning != OS_STATE_OS_RUNNING) //Judge whether UCOSIII is running
{
return
}
if (OSIntNestingCtr >= (OS_NESTING_CTR)250u)//Judge the number of interrupt nesting
{
return;
}
OSIntNestingCtr++; //Record interrupt nesting times. UCOSIII supports up to 250 levels of interrupt nesting.
}
When exiting the interrupt service function, call the function OSIntExit();
UCOSIII critical section code protection
Critical sections are also called critical regions, which are sections of code that must run completely and continuously and cannot be interrupted. When accessing these critical sections, they need to be protected.
Two different protection methods:
①When the macro OS_CFG_ISR_POST_DEFERRED_EN is 0, UCOSIII uses the method of disabling interrupts to protect the critical section code. When it is set to 1, it will use the method of locking the scheduler to protect the critical section code.
②UCOSIII defines a macro for entering the critical section code: OS_CRITICAL_ENTER(), and defines two macros for exiting the critical section code: OS_CRITICAL_EXIT and OS_CRITICAL_EXIT_NO_SCHED().
For example, in the main function, when creating the start function, it is considered uninterruptible, so it is protected and then unprotected after creation:
int main(void)
{
OS_ERR err; //Error value: all are macro definitions. Find the meaning of the returned error value according to the corresponding value.
CPU_SR_ALLOC();
delay_init(168); //Clock initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt group configuration
uart_init(115200); //Serial port initialization
LED_Init(); //LED initialization
OSInit(&err); //Initialize UCOSIII
OS_CRITICAL_ENTER(); //Enter the critical section
//Create the start task
OSTaskCreate((OS_TCB * )&StartTaskTCB, //Task control block
(CPU_CHAR * )"start task", //task name
(OS_TASK_PTR )start_task, //task function
(void *)0, //Parameters passed to the task function
(OS_PRIO )START_TASK_PRIO, //Task priority
(CPU_STK * )&START_TASK_STK[0], //task stack base address
(CPU_STK_SIZE)START_STK_SIZE/10, //Task stack depth limit
(CPU_STK_SIZE)START_STK_SIZE, //Task stack size
(OS_MSG_QTY) 0, //The maximum number of messages that the task internal message queue can receive. When it is 0, it is forbidden to receive messages.
(OS_TICK) 0, //When the time slice rotation is enabled, the time slice length is 0, which is the default length.
(void *) 0, // user-supplemented storage area
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, //Task options
(OS_ERR * )&err); //Store the return value of the function when an error occurs
OS_CRITICAL_EXIT(); //Exit the critical section
OSStart(&err); //Start UCOSIII
while(1);
}
UCOSIII Time Management
Task Delay
The task in UCOSIII is an infinite loop and a preemptive kernel. In order to prevent high-priority tasks from monopolizing the CPU, other lower-priority tasks can be given the opportunity to obtain CPU usage rights. All tasks except the idle task in UCOSIII must call the delay function provided by the system at the appropriate location to pause the current task for a period of time and perform a task switch.
There are two delay functions, OSTimeDly() and OSTimeDlyHMSM().
The OSTimeDly() function has three working modes: relative mode, periodic mode and absolute mode.
void OSTimeDly (OS_TICK dly, //Time slice number
OS_OPT opt, //Working mode
OS_ERR *p_err) // Error code
The OSTimeDlyHMSM() function works only in relative mode.
void OSTimeDlyHMSM (CPU_INT16U hours,//hour
CPU_INT16U minutes, // minutes
CPU_INT16U seconds, // seconds
CPU_INT32U milli, // subtle
OS_OPT opt, //Working mode, different working modes have different time ranges
OS_ERR *p_err)
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------
Cancel the delay of a task
The delayed task can enter the ready state by calling the function OSTimeDlyResume() in other tasks to cancel the delay. This function will eventually trigger a task scheduling.
Getting and setting the system time
UCOSIII defines a global variable OSTickCtr of type CPU_INT32U to record the number of system clock beats. It is initialized to 0 when OSInit() is called. After that, OSTickCtr increases by 1 for each clock beat.
OSTimeSet() allows users to change the value of the current clock tick counter, use with caution! ! ! !
OSTimeGet() is used to obtain the value of the migration clock beat counter.
Previous article:Communication between UCOSIII tasks
Next article:UCOSIII's five system tasks and hook function writing
Recommended ReadingLatest update time:2024-11-23 06:46
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- The difference between bit rate, baud rate and data transmission rate
- Huawei Comic: What is DSP? - A fast calculator of digital signals
- The annoying computer language - Python
- After Dangdang snatched the company seal from ARM, ARM China staged a drama of snatching the company seal. Was the CEO dismissed?
- *** error 65: access violation at 0x1F0004E8 : no 'read' permission
- MSP430
- 【GD32E503 Review】——01. Preparation
- CircuitPython 6.0.0 released
- Research on on-chip communication architecture in SoC design.pdf
- 【Ufan Learning】Learning Part 1: Ufan Learning Board Function Test