Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HardwareTimersLib by
HardwareTimer.cpp
00001 /* HardwareTimer.cpp 00002 * Tested with mbed board: FRDM-KL46Z 00003 * Author: Mark Gottscho 00004 * mgottscho@ucla.edu 00005 */ 00006 00007 #include "mbed.h" 00008 #include "HardwareTimer.h" 00009 00010 HardwareTimer::HardwareTimer(uint32_t maxRolloverTick, float tickValue, tick_units_t tickUnits) : 00011 __valid(false), 00012 __count(0), 00013 __rolloverValue(0), 00014 __periodic(false), 00015 __num_callbacks(0), 00016 __user_fptr(NULL), 00017 __enabled(false), 00018 __running(false), 00019 __maxRolloverTick(maxRolloverTick), 00020 __tickValue(tickValue), 00021 __tickUnits(tickUnits) 00022 { 00023 } 00024 00025 HardwareTimer::~HardwareTimer() { 00026 disable(); 00027 if (__user_fptr != NULL) //double check 00028 delete __user_fptr; 00029 } 00030 00031 bool HardwareTimer::valid () { 00032 return __valid; 00033 } 00034 00035 bool HardwareTimer::enabled () { 00036 return __enabled; 00037 } 00038 00039 bool HardwareTimer::running () { 00040 return __running; 00041 } 00042 00043 float HardwareTimer::tickValue () { 00044 return __tickValue; 00045 } 00046 00047 float HardwareTimer::tickUnits () { 00048 switch (__tickUnits) { 00049 default: 00050 case ns: 00051 return 1e-9; 00052 case us: 00053 return 1e-6; 00054 case ms: 00055 return 1e-3; 00056 case s: 00057 return 1; 00058 case m: 00059 return 60; 00060 case h: 00061 return 3600; 00062 } 00063 } 00064 00065 void HardwareTimer::enable(void (*fptr)(void)) { 00066 if (!__valid) 00067 return; 00068 00069 //set user function pointer 00070 if (__user_fptr != NULL) 00071 delete __user_fptr; 00072 if (fptr != NULL) 00073 __user_fptr = new FunctionPointer(fptr); 00074 00075 __init_timer(); //Do hardware-specific initialization 00076 00077 __enabled = true; 00078 } 00079 00080 00081 template <typename T> void HardwareTimer::enable(T *tptr, void (T::*mptr)(void)) { 00082 if (!__valid) 00083 return; 00084 00085 if (__enabled) 00086 disable(); 00087 00088 //set user function pointer 00089 if (__user_fptr != NULL) 00090 delete __user_fptr; 00091 if (tptr != NULL && mptr != NULL) 00092 __user_fptr = new FunctionPointer(tptr, mptr); 00093 00094 __init_timer(); //Do hardware-specific initialization 00095 00096 __enabled = true; 00097 } 00098 00099 void HardwareTimer::disable() { 00100 if (!__valid) 00101 return; 00102 00103 __stop_timer(); //Do hardware-specific stop 00104 __running = false; 00105 00106 if (__user_fptr != NULL) //Detach user callback function 00107 delete __user_fptr; 00108 __user_fptr = NULL; 00109 00110 __enabled = false; 00111 } 00112 00113 void HardwareTimer::start(uint32_t callback_tick_count, bool periodic, uint32_t num_callbacks) { 00114 if (!__valid || !__enabled || callback_tick_count > __maxRolloverTick) 00115 return; 00116 00117 __rolloverValue = callback_tick_count; 00118 __periodic = periodic; 00119 if (__periodic) 00120 __num_callbacks = 0; 00121 else 00122 __num_callbacks = num_callbacks; 00123 00124 __start_timer(); //Do hardware-specific start 00125 __running = true; 00126 } 00127 00128 uint32_t HardwareTimer::getMaxCallbackTickCount () { 00129 return __maxRolloverTick; 00130 } 00131 00132 PreciseTime HardwareTimer::getTime() { 00133 if (!__valid) 00134 return PreciseTime(); 00135 00136 uint32_t tick = getTick (); 00137 uint32_t converted_ticks = tick * __tickValue; 00138 00139 switch (__tickUnits) { 00140 default: 00141 case ns: 00142 return PreciseTime::from_ns(converted_ticks); 00143 case us: 00144 return PreciseTime::from_us(converted_ticks); 00145 case ms: 00146 return PreciseTime::from_ms(converted_ticks); 00147 case s: 00148 return PreciseTime::from_s(converted_ticks); 00149 case m: 00150 return PreciseTime::from_m(converted_ticks); 00151 case h: 00152 return PreciseTime::from_h(converted_ticks); 00153 } 00154 }
Generated on Tue Jul 12 2022 20:37:48 by
1.7.2
