command.c

Go to the documentation of this file.
00001 /*
00002  * Command description and interfacing for Sensor Control
00003  * (c) 2007 by Matthias Arndt <matthias.arndt@tu-clausthal.de>
00004  *
00005  */
00007 #include "t89c51cc02.h"
00008 #include "datatypes.h"
00009 #include "command.h"
00010 #include "eeprom.h"
00011 
00017 LONG readenable;
00018 
00020 LONG monitor;
00021 
00023 LONG monitor_direction;
00024 
00026 volatile bit reporting;
00028 volatile bit timecheck;
00029 
00031 volatile bit read_eeprom_config;
00033 volatile bit write_eeprom_config;
00034 
00036 BYTE report,report_reload;
00037 
00043 WORD idata monitor_boundary[NR_SENSORS];
00044 
00046 const unsigned long code masks[] = { 0x00001,0x00002,0x00004,0x00008,0x00010,0x00020,
00047                                   0x00040,0x00080,0x00100,0x00200,0x00400,0x00800,
00048                                   0x01000,0x02000,0x04000,0x08000,0x10000,0x20000};
00049 
00056 void Command_SetRead(BYTE marker)
00057 {
00058         if((marker>=0)&&(marker<NR_SENSORS))
00059                 readenable |= masks[marker]; /* set bit */
00060 }
00061 
00070 void Command_EnableReport(BYTE timeframes)
00071 {
00072         reporting=1;
00073         report=timeframes;
00074         report_reload=timeframes;
00075 }
00076 
00084 BYTE Command_ReportDue()
00085 {
00086         BYTE retval;
00087         retval=0;
00088         if(reporting)
00089         {
00090                 /* reports can only be due when reporting has been enabled */
00091                 if(report_reload==0)
00092                 {
00093                         /* skip some code if there is a report request for every frame */
00094                         retval=0xff;
00095                 } else {
00096                         /* else: count number of time frames elapsed */
00097                         report--;
00098                         if(report==0)
00099                         {
00100                                 report=report_reload;   /* restart timeframe counter */
00101                                 retval=0xff;
00102                         }
00103                 }
00104         }
00105         return(retval);
00106 }
00107 
00119 void Command_EnableMonitor(BYTE marker,WORD boundary,bit direction)
00120 {
00121         if((marker>=0)&&(marker<NR_SENSORS))
00122         {
00123                 monitor |= masks[marker]; /* mark sensor as monitored */
00124                 monitor_boundary[marker]=boundary; /* apply boundary */
00125 
00126                 monitor_direction &= ~masks[marker];
00127                 if(direction)
00128                         monitor_direction |= masks[marker];
00129         }
00130 }
00131 
00137 void Command_DisableMonitor(BYTE marker)
00138 {
00139         if((marker>=0)&&(marker<NR_SENSORS))
00140                 monitor &= ~masks[marker];
00141 }
00142 
00153 BYTE CheckMonitor(BYTE marker, WORD value)
00154 {
00155         BYTE retval;
00156         retval=0;
00157         if((marker>=0)&&(marker<NR_SENSORS))
00158         {
00159                 if(monitor & masks[marker])
00160                 {
00161                         /* only check if monitor was set for the given marker */
00162                         if(monitor_direction & masks[marker])
00163                         {
00164                                 /* report larger values */
00165                                 if(value>=monitor_boundary[marker]) retval=0xff;
00166                         } else {
00167                                 /* report lower values */
00168                                 if(value<=monitor_boundary[marker]) retval=0xff;
00169                         }
00170                 }
00171         }
00172         return(retval);
00173 }
00174 
00184 WORD Command_GetBoundary(BYTE marker)
00185 {
00186         return(monitor_boundary[marker]);
00187 }
00188 
00195 void Command_SetBoundary(BYTE marker,WORD bound)
00196 {
00197         monitor_boundary[marker]=bound;
00198 }
00199 
00206 void Command_ReadDefaultConfiguration()
00207 {
00208         BYTE i,chksum;
00209         WORD sum;
00210 
00211         /* TODO: read CANID configuration from EEPROM */
00212         /* checksum the EEPROM contents - we only set the monitor if the checksum was correct */
00213         sum=CHECKSUM_FILLER;
00214         for(i=EEPROM_MONITOR;i<EEPROM_CHECKSUM;i++)
00215                 sum += EEPROM_read(i);
00216 
00217         chksum = 0xff-((BYTE)((sum & 0x00ff)));
00218         
00219 
00220         if(chksum == EEPROM_read(EEPROM_CHECKSUM))
00221         {
00222                 /* reading and setting the monitoring values */
00223                 monitor=0;
00224                 monitor= ((LONG) (EEPROM_read(EEPROM_MONITOR+0)) << 24)
00225                         |((LONG) (EEPROM_read(EEPROM_MONITOR+1)) << 16)
00226                         |((LONG) (EEPROM_read(EEPROM_MONITOR+2)) <<  8)
00227                         |((LONG) (EEPROM_read(EEPROM_MONITOR+3)));
00228                 monitor_direction=0;
00229                 monitor_direction= ((LONG) (EEPROM_read(EEPROM_DIRECTION+0)) << 24)
00230                                   |((LONG) (EEPROM_read(EEPROM_DIRECTION+1)) << 16)
00231                                   |((LONG) (EEPROM_read(EEPROM_DIRECTION+2)) <<  8)
00232                                   |((LONG) (EEPROM_read(EEPROM_DIRECTION+3)));
00233                 for(i=0;i<18;i++)
00234                 {
00235                         sum= (((WORD) (EEPROM_read(EEPROM_BOUNDARY+i*2+0)) << 8))
00236                             +(((WORD) (EEPROM_read(EEPROM_BOUNDARY+i*2+1))));
00237                         monitor_boundary[i]=sum;
00238                 }
00239 
00240         }       
00241 }
00242 
00251 void Command_WriteDefaultConfiguration()
00252 {
00253         BYTE i,chksum;
00254         WORD sum;
00255 
00256         EA=0; /* to ensure the values can't be altered, we disable interrupts*/
00257         /* checksum the EEPROM contents - we only set the monitor if the checksum was correct */
00258         sum=CHECKSUM_FILLER;
00259         sum = sum + ((BYTE) ((monitor & 0xff000000) >> 24))
00260                   + ((BYTE) ((monitor & 0x00ff0000) >> 16))
00261                   + ((BYTE) ((monitor & 0x0000ff00) >>  8)) 
00262                   + ((BYTE) ((monitor & 0x000000ff)));
00263         sum = sum + ((BYTE) ((monitor_direction & 0xff000000) >> 24))
00264                   + ((BYTE) ((monitor_direction & 0x00ff0000) >> 16))
00265                   + ((BYTE) ((monitor_direction & 0x0000ff00) >>  8)) 
00266                   + ((BYTE) ((monitor_direction & 0x000000ff)));
00267         for(i=0;i<18;i++)
00268         {
00269                 sum = sum + ((BYTE) ((monitor_boundary[i] & 0xff00) >> 8))
00270                           + ((BYTE) ((monitor_boundary[i] & 0x00ff)));
00271         }
00272 
00273         chksum = 0xff-((BYTE)((sum & 0x00ff)));
00274         /* prepare EEPROM buffer */
00275         EEPROM_write((BYTE) ((monitor & 0xff000000) >> 24),EEPROM_MONITOR);     /* latch monitored sensors */
00276         EEPROM_write((BYTE) ((monitor & 0x00ff0000) >> 16),EEPROM_MONITOR+1);
00277         EEPROM_write((BYTE) ((monitor & 0x0000ff00) >>  8),EEPROM_MONITOR+2);
00278         EEPROM_write((BYTE) ((monitor & 0x000000ff)),EEPROM_MONITOR+3);
00279 
00280         EEPROM_write((BYTE) ((monitor_direction & 0xff000000) >> 24),EEPROM_DIRECTION); /* latch direction information */
00281         EEPROM_write((BYTE) ((monitor_direction & 0x00ff0000) >> 16),EEPROM_DIRECTION+1);
00282         EEPROM_write((BYTE) ((monitor_direction & 0x0000ff00) >>  8),EEPROM_DIRECTION+2);
00283         EEPROM_write((BYTE) ((monitor_direction & 0x000000ff)),EEPROM_DIRECTION+3);
00284 
00285         /* latch boundary values */
00286         for(i=0;i<18;i++)
00287         {
00288                 EEPROM_write((BYTE) ((monitor_boundary[i] & 0xff00) >>  8),EEPROM_BOUNDARY+i*2);
00289                 EEPROM_write((BYTE) ((monitor_boundary[i] & 0x00ff)),EEPROM_BOUNDARY+i*2+1);
00290         }
00291         /* latch checksum */
00292         EEPROM_write(chksum,EEPROM_CHECKSUM);
00293         /* write new EEPROM contents */
00294         EEPROM_flush();
00295         EA=1;   /* activate interrupts again */
00296 }

Generated on Wed Oct 24 12:57:24 2007 for SensorControl by  doxygen 1.4.6