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 void reverseString(char *psz_s1, char *psz_s2); 00075 00076 // GLOBALs go here 00077 // Generally, the user-created semaphores will be defined/allocated here 00078 00079 #ifdef __linux 00080 /* 00081 * Simulate the timer ISR found on a MCU 00082 * The PC doesn't have a timer ISR, so this task will periodically 00083 * call the timer services callback instead. 00084 * USED ONLY FOR DEVELOPMENT AND TESTING ON PC. 00085 * Real MCU hardware doesn't need this task 00086 */ 00087 ESOS_USER_TASK( __simulated_isr ) { 00088 ESOS_TASK_BEGIN(); 00089 while (TRUE) { 00090 // call the ESOS timer services callback just like a real H/W ISR would 00091 __esos_tmrSvcsExecute(); 00092 ESOS_TASK_WAIT_TICKS( 1 ); 00093 00094 } // endof while(TRUE) 00095 ESOS_TASK_END(); 00096 } // end child_task 00097 #endif 00098 00099 /************************************************************************ 00100 * User supplied functions 00101 ************************************************************************ 00102 */ 00103 00119 ESOS_USER_TASK(heartbeat_LED) { 00120 ESOS_TASK_BEGIN(); 00121 while (TRUE) { 00122 LED1 = !LED1; 00123 #ifdef __linux 00124 if (LED1) { 00125 printf("\a"); 00126 fflush(stdout); 00127 } 00128 #endif 00129 ESOS_TASK_WAIT_TICKS( 500 ); 00130 } // endof while(TRUE) 00131 ESOS_TASK_END(); 00132 } // end upper_case() 00133 00138 ESOS_USER_TASK( reverse_string ) { 00139 static char sz_in[257]; 00140 static char sz_out[257]; 00141 00142 ESOS_TASK_BEGIN(); 00143 while (TRUE) { 00144 ESOS_TASK_WAIT_ON_AVAILABLE_IN_COMM(); 00145 ESOS_TASK_WAIT_ON_GET_STRING( sz_in ); 00146 ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM(); 00147 reverseString( sz_in, sz_out ); 00148 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM(); 00149 ESOS_TASK_WAIT_ON_SEND_STRING( sz_out ); 00150 ESOS_TASK_WAIT_ON_SEND_UINT8('\n'); 00151 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM(); 00152 } // endof while(TRUE) 00153 ESOS_TASK_END(); 00154 } // end reverse_string() 00155 00156 00157 /* 00158 * Inputs a string, outputs the reverse. This file is used 00159 * in three MPLAB projects: 00160 * reverse_string.mcp - polled RX, TX I/O 00161 * uartrx_fifo.mcp - interrupt RX, polled TX I/O 00162 * uartrxtx_fifo.mcp - interrupt RX, interrupt TX I/O 00163 * Interrupt RX inChar1() is selected by defining UART1_RX_INTERRUPT macro 00164 * Interrupt TX outChar1() is selected by defining UART1_TX_INTERRUPT macro 00165 * These macros are defined in their respective MPLAB projects. 00166 * 00167 * EXACTLY THE SAME ROUTINE AS USED IN CHAPTER 10. PLACED IN 00168 * THIS SOURCE FILE FOR CONVENIENCE ONLY!!! 00169 */ 00170 void reverseString(char *psz_s1, char *psz_s2) { 00171 char *psz_s1end; 00172 if (!(*psz_s1)) { 00173 *psz_s2 = 0; //psz_s1 is empty, return. 00174 return; 00175 } 00176 psz_s1end = psz_s1; 00177 //find end of first string 00178 while (*psz_s1end) psz_s1end++; 00179 psz_s1end--; //backup one to first non-zero byte 00180 //now copy to S2 in reverse order 00181 while (psz_s1end != psz_s1) { 00182 *psz_s2 = *psz_s1end; 00183 psz_s1end--; 00184 psz_s2++; 00185 } 00186 //copy last byte 00187 *psz_s2 = *psz_s1end; 00188 psz_s2++; 00189 //mark end of string 00190 *psz_s2 = 0; 00191 } 00192 00193 00194 /**************************************************** 00195 * user_init() 00196 **************************************************** 00197 */ 00198 void user_init(void) { 00199 00200 // Call the hardware-provided routines to print the 00201 // HELLO_MSG to the screen. Must use this call because 00202 // the ESOS communications subsystems is not yet fully 00203 // initialized, since this call is in user_init() 00204 // 00205 // In general, users should call hardware-specific 00206 // function like this. 00207 __esos_unsafe_PutString( HELLO_MSG ); 00208 00209 #ifdef __linux 00210 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00211 // the ESOS S/W timers when they expire 00212 esos_RegisterTask( __simulated_isr ); 00213 #endif 00214 00215 // configure our hardware to support to support our application 00216 CONFIG_LED1(); 00217 00218 // user_init() should register at least one user task 00219 esos_RegisterTask(heartbeat_LED); 00220 esos_RegisterTask(reverse_string); 00221 00222 } // end user_init()