/************************************************************************ * * * Basic access to SPI EEPROM 25LC256. * * * ************************************************************************* * * Author Date Comment * *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* * Michael Cecere Sep, 2006 .... * ************************************************************************/ /************************************************************************ * EEPROM Commands * * * ************************************************************************/ #define EEPROM_PAGE_SIZE (unsigned)64 #define EEPROM_PAGE_MASK (unsigned)0x003f #define EEPROM_CMD_READ (unsigned)0b00000011 #define EEPROM_CMD_WRITE (unsigned)0b00000010 #define EEPROM_CMD_WRDI (unsigned)0b00000100 #define EEPROM_CMD_WREN (unsigned)0b00000110 #define EEPROM_CMD_RDSR (unsigned)0b00000101 #define EEPROM_CMD_WRSR (unsigned)0b00000001 #define EE_CONFIG_BASE 0 // start address of configuration storage // BCM Board #define EE_CS LATBbits.LATB4 // EEPROM Select, out, active low typedef struct Board_Cal_type { float DAC_Act_1000pC[10][8]; // possible 10 boards by 8 channels of DAC float DAC_Act_0pC[10][8]; float Chan_Gain[10][8]; // holds channel gains for all boards/channels float Chan_Offset[10][8]; // holds channel offsets for all boards/channels float Cal_0pC[10][4]; // holds size of cal pulse for 8 boards by 4 channels/board float Cal_200pC[10][4]; // holds size of cal pulse for 8 boards by 4 channels/board float Cal_1000pC[10][4]; // holds size of cal pulse for 8 boards by 4 channels/board } Board_Cal_struct; /************************************************************************ * Structure STATREG and union _EEPROMStatus_ * * * * Overview: Provide a bits and byte access to EEPROM status value. * * * ************************************************************************/ struct STATREG{ char WIP:1; char WEL:1; char BP0:1; char BP1:1; char RESERVED:3; char WPEN:1; }; union _EEPROMStatus_{ struct STATREG Bits; char Char; }; /************************************************************************ * Macro: Lo * * * * Preconditions: None * * * * Overview: This macro extracts a low byte from a 2 byte word. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ #define Lo(X) (unsigned char)(X&0x00ff) /************************************************************************ * Macro: Hi * * * * Preconditions: None * * * * Overview: This macro extracts a high byte from a 2 byte word. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ #define Hi(X) (unsigned char)((X>>8)&0x00ff) /************************************************************************ * Function: EEPROMCSLow * * * * Preconditions: CS IO must be configured as output. * * * * Overview: This macro pulls down SS line * * to start a new EEPROM operation. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ extern void EEPROMCSLow(); /************************************************************************ * Function: EEPROMCSHigh * * * * Preconditions: CS IO must be configured as output. * * * * Overview: This macro set SS line to high level * * to start a new EEPROM operation. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ extern void EEPROMCSHigh(); /************************************************************************ * Function: EEPROMInit * * * * Preconditions: SPI module must be configured to operate with * * parameters: Master, MODE16 = 0, CKP = 1, SMP = 1. * * * * Overview: This function setup SPI IOs connected to EEPROM. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ void EEPROMInit(); /************************************************************************ * Function: EEPROMReadStatus() * * * * Preconditions: SPI module must be configured to operate with EEPROM. * * * * Overview: This function reads status register from EEPROM. * * * * Input: None. * * * * Output: Status register value. * * * ************************************************************************/ extern union _EEPROMStatus_ EEPROMReadStatus(); /************************************************************************ * Function: EEPROMWriteByte() * * * * Preconditions: SPI module must be configured to operate with EEPROM. * * * * Overview: This function writes a new value to address specified. * * * * Input: Data to be written and address. * * * * Output: None. * * * ************************************************************************/ extern void EEPROMWriteByte(unsigned Data, unsigned Address); /************************************************************************ * Function: EEPROMReadByte() * * * * Preconditions: SPI module must be configured to operate with EEPROM. * * * * Overview: This function reads a value from address specified. * * * * Input: Address. * * * * Output: Data read. * * * ************************************************************************/ extern unsigned EEPROMReadByte(unsigned Address); /************************************************************************ * Function: EEPROMWriteEnable() * * * * Preconditions: SPI module must be configured to operate with EEPROM. * * * * Overview: This function allows a writing into EEPROM. Must be called * * before every writing command. * * * * Input: None. * * * * Output: None. * * * ************************************************************************/ extern void EEPROMWriteEnable(); int ftoa ( float x, char *str, char prec, char format); /***************************************************************************** * Function: Ptr_To_EEPROM * Preconditions: EEPROM_Init * Overview: Saves data to EEPROM starting at location adr with data pointed * to by p_dat for num_bytes bytes * Input: none * Output: number of bytes written to EEPROM * ******************************************************************************/ extern unsigned int Ptr_To_EEPROM(unsigned int Address, unsigned char *p_dat, unsigned int num_bytes); /***************************************************************************** * Function: Ptr_From_EEPROM * Preconditions: EEPROM_Init * Overview: Reads data from EEPROM starting at location adr into data pointed * to by p_dat for num_bytes bytes * Input: none * Output: number of bytes written to EEPROM * ******************************************************************************/ extern unsigned int Ptr_From_EEPROM(unsigned int Address, unsigned char *p_dat, unsigned int num_bytes); /***************************************************************************** * Function: Save_Config_EEPROM * Preconditions: EEPROM_Init * Overview: Saves config/calibration data to EEPROM * Input: none * Output: number of bytes written to EEPROM * ******************************************************************************/ unsigned int Save_Config_EEPROM(); /***************************************************************************** * Function: Read_Config_EEPROM * Preconditions: EEPROM_Init * Overview: Reads config/calibration data from EEPROM * Input: none * Output: number of bytes written to EEPROM * ******************************************************************************/ unsigned int Read_Config_EEPROM(); /***************************************************************************** * Function: Update_Cal_Data * Preconditions: Load_Cal_Data has been called * Overview: Loads the cal data into SYS_Config for the board ID set by Board_ID * Input: none * Output: none * ******************************************************************************/ void Update_Cal_Data(); /***************************************************************************** * Function: Load_Cal_Data * Preconditions: none * Overview: loads calibration data for all boards into Board_Cal structure * Input: none * Output: * ******************************************************************************/ void Load_Cal_Data(); /////////////////////////////////////////////////////////////////////////////// ///// / ///// Parameter Initilization Values / ///// / ///// Run this function once for a board's initial EEPROM configuration / ///// uncomment-out this function call in EEPROM_Init() / ///// Adjust MPS parameters as needed, possibly others / ///// which can be adjusted and saved later individually anyways / /////////////////////////////////////////////////////////////////////////////// void Init_EEPROM_Parameters(); /************************************************************************ * EOF * ************************************************************************/