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"); fflush(stdout); 00136 } 00137 #endif 00138 } //endof swTimerLED 00139 00140 /* == task1 == 00141 * Print out a counter value local to this task, then delay 00142 * for a random period of time. (Delays range from approx. 00143 * 0.25 - 4.1 seconds) 00144 */ 00145 ESOS_USER_TASK(task1) { 00146 static uint8 u8_cnt=6; 00147 00148 ESOS_TASK_BEGIN(); 00149 while (u8_cnt) { 00150 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00151 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T1 ); 00152 ESOS_TASK_WAIT_ON_SEND_UINT8_AS_HEX_STRING( u8_cnt ); 00153 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00154 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00155 u8_cnt--; 00156 ESOS_TASK_YIELD(); 00157 } // endof while() 00158 ESOS_SIGNAL_SEMAPHORE(sem_T2CanRun, 1); 00159 ESOS_TASK_WAIT_SEMAPHORE(sem_T1CanRun, 1); 00160 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T1 ); 00161 ESOS_TASK_WAIT_ON_SEND_STRING( psz_rv ); 00162 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00163 ESOS_TASK_END(); 00164 } // end task1() 00165 00166 /* == task2 == 00167 * Wait for a signal from task1, then 00168 * Print out a counter value local to this task 00169 */ 00170 ESOS_USER_TASK(task2) { 00171 static uint8 u8_cnt=4; 00172 00173 ESOS_TASK_BEGIN(); 00174 while (u8_cnt) { 00175 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00176 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T2 ); 00177 ESOS_TASK_WAIT_ON_SEND_UINT8_AS_HEX_STRING( u8_cnt ); 00178 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00179 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00180 u8_cnt--; 00181 ESOS_TASK_YIELD(); 00182 } // endof while() 00183 ESOS_SIGNAL_SEMAPHORE(sem_T1CanRun, 1); 00184 ESOS_TASK_WAIT_SEMAPHORE(sem_T2CanRun, 1); 00185 ESOS_TASK_WAIT_ON_SEND_STRING( psz_T2 ); 00186 ESOS_TASK_WAIT_ON_SEND_STRING( psz_rv ); 00187 ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL ); 00188 ESOS_TASK_END(); 00189 } // end task2() 00190 00191 00192 /**************************************************** 00193 * user_init() 00194 **************************************************** 00195 */ 00196 void user_init(void) { 00197 00198 // Call the hardware-provided routines to print the 00199 // HELLO_MSG to the screen. Must use this call because 00200 // the ESOS communications subsystems is not yet fully 00201 // initialized, since this call is in user_init() 00202 // 00203 // In general, users should call hardware-specific 00204 // function like this. 00205 __esos_unsafe_PutString( HELLO_MSG ); 00206 00207 #ifdef __linux 00208 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00209 // the ESOS S/W timers when they expire 00210 esos_RegisterTask( __simulated_isr ); 00211 #endif 00212 00213 // configure our hardware to support to support our application 00214 CONFIG_LED1(); 00215 00216 // initialize the semaphore (initially blocking) 00217 ESOS_INIT_SEMAPHORE( sem_T1CanRun, 0 ); 00218 ESOS_INIT_SEMAPHORE( sem_T2CanRun, 0 ); 00219 00220 // user_init() should register at least one user task 00221 esos_RegisterTask(task1); 00222 esos_RegisterTask(task2); 00223 00224 // register our callback function with ESOS to create a software timer 00225 //esos_RegisterTimer( swTimerLED, 250 ); 00226 00227 } // end user_init()