Clock interruption is the driving force of the rt-thread scheduler.

Source: Internet
Author: User

1. How does one implement the system clock interrupt driver rt-thread when the thread time slice is exhausted or the thread is awakened and scheduled after sleep for a period of time? Who is driving this process? The answer is the clock interruption source. Let's take a look at the clock interruption routine: In bsp/stm32f20x/drivers/board. the c source file contains the code of such a clock interruption routine: (the MCU of stm32f20x is used as an example) [cpp]/*** This is the timer interrupt service routine. **/void javasick_handler (void) {/* enter interrupt */rt_interrupt_enter (); // The rt_tick_increase () is interrupted (); // Add tick/* leave interrupt */rt_interrupt_leave (); // exit interrupt} The rt_tick_increase function is defined as follows: [cpp]/*** This function will handle Y kernel there is one tick passed. nor Mally, * this function is invoked by clock ISR. */void rt_tick_increase (void) {struct rt_thread * thread;/* increase the global tick */++ rt_tick; // Add the global variable rt_tick with 1/* check time slice */thread = rt_thread_self (); // obtain the currently scheduled thread -- thread-> remaining_tick; // The remaining time slice of the currently scheduled thread minus 1 if (thread-> remaining_tick = 0) // If the currently scheduled thread does not have any time slice {/* change to initialized tick */thread-> remaining_tick = thread-> init_tick; // Reset the remaining time slice to the initialization time slice/* yield */rt_thread_yield (); // extract the thread from the scheduler ready queue and put it to the end, schedule again}/* check timer */rt_timer_check (); // check whether there is a time to arrive clock on the linked list} The rt_thread_yield function is already done in the previous article. As can be seen from the code above, once the system generates a clock interruption, In the Interrupt Routine, the system first checks whether the remaining time slice of the currently running thread is exhausted, if it is exhausted, it will be removed from the scheduler ready queue to the end, and then re-schedule the thread, and then check whether there is a sleep thread time to arrive. If yes, this will trigger the clock timeout callback function, which was written in the previous article. /* init thread timer */rt_timer_init (& (thread-> thread_timer), // initialize the thread's timer thread-> name, rt_thread_timeout, thread, 0, RT_TIMER_FLAG_ONE_SHOT ); //... it can be seen that during thread Initialization It sets the timeout callback function rt_thread_timeout for the internal clock of the thread. The key is that this function will perform some thread scheduling operations. The source code is defined as follows: [cpp]/*** This function is the timeout function for thread, normally which is invoked * when thread is timeout to wait some resource. ** @ param parameter the parameter of thread timeout function */void rt_thread_timeout (void * parameter) {struct rt_thread * thread; thread = (struct rt_thread *) parameter; /* thread check */RT_AS SERT (thread! = RT_NULL); RT_ASSERT (thread-> stat = RT_THREAD_SUSPEND);/* set error number */thread-> error =-RT_ETIMEOUT; /* remove from suspend list * // remove rt_list_remove (& (thread-> tlist) from the suspended linked list;/* insert to schedule ready list */rt_schedule_insert_thread (thread ); // Add the scheduler/* do schedule */rt_schedule (); // reschedule}. It will add the currently suspended thread to the scheduler ready queue and then reschedule it. 2. Summary of the system clock interrupt engine: when the system generates a clock interrupt, first check whether the currently running thread has any time slice, if it is exhausted, it will be removed from the ready queue and placed at the end, and then rescheduled. Then, it will check whether a suspended thread has reached the time. If so, it will be added to the ready queue of the scheduler, and then reschedule. 3. In the software timer mode, note that if you use the software timer mode, a clock thread timer_thread exists in the system. For details, see worker. It can be seen that when the software timer mode is set (by default, the rt-thread uses the hardware timer), The timer_thread thread is also the driver engine for the rt_thread operating system thread scheduling. 4. How to set the system clock interruption interval when viewing the user manual of the rt-thread operating system, the above mentioned the rt_thead operating system clock, the default interval of each tick is 10 ms, so how does the 10 ms Double come from? The answer is in/bsp/stm32f20x/drivers/board. c source file, and see the implementation of the javasick_configuration function: [cpp]/********************************** **************************************** * *** Function Name: required ick_configuration * Description: Configures the specified ick for OS tick. * Input: None * Output: None * Return: none *************************************** **************************************** /void SysTick_Configuration (Void) {RCC_ClocksTypeDef rcc_clocks; rt_uint32_t CNT; notify (& rcc_clocks); // obtain the system's crystal oscillator frequency (CNT = (rt_uint32_t) Notify/RT_TICK_PER_SECOND; // calculate the number of times the crystal oscillator is a tick time slice SysTick_Config (CNT); // configure the system tick SysTick_CLKSourceConfig (SysTick_CLKSource_HCLK); // configure the clock source} as shown in the preceding code, the rt_thread system tick is configured by the RT_TICK_PER_SECOND Macro. RT_TICK_PER_SECOND is in the header file rtconfig. [cpp]/* Tick per Second */ # Define RT_TICK_PER_SECOND 100 this parameter indicates how many tick s are contained in one second. The default value is 100. By default, 100 tick s are contained in one second, that is, each tick is 10 ms, do you understand now? If we want to modify the time interval of each tick, we only need to modify the value of the RT_TICK_PER_SECOND macro.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.