Non-blocking software timers that have to be polled to determine if they have expired. More...
Data Structures | |
| struct | tmr_t |
| Structure to track state of a timer. More... | |
Defines | |
| #define | TMR_TICKS_PER_SEC SYSTMR_TICKS_PER_SEC |
| The number of timer ticks per second. | |
| #define | TMR_MS_TO_TICKS(delay_in_ms) DIV_ROUND((delay_in_ms)*TMR_TICKS_PER_SEC,1000ul) |
| Macro used to convert a timeout in milliseconds to timer ticks. | |
Typedefs | |
| typedef systmr_ticks_t | tmr_ticks_t |
| Size definition of the tick counter. | |
Enumerations | |
| enum | tmr_state_t |
Timer state. | |
Functions | |
| void | tmr_start (tmr_t *tmr, const tmr_ticks_t delay_in_ticks) |
| Start a timer. | |
| bool_t | tmr_has_expired (tmr_t *tmr) |
| See if a timer has expired. | |
| void | tmr_stop (tmr_t *tmr) |
| Stop a running timer. | |
| void | tmr_restart (tmr_t *tmr) |
| Restart a timer with the delay set with tmr_start(). | |
| void | tmr_reset (tmr_t *tmr) |
| Reset a timer with the delay set with tmr_start(). | |
| void | tmr_wait (const tmr_ticks_t delay_in_ticks) |
| Blocking wait for specified number of ticks. | |
| tmr_ticks_t | tmr_ticks_elapsed (tmr_t *tmr) |
| Return the number of ticks that have elapsed sinced the timer has been started. | |
Example:
#include <avr/interrupt.h> #include "systmr.h" #include "tmr.h" void tmr_test(void) { // Create a timer object tmr_t tmr; // Initialise PIO BIT_SET_HI(PORT_LED_O, BIT_LED_O); BIT_SET_HI(DDR_LED_O, BIT_LED_O); // Initialise module systmr_init(); // Enable global interrupts sei(); // Start timer with a 250 ms timeout tmr_start(&tmr, TMR_MS_TO_TICKS(250)); for(;;) { // Wait until timer has expired while(!tmr_has_expired(&tmr)) { ; } // Restart timer tmr_restart(&tmr); // Toggle LED LED_TOGGLE(); } }
| #define TMR_MS_TO_TICKS | ( | delay_in_ms | ) | DIV_ROUND((delay_in_ms)*TMR_TICKS_PER_SEC,1000ul) |
| [in] | delay_in_ms | Delay in milliseconds |
Definition at line 184 of file tmr.h.
Referenced by xmodem_rx_file().
| void tmr_start | ( | tmr_t * | tmr, | |
| const tmr_ticks_t | delay_in_ticks | |||
| ) |
| [in,out] | tmr | Pointer to a timer object |
| [in] | delay_in_ticks | Delay in timer ticks |
Definition at line 48 of file tmr.c.
References tmr_t::delay_in_ticks, tmr_t::start_tick, tmr_t::state, and systmr_get_counter().
Referenced by tmr_wait(), and xmodem_rx_file().
00049 { 00050 // Save delay in case timer is restarted 00051 tmr->delay_in_ticks = delay_in_ticks; 00052 00053 // Store start tick 00054 tmr->start_tick = systmr_get_counter(); 00055 00056 // Set state to indicate that timer has started 00057 tmr->state = TMR_STARTED; 00058 }
| bool_t tmr_has_expired | ( | tmr_t * | tmr | ) |
| [in,out] | tmr | Pointer to a timer object |
| TRUE | timer expired | |
| FALSE | timer not expired or timer stopped |
Definition at line 60 of file tmr.c.
References tmr_t::delay_in_ticks, tmr_t::start_tick, tmr_t::state, and systmr_get_counter().
Referenced by tmr_wait(), and xmodem_rx_file().
00061 { 00062 tmr_ticks_t tick; 00063 00064 // See if timer has been stopped 00065 if (tmr->state == TMR_STOPPED) return FALSE; 00066 00067 // See if timer has already expired 00068 if (tmr->state == TMR_EXPIRED) return TRUE; 00069 00070 // Fetch current time 00071 tick = systmr_get_counter(); 00072 00073 // Timer expire test 00074 if( (tick - tmr->start_tick) < tmr->delay_in_ticks ) 00075 { 00076 return FALSE; 00077 } 00078 00079 // Set state to indicate that timer has expired 00080 tmr->state = TMR_EXPIRED; 00081 00082 return TRUE; 00083 }
| void tmr_stop | ( | tmr_t * | tmr | ) |
| [in,out] | tmr | Pointer to a timer object |
Definition at line 85 of file tmr.c.
References tmr_t::state.
00086 { 00087 // Stop timer 00088 tmr->state = TMR_STOPPED; 00089 }
| void tmr_restart | ( | tmr_t * | tmr | ) |
The timer will expire from the current timer tick + tmr->delay_in_ticks.
| [in,out] | tmr | Pointer to a timer object |
Definition at line 91 of file tmr.c.
References tmr_t::start_tick, tmr_t::state, and systmr_get_counter().
00092 { 00093 // Store start tick 00094 tmr->start_tick = systmr_get_counter(); 00095 00096 // Set state to indicate that timer has started 00097 tmr->state = TMR_STARTED; 00098 }
| void tmr_reset | ( | tmr_t * | tmr | ) |
The timer will expire on tmr->start_tick + tmr->delay_in_ticks.
Use this function instead of tmr_restart() for periodic timers, because the frequency will not drift over time. An error may accumulate if there is a delay between tmr_has_expired() and tmr_restart(). Thus the prefered usage for a periodic timer is:
for(;;) { // Wait until timer has expired while(tmr_has_expired(&tmr) == FALSE) { ; } // Reset periodic timer tmr_reset(&tmr); // Do something... }
| [in,out] | tmr | Pointer to a timer object |
Definition at line 100 of file tmr.c.
References tmr_t::delay_in_ticks, tmr_t::start_tick, and tmr_t::state.
00101 { 00102 // Calculate and store new start tick 00103 tmr->start_tick += tmr->delay_in_ticks; 00104 00105 // Set state to indicate that timer has started 00106 tmr->state = TMR_STARTED; 00107 }
| void tmr_wait | ( | const tmr_ticks_t | delay_in_ticks | ) |
| [in] | delay_in_ticks | Delay in timer ticks |
Definition at line 109 of file tmr.c.
References tmr_has_expired(), and tmr_start().
00110 { 00111 tmr_t tmr; 00112 00113 tmr_start(&tmr, delay_in_ticks); 00114 00115 while (!tmr_has_expired(&tmr)) 00116 { 00117 ; 00118 } 00119 }
| tmr_ticks_t tmr_ticks_elapsed | ( | tmr_t * | tmr | ) |
| [in,out] | tmr | Pointer to a timer object |
Definition at line 120 of file tmr.c.
References tmr_t::start_tick, and systmr_get_counter().
00121 { 00122 // Fetch current time 00123 tmr_ticks_t tick = systmr_get_counter(); 00124 00125 return (tick - tmr->start_tick); 00126 }
1.6.3