/****************************************************************************** * R_EEPROM subroutine * * This routine will read the last set of data from EEPROM and save that data * into holding registers in RAM for tranmission across USART. * * Inputs: addr = EEPROM memory address for pulling data from * mem = RAM location to write data to * Outputs: None ******************************************************************************/ void R_EEPROM(unsigned short long addr, unsigned char * mem) { unsigned char temp; unsigned char i; unsigned char high; unsigned char med; unsigned char low; high = addr>>16; //High byte of EEPROM memory location med = addr>>8; //Medium byte of EEPROM memory location low = addr; //Low byte of EEPROM memory location EEPROMEnable(); //Pull CS low to write WriteSPI(ReadEEPROM); //Set READ on EEPROM WriteSPI(high); //Send high address to EEPROM WriteSPI(med); //Send medium address to EEPROM WriteSPI(low); //Send low address to EEPROM for (i = 0; i < 30; i++) { temp = WriteSPI(0x00); //Save the data to holding location in RAM mem[i] = temp; } EEPROMDisable(); //Push CS high to deselect EEPROM Delay1KTCYx(1); //Wait for read to finish just in case }
/**********读取字节*********************************/ UINT8 EEPROMReadByte(UINT16 addr) { EEPROMEnable(); // EA =0; ISP_CMD = 0x01;//读模式 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; EEPROMStart(); Delay_US(11); return (ISP_DATA); // EA = 1; }
/**********写入一个字节****************************/ void EEPROMWriteByte(UINT16 addr,UINT8 byte) { EEPROMEnable(); // EA =0; ISP_CMD = 0x02;//写模式 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; ISP_DATA = byte;//写入数据,(一个字节) EEPROMStart(); Delay_US(60);//写字节的等待时间 EEPROMDisable(); // EA = 1; }
/**********擦除扇形区*****************************/ void EEPROMSectorErase(UINT16 addr) { EEPROMEnable(); // EA =0; ISP_CMD = 0x03;//触发命令,擦除扇区 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; EEPROMStart(); Delay_MS(10);//擦除等待时间,约10毫秒 Delay_US(900);//万一10毫秒延时等待不够,增加900微秒的延时等待时间,确保扇区擦除完成。 EEPROMDisable(); // EA = 1; }
bool i82557eeprom::initWithAddress(volatile eeprom_control_t * p) { int i; UInt16 sum; if (!super::init()) return false; ee_p = p; /* * Find out the number of bits in the address by issuing a read to address * 0 ie. keep feeding eeprom address bits with value 0, until the eeprom * says that the address is complete. It tells us by setting EEDO to 0 * after a write cycle. */ EEPROMEnable(ee_p); EEPROMWriteBit(ee_p, 1); /* read */ EEPROMWriteBit(ee_p, 1); EEPROMWriteBit(ee_p, 0); nbits = 1; do { EEPROMWriteBit(ee_p, 0); if ((ReadLE16(ee_p) & EEPROM_CONTROL_EEDO) == 0) break; nbits++; } while (nbits <= 32); // IOLog("nbits: %d\n", nbits); EEPROMDisable(ee_p); // Read NUM_EEPROM_WORDS words into memory. // Also compute a sum of the entire EEPROM. for (sum = 0, i = 0; i < (1 << nbits); i++) { UInt16 w = readWord(i); sum += w; if (i < NUM_EEPROM_WORDS) WriteLE16(&image.words[i], w); } if (sum != EEPROM_CHECKSUM_VALUE) { IOLog("i82557eeprom: checksum %x incorrect\n", sum); return false; } return true; }
/****************************************************************************** * W_EEPROM subroutine * * This routine will store the latest 30 bytes of temperature into EEPROM memory. * * NOTE: When writing in 30 byte chunks, the AAI mode must be set to save time. * This follows a specific procedure as outlined in the SST25VF080B * data sheet - pp. 13-15. * * Inputs: addr = EEPROM memory address for storing data * mem = RAM location to pull data from to send to EEPROM * Outputs: None ******************************************************************************/ void W_EEPROM(unsigned short long addr, unsigned char * mem) { unsigned char i; unsigned char high; unsigned char med; unsigned char low; high = addr>>16; //High byte of EEPROM memory location med = addr>>8; //Medium byte of EEPROM memory location low = addr; //Low byte of EEPROM memory location EEPROMEnable(); //Select EEPROM WriteSPI(WriteENABLE); //Set Write enable Latch (WREN) EEPROMDisable(); //Push CS high according to data sheet EEPROMEnable(); //Pull CS low to write WriteSPI(AutoWRITE); //Begin AAI mode WriteSPI(high); //Send high address to EEPROM WriteSPI(med); //Send medium address to EEPROM WriteSPI(low); //Send low address to EEPROM WriteSPI(mem[0]); //Send first of two bytes WriteSPI(mem[1]); //Send second of two bytes EEPROMDisable(); //Push CS high according to data sheet Delay1KTCYx(1); //Wait for write to finish for (i = 2; i < 29; i+=2) { EEPROMEnable(); //Pull CS low to write WriteSPI(AutoWRITE); //Continue AAI mode WriteSPI(mem[i]); //Send first of two bytes WriteSPI(mem[i+1]); //Send second of two bytes EEPROMDisable(); //Push CS high according to data sheet EEPROMEnable(); WriteSPI(ReadSTATUS); while(1) { if( WriteSPI(0x00) != 0x42 ) { continue; } else { break; } } EEPROMDisable(); } EEPROMEnable(); WriteSPI(WriteDISABLE); //Write disable to exit AAI mode EEPROMDisable(); EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); Delay10KTCYx(2); //Wait for 6 ms for write to finish }
/* READ command bit sequence: 1 1 0 a5a4a3a2a1a0 */ UInt16 i82557eeprom::readWord(int offset) { int i; UInt16 value; EEPROMEnable(ee_p); EEPROMWriteBit(ee_p, 1); EEPROMWriteBit(ee_p, 1); EEPROMWriteBit(ee_p, 0); for (i = (nbits - 1); i >= 0; i--) { EEPROMWriteBit(ee_p, (offset >> i) & 1); } value = 0; for (i = BITS_IN_SHORT - 1; i >= 0; i--) { value |= (EEPROMReadBit(ee_p) << i); } EEPROMDisable(ee_p); return (value); }
/****************************************************************************** * Initialize SPI subroutine * * This routine will initialize the SPI for future reading and writing. It also * tells the EEPROM that we are allowing writing to all of the memory space. * * Inputs: None * Outputs: None ******************************************************************************/ void InitSPI1(void) { unsigned char i; SSP1STAT = 0b10000000; //Data sampled at end, transition from idle SSP1CON1 = 0b00110000; //Enable MSSP, idle state high, FOSC/4 PIE1bits.SSP1IE = 0; //Disable MSSP1 interrupts IPR1bits.SSP1IP = 0; //Set low priority to MSSP1 interrupts if enabled EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); //This little block allows writing to the whole EEPROM chip EEPROMEnable(); WriteSPI(WriteENABLE); EEPROMDisable(); EEPROMEnable(); WriteSPI(WriteSTATUS); WriteSPI(0x00); //Ensure that BP0, BP1, and BP2 are EEPROMDisable(); // 0 to disable write protection Delay1KTCYx(1); EEPROMEnable(); WriteSPI(ReadSTATUS); i = WriteSPI(0x00); EEPROMDisable(); Delay1KTCYx(1); //Erase all of the EEPROM EEPROMEnable(); WriteSPI(WriteENABLE); EEPROMDisable(); EEPROMEnable(); WriteSPI(EraseMEMORY); EEPROMDisable(); Delay10KTCYx(20); //Wait for 100 ms for erase to finish }