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 00047 // INCLUDEs go here (First include the main esos.h file) 00048 // After that, the user can include what they need 00049 #include "esos.h" 00050 #ifdef __linux 00051 #include "esos_pc.h" 00052 #include "esos_pc_stdio.h" 00053 00054 // INCLUDE these so that printf() and our PC hacks work 00055 #include <stdio.h> 00056 #include <sys/select.h> 00057 #include <termios.h> 00058 #include <unistd.h> 00059 #else 00060 #include "esos_pic24.h" 00061 #include "esos_pic24_rs232.h" 00062 #endif 00063 00064 // DEFINEs go here 00065 #ifndef __linux 00066 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT() 00067 #define LED1 _LATB15 00068 #else 00069 #define CONFIG_LED1() printf("called CONFIG_LED1()\n"); 00070 uint8 LED1 = TRUE; // LED1 is initially "on" 00071 #endif 00072 00073 // PROTOTYPEs go here 00074 00075 // GLOBALs go here 00076 // Generally, the user-created semaphores will be defined/allocated here 00077 char psz_CRNL[3]= {0x0D, 0x0A, 0}; 00078 char psz_prompt[] = "Enter number 0-9 for echo increment: "; 00079 char psz_done[9]= {' ','D','O','N','E','!',0x0D, 0x0A, 0}; 00080 00081 #ifdef __linux 00082 /* 00083 * Simulate the timer ISR found on a MCU 00084 * The PC doesn't have a timer ISR, so this task will periodically 00085 * call the timer services callback instead. 00086 * USED ONLY FOR DEVELOPMENT AND TESTING ON PC. 00087 * Real MCU hardware doesn't need this task 00088 */ 00089 ESOS_USER_TASK( __simulated_isr ) { 00090 ESOS_TASK_BEGIN(); 00091 while (TRUE) { 00092 // call the ESOS timer services callback just like a real H/W ISR would 00093 __esos_tmrSvcsExecute(); 00094 ESOS_TASK_WAIT_TICKS( 1 ); 00095 00096 } // endof while(TRUE) 00097 ESOS_TASK_END(); 00098 } // end child_task 00099 #endif 00100 00101 /************************************************************************ 00102 * User supplied functions 00103 ************************************************************************ 00104 */ 00105 00106 /* 00107 * An ESOS software timer callback function strobe the heartbeat LED. 00108 * 00109 * Toggles LED1 everytime the callback is called. Exact period is 00110 * determined by application when this timer callback function is 00111 * registered with ESOS. See \ref esos_RegisterTimer 00112 * Application can change timer period on-the-fly with \ref esos_ChangeTimerPeriod 00113 * 00114 * \note Since this heartbeat is performed in an ESOS software 00115 * timer callabck, a flashing LED indicates that the ESOS system 00116 * tick ISR is being called properly. If the LED quits flashing, 00117 * then the ESOS system tick has ceased working. This probably indicates 00118 * some catastrophic failure of the system. However, the cause could 00119 * be poorly-behaved user code that is manipulating the hardware registers 00120 * with the timer or interrupt enables directly. ESOS provides functions 00121 * to change state of interrupts and user code should never modify the 00122 * hardware used by ESOS to implement the system tick. 00123 * \hideinitializer 00124 */ 00125 00126 // user-created timer callback 00127 ESOS_USER_TIMER( swTimerLED ) { 00128 LED1 = !LED1; 00129 #ifdef __linux 00130 if (LED1) { 00131 printf("\a"); 00132 fflush(stdout); 00133 } 00134 #endif 00135 } //endof swTimerLED 00136 00137 /* 00138 * Child task to echo incoming characters back with an 00139 * arbitrary increment. Will continue echoing characters 00140 * until user sends an exclamation point "!" 00141 */ 00142 ESOS_CHILD_TASK(echo_child, uint8 u8_in) { 00143 static uint8 u8_char; 00144 ESOS_TASK_BEGIN(); 00145 do { 00146 ESOS_TASK_WAIT_ON_AVAILABLE_IN_COMM(); 00147 ESOS_TASK_WAIT_ON_GET_UINT8(u8_char); 00148 ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM(); 00149 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00150 ESOS_TASK_WAIT_ON_SEND_UINT8(u8_char+u8_in); 00151 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00152 } while (u8_char != '!'); 00153 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00154 ESOS_TASK_WAIT_ON_SEND_STRING(psz_done); 00155 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00156 ESOS_TASK_END(); 00157 } // end echo_child() 00158 00159 00160 /* 00161 * Task to prompt user for the desired echo increment 00162 * that is between 0-9. After getting increment, this 00163 * task will spawn a child task to do the actual 00164 * echo operation. 00165 */ 00166 ESOS_USER_TASK(prompter) { 00167 static uint8 u8_char; 00168 static ESOS_TASK_HANDLE th_child; 00169 00170 ESOS_TASK_BEGIN(); 00171 while (TRUE) { 00172 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00173 ESOS_TASK_WAIT_ON_SEND_STRING(psz_prompt); 00174 ESOS_TASK_WAIT_ON_AVAILABLE_IN_COMM(); 00175 do { 00176 ESOS_TASK_WAIT_ON_GET_UINT8(u8_char); 00177 } while ((u8_char < '0') | (u8_char > '9')); 00178 ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM(); 00179 ESOS_TASK_WAIT_ON_SEND_STRING(psz_CRNL); 00180 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00181 ESOS_ALLOCATE_CHILD_TASK(th_child); 00182 ESOS_TASK_SPAWN_AND_WAIT(th_child, echo_child, u8_char-'0'); 00183 } 00184 ESOS_TASK_END(); 00185 } // end prompter() 00186 00187 /**************************************************** 00188 * user_init() 00189 **************************************************** 00190 */ 00191 void user_init(void) { 00192 00193 // Call the hardware-provided routines to print the 00194 // HELLO_MSG to the screen. Must use this call because 00195 // the ESOS communications subsystems is not yet fully 00196 // initialized, since this call is in user_init() 00197 // 00198 // In general, users should call hardware-specific 00199 // function like this. 00200 __esos_unsafe_PutString( HELLO_MSG ); 00201 00202 #ifdef __linux 00203 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00204 // the ESOS S/W timers when they expire 00205 esos_RegisterTask( __simulated_isr ); 00206 #endif 00207 00208 // configure our hardware to support to support our application 00209 CONFIG_LED1(); 00210 00211 // user_init() should register at least one user task 00212 esos_RegisterTask(prompter); 00213 00214 // register our callback function with ESOS to create a software timer 00215 esos_RegisterTimer( swTimerLED, 250); 00216 00217 } // end user_init()