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 00042 // INCLUDEs go here (First include the main esos.h file) 00043 // After that, the user can include what they need 00044 #include "esos.h" 00045 #ifdef __linux 00046 #include "esos_pc.h" 00047 #include "esos_pc_stdio.h" 00048 // INCLUDE these so that printf() and our PC hacks work 00049 #include <stdio.h> 00050 #include <sys/select.h> 00051 #include <termios.h> 00052 #include <unistd.h> 00053 #else 00054 #include "esos_pic24.h" 00055 #include "esos_pic24_rs232.h" 00056 #include "esos_pic24_i2c.h" 00057 #include <stdio.h> 00058 #endif 00059 00060 // DEFINEs go here 00061 #define DS1631ADDR 0x90 //DS1631 address with all pins tied low 00062 #define ACCESS_CONFIG 0xAC 00063 #define CONFIG_COMMAND 0x0C //continuous conversion, 12-bit mode 00064 #define START_CONVERT 0x51 00065 #define READ_TEMP 0xAA 00066 00067 #ifndef __linux 00068 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT() 00069 #define LED1 _LATB15 00070 #else 00071 #define CONFIG_LED1() printf("called CONFIG_LED1()\n"); 00072 uint8 LED1 = TRUE; // LED1 is initially "on" 00073 #endif 00074 00075 // PROTOTYPEs go here 00076 00077 // GLOBALs go here 00078 // Generally, the user-created semaphores will be defined/allocated here 00079 char psz_CRNL[3]= {0x0D, 0x0A, 0}; 00080 char psz_prompt[] = "Temp is "; 00081 char psz_done[9]= {' ','D','O','N','E','!',0x0D, 0x0A, 0}; 00082 int16 i16_temp; 00083 UINT16 U16_raw; 00084 00085 ESOS_SEMAPHORE(sem_dataReady); 00086 ESOS_SEMAPHORE(sem_dataPrinted); 00087 ESOS_SEMAPHORE(sem_ds1631Ready); 00088 00089 #ifdef __linux 00090 /* 00091 * Simulate the timer ISR found on a MCU 00092 * The PC doesn't have a timer ISR, so this task will periodically 00093 * call the timer services callback instead. 00094 * USED ONLY FOR DEVELOPMENT AND TESTING ON PC. 00095 * Real MCU hardware doesn't need this task 00096 */ 00097 ESOS_USER_TASK( __simulated_isr ) { 00098 ESOS_TASK_BEGIN(); 00099 while (TRUE) { 00100 // call the ESOS timer services callback just like a real H/W ISR would 00101 __esos_tmrSvcsExecute(); 00102 ESOS_TASK_WAIT_TICKS( 1 ); 00103 00104 } // endof while(TRUE) 00105 ESOS_TASK_END(); 00106 } // end child_task 00107 #endif 00108 00109 /************************************************************************ 00110 * User supplied functions 00111 ************************************************************************ 00112 */ 00113 00114 /* 00115 * An ESOS software timer callback function strobe the heartbeat LED. 00116 * 00117 * Toggles LED1 everytime the callback is called. Exact period is 00118 * determined by application when this timer callback function is 00119 * registered with ESOS. See \ref esos_RegisterTimer 00120 * Application can change timer period on-the-fly with \ref esos_ChangeTimerPeriod 00121 * 00122 * \note Since this heartbeat is performed in an ESOS software 00123 * timer callabck, a flashing LED indicates that the ESOS system 00124 * tick ISR is being called properly. If the LED quits flashing, 00125 * then the ESOS system tick has ceased working. This probably indicates 00126 * some catastrophic failure of the system. However, the cause could 00127 * be poorly-behaved user code that is manipulating the hardware registers 00128 * with the timer or interrupt enables directly. ESOS provides functions 00129 * to change state of interrupts and user code should never modify the 00130 * hardware used by ESOS to implement the system tick. 00131 * \hideinitializer 00132 */ 00133 00134 // user-created timer callback 00135 ESOS_USER_TIMER( swTimerLED ) { 00136 LED1 = !LED1; 00137 #ifdef __linux 00138 if (LED1) { 00139 printf("\a"); 00140 fflush(stdout); 00141 } 00142 #endif 00143 } //endof swTimerLED 00144 00145 /* 00146 * user task to setup DS1631 for continuous temperature 00147 * conversion. Will signal when DS1631 is ready to be 00148 * used. 00149 */ 00150 ESOS_USER_TASK(start_ds1631) { 00151 ESOS_TASK_BEGIN(); 00152 ESOS_TASK_WAIT_TICKS(500); 00153 ESOS_TASK_WAIT_ON_WRITE2I2C1(DS1631ADDR, ACCESS_CONFIG, CONFIG_COMMAND); 00154 ESOS_TASK_WAIT_ON_WRITE1I2C1(DS1631ADDR, START_CONVERT); 00155 ESOS_TASK_WAIT_TICKS(500); 00156 ESOS_SIGNAL_SEMAPHORE(sem_ds1631Ready, 1); 00157 ESOS_TASK_END(); 00158 } //end task start_ds1631 00159 00160 /* 00161 * user task to read DS1631 every 3/4 second 00162 * will signal when data has been successfully read 00163 */ 00164 ESOS_USER_TASK(read_ds1631) { 00165 static uint8 u8_lo, u8_hi; 00166 00167 ESOS_TASK_BEGIN(); 00168 ESOS_TASK_WAIT_SEMAPHORE(sem_ds1631Ready, 1); 00169 while (TRUE) { 00170 ESOS_TASK_WAIT_ON_WRITE1I2C1(DS1631ADDR, READ_TEMP); 00171 ESOS_TASK_WAIT_ON_READ2I2C1(DS1631ADDR, u8_hi, u8_lo); 00172 U16_raw.u8Msb = u8_hi; 00173 U16_raw.u8Lsb = u8_lo; 00174 i16_temp = u8_hi; 00175 i16_temp = ((i16_temp<<8)|u8_lo); 00176 ESOS_SIGNAL_SEMAPHORE(sem_dataReady, 1); 00177 ESOS_TASK_WAIT_TICKS(750); 00178 ESOS_TASK_WAIT_SEMAPHORE(sem_dataPrinted, 1); 00179 } //end while() 00180 ESOS_TASK_END(); 00181 } //end task read_ds1631 00182 00183 /* 00184 * User task to display temperature results from DS1631. 00185 * Used printf and floating point for convenience. A 00186 * production application would likely create custom functions 00187 * to avoid including these huge libraries. 00188 */ 00189 ESOS_USER_TASK(update) { 00190 float f_tempC, f_tempF; 00191 00192 ESOS_TASK_BEGIN(); 00193 while (TRUE) { 00194 ESOS_TASK_WAIT_SEMAPHORE(sem_dataReady, 1); 00195 f_tempC = (float) i16_temp; //convert to floating point 00196 f_tempC = f_tempC/256; //divide by precision 00197 f_tempF = f_tempC*9/5 + 32; 00198 printf("Temp is: 0x%0X, %4.4f (C), %4.4f (F)\n", i16_temp, (double) f_tempC, (double) f_tempF); 00199 ESOS_SIGNAL_SEMAPHORE(sem_dataPrinted, 1); 00200 } // end while(TRUE) 00201 ESOS_TASK_END(); 00202 } // end update() 00203 00204 /**************************************************** 00205 * user_init() 00206 **************************************************** 00207 */ 00208 void user_init(void) { 00209 00210 // Call the hardware-provided routines to print the 00211 // HELLO_MSG to the screen. Must use this call because 00212 // the ESOS communications subsystems is not yet fully 00213 // initialized, since this call is in user_init() 00214 // 00215 // In general, users should call hardware-specific 00216 // function like this. 00217 __esos_unsafe_PutString( HELLO_MSG ); 00218 00219 #ifdef __linux 00220 // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off 00221 // the ESOS S/W timers when they expire 00222 esos_RegisterTask( __simulated_isr ); 00223 #endif 00224 00225 // configure our hardware to support to support our application 00226 CONFIG_LED1(); 00227 esos_pic24_configI2C1(400); //configure I2C for 400 KHz 00228 00229 ESOS_INIT_SEMAPHORE(sem_ds1631Ready, 0); 00230 ESOS_INIT_SEMAPHORE(sem_dataReady, 0); 00231 ESOS_INIT_SEMAPHORE(sem_dataPrinted, 0); 00232 00233 // user_init() should register at least one user task 00234 esos_RegisterTask(start_ds1631); 00235 esos_RegisterTask(read_ds1631); 00236 esos_RegisterTask(update); 00237 00238 // register our callback function with ESOS to create a software timer 00239 esos_RegisterTimer( swTimerLED, 250); 00240 00241 } // end user_init()