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 #endif 00054 00055 // DEFINEs go here 00056 #ifndef __linux 00057 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT() 00058 #define LED1 _LATB15 00059 #else 00060 #define CONFIG_LED1() printf("called CONFIG_LED1()\n"); 00061 uint8 LED1 = TRUE; // LED1 is initially "on" 00062 #endif 00063 00064 // PROTOTYPEs go here 00065 00066 // GLOBALs go here 00067 // Generally, the user-created semaphores will be defined/allocated here 00068 char psz_CRNL[3]= {0x0D, 0x0A, 0}; 00069 char psz_T1[] = "Task 1: "; 00070 char psz_T2[] = "Task 2: "; 00071 00072 ESOS_SEMAPHORE( sem_T2CanRun ); 00073 00074 #ifdef __linux 00075 /* 00076 * Simulate the timer ISR found on a MCU 00077 * The PC doesn't have a timer ISR, so this task will periodically 00078 * call the timer services callback instead. 00079 * USED ONLY FOR DEVELOPMENT AND TESTING ON PC. 00080 * Real MCU hardware doesn't need this task 00081 */ 00082 ESOS_USER_TASK( __simulated_isr ) { 00083 ESOS_TASK_BEGIN(); 00084 while (TRUE) { 00085 // call the ESOS timer services callback just like a real H/W ISR would 00086 __esos_tmrSvcsExecute(); 00087 ESOS_TASK_WAIT_TICKS( 1 ); 00088 00089 } // endof while(TRUE) 00090 ESOS_TASK_END(); 00091 } // end child_task 00092 #endif 00093 00094 /************************************************************************ 00095 * User supplied functions 00096 ************************************************************************ 00097 */ 00098 00099 /* 00100 * return an uint32 that can be used for a reasonable delay 00101 * should not be too short (~255 ticks) and not too long (~4096 ticks) 00102 */ 00103 inline uint32 getRandomDelay() { 00104 return ((esos_GetRandomUint32() & 0x0FFF)|0x100); 00105 } 00106 00107 /* 00108 * An ESOS software timer callback function strobe the heartbeat LED. 00109 * 00110 * Toggles LED1 everytime the callback is called. Exact period is 00111 * determined by application when this timer callback function is 00112 * registered with ESOS. See \ref esos_RegisterTimer 00113 * Application can change timer period on-the-fly with \ref esos_ChangeTimerPeriod 00114 * 00115 * \note Since this heartbeat is performed in an ESOS software 00116 * timer callabck, a flashing LED indicates that the ESOS system 00117 * tick ISR is being called properly. If the LED quits flashing, 00118 * then the ESOS system tick has ceased working. This probably indicates 00119 * some catastrophic failure of the system. However, the cause could 00120 * be poorly-behaved user code that is manipulating the hardware registers 00121 * with the timer or interrupt enables directly. ESOS provides functions 00122 * to change state of interrupts and user code should never modify the 00123 * hardware used by ESOS to implement the system tick. 00124 * \hideinitializer 00125 */ 00126 00127 // user-created timer callback 00128 ESOS_USER_TIMER( swTimerLED ) { 00129 LED1 = !LED1; 00130 #ifdef __linux 00131 if (LED1) { 00132 printf("\a"); 00133 fflush(stdout); 00134 } 00135 #endif 00136 } //endof swTimerLED 00137 00138 /* == task1 == 00139 * Print out a counter value local to this task, then delay 00140 * for a random period of time. (Delays range from approx. 00141 * 0.25 - 4.1 seconds) 00142 */ 00143 ESOS_USER_TASK(task1) { 00144 static uint8 u8_cnt=0; 00145 00146 ESOS_TASK_BEGIN(); 00147 while (TRUE) { 00148 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00149 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T1 ); 00150 ESOS_TASK_WAIT_ON_SEND_UINT8_AS_HEX_STRING( u8_cnt ); 00151 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00152 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00153 ESOS_SIGNAL_SEMAPHORE(sem_T2CanRun, 1); 00154 u8_cnt++; 00155 ESOS_TASK_WAIT_TICKS( getRandomDelay() ); 00156 } // endof while(TRUE) 00157 ESOS_TASK_END(); 00158 } // end task1() 00159 00160 /* == task2 == 00161 * Wait for a signal from task1, then 00162 * Print out a counter value local to this task 00163 */ 00164 ESOS_USER_TASK(task2) { 00165 static uint8 u8_cnt=0; 00166 00167 ESOS_TASK_BEGIN(); 00168 while (TRUE) { 00169 ESOS_TASK_WAIT_SEMAPHORE(sem_T2CanRun, 5); 00170 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00171 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T2 ); 00172 ESOS_TASK_WAIT_ON_SEND_UINT8_AS_HEX_STRING( u8_cnt ); 00173 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00174 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00175 u8_cnt++; 00176 } // endof while(TRUE) 00177 ESOS_TASK_END(); 00178 } // end task1() 00179 00180 00181 /**************************************************** 00182 * user_init() 00183 **************************************************** 00184 */ 00185 void user_init(void) { 00186 00187 // Call the hardware-provided routines to print the 00188 // HELLO_MSG to the screen. Must use this call because 00189 // the ESOS communications subsystems is not yet fully 00190 // initialized, since this call is in user_init() 00191 // 00192 // In general, users should call hardware-specific 00193 // function like this. 00194 __esos_unsafe_PutString( HELLO_MSG ); 00195 00196 #ifdef __linux 00197 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00198 // the ESOS S/W timers when they expire 00199 esos_RegisterTask( __simulated_isr ); 00200 #endif 00201 00202 // configure our hardware to support to support our application 00203 CONFIG_LED1(); 00204 00205 // initialize the semaphore (initially blocking) 00206 ESOS_INIT_SEMAPHORE( sem_T2CanRun, 0 ); 00207 00208 // user_init() should register at least one user task 00209 esos_RegisterTask(task1); 00210 esos_RegisterTask(task2); 00211 00212 // register our callback function with ESOS to create a software timer 00213 esos_RegisterTimer( swTimerLED, 250 ); 00214 00215 } // end user_init()