/home/daqweb/fgddaq/c8051/loader.c File Reference

#include <stdio.h>
#include <math.h>
#include <string.h>
#include "mscbemb.h"
#include "loader.h"
#include "Devices/AT25160A.h"

Functions

void user_init (unsigned char init)
void user_write (unsigned char index) reentrant
unsigned char user_read (unsigned char index)
unsigned char user_func (unsigned char *data_in, unsigned char *data_out)
void user_loop (void)

Variables

char code node_name [] = "loader"
unsigned long code snlocal
unsigned long xdata currentTime = 0
unsigned char idata _n_sub_addr = 1
int timeout = 0
int progFlag
int lstructFlag
int doneonce = 0
MSCB_INFO_VAR code vars []
MSCB_INFO_VARvariables = vars
SYS_INFO sys_info

Function Documentation

unsigned char user_func ( unsigned char *  data_in,
unsigned char *  data_out 
)

00299 {
00300   /* echo input data */
00301   data_out[0] = data_in[0];
00302   data_out[1] = data_in[1];
00303   return 2;
00304 }

void user_init ( unsigned char  init  ) 

00086 {
00087   char xdata pca_add=0;
00088   unsigned int xdata crate_add=0, board_address=0;
00089 
00090 
00091   if (init) {}
00092 
00093 
00094   // Initialize control and status
00095   user_data.control    = 0;
00096   user_data.status     = 0;
00097   user_data.structsze0 = 0;
00098   EEPROM_FLAG=0;
00099 
00100 //-----------------------------------------------------------------------------
00101 //
00102 #ifdef L_TEMP36
00103   sprintf(sys_info.node_name,"TEMP36");
00104   SFRPAGE  = CONFIG_PAGE;
00105   P2MDOUT |= 0x31; // Setting the RAM_CSn. SPI_MOSI, SPI_SCK to push pull
00106   P2MDOUT &= ~0x04; // Setting the RAM_WPn to open drain
00107   P0MDOUT |= 0x04; // RS485 in PP
00108 
00109 //-----------------------------------------------------------------------------
00110 //
00111 #elif defined(L_FEB64)
00112 #ifdef FEB64REV1
00113   // P3.7:RAMCSn   .6:CSn6      .5:CSn4     .4:SPARE5  | .3:SPARE4  .2:REG_EN   .1:CSn3    .0:CSn2
00114   // P2.7:SPARE1   .6:CSn7      .5:CSn6     .4:SPIMOSI | .3:SPISCK  .2:RAMHLDn  .1:SPIMISO .0:RAMWPn
00115   // P1.7:ASUMSync .6:ASUMTestn .5:ASUMPWDn .4:ASUMCSn | .3:ResetN  .2:SPARE2   .1:SPARE3  .0:SST_DRV
00116   // P0.7:CSn1     .6:CSn0      .5:485TXEN  .4:QPUMPCLK| .3:SMBCLK  .2:SMBDAT   .1:Rx      .0:Tx
00117   sprintf(sys_info.node_name,"FEB64");
00118   SFRPAGE = CONFIG_PAGE;
00119   P3MDOUT |= 0x80; // RAM_CSn in PP, REG_EN in OD
00120   P2MDOUT |= 0x18; // SPI_MOSI, SPI_SCK in PP
00121   P2MDOUT &= ~0x01; // RAM_WPn in OD
00122   P1MDOUT |= 0x08; // ResetN in PP
00123   P0MDOUT |= 0x20; // RS485 in PP
00124   }
00125 
00126 #elif defined(FEB64REV2)
00127 // P3.7:RAMCSn   .6:CSn5      .5:CSn4     .4:SPARE5  | .3:SPARE4   .2:REG_EN    .1:CSn3     .0:CSn2 
00128 // P2.7:SPARE1   .6:CSn7      .5:CSn6     .4:SPIMOSI | .3:SPISCK   .2:RAMHLDn   .1:SPIMISO  .0:RAMWPn 
00129 // P1.7:ASUMSync .6:ASUMTestn .5:ASUMPWDn .4:ASUMCSn | .3:SST_DRV2 .2:PIORESETN .1:SST_DRV1 .0:DACRESETN 
00130 // P0.7:CSn1     .6:CSn0      .5:485TXEN  .4:QPUMPCLK| .3:SMBCLK   .2:SMBDAT    .1:Rx       .0:Tx 
00131   sprintf(sys_info.node_name,"FEB64");
00132   SFRPAGE = CONFIG_PAGE;
00133   P3MDOUT |= 0x80; // RAM_CSn in PP, REG_EN in OD
00134   P2MDOUT |= 0x18; // SPI_MOSI, SPI_SCK in PP
00135   P2MDOUT &= ~0x01; // RAM_WPn in OD
00136   P1MDOUT |= 0x05; // ResetN in PP
00137   P0MDOUT |= 0x20; // RS485 in PP
00138   P1 |= 0x5;  // Set PIO/DAC Reset High
00139 #endif // FEB64REVx
00140   delay_us(100);
00141 
00142 //
00143 // Get Card address through SMB PCA port
00144 //-----------------------------------------------------------------------------
00145 #ifdef _PCA9539_
00146   SFRPAGE = CONFIG_PAGE;
00147 
00148 // Enable SMB
00149   XBR0 |= 0x01;                 // Enable SMBus
00150   PCA9539_Init(); //PCA General I/O (Bias Enables and Backplane Addr) initialization
00151   delay_us(10);
00152 
00153   //Write to the PCA register for setting 1.x to input
00154   PCA9539_WriteByte(BACKPLANE_INPUT_ENABLE);
00155   delay_us(10);
00156 
00157   PCA9539_Read(BACKPLANE_READ, &pca_add, 1);
00158   delay_us(10);
00159   //C C C C C C 0 B B is the MSCB Addr[8..0], 9 bits
00160   //Modifying what the board reads from the PCA
00161   //Externally, the crate address are reversed
00162 
00163   crate_add= ((~pca_add)<<1)  & 0x01F8;
00164   //Internally, the board address are not reversed
00165   board_address = crate_add | ((pca_add) & 0x0003);
00166   if (board_address == 504) board_address = 0;
00167   sys_info.node_addr   = board_address;
00168 #endif
00169 
00170 //-----------------------------------------------------------------------------
00171 //
00172 #elif defined(L_LPB)
00173 // P3.7:A7       .6:A6        .5:A5       .4:A4      | .3:A3      .2:A2       .1:A1      .0:A0 
00174 // P2.7:+1.8En   .6:+3.3En    .5:+5En     .4:SPIMOSI | .3:SPISCK  .2:RAMHLDn  .1:SPIMISO .0:RAMWP
00175 // P1.7:NC       .6:+6ddFlag  .5:R/HClock .4:R/HData | .3:+6ddEN  .2:RAMCS    .1:NC      .0:SST_DRV 
00176 // P0.7:DEL_CLK  .6:DEL_DATA  .5:485TXEN  .4:NC      | .3:DELCS1  .2:DELCS2   .1:Rx      .0:Tx 
00177   sprintf(sys_info.node_name,"LPB");
00178   SFRPAGE  = CONFIG_PAGE;
00179   P2MDOUT |= 0x18;        // SPI_MOSI, SPI_SCK  PP
00180   P3MDOUT |= 0xFF; // PushPull for the address
00181   P2MDOUT &= 0xFE; // RAM_WPn in OD
00182   P1MDOUT |= 0x04; // RAM_CSn in PP
00183   P0MDOUT |= 0x20; // RS485_ENABLE in PP
00184   XBR2 &= ~0x04;   // Disable UART1
00185 
00186   // Read Board address from P3 port directly
00187   // Configure and read the address
00188   // C C C C C C 1 0 1 is the MSCB Addr[8..0], 9 bits
00189   // Modifying what the board reads from the Pins
00190   pca_add= P3;
00191   board_address= (((~pca_add)<<1) & 0x01F8)| 0x0005;
00192   sys_info.node_addr   = board_address;
00193 
00194 //-----------------------------------------------------------------------------
00195 //
00196 #elif defined(L_CMB)
00197   sprintf(sys_info.node_name,"CMB");
00198   SFRPAGE = CONFIG_PAGE;
00199   P0MDOUT |= 0x20; // RS485_ENABLE in PP
00200   P3MDOUT |= 0x80; // RAM_CSn in PP
00201   P2MDOUT |= 0x18; // SPI_MOSI, SPI_SCK in PP
00202   P2MDOUT &= 0xFE; // RAM_WPn in OD
00203 
00204   //
00205   // Address retrieval
00206   //-----------------------------------------------------------------------------
00207   // Configure and read the address
00208   // C C C C C C 1 0 0 is the MSCB Addr[8..0], 9 bits
00209   // Modifying what the board reads from the Pins 
00210   SFRPAGE = CONFIG_PAGE;
00211   // Change p3 to digital input
00212   P3MDOUT = 0x00;
00213   P3=0xFF;
00214   // Read crate address
00215   pca_add= P3;
00216   crate_add= ((~pca_add)<<3)  & 0x01F8;
00217   board_address=(crate_add &  0x01FC) | 0x0004;
00218   sys_info.node_addr   = board_address; 
00219 
00220 #endif
00221 
00222 
00223 //-----------------------------------------------------------------------------
00224 //
00225 // Initialize EEPAGE access (SPI)
00226   if(ExtEEPROM_Init(PAGE_SIZE)) {
00227     DISABLE_INTERRUPTS;
00228     user_data.status = 0x7F;
00229     ENABLE_INTERRUPTS;
00230   }
00231 
00232   //
00233   // snlocal is set by the Jtag Programmer
00234   // In this case bypass the user command as we're in auto-programming
00235   progFlag = 0;
00236   if ((snlocal > 76000000) && (snlocal < 76900000)) progFlag = 1;
00237  /*  DONE below
00238   //
00239   // Read S/N and struct size only from Protected page
00240   // S/N ans Strtsze at the same location for all the cards
00241   ExtEEPROM_Read(WP_START_ADDR, (unsigned char*)&SNWp, SERIALN_LENGTH);
00242   ExtEEPROM_Read(WP_START_ADDR+SERIALN_LENGTH, (unsigned char*)&structSzeWp, 2);
00243   DISABLE_INTERRUPTS;
00244   user_data.serialNWp   = SNWp;
00245   user_data.structszeWp = structSzeWp;
00246   ENABLE_INTERRUPTS;
00247  */
00248 
00249   // Read WP page as we need to figure out if the 
00250   // memory contains valid information or if it is empty
00251   ExtEEPROM_Read(WP_START_ADDR, (unsigned char*)&eepageWp, PAGE_SIZE);
00252   DISABLE_INTERRUPTS;
00253   user_data.serialN = user_data.serialNWp   = eepageWp.SerialN;
00254   user_data.structszeWp = eepageWp.structsze;
00255   ENABLE_INTERRUPTS;
00256  
00257   // Read Page 0 in temporatry struct as we need to figure out if the 
00258   // memory contains valid information or if it is empty
00259   ExtEEPROM_Read(page_addr[0], (unsigned char*)&eepage0, PAGE_SIZE);
00260   DISABLE_INTERRUPTS;
00261   user_data.serialN0   = eepage0.SerialN;
00262   user_data.structsze0 = eepage0.structsze;
00263   ENABLE_INTERRUPTS;
00264  
00265   // If it doesn't match use local eepage structure
00266   // If it matches keep option to use eepage0 (P0) or local struct
00267   lstructFlag = 1;
00268   if (eepage.structsze != eepage0.structsze) lstructFlag = 0;
00269 
00270   //
00271   // Possible extra initialization
00272 #ifdef L_FEB64
00273 #elif defined (L_LPB)
00274 #elif defined (L_CMB)
00275 #elif defined (L_TEMP36)
00276 #endif
00277 
00278 }

void user_loop ( void   ) 

00308 {
00309   unsigned char FAILED=0;
00310 
00311   // Bypass if progflag is ON as we expect the S/N to be provided 
00312   // by the programmer
00313   // Wait 1 second before initiating command
00314   if (((uptime() - currentTime) > 1) && progFlag) {
00315      // S/N coming from programmer, issue the EEPROM save
00316          // forcing the local struct to WP and P0
00317          EEPROM_FLAG = 1;
00318   }
00319      
00320   // Wait for the EEPROM_FLAG to be set
00321   if(EEPROM_FLAG && (doneonce == 0)) {
00322     if (progFlag) {
00323       // programmer loop
00324           // use local struct for Wp and P0
00325       eepage.SerialN = snlocal;
00326       user_data.serialN = snlocal;
00327     } else {
00328           // manual request
00329       if (user_data.control & 0x1) {
00330             // Use local struct for WP and P0
00331             // Update S/N from mscb to eepage
00332             eepage.SerialN = user_data.serialN;
00333       eepage.structsze = PAGE_SIZE;
00334             }
00335       if (user_data.control & 0x2) {
00336               // Use P0 but update S/N from WP
00337               // Update S/N from mscb to eepage
00338         // ONLY if the struct size matches
00339                     if (lstructFlag)  {
00340           memcpy ((char *) &eepage, (char *) &eepage0, PAGE_SIZE);
00341                       eepage.SerialN = user_data.serialN;
00342         }
00343       }
00344     } // progFlag
00345 
00346     // Use Wp as temporary storage for checking S/N
00347     memset((char *) &eepageWp, 0x00, PAGE_SIZE);
00348 
00349     // Write local struct
00350     FAILED = ExtEEPROM_WriteProtect((unsigned char*)&eepage, PAGE_SIZE);
00351     if(!FAILED) {
00352       DISABLE_INTERRUPTS;
00353       user_data.status = 1;
00354       ENABLE_INTERRUPTS;
00355     }
00356 
00357     // Check S/N readback on WP 
00358     FAILED = ExtEEPROM_Read(WP_START_ADDR,(unsigned char*)&eepageWp, PAGE_SIZE);
00359     user_data.structszeWp = eepageWp.structsze;
00360     if(!FAILED && (user_data.serialN == eepageWp.SerialN)) {
00361       DISABLE_INTERRUPTS;
00362       user_data.status |= (1<<1);
00363       ENABLE_INTERRUPTS;
00364     }
00365 
00366     // Write to Page 0 as well
00367     FAILED = ExtEEPROM_Write_Clear(page_addr[0],(unsigned char*)&eepage, PAGE_SIZE, WRITE);
00368     if(!FAILED) {
00369       DISABLE_INTERRUPTS;
00370       user_data.status |= (1<<2);
00371       ENABLE_INTERRUPTS;
00372     }
00373 
00374     // Use Wp as temperary storage for checking S/N
00375     memset((char *) &eepageWp, 0x00, PAGE_SIZE);
00376 
00377     // Check S/N readback
00378     FAILED = ExtEEPROM_Read(page_addr[0],(unsigned char*)&eepageWp, PAGE_SIZE);
00379     user_data.structszeWp = eepageWp.structsze;
00380     if(!FAILED && (user_data.serialN == eepageWp.SerialN)) {
00381       DISABLE_INTERRUPTS;
00382       user_data.status |= (1<<3);
00383       ENABLE_INTERRUPTS;
00384     }
00385 
00386     // Check for error
00387     if(user_data.status == 0xF) {
00388       DISABLE_INTERRUPTS;
00389       user_data.control = 0;
00390       EEPROM_FLAG=0;
00391       ENABLE_INTERRUPTS;
00392     }
00393     // if successful should read 0xF (00001111) in status
00394         // Reset the S/N programmer  flag
00395         progFlag = 0;
00396     doneonce = 1;
00397 
00398   } // EEPROM_FLAG
00399 
00400  led_blink(1, 1, 50);
00401  delay_ms(500);
00402 
00403 }

unsigned char user_read ( unsigned char  index  ) 

00292 {
00293   if (index);
00294   return 0;
00295 }

void user_write ( unsigned char  index  ) 

00284 {
00285   if(index==IDXCTL) {
00286     EEPROM_FLAG=1;
00287   }
00288 }


Variable Documentation

unsigned char idata _n_sub_addr = 1

unsigned long xdata currentTime = 0

int doneonce = 0

int lstructFlag

char code node_name[] = "loader"

int progFlag

unsigned long code snlocal

SYS_INFO sys_info

int timeout = 0

MSCB_INFO_VAR* variables = vars

MSCB_INFO_VAR code vars[]

Initial value:

 {
  4, UNIT_BYTE,            0, 0,           0, "SerialN",    &user_data.serialN,      
  1, UNIT_BYTE,            0, 0,           0, "Control",    &user_data.control,      
  1, UNIT_BYTE,            0, 0,           0, "EEPage",     &user_data.eepage,       
  1, UNIT_BYTE,            0, 0,           0, "Status",     &user_data.status,       
  4, UNIT_BYTE,            0, 0,           0, "SNWp",       &user_data.serialNWp,    
  2, UNIT_BYTE,            0, 0,           0, "StrcSzWp",   &user_data.structszeWp,  
  4, UNIT_BYTE,            0, 0,           0, "SN0",        &user_data.serialN0,     
  2, UNIT_BYTE,            0, 0,           0, "StrcSze0",   &user_data.structsze0,   
  0
}


Generated on 10 Jun 2013 for FGDC8051 by  doxygen 1.4.7