eeprom.c (3006B)
1 /* Copyright 2017 Fred Sundvik 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 "eeprom.h" 18 19 static uint8_t buffer[TOTAL_EEPROM_BYTE_COUNT]; 20 21 uint8_t eeprom_read_byte(const uint8_t *addr) { 22 uintptr_t offset = (uintptr_t)addr; 23 return buffer[offset]; 24 } 25 26 void eeprom_write_byte(uint8_t *addr, uint8_t value) { 27 uintptr_t offset = (uintptr_t)addr; 28 buffer[offset] = value; 29 } 30 31 uint16_t eeprom_read_word(const uint16_t *addr) { 32 const uint8_t *p = (const uint8_t *)addr; 33 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); 34 } 35 36 uint32_t eeprom_read_dword(const uint32_t *addr) { 37 const uint8_t *p = (const uint8_t *)addr; 38 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); 39 } 40 41 void eeprom_read_block(void *buf, const void *addr, size_t len) { 42 const uint8_t *p = (const uint8_t *)addr; 43 uint8_t *dest = (uint8_t *)buf; 44 while (len--) { 45 *dest++ = eeprom_read_byte(p++); 46 } 47 } 48 49 void eeprom_write_word(uint16_t *addr, uint16_t value) { 50 uint8_t *p = (uint8_t *)addr; 51 eeprom_write_byte(p++, value); 52 eeprom_write_byte(p, value >> 8); 53 } 54 55 void eeprom_write_dword(uint32_t *addr, uint32_t value) { 56 uint8_t *p = (uint8_t *)addr; 57 eeprom_write_byte(p++, value); 58 eeprom_write_byte(p++, value >> 8); 59 eeprom_write_byte(p++, value >> 16); 60 eeprom_write_byte(p, value >> 24); 61 } 62 63 void eeprom_write_block(const void *buf, void *addr, size_t len) { 64 uint8_t *p = (uint8_t *)addr; 65 const uint8_t *src = (const uint8_t *)buf; 66 while (len--) { 67 eeprom_write_byte(p++, *src++); 68 } 69 } 70 71 void eeprom_update_byte(uint8_t *addr, uint8_t value) { 72 eeprom_write_byte(addr, value); 73 } 74 75 void eeprom_update_word(uint16_t *addr, uint16_t value) { 76 uint8_t *p = (uint8_t *)addr; 77 eeprom_write_byte(p++, value); 78 eeprom_write_byte(p, value >> 8); 79 } 80 81 void eeprom_update_dword(uint32_t *addr, uint32_t value) { 82 uint8_t *p = (uint8_t *)addr; 83 eeprom_write_byte(p++, value); 84 eeprom_write_byte(p++, value >> 8); 85 eeprom_write_byte(p++, value >> 16); 86 eeprom_write_byte(p, value >> 24); 87 } 88 89 void eeprom_update_block(const void *buf, void *addr, size_t len) { 90 uint8_t *p = (uint8_t *)addr; 91 const uint8_t *src = (const uint8_t *)buf; 92 while (len--) { 93 eeprom_write_byte(p++, *src++); 94 } 95 }