eeprom_driver.c (2454B)
1 /* Copyright 2019 Nick Brassel (tzarc) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <stdint.h> 18 #include <string.h> 19 20 #include "eeprom_driver.h" 21 22 uint8_t eeprom_read_byte(const uint8_t *addr) { 23 uint8_t ret = 0; 24 eeprom_read_block(&ret, addr, 1); 25 return ret; 26 } 27 28 uint16_t eeprom_read_word(const uint16_t *addr) { 29 uint16_t ret = 0; 30 eeprom_read_block(&ret, addr, 2); 31 return ret; 32 } 33 34 uint32_t eeprom_read_dword(const uint32_t *addr) { 35 uint32_t ret = 0; 36 eeprom_read_block(&ret, addr, 4); 37 return ret; 38 } 39 40 void eeprom_write_byte(uint8_t *addr, uint8_t value) { 41 eeprom_write_block(&value, addr, 1); 42 } 43 44 void eeprom_write_word(uint16_t *addr, uint16_t value) { 45 eeprom_write_block(&value, addr, 2); 46 } 47 48 void eeprom_write_dword(uint32_t *addr, uint32_t value) { 49 eeprom_write_block(&value, addr, 4); 50 } 51 52 void eeprom_update_block(const void *buf, void *addr, size_t len) { 53 uint8_t read_buf[len]; 54 eeprom_read_block(read_buf, addr, len); 55 if (memcmp(buf, read_buf, len) != 0) { 56 eeprom_write_block(buf, addr, len); 57 } 58 } 59 60 void eeprom_update_byte(uint8_t *addr, uint8_t value) { 61 uint8_t orig = eeprom_read_byte(addr); 62 if (orig != value) { 63 eeprom_write_byte(addr, value); 64 } 65 } 66 67 void eeprom_update_word(uint16_t *addr, uint16_t value) { 68 uint16_t orig = eeprom_read_word(addr); 69 if (orig != value) { 70 eeprom_write_word(addr, value); 71 } 72 } 73 74 void eeprom_update_dword(uint32_t *addr, uint32_t value) { 75 uint32_t orig = eeprom_read_dword(addr); 76 if (orig != value) { 77 eeprom_write_dword(addr, value); 78 } 79 } 80 81 void eeprom_driver_format(bool erase) __attribute__((weak)); 82 void eeprom_driver_format(bool erase) { 83 (void)erase; /* The default implementation assumes that the eeprom must be erased in order to be usable. */ 84 eeprom_driver_erase(); 85 }