revised code

Dependencies:   mbed

Committer:
nmaududi
Date:
Sat Oct 05 21:17:17 2019 +0000
Revision:
2:64a34ae90bb1
Parent:
0:4fb921928934
revised version for module 4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nmaududi 0:4fb921928934 1 /**----------------------------------------------------------------------------
nmaududi 0:4fb921928934 2 *
nmaududi 0:4fb921928934 3 * \file timer0.cpp
nmaududi 0:4fb921928934 4 -- --
nmaududi 0:4fb921928934 5 -- ECEN 5803 Mastering Embedded System Architecture --
nmaududi 0:4fb921928934 6 -- Project 1 Module 4 --
nmaududi 0:4fb921928934 7 -- Microcontroller Firmware --
nmaududi 0:4fb921928934 8 -- Timer0.cpp --
nmaududi 0:4fb921928934 9 -- --
nmaududi 0:4fb921928934 10 -------------------------------------------------------------------------------
nmaududi 0:4fb921928934 11 --
nmaududi 0:4fb921928934 12 -- Designed for: University of Colorado at Boulder
nmaududi 0:4fb921928934 13 --
nmaududi 0:4fb921928934 14 --
nmaududi 0:4fb921928934 15 -- Designed by: Tim Scherr
nmaududi 0:4fb921928934 16 -- Revised by: Student's name
nmaududi 0:4fb921928934 17 --
nmaududi 0:4fb921928934 18 -- Version: 2.1
nmaududi 0:4fb921928934 19 -- Date of current revision: 2017-09-25
nmaududi 0:4fb921928934 20 -- Target Microcontroller: Freescale MKL25ZVMT4
nmaududi 0:4fb921928934 21 -- Tools used: ARM mbed compiler
nmaududi 0:4fb921928934 22 -- ARM mbed SDK
nmaududi 0:4fb921928934 23 -- Freescale FRDM-KL25Z Freedom Board
nmaududi 0:4fb921928934 24 --
nmaududi 0:4fb921928934 25 --
nmaududi 0:4fb921928934 26 Functional Description:
nmaududi 0:4fb921928934 27 This file contains code for the only interrupt routine, based on the System
nmaududi 0:4fb921928934 28 Timer.
nmaududi 0:4fb921928934 29 The System Timer interrupt happens every
nmaududi 0:4fb921928934 30 100 us as determined by mbed Component Configuration.
nmaududi 0:4fb921928934 31 The System Timer interrupt acts as the real time scheduler for the firmware.
nmaududi 0:4fb921928934 32 Each time the interrupt occurs, different tasks are done based on critical
nmaududi 0:4fb921928934 33 timing requirement for each task.
nmaududi 0:4fb921928934 34 There are 256 timer states (an 8-bit counter that rolls over) so the
nmaududi 0:4fb921928934 35 period of the scheduler is 25.6 ms. However, some tasks are executed every
nmaududi 0:4fb921928934 36 other time (the 200 us group) and some every 4th time (the 400 us group) and
nmaududi 0:4fb921928934 37 so on. Some high priority tasks are executed every time. The code for the
nmaududi 0:4fb921928934 38 tasks is divided up into the groups which define how often the task is
nmaududi 0:4fb921928934 39 executed. The structure of the code is shown below:
nmaududi 0:4fb921928934 40
nmaududi 0:4fb921928934 41 I. Entry and timer state calculation
nmaududi 0:4fb921928934 42 II. 100 us group
nmaududi 0:4fb921928934 43 A. Fast Software timers
nmaududi 0:4fb921928934 44 B. Read Sensors
nmaududi 0:4fb921928934 45 C. Update
nmaududi 0:4fb921928934 46 III. 200 us group
nmaududi 0:4fb921928934 47 A.
nmaududi 0:4fb921928934 48 B.
nmaududi 0:4fb921928934 49 IV. 400 us group
nmaududi 0:4fb921928934 50 A. Medium Software timers
nmaududi 0:4fb921928934 51 B.
nmaududi 0:4fb921928934 52 V. 800 us group
nmaududi 0:4fb921928934 53 A. Set 420 PWM Period
nmaududi 0:4fb921928934 54 VI 1.6 ms group
nmaududi 0:4fb921928934 55 A. Display timer and flag
nmaududi 0:4fb921928934 56 B. Heartbeat/ LED outputs
nmaududi 0:4fb921928934 57 VII 3.2 ms group
nmaududi 0:4fb921928934 58 A. Slow Software Timers
nmaududi 0:4fb921928934 59 VIII 6.4 ms group A
nmaududi 0:4fb921928934 60 A. Very Slow Software Timers
nmaududi 0:4fb921928934 61 IX. Long time group
nmaududi 0:4fb921928934 62 A. Determine Mode
nmaududi 0:4fb921928934 63 B. Heartbeat/ LED outputs
nmaududi 0:4fb921928934 64 X. Exit
nmaududi 0:4fb921928934 65
nmaududi 0:4fb921928934 66 --
nmaududi 0:4fb921928934 67 -- Copyright (c) 2015 Tim Scherr All rights reserved.
nmaududi 0:4fb921928934 68 */
nmaududi 0:4fb921928934 69
nmaududi 0:4fb921928934 70
nmaududi 0:4fb921928934 71 #include "shared.h"
nmaududi 0:4fb921928934 72 //#include "mbed.h"
nmaududi 0:4fb921928934 73 //#include "MKL25Z4.h"
nmaududi 0:4fb921928934 74 #define System Timer_INCREMENT_IN_US 1000
nmaududi 0:4fb921928934 75
nmaududi 0:4fb921928934 76 typedef unsigned char UCHAR;
nmaududi 0:4fb921928934 77 typedef unsigned char bit;
nmaududi 0:4fb921928934 78 typedef unsigned int uint32_t;
nmaududi 0:4fb921928934 79 typedef unsigned short uint16_t;
nmaududi 0:4fb921928934 80
nmaududi 0:4fb921928934 81 /*******************/
nmaududi 0:4fb921928934 82 /* Configurations */
nmaududi 0:4fb921928934 83 /*******************/
nmaududi 0:4fb921928934 84 #ifdef __cplusplus
nmaududi 0:4fb921928934 85 extern "C" {
nmaududi 0:4fb921928934 86 #endif
nmaududi 0:4fb921928934 87 /**********************/
nmaududi 0:4fb921928934 88 /* Definitions */
nmaududi 0:4fb921928934 89 /**********************/
nmaududi 0:4fb921928934 90
nmaududi 0:4fb921928934 91 volatile UCHAR swtimer0 = 0;
nmaududi 0:4fb921928934 92 volatile UCHAR swtimer1 = 0;
nmaududi 0:4fb921928934 93 volatile UCHAR swtimer2 = 0;
nmaududi 0:4fb921928934 94 volatile UCHAR swtimer3 = 0;
nmaududi 0:4fb921928934 95 volatile UCHAR swtimer4 = 0;
nmaududi 0:4fb921928934 96 volatile UCHAR swtimer5 = 0;
nmaududi 0:4fb921928934 97 volatile UCHAR swtimer6 = 0;
nmaududi 0:4fb921928934 98 volatile UCHAR swtimer7 = 0;
nmaududi 0:4fb921928934 99
nmaududi 0:4fb921928934 100 volatile uint16_t SwTimerIsrCounter = 0U;
nmaududi 0:4fb921928934 101 UCHAR display_timer = 0; // 1 second software timer for display
nmaududi 0:4fb921928934 102 UCHAR display_flag = 0; // flag between timer interrupt and monitor.c, like
nmaududi 0:4fb921928934 103 UCHAR LED_timer_flag = 0; // flag between timer interrupt and main.cc
nmaududi 0:4fb921928934 104 UCHAR REDLEDtimer = 0; // timer for the red LED heartbeat
nmaududi 0:4fb921928934 105 // a binary semaphore
nmaududi 0:4fb921928934 106
nmaududi 0:4fb921928934 107 static uint32_t System_Timer_count = 0; // 32 bits, counts for
nmaududi 0:4fb921928934 108 // 119 hours at 100 us period
nmaududi 0:4fb921928934 109 static uint16_t timer0_count = 0; // 16 bits, counts for
nmaududi 0:4fb921928934 110 // 6.5 seconds at 100 us period
nmaududi 0:4fb921928934 111 static UCHAR timer_state = 0;
nmaududi 0:4fb921928934 112 static UCHAR long_time_state = 0;
nmaududi 0:4fb921928934 113 // variable which splits timer_states into groups
nmaududi 0:4fb921928934 114 // tasks are run in their assigned group times
nmaududi 0:4fb921928934 115 // DigitalOut BugMe (PTB9); // debugging information out on PTB9
nmaududi 0:4fb921928934 116 #ifdef __cplusplus
nmaududi 0:4fb921928934 117 }
nmaududi 0:4fb921928934 118 #endif
nmaududi 0:4fb921928934 119
nmaududi 0:4fb921928934 120 /*********************************/
nmaududi 0:4fb921928934 121 /* Start of Code */
nmaududi 0:4fb921928934 122 /*********************************/
nmaududi 0:4fb921928934 123 // I. Entry and Timer State Calculation
nmaududi 0:4fb921928934 124
nmaududi 0:4fb921928934 125 void timer0(void)
nmaududi 0:4fb921928934 126 {
nmaududi 0:4fb921928934 127
nmaududi 0:4fb921928934 128 // BugMe = 1; // debugging signal high during Timer0 interrupt on PTB9
nmaududi 0:4fb921928934 129
nmaududi 0:4fb921928934 130 /************************************************/
nmaududi 0:4fb921928934 131 // Determine Timer0 state and task groups
nmaududi 0:4fb921928934 132 /************************************************/
nmaududi 0:4fb921928934 133 timer_state++; // increment timer_state each time
nmaududi 0:4fb921928934 134 if (timer_state == 0)
nmaududi 0:4fb921928934 135 {
nmaududi 0:4fb921928934 136 long_time_state++; // increment long time state every 25.6 ms
nmaududi 0:4fb921928934 137
nmaududi 0:4fb921928934 138 }
nmaududi 0:4fb921928934 139
nmaududi 0:4fb921928934 140 /*******************************************************************/
nmaududi 0:4fb921928934 141 /* 100 us Group */
nmaududi 0:4fb921928934 142 /*******************************************************************/
nmaududi 0:4fb921928934 143 // II. 100 us Group
nmaududi 0:4fb921928934 144
nmaududi 0:4fb921928934 145 // A. Update Fast Software timers
nmaududi 0:4fb921928934 146 if (swtimer0 > 0) // if not yet expired,
nmaududi 0:4fb921928934 147 (swtimer0)--; // then decrement fast timer (1 ms to 256 ms)
nmaududi 0:4fb921928934 148 if (swtimer1 > 0) // if not yet expired,
nmaududi 0:4fb921928934 149 (swtimer1)--; // then decrement fast timer (1 ms to 256 ms)
nmaududi 0:4fb921928934 150
nmaududi 0:4fb921928934 151 // B. Update Sensors
nmaududi 0:4fb921928934 152
nmaududi 0:4fb921928934 153
nmaududi 0:4fb921928934 154 /*******************************************************************/
nmaududi 0:4fb921928934 155 /* 200 us Group */
nmaududi 0:4fb921928934 156 /*******************************************************************/
nmaududi 0:4fb921928934 157
nmaududi 0:4fb921928934 158 if ((timer_state & 0x01) != 0) // 2 ms group, odds only
nmaududi 0:4fb921928934 159 {
nmaududi 0:4fb921928934 160 ;
nmaududi 0:4fb921928934 161 } // end 2 ms group
nmaududi 0:4fb921928934 162
nmaududi 0:4fb921928934 163 /*******************************************************************/
nmaududi 0:4fb921928934 164 /* 400 us Group */
nmaududi 0:4fb921928934 165 /*******************************************************************/
nmaududi 0:4fb921928934 166 else if ((timer_state & 0x02) != 0)
nmaududi 0:4fb921928934 167 {
nmaududi 0:4fb921928934 168 // IV. 400 us group
nmaududi 0:4fb921928934 169 // timer states 2,6,10,14,18,22,...254
nmaududi 0:4fb921928934 170
nmaududi 0:4fb921928934 171 // A. Medium Software timers
nmaududi 0:4fb921928934 172 if (swtimer2 > 0) // if not yet expired, every other time
nmaududi 0:4fb921928934 173 (swtimer2)--; // then decrement med timer (4 ms to 1024 ms)
nmaududi 0:4fb921928934 174 if (swtimer3 > 0) // if not yet expired, every other time
nmaududi 0:4fb921928934 175 (swtimer3)--; // then decrement med timer (4 ms to 1024 ms)
nmaududi 0:4fb921928934 176
nmaududi 0:4fb921928934 177 // B.
nmaududi 0:4fb921928934 178 } // end 4 ms group
nmaududi 0:4fb921928934 179
nmaududi 0:4fb921928934 180 /*******************************************************************/
nmaududi 0:4fb921928934 181 /* 800 us Group */
nmaududi 0:4fb921928934 182 /*******************************************************************/
nmaududi 0:4fb921928934 183 else if ((timer_state & 0x04) != 0)
nmaududi 0:4fb921928934 184 {
nmaududi 0:4fb921928934 185 // V. 8 ms group
nmaududi 0:4fb921928934 186 // timer states 4, 12, 20, 28 ... 252 every 1/8
nmaududi 0:4fb921928934 187
nmaududi 0:4fb921928934 188 // A. Set
nmaududi 0:4fb921928934 189 } // end 8 ms group
nmaududi 0:4fb921928934 190
nmaududi 0:4fb921928934 191 /*******************************************************************/
nmaududi 0:4fb921928934 192 /* 1.6 ms Group */
nmaududi 0:4fb921928934 193 /*******************************************************************/
nmaududi 0:4fb921928934 194 else if ((timer_state & 0x08) != 0)
nmaududi 0:4fb921928934 195 {
nmaududi 0:4fb921928934 196 // VI 1.6 ms group
nmaududi 0:4fb921928934 197 // timer states 8, 24, 40, 56, .... 248 every 1/16
nmaududi 0:4fb921928934 198
nmaududi 0:4fb921928934 199 } // end 1.6 ms group
nmaududi 0:4fb921928934 200
nmaududi 0:4fb921928934 201 /*******************************************************************/
nmaududi 0:4fb921928934 202 /* 3.2 ms Group */
nmaududi 0:4fb921928934 203 /*******************************************************************/
nmaududi 0:4fb921928934 204 else if ((timer_state & 0x10) != 0)
nmaududi 0:4fb921928934 205 {
nmaududi 0:4fb921928934 206 // VII 3.2 ms group
nmaududi 0:4fb921928934 207 // timer states 16, 48, 80, 112, 144, 176, 208, 240
nmaududi 0:4fb921928934 208
nmaududi 0:4fb921928934 209 // A. Slow Software Timers
nmaududi 0:4fb921928934 210 if (swtimer4 > 0) // if not yet expired, every 32nd time
nmaududi 0:4fb921928934 211 (swtimer4)--; // then decrement slow timer (32 ms to 8 s)
nmaududi 0:4fb921928934 212 if (swtimer5 > 0) // if not yet expired, every 32nd time
nmaududi 0:4fb921928934 213 (swtimer5)--; // then decrement slow timer (32 ms to 8 s)
nmaududi 0:4fb921928934 214
nmaududi 0:4fb921928934 215 // B. Update
nmaududi 0:4fb921928934 216
nmaududi 0:4fb921928934 217 } // end 3.2 ms group
nmaududi 0:4fb921928934 218
nmaududi 0:4fb921928934 219 /*******************************************************************/
nmaududi 0:4fb921928934 220 /* 6.4 ms Group A */
nmaududi 0:4fb921928934 221 /*******************************************************************/
nmaududi 0:4fb921928934 222 else if ((timer_state & 0x20) != 0)
nmaududi 0:4fb921928934 223 {
nmaududi 0:4fb921928934 224 // VIII 6.4 ms group A
nmaududi 0:4fb921928934 225 // timer states 32, 96, 160, 224
nmaududi 0:4fb921928934 226
nmaududi 0:4fb921928934 227 // A. Very Slow Software Timers
nmaududi 0:4fb921928934 228 if (swtimer6 > 0) // if not yet expired, every 64th
nmaududi 0:4fb921928934 229 // time
nmaududi 0:4fb921928934 230 (swtimer6)--; // then decrement very slow timer (6.4 ms to 1.6s)
nmaududi 0:4fb921928934 231
nmaududi 0:4fb921928934 232 if (swtimer7 > 0) // if not yet expired, every 64th
nmaududi 0:4fb921928934 233 // time
nmaududi 0:4fb921928934 234 (swtimer7)--; // then decrement very slow timer (64 ms to 1.6s)
nmaududi 0:4fb921928934 235
nmaududi 0:4fb921928934 236 // B. Update
nmaududi 0:4fb921928934 237
nmaududi 0:4fb921928934 238 } // end 6.4 ms group A
nmaududi 0:4fb921928934 239
nmaududi 0:4fb921928934 240 /*******************************************************************/
nmaududi 0:4fb921928934 241 /* 6.4 ms Group B */
nmaududi 0:4fb921928934 242 /*******************************************************************/
nmaududi 0:4fb921928934 243 else
nmaududi 0:4fb921928934 244 {
nmaududi 0:4fb921928934 245 // IX. 6.4 ms group B
nmaududi 0:4fb921928934 246 // timer states 0, 64, 128, 192
nmaududi 0:4fb921928934 247
nmaududi 0:4fb921928934 248 // A. Update
nmaududi 0:4fb921928934 249
nmaududi 0:4fb921928934 250 // A. Display timer and flag
nmaududi 0:4fb921928934 251 display_timer--; // decrement display timer every 6.4 ms. Total time is
nmaududi 0:4fb921928934 252 // 256*6.4ms = 1.6384 seconds.
nmaududi 0:4fb921928934 253 if (display_timer == 1)
nmaududi 0:4fb921928934 254 display_flag = 1; // every 1.6384 seconds, now OK to display
nmaududi 0:4fb921928934 255
nmaududi 0:4fb921928934 256
nmaududi 0:4fb921928934 257 // B. Heartbeat/ LED outputs
nmaududi 0:4fb921928934 258 // Generate Outputs ************************************
nmaududi 0:4fb921928934 259
nmaududi 0:4fb921928934 260 //ECEN 5803 add code as indicated
nmaududi 0:4fb921928934 261 // Create an 0.5 second RED LED heartbeat here. timer0_count == 50 (toggle LED)
nmaududi 0:4fb921928934 262 REDLEDtimer++; // increment LED timer every 6.4 ms. Total time is
nmaududi 0:4fb921928934 263 // 78*6.4ms = 499.2 milliseconds.
nmaududi 0:4fb921928934 264 if (REDLEDtimer ==0x4E)
nmaududi 0:4fb921928934 265 {
nmaududi 0:4fb921928934 266 LED_timer_flag = 1; // // every 499.2 milliseconds, now OK toggled red LED
nmaududi 0:4fb921928934 267 REDLEDtimer = 0; //reset the value of LED timer back to 0
nmaududi 0:4fb921928934 268 }
nmaududi 0:4fb921928934 269
nmaududi 0:4fb921928934 270
nmaududi 0:4fb921928934 271
nmaududi 0:4fb921928934 272 } // end 6.4 ms group B
nmaududi 0:4fb921928934 273
nmaududi 0:4fb921928934 274 /*******************************************************************/
nmaududi 0:4fb921928934 275 /* Long Time Group */
nmaududi 0:4fb921928934 276 /*******************************************************************/
nmaududi 0:4fb921928934 277 if (((long_time_state & 0x01) != 0) && (timer_state == 0))
nmaududi 0:4fb921928934 278 // every other long time, every 51.2 ms
nmaududi 0:4fb921928934 279 {
nmaududi 0:4fb921928934 280 // X. Long time group
nmaududi 0:4fb921928934 281 //
nmaududi 0:4fb921928934 282 // clear_watchdog_timer();
nmaududi 0:4fb921928934 283 }
nmaududi 0:4fb921928934 284 // Re-enable interrupts and return
nmaududi 0:4fb921928934 285 System_Timer_count++;
nmaududi 0:4fb921928934 286 timer0_count++;
nmaududi 0:4fb921928934 287 SwTimerIsrCounter++;
nmaududi 0:4fb921928934 288 // Bugme = 0; // debugging signal high during Timer0 interrupt on PTB9
nmaududi 0:4fb921928934 289 // unmask Timer interrupt (now done by mBed library)
nmaududi 0:4fb921928934 290
nmaududi 0:4fb921928934 291 // enables timer interrupt again (now done by mBed Library
nmaududi 0:4fb921928934 292
nmaududi 0:4fb921928934 293 }
nmaududi 0:4fb921928934 294
nmaududi 0:4fb921928934 295
nmaududi 0:4fb921928934 296