Multi-timer Processing 3 (30-Day homemade operating system – reading notes)

Source: Internet
Author: User

Continuation of timer interrupt processing improvements.

1, Timer interrupt program Timer_interrupt is this.

//Timer Interrupt functionvoidTimer_interrupt (void){    intI=0; Timerctl.count++;  for(i=0; i<max_timer;i++)//Scan all timers.    {        if(Timerctl.timer[i].flag = =timer_flag_using) {Timerctl.count--; if(Timerctl.count = =0)//It's time to lose.{fifo8_put (timerctl.timer[i].fifo,timerctl.timer[i].data); //writing data to the associated FIFOTimerctl.timer[i].flag = Timer_flag_alloc;//change the flag bit            }        }    }        return ;}

The drawback of the program is that each interrupt, the program to cycle Max_timer times, to find the tmer_flag_using in the timer, whether there is a timer time to.
To improve the thought, use the nexttimeout variable to record the next timing time at any time. Special case: At the very beginning, the timer is not set, then the next timer time to the value is big infinity.

Look at the code, change the TIMERCTL data description structure:

struct timer{    int timeout,flag,nexttimeout;     // Set timing time, timer status, next timer time to    struct FIFO8 *fifo;            // Timer-associated FIFO    char data;            // The value to write to the FIFO when the timing time is reached }

To change the initialization code:

 void  timerctl_initial (void   char  i=0  ;               Timer.count  = 0 ; //         radix 0  timerctl.nexttimeout = 0xffffffff ; //  to the next timed time to almost impossible, that is, the timer has not been set here!!!!  for  (I=0 ; i<max_timer;i++< Span style= "color: #000000;"   >) {Timerctl.timer[i].flag  = Timer_flag_nouse; /*   marked as not used  */  

To change the timer setting function:

//Set Timer timer time to timeoutvoidTimer_settime (structtimer* timer,unsignedintTimeout) {Timer.timeout= Timeout +Timrctl.count; Timer.flag= timer_flag_using;//This statement is equivalent to starting the timer//Here 's the code behind this.   if(Timerctl.nexttimeout > Timer.timeout)//now added a new timer, the newly added timer time is shorter, than the original timer all set to go first{timerctl.nexttimeout= Timer.timeout;//Adjust the Nexttimeout value   }        return ;}

Then change the timer interrupt function:

//Timer Interrupt functionvoidTimer_interrupt (void){    intI=0; Timerctl.count++; if(Timerctl.count >timectl.nexttimeout)return;//It 's not even the most recent time.Timerctl.nexttimeout=0xFFFFFFFF;  for(i=0; i<max_timer;i++)//Scan all timers.    {        if(Timerctl.timer[i].flag = =timer_flag_using) {            if(Timerctl.timer[i].timeout <=timerctl.count) {fifo8_put (timerctl.timer[i].fifo,timerctl.timer[i].data); //writing data to the associated FIFOTimerctl.timer[i].flag = Timer_flag_alloc;//change the flag bit            }            Else            {                 if(Timerctl.timer[i].count <timerctl.nexttimeout) {timerctl.nexttimeout= Timerctl.timer[i].count;//Update Minimum value                 }            }        }    }        return ;}

The above code idea is very simple, if the current time is less than the recent timing time, then quit. If the count time exceeds the most recent time, then there must be a timer timing time to, then find this timer, next, is to update the most recent timing time. The above code timer_flag_using from for (i=0;i<max_timer;i++), that is, in the timer being used to find, who is the timer time, the other is not, then it is going to compare with the most recent timing time to update the most recent time.

Multi-timer Processing 3 (30-Day homemade operating system – reading notes)

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.