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