diff options
Diffstat (limited to 'drivers/eeprom/eeprom_driver.c')
| -rw-r--r-- | drivers/eeprom/eeprom_driver.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/drivers/eeprom/eeprom_driver.c b/drivers/eeprom/eeprom_driver.c new file mode 100644 index 000000000..3835e5e9d --- /dev/null +++ b/drivers/eeprom/eeprom_driver.c | |||
| @@ -0,0 +1,73 @@ | |||
| 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; | ||
| 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; | ||
| 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; | ||
| 36 | eeprom_read_block(&ret, addr, 4); | ||
| 37 | return ret; | ||
| 38 | } | ||
| 39 | |||
| 40 | void eeprom_write_byte(uint8_t *addr, uint8_t value) { eeprom_write_block(&value, addr, 1); } | ||
| 41 | |||
| 42 | void eeprom_write_word(uint16_t *addr, uint16_t value) { eeprom_write_block(&value, addr, 2); } | ||
| 43 | |||
| 44 | void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_block(&value, addr, 4); } | ||
| 45 | |||
| 46 | void eeprom_update_block(const void *buf, void *addr, size_t len) { | ||
| 47 | uint8_t read_buf[len]; | ||
| 48 | eeprom_read_block(read_buf, addr, len); | ||
| 49 | if (memcmp(buf, read_buf, len) != 0) { | ||
| 50 | eeprom_write_block(buf, addr, len); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | void eeprom_update_byte(uint8_t *addr, uint8_t value) { | ||
| 55 | uint8_t orig = eeprom_read_byte(addr); | ||
| 56 | if (orig != value) { | ||
| 57 | eeprom_write_byte(addr, value); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | void eeprom_update_word(uint16_t *addr, uint16_t value) { | ||
| 62 | uint16_t orig = eeprom_read_word(addr); | ||
| 63 | if (orig != value) { | ||
| 64 | eeprom_write_word(addr, value); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | void eeprom_update_dword(uint32_t *addr, uint32_t value) { | ||
| 69 | uint32_t orig = eeprom_read_dword(addr); | ||
| 70 | if (orig != value) { | ||
| 71 | eeprom_write_dword(addr, value); | ||
| 72 | } | ||
| 73 | } | ||
