00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 00038 // INCLUDEs go here (First include the main esos.h file) 00039 // After that, the user can include what they need 00040 #include "esos.h" 00041 #ifdef __linux 00042 #include "esos_pc.h" 00043 #include "esos_pc_stdio.h" 00044 00045 // INCLUDE these so that printf() and our PC hacks work 00046 #include <stdio.h> 00047 #include <sys/select.h> 00048 #include <termios.h> 00049 #include <unistd.h> 00050 #else 00051 #include "esos_pic24.h" 00052 #include "esos_pic24_rs232.h" 00053 #include "pic24_timer.h" 00054 #endif 00055 00056 // DEFINEs go here 00057 #ifndef __linux 00058 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT() 00059 #define LED1 _LATB15 00060 #else 00061 #define CONFIG_LED1() printf("called CONFIG_LED1()\n"); 00062 uint8 LED1 = TRUE; // LED1 is initially "on" 00063 #endif 00064 00065 00066 #define WAITING_FOR_FALLING_EDGE ESOS_USER_FLAG_0 00067 #define CAPTURED_FLAG ESOS_USER_FLAG_1 00068 00069 // PROTOTYPEs go here 00070 00071 // GLOBALs go here 00072 // Generally, the user-created semaphores will be defined/allocated here 00073 char psz_CRNL[3]= {0x0D, 0x0A, 0}; 00074 char psz_prompt[] = "Press button..."; 00075 char psz_r1[] = "Pulse width = "; 00076 char psz_r2[] = "us\n"; 00077 00078 ESOS_SEMAPHORE( sem_CapturedData ); 00079 00080 volatile UINT32 U32_lastCapture; // UINT32 declared in all_generic.h 00081 volatile UINT32 U32_thisCapture; 00082 volatile int32 u32_delta; 00083 00085 inline void CONFIG_SW1() { 00086 CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input 00087 ENABLE_RB13_PULLUP(); //enable the pullup 00088 CONFIG_INT1_TO_RP(13); //map INT1 to RP13 00089 } 00090 00091 //Timer2/3 used as single 32-bit timer, control word of Timer2 controls timer, 00092 //interrupt status of Timer3 used for the combined timer 00093 void configTimer23(void) { 00094 T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF 00095 | T2_32BIT_MODE_ON 00096 | T2_SOURCE_INT 00097 | T2_PS_1_1 ; 00098 PR2 = 0xFFFF; //maximum period 00099 PR3 = 0xFFFF; //maximum period 00100 TMR3HLD = 0; //write MSW first 00101 TMR2 = 0; //then LSW 00102 ESOS_MARK_PIC24_USER_INTERRUPT_SERVICED(ESOS_IRQ_PIC24_T3); 00103 T2CONbits.TON = 1; //turn on the timer 00104 } 00105 00106 //Interrupt Service Routine for INT1 00107 //void _ISRFAST _INT1Interrupt (void) { 00108 ESOS_USER_INTERRUPT( ESOS_IRQ_PIC24_INT1 ) { 00109 ESOS_MARK_PIC24_USER_INTERRUPT_SERVICED(ESOS_IRQ_PIC24_INT1); //clear the interrupt bit 00110 if (esos_IsUserFlagSet(WAITING_FOR_FALLING_EDGE)) { 00111 if (esos_IsUserFlagClear(CAPTURED_FLAG)) { 00112 U32_lastCapture.u16LoWord = TMR2; 00113 U32_lastCapture.u16HiWord = TMR3HLD; 00114 _INT1EP = 0; //configure for rising edge 00115 esos_ClearUserFlag(WAITING_FOR_FALLING_EDGE); 00116 } //endif CAPTURED_FLAG 00117 } else { 00118 U32_thisCapture.u16LoWord = TMR2; 00119 U32_thisCapture.u16HiWord = TMR3HLD; 00120 u32_delta = U32_thisCapture.u32 - U32_lastCapture.u32; 00121 esos_SetUserFlag(CAPTURED_FLAG); 00122 _INT1EP = 1; //configure for falling edge 00123 esos_SetUserFlag(WAITING_FOR_FALLING_EDGE); 00124 } //endif-else 00125 } //end INT1 ISR 00126 00127 00128 /* 00129 * An ESOS software timer callback function strobe the heartbeat LED. 00130 * 00131 * Toggles LED1 everytime the callback is called. Exact period is 00132 * determined by application when this timer callback function is 00133 * registered with ESOS. See \ref esos_RegisterTimer 00134 * Application can change timer period on-the-fly with \ref esos_ChangeTimerPeriod 00135 * 00136 * \note Since this heartbeat is performed in an ESOS software 00137 * timer callabck, a flashing LED indicates that the ESOS system 00138 * tick ISR is being called properly. If the LED quits flashing, 00139 * then the ESOS system tick has ceased working. This probably indicates 00140 * some catastrophic failure of the system. However, the cause could 00141 * be poorly-behaved user code that is manipulating the hardware registers 00142 * with the timer or interrupt enables directly. ESOS provides functions 00143 * to change state of interrupts and user code should never modify the 00144 * hardware used by ESOS to implement the system tick. 00145 * \hideinitializer 00146 */ 00147 00148 // user-created timer callback 00149 ESOS_USER_TIMER( swTimerLED ) { 00150 LED1 = !LED1; 00151 #ifdef __linux 00152 if (LED1) { 00153 printf("\a"); 00154 fflush(stdout); 00155 } 00156 #endif 00157 } //endof swTimerLED 00158 00159 /* == task1 == 00160 * Print out a counter value local to this task, then delay 00161 * for a random period of time. (Delays range from approx. 00162 * 0.25 - 4.1 seconds) 00163 */ 00164 ESOS_USER_TASK(task1) { 00165 static uint32 u32_pulseWidth; 00166 00167 ESOS_TASK_BEGIN(); 00168 while (TRUE) { 00169 ESOS_TASK_WAIT_ON_SEND_STRING(psz_prompt); 00170 ESOS_TASK_WAIT_UNTIL(esos_IsUserFlagSet(CAPTURED_FLAG)); 00171 u32_pulseWidth = ticksToUs(u32_delta, getTimerPrescale(T2CONbits)); 00172 esos_ClearUserFlag(CAPTURED_FLAG); 00173 ESOS_TASK_WAIT_ON_SEND_STRING(psz_r1); 00174 ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING(u32_pulseWidth); 00175 ESOS_TASK_WAIT_ON_SEND_STRING(psz_r2); 00176 } // endof while 00177 ESOS_TASK_END(); 00178 } // end task1() 00179 00180 /**************************************************** 00181 * user_init() 00182 **************************************************** 00183 */ 00184 void user_init(void) { 00185 00186 // Call the hardware-provided routines to print the 00187 // HELLO_MSG to the screen. Must use this call because 00188 // the ESOS communications subsystems is not yet fully 00189 // initialized, since this call is in user_init() 00190 // 00191 // In general, users should call hardware-specific 00192 // function like this. 00193 __esos_unsafe_PutString( HELLO_MSG ); 00194 00195 #ifdef __linux 00196 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00197 // the ESOS S/W timers when they expire 00198 esos_RegisterTask( __simulated_isr ); 00199 #endif 00200 00201 // configure our hardware to support to support our application 00202 CONFIG_LED1(); 00203 CONFIG_SW1(); 00204 configTimer23(); 00205 00206 // initialize the semaphore (initially blocking) 00207 //ESOS_INIT_SEMAPHORE( sem_T1CanRun, 0 ); 00208 //ESOS_INIT_SEMAPHORE( sem_T2CanRun, 0 ); 00209 00210 // user_init() should register at least one user task 00211 esos_RegisterTask(task1); 00212 //esos_RegisterTask(task2); 00213 00214 // register our callback function with ESOS to create a software timer 00215 esos_RegisterTimer( swTimerLED, 250 ); 00216 00217 // Configure INT1 00218 esos_SetUserFlag(WAITING_FOR_FALLING_EDGE); 00219 _INT1EP = 1; //negative edge triggerred 00220 ESOS_REGISTER_PIC24_USER_INTERRUPT( ESOS_IRQ_PIC24_INT1, ESOS_USER_IRQ_LEVEL1, _INT1Interrupt ); 00221 ESOS_ENABLE_PIC24_USER_INTERRUPT(ESOS_IRQ_PIC24_INT1); 00222 00223 } // end user_init() 00224