/*********************************************************************** module : [EEPROM] function : [EEPROM写] return : [0:写入成功 -1:写入失败] comment : [全局普通函数] machine : [EH-0818] language : [CHN] keyword : [EEPROM] date : [11/07/27] author : [chen-zhengkai] ************************************************************************/ char eeprom_write(unsigned char* writeBuffer, unsigned short sizeLen, int romOffset) { unsigned short idx = 0; char ret = 0; EEPROM_Init(EEPROM_ON); for (idx = 0; idx < sizeLen; idx++) { if (EEPROM_Write_Byte(idx+romOffset, writeBuffer[idx])) { //写入失败 ret = -1; //写入失败 break; } } EEPROM_Init(EEPROM_OFF); return ret; }
/* ---------------------------------------------------------------------------- * Function : void EEPROM_Write_Word(uint32_t spi_interface, * uint32_t data) * ---------------------------------------------------------------------------- * Description : Write a 32-bit word to the EEPROM * Inputs : - spi_interface - Index of SPI interface; use 0, 1 * - data - Word to write * Outputs : None * Assumptions : - EEPROM is initialized to an address * - Current EEPROM address is outside write protected blocks * - EEPROM is selected by chip select * ------------------------------------------------------------------------- */ void EEPROM_Write_Word(uint32_t spi_interface, uint32_t data) { uint32_t i; /* Check if SPI interface exists. If interface is invalid, return. */ if(spi_interface > (EEPROM_SPI_NUM_INTERFACE - 1)) { return; } for (i = 0; i < 4; i++) { EEPROM_Write_Byte(spi_interface, ((uint8_t *)&data)[i]); } /* NOTE: Chip select is left low. */ }
/* ---------------------------------------------------------------------------- * Function : void EEPROM_Write(uint32_t spi_interface, * uint16_t address, * uint8_t* buffer, * uint32_t byte_count) * ---------------------------------------------------------------------------- * Description : Write a number of bytes starting at an address * Inputs : - spi_interface - Index of SPI interface; use 0, 1 * - address - EEPROM address * - buffer - Data to write to EEPROM * - byte_count - Number of bytes to write * Outputs : None * Assumptions : - byte_count is less than the buffer size * - Write-enable opcode is set * - Block of data to be written is outside write protected * blocks * ------------------------------------------------------------------------- */ void EEPROM_Write(uint32_t spi_interface, uint16_t address, uint8_t* buffer, uint32_t byte_count) { uint16_t page_address; /* Check if SPI interface exists. If interface is invalid, return. */ if(spi_interface > (EEPROM_SPI_NUM_INTERFACE - 1)) { return; } page_address = (address & EEPROM_PAGE_ADDRESS_Mask); while(byte_count > 0) { /* Set page_address to the end of current page */ page_address += EEPROM_PAGE_LENGTH; EEPROM_Write_Enable(spi_interface); EEPROM_Write_Init(spi_interface, address); /* Write bytes until either byte_count is zero or writing past a * page. When writing past a page, commit to memory before * proceeding. */ while((address != page_address) && (byte_count > 0)) { EEPROM_Write_Byte(spi_interface, *buffer); buffer++; address++; byte_count--; } EEPROM_Write_Done(spi_interface); } }