static void ICACHE_FLASH_ATTR eeprom_dump(uint8_t devaddr, uint32_t addr, uint32_t length) { uint32_t i; int j; char buffer[16]; uint32_t startaddr = addr; uint32_t stopaddr = addr + length; for (i = startaddr; i < stopaddr; i += 16) { console_printf("%03x: ", i); os_sprintf(buffer, "%s", eeprom_readPage(devaddr, i, 16), 16); for(j = 0; j < 16; j++) { if (j == 8) { console_printf(" "); } console_printf("%02x ", buffer[j]); } console_printf(" | "); for(j = 0; j < 16; j++) { console_printf("%c ", isprint(buffer[j]) ? buffer[j] : '.'); } console_printf("|\r\n"); } }
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); }
/** * Read a single byte from the specific address in EEPROM. * * @param address EEPROM address, 0..32K * @param buffer buffer to receive one byte from EEPROM. * @return 0 - success, error otherwise. */ unsigned char eeprom_readByte(unsigned int address, unsigned char* buffer) { return eeprom_readPage(address, buffer, 1); }