static void ICACHE_FLASH_ATTR eeprom_erase(uint8_t devaddr, uint32_t startaddr, uint32_t length) { uint8_t msgff[16] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; uint32_t i; uint32_t stopaddr = startaddr + length; for (i = startaddr; i < stopaddr; i += sizeof(msgff)) { if(!eeprom_writePage(DEVICEADDRESS, i, msgff, sizeof(msgff))) console_printf("Failed to clean up the memory address %03x, block size %d\r\n", i, sizeof(msgff)); else console_printf("Memory address %03x, block size %d erased.\r\n", i, sizeof(msgff)); } }
void user_init(void) { int i; uint8_t buffer[16]; loop_size = rand(); // Data to write uint8_t msg1[] = "Test Message #1"; uint8_t msg2[] = "Test Message #2"; uint8_t msg3[] = "Hello world!"; //Init uart uart_init(BIT_RATE_115200, BIT_RATE_115200); sleepms(1); console_printf("Booting...\r\n"); console_printf("-----------------------------------------------\r\n"); console_printf("AT24C512 Library Benchmark\r\n"); console_printf("-----------------------------------------------\r\n"); // i2c init i2c_init(); // Erase memory eeprom_erase(DEVICEADDRESS, 0, 224); console_printf("-----------------------------------------------\r\n"); // Write some stuff to EEPROM if(!eeprom_writePage(DEVICEADDRESS, 0x00, msg1, sizeof(msg1))) console_printf("Failed write, address: %d, data: %s\r\n", 0x00, msg1); else console_printf("Message 1 stored in the memory.\r\n"); if(!eeprom_writePage(DEVICEADDRESS, 0x40, msg2, sizeof(msg2))) console_printf("Failed write, address: %d, data: %s\r\n", 0x40, msg2); else console_printf("Message 2 stored in the memory.\r\n"); if(!eeprom_writePage(DEVICEADDRESS, 0x80, msg3, sizeof(msg3))) console_printf("Failed write, address: %d, data: %s\r\n", 0x80, msg3); else console_printf("Message 3 stored in the memory.\r\n"); console_printf("-----------------------------------------------\r\n"); // Read the first page in EEPROM memory, a byte at a time console_printf("EEPROM read byte, starting at 0x000:\r\n"); for (i = 0; i < sizeof(msg1)-1; i++) { uint8_t b = eeprom_readByte(DEVICEADDRESS, i); console_printf("0x%02x ", b); } console_printf("\r\n"); for (i = 0; i < sizeof(msg1)-1; i++) { uint8_t b = eeprom_readByte(DEVICEADDRESS, i); console_printf("%c ", isprint(b) ? b : '.'); } console_printf("\r\n"); console_printf("-----------------------------------------------\r\n"); console_printf("EEPROM read buffer, starting at 0x000:\r\n"); os_sprintf(buffer, "%s", eeprom_readPage(DEVICEADDRESS, 0, sizeof(buffer)), sizeof(buffer)); //First print the hex bytes on this row for (i = 0; i < sizeof(buffer)-1; i++) { char outbuf[6]; console_printf("0x%02x ", buffer[i]); } console_printf("\r\n"); for (i = 0; i < sizeof(buffer)-1; i++) { char outbuf[6]; console_printf("%c ", isprint(buffer[i]) ? buffer[i] : '.'); } console_printf("\r\n"); console_printf("-----------------------------------------------\r\n"); // Now dump 224 bytes console_printf("EEPROM dump:\r\n"); eeprom_dump(DEVICEADDRESS, 0, 224); // Start write benchmark test writeByByteTest(); // Start read benchmark test readByByteTest(); system_os_task(user_procTask, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); }
/** * Write a single byte to specified address of EEPROM. * * @param address address in the EEPROM. * @param data data to write. * @return 0 - success, error code otherwise. */ unsigned char eeprom_writeByte(unsigned int address, unsigned char data) { return eeprom_writePage(address, &data, 1); }