revised code
Embed:
(wiki syntax)
Show/hide line numbers
timer0.cpp
Go to the documentation of this file.
00001 /**---------------------------------------------------------------------------- 00002 * 00003 * \file timer0.cpp 00004 -- -- 00005 -- ECEN 5803 Mastering Embedded System Architecture -- 00006 -- Project 1 Module 4 -- 00007 -- Microcontroller Firmware -- 00008 -- Timer0.cpp -- 00009 -- -- 00010 ------------------------------------------------------------------------------- 00011 -- 00012 -- Designed for: University of Colorado at Boulder 00013 -- 00014 -- 00015 -- Designed by: Tim Scherr 00016 -- Revised by: Student's name 00017 -- 00018 -- Version: 2.1 00019 -- Date of current revision: 2017-09-25 00020 -- Target Microcontroller: Freescale MKL25ZVMT4 00021 -- Tools used: ARM mbed compiler 00022 -- ARM mbed SDK 00023 -- Freescale FRDM-KL25Z Freedom Board 00024 -- 00025 -- 00026 Functional Description: 00027 This file contains code for the only interrupt routine, based on the System 00028 Timer. 00029 The System Timer interrupt happens every 00030 100 us as determined by mbed Component Configuration. 00031 The System Timer interrupt acts as the real time scheduler for the firmware. 00032 Each time the interrupt occurs, different tasks are done based on critical 00033 timing requirement for each task. 00034 There are 256 timer states (an 8-bit counter that rolls over) so the 00035 period of the scheduler is 25.6 ms. However, some tasks are executed every 00036 other time (the 200 us group) and some every 4th time (the 400 us group) and 00037 so on. Some high priority tasks are executed every time. The code for the 00038 tasks is divided up into the groups which define how often the task is 00039 executed. The structure of the code is shown below: 00040 00041 I. Entry and timer state calculation 00042 II. 100 us group 00043 A. Fast Software timers 00044 B. Read Sensors 00045 C. Update 00046 III. 200 us group 00047 A. 00048 B. 00049 IV. 400 us group 00050 A. Medium Software timers 00051 B. 00052 V. 800 us group 00053 A. Set 420 PWM Period 00054 VI 1.6 ms group 00055 A. Display timer and flag 00056 B. Heartbeat/ LED outputs 00057 VII 3.2 ms group 00058 A. Slow Software Timers 00059 VIII 6.4 ms group A 00060 A. Very Slow Software Timers 00061 IX. Long time group 00062 A. Determine Mode 00063 B. Heartbeat/ LED outputs 00064 X. Exit 00065 00066 -- 00067 -- Copyright (c) 2015 Tim Scherr All rights reserved. 00068 */ 00069 00070 00071 #include "shared.h" 00072 //#include "mbed.h" 00073 //#include "MKL25Z4.h" 00074 #define System Timer_INCREMENT_IN_US 1000 00075 00076 typedef unsigned char UCHAR; 00077 typedef unsigned char bit; 00078 typedef unsigned int uint32_t; 00079 typedef unsigned short uint16_t; 00080 00081 /*******************/ 00082 /* Configurations */ 00083 /*******************/ 00084 #ifdef __cplusplus 00085 extern "C" { 00086 #endif 00087 /**********************/ 00088 /* Definitions */ 00089 /**********************/ 00090 00091 volatile UCHAR swtimer0 = 0; 00092 volatile UCHAR swtimer1 = 0; 00093 volatile UCHAR swtimer2 = 0; 00094 volatile UCHAR swtimer3 = 0; 00095 volatile UCHAR swtimer4 = 0; 00096 volatile UCHAR swtimer5 = 0; 00097 volatile UCHAR swtimer6 = 0; 00098 volatile UCHAR swtimer7 = 0; 00099 00100 volatile uint16_t SwTimerIsrCounter = 0U; 00101 UCHAR display_timer = 0; // 1 second software timer for display 00102 UCHAR display_flag = 0; // flag between timer interrupt and monitor.c, like 00103 UCHAR LED_timer_flag = 0; // flag between timer interrupt and main.cc 00104 UCHAR REDLEDtimer = 0; // timer for the red LED heartbeat 00105 // a binary semaphore 00106 00107 static uint32_t System_Timer_count = 0; // 32 bits, counts for 00108 // 119 hours at 100 us period 00109 static uint16_t timer0_count = 0; // 16 bits, counts for 00110 // 6.5 seconds at 100 us period 00111 static UCHAR timer_state = 0; 00112 static UCHAR long_time_state = 0; 00113 // variable which splits timer_states into groups 00114 // tasks are run in their assigned group times 00115 // DigitalOut BugMe (PTB9); // debugging information out on PTB9 00116 #ifdef __cplusplus 00117 } 00118 #endif 00119 00120 /*********************************/ 00121 /* Start of Code */ 00122 /*********************************/ 00123 // I. Entry and Timer State Calculation 00124 00125 void timer0(void) 00126 { 00127 00128 // BugMe = 1; // debugging signal high during Timer0 interrupt on PTB9 00129 00130 /************************************************/ 00131 // Determine Timer0 state and task groups 00132 /************************************************/ 00133 timer_state++; // increment timer_state each time 00134 if (timer_state == 0) 00135 { 00136 long_time_state++; // increment long time state every 25.6 ms 00137 00138 } 00139 00140 /*******************************************************************/ 00141 /* 100 us Group */ 00142 /*******************************************************************/ 00143 // II. 100 us Group 00144 00145 // A. Update Fast Software timers 00146 if (swtimer0 > 0) // if not yet expired, 00147 (swtimer0)--; // then decrement fast timer (1 ms to 256 ms) 00148 if (swtimer1 > 0) // if not yet expired, 00149 (swtimer1)--; // then decrement fast timer (1 ms to 256 ms) 00150 00151 // B. Update Sensors 00152 00153 00154 /*******************************************************************/ 00155 /* 200 us Group */ 00156 /*******************************************************************/ 00157 00158 if ((timer_state & 0x01) != 0) // 2 ms group, odds only 00159 { 00160 ; 00161 } // end 2 ms group 00162 00163 /*******************************************************************/ 00164 /* 400 us Group */ 00165 /*******************************************************************/ 00166 else if ((timer_state & 0x02) != 0) 00167 { 00168 // IV. 400 us group 00169 // timer states 2,6,10,14,18,22,...254 00170 00171 // A. Medium Software timers 00172 if (swtimer2 > 0) // if not yet expired, every other time 00173 (swtimer2)--; // then decrement med timer (4 ms to 1024 ms) 00174 if (swtimer3 > 0) // if not yet expired, every other time 00175 (swtimer3)--; // then decrement med timer (4 ms to 1024 ms) 00176 00177 // B. 00178 } // end 4 ms group 00179 00180 /*******************************************************************/ 00181 /* 800 us Group */ 00182 /*******************************************************************/ 00183 else if ((timer_state & 0x04) != 0) 00184 { 00185 // V. 8 ms group 00186 // timer states 4, 12, 20, 28 ... 252 every 1/8 00187 00188 // A. Set 00189 } // end 8 ms group 00190 00191 /*******************************************************************/ 00192 /* 1.6 ms Group */ 00193 /*******************************************************************/ 00194 else if ((timer_state & 0x08) != 0) 00195 { 00196 // VI 1.6 ms group 00197 // timer states 8, 24, 40, 56, .... 248 every 1/16 00198 00199 } // end 1.6 ms group 00200 00201 /*******************************************************************/ 00202 /* 3.2 ms Group */ 00203 /*******************************************************************/ 00204 else if ((timer_state & 0x10) != 0) 00205 { 00206 // VII 3.2 ms group 00207 // timer states 16, 48, 80, 112, 144, 176, 208, 240 00208 00209 // A. Slow Software Timers 00210 if (swtimer4 > 0) // if not yet expired, every 32nd time 00211 (swtimer4)--; // then decrement slow timer (32 ms to 8 s) 00212 if (swtimer5 > 0) // if not yet expired, every 32nd time 00213 (swtimer5)--; // then decrement slow timer (32 ms to 8 s) 00214 00215 // B. Update 00216 00217 } // end 3.2 ms group 00218 00219 /*******************************************************************/ 00220 /* 6.4 ms Group A */ 00221 /*******************************************************************/ 00222 else if ((timer_state & 0x20) != 0) 00223 { 00224 // VIII 6.4 ms group A 00225 // timer states 32, 96, 160, 224 00226 00227 // A. Very Slow Software Timers 00228 if (swtimer6 > 0) // if not yet expired, every 64th 00229 // time 00230 (swtimer6)--; // then decrement very slow timer (6.4 ms to 1.6s) 00231 00232 if (swtimer7 > 0) // if not yet expired, every 64th 00233 // time 00234 (swtimer7)--; // then decrement very slow timer (64 ms to 1.6s) 00235 00236 // B. Update 00237 00238 } // end 6.4 ms group A 00239 00240 /*******************************************************************/ 00241 /* 6.4 ms Group B */ 00242 /*******************************************************************/ 00243 else 00244 { 00245 // IX. 6.4 ms group B 00246 // timer states 0, 64, 128, 192 00247 00248 // A. Update 00249 00250 // A. Display timer and flag 00251 display_timer--; // decrement display timer every 6.4 ms. Total time is 00252 // 256*6.4ms = 1.6384 seconds. 00253 if (display_timer == 1) 00254 display_flag = 1; // every 1.6384 seconds, now OK to display 00255 00256 00257 // B. Heartbeat/ LED outputs 00258 // Generate Outputs ************************************ 00259 00260 //ECEN 5803 add code as indicated 00261 // Create an 0.5 second RED LED heartbeat here. timer0_count == 50 (toggle LED) 00262 REDLEDtimer++; // increment LED timer every 6.4 ms. Total time is 00263 // 78*6.4ms = 499.2 milliseconds. 00264 if (REDLEDtimer ==0x4E) 00265 { 00266 LED_timer_flag = 1; // // every 499.2 milliseconds, now OK toggled red LED 00267 REDLEDtimer = 0; //reset the value of LED timer back to 0 00268 } 00269 00270 00271 00272 } // end 6.4 ms group B 00273 00274 /*******************************************************************/ 00275 /* Long Time Group */ 00276 /*******************************************************************/ 00277 if (((long_time_state & 0x01) != 0) && (timer_state == 0)) 00278 // every other long time, every 51.2 ms 00279 { 00280 // X. Long time group 00281 // 00282 // clear_watchdog_timer(); 00283 } 00284 // Re-enable interrupts and return 00285 System_Timer_count++; 00286 timer0_count++; 00287 SwTimerIsrCounter++; 00288 // Bugme = 0; // debugging signal high during Timer0 interrupt on PTB9 00289 // unmask Timer interrupt (now done by mBed library) 00290 00291 // enables timer interrupt again (now done by mBed Library 00292 00293 } 00294 00295 00296
Generated on Tue Jul 12 2022 20:46:01 by
1.7.2