📄 eeprom.c
字号:
offset += bytes; crc = read_crc(offset, dest+(offset-begin)); if (crc != getcrc16(dest, offset-begin, INITIAL_CRC)) { return EEPROM_ERR_CRC; } offset += 2; // Skip CRC if ((item->tag.length == 0) && (item->value.length == 0)) { return EEPROM_EOF; } return offset-begin;}////////////////////////////////////////////////////////////////////////////////// eeprom_get_item// PURPOSE: Read Data associated with a Key from EEPROM.// PARAMS: (IN) void* key - Key to lookup in eeprom.// (IN) u16 keylen - Length of the key.// (OUT)void* data - Data associated with the key.// NULL will not store the data.// (IN/OUT)u16* len - Length of buffer/number of data bytes returned.// RETURNS:// >0 = Offset// 0 = Not Found (EOF)// -1 = READ_ERROR// -2 = INVALID_SECTION// -3 = INVALID_CRC// NOTES: None.////////////////////////////////////////////////////////////////////////////////inteeprom_get_item(const void* key, u16 keylen, void* data, u16* len){ int retval = EEPROM_EOF; u16 next = EEPROM_HEADER_SIZE; int bytes = 0; u8 tmp[MAX_EEPROM_SIZE]; eeItem item; if (!check_eeprom_header()) { DEBUG_FAIL("Error reading EEPROM header\r\n"); return EEPROM_ERR_READ; } do { next += bytes; bytes = read_record(next, tmp, &item); if (bytes <= 0) { retval = bytes; break; } retval = next; } while (item.tag.length != keylen || itc_memcmp(key, item.tag.data, keylen)); if (retval > 0) { if (data) itc_memcpy(data, item.value.data, MIN(item.value.length, *len)); *len = item.value.length; retval = next; } return retval;}////////////////////////////////////////////////////////////////////////////////// eeprom_dump// PURPOSE: Debug function to display the raw tags contained in the EEPROM.// PARAMS: None.// RETURNS:// >0 = Offset// -1 = READ_ERROR// NOTES: None.////////////////////////////////////////////////////////////////////////////////int eeprom_dump(void){ int retval = 0; u16 next = EEPROM_HEADER_SIZE; int bytes = 0; eeItem item; u8 tmp[MAX_EEPROM_SIZE]; if (!check_eeprom_header()) { DEBUG_FAIL("Error reading EEPROM header\r\n"); return EEPROM_ERR_READ; } do { next += bytes; bytes = read_record(next, tmp, &item); if (bytes <= 0) { retval = bytes; break; } retval = next; item.tag.data[item.tag.length] = 0; itc_printf("%s\r\n",item.tag.data); print_bytes(item.value.data, item.value.length); } while (1); if (retval < 0) { DEBUG_FAIL("Error %d reading EEPROM\r\n", retval); } return retval;}////////////////////////////////////////////////////////////////////////////////// eeprom_find_item// PURPOSE: Read Data associated with a Key from EEPROM.// PARAMS: (IN) void* key - Key to lookup in eeprom.// (IN) u16 keylen - Length of the key.// (OUT)u16* datalength - Length of the associated data.// RETURNS:// >0 = Offset// 0 = Not Found (EOF)// -1 = READ_ERROR// -2 = INVALID_SECTION// -3 = INVALID_CRC// NOTES: None.////////////////////////////////////////////////////////////////////////////////inteeprom_find_item(const void* key, u16 keylen, u16* datalength){ return eeprom_get_item(key, keylen, NULL, datalength);}////////////////////////////////////////////////////////////////////////////////// eeprom_write_item// PURPOSE: Write a Key and associated Data to the EEPROM.// PARAMS: (IN) void* key - Key to lookup in eeprom.// (IN) u16 keylen - Length of the key.// (OUT)void* data - Data associated with the key.// (OUT)u16 datalen - Length of the data returned.// RETURNS:// >0 = Offset// 0 = Not Found (EOF)// -1 = READ_ERROR// -2 = INVALID_SECTION// -3 = INVALID_CRC// -4 = WRITE_ERROR// NOTES: None.////////////////////////////////////////////////////////////////////////////////inteeprom_write_item(void const *key, u16 keylen, void const *data, u16 datalen){ int retval = EEPROM_EOF; u16 next = EEPROM_HEADER_SIZE; int bytes = 0; u16 space_needed; eeItem item; eeItem changed_item; u8 tmp[MAX_EEPROM_SIZE]; if (!check_eeprom_header()) { DEBUG_FAIL("Error reading EEPROM header\r\n"); return EEPROM_ERR_READ; } // Find the Item in the list. do { next += bytes; bytes = read_record(next, tmp, &item); if (bytes <= 0) { retval = bytes; break; } retval = next; } while (item.tag.length != keylen || itc_memcmp(key, item.tag.data, keylen)); // Save the new item. create_item(&changed_item, key, keylen, data, datalen); space_needed = record_length(&changed_item); // We found the Item or EOF if (retval > 0) { // We found the Item, try to update if (space_needed == bytes) { // Same size, so replace in place. write_record(next, &changed_item); retval = next; } else { // Delete Item and append to the end of the list. u16 read_ptr = next; u16 write_ptr = next; u16 byte_count; do { read_ptr += bytes; // Skip forward. bytes = read_record(read_ptr, tmp, &item); if (bytes <= 0) { retval = bytes; break; } byte_count = bytes; if (!write_data_eeprom(write_ptr, tmp, &byte_count)) { retval = EEPROM_ERR_WRITE; break; } write_ptr += bytes; } while (1); if (retval >= 0) { const u16 record_ptr = write_ptr; // save the location retval = write_ptr; if (write_record(write_ptr, &changed_item)) { write_ptr += space_needed; } else retval = EEPROM_ERR_WRITE; // Attempt to write the EOF marker even if the data write // failed. If the failure was due to running out of space on // the device, the EOF might succeed and reestablish the // validity of the entire EEPROM tag chain. if (!write_eof(write_ptr)) { // If we're out of space on the EOF write, backtrack and // try overwriting the last item to keep the list valid. write_eof(record_ptr); retval = EEPROM_ERR_WRITE; } } } } else if (retval == EEPROM_EOF) { // EOF, just append item and new EOF const u16 record_ptr = next; // save the location retval = next; if (write_record(next, &changed_item)) { next += space_needed; } else retval = EEPROM_ERR_WRITE; // Attempt to write the EOF marker even if the data write failed. if (!write_eof(next)) { // If we're out of space on the EOF write, backtrack and // try overwriting the last item to keep the list valid. write_eof(record_ptr); retval = EEPROM_ERR_WRITE; } } return retval;}#ifdef NEED_EEPROM_DELETE////////////////////////////////////////////////////////////////////////////////// eeprom_delete_item// PURPOSE: Delete a Key and associated Data to the EEPROM.// PARAMS: (IN) void* key - Key to lookup in eeprom.// (IN) u16 keylen - Length of the key.// RETURNS:// >0 = Offset// 0 = Not Found (EOF)// -1 = READ_ERROR// -2 = INVALID_SECTION// -3 = INVALID_CRC// -4 = WRITE_ERROR// NOTES: None.////////////////////////////////////////////////////////////////////////////////inteeprom_delete_item(const void* key, u16 keylen){ int retval = EEPROM_EOF; u16 next = EEPROM_HEADER_SIZE; int bytes = 0; eeItem item; u8 tmp[MAX_EEPROM_SIZE]; if (!check_eeprom_header()) { DEBUG_FAIL("Error reading EEPROM header\r\n"); return EEPROM_ERR_READ; } // Find the Item in the list. do { next += bytes; bytes = read_record(next, tmp, &item); if (bytes <= 0) { retval = bytes; break; } retval = next; } while (item.tag.length != keylen || itc_memcmp(key, item.tag.data, keylen)); // We found the Item or EOF if (retval > 0) { // We found the Item, try to update // Delete Item and append to the end of the list. u16 read_ptr = next; u16 write_ptr = next; u16 byte_count; do { read_ptr += bytes; // Skip forward. bytes = read_record(read_ptr, tmp, &item); if (bytes <= 0) { retval = bytes; break; } byte_count = bytes; if (!write_data_eeprom(write_ptr, tmp, &byte_count)) { retval = EEPROM_ERR_WRITE; break; } write_ptr += bytes; } while (1); if (retval >= 0) { retval = write_ptr; if (!write_eof(write_ptr)) retval = EEPROM_ERR_WRITE; } } return retval;}#endif // NEED_EEPROM_DELETE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -