You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

  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. #include <stdint.h>
  17. #include <string.h>
  18. #include "eeprom_driver.h"
  19. uint8_t eeprom_read_byte(const uint8_t *addr) {
  20. uint8_t ret = 0;
  21. eeprom_read_block(&ret, addr, 1);
  22. return ret;
  23. }
  24. uint16_t eeprom_read_word(const uint16_t *addr) {
  25. uint16_t ret = 0;
  26. eeprom_read_block(&ret, addr, 2);
  27. return ret;
  28. }
  29. uint32_t eeprom_read_dword(const uint32_t *addr) {
  30. uint32_t ret = 0;
  31. eeprom_read_block(&ret, addr, 4);
  32. return ret;
  33. }
  34. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  35. eeprom_write_block(&value, addr, 1);
  36. }
  37. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  38. eeprom_write_block(&value, addr, 2);
  39. }
  40. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  41. eeprom_write_block(&value, addr, 4);
  42. }
  43. void eeprom_update_block(const void *buf, void *addr, size_t len) {
  44. uint8_t read_buf[len];
  45. eeprom_read_block(read_buf, addr, len);
  46. if (memcmp(buf, read_buf, len) != 0) {
  47. eeprom_write_block(buf, addr, len);
  48. }
  49. }
  50. void eeprom_update_byte(uint8_t *addr, uint8_t value) {
  51. uint8_t orig = eeprom_read_byte(addr);
  52. if (orig != value) {
  53. eeprom_write_byte(addr, value);
  54. }
  55. }
  56. void eeprom_update_word(uint16_t *addr, uint16_t value) {
  57. uint16_t orig = eeprom_read_word(addr);
  58. if (orig != value) {
  59. eeprom_write_word(addr, value);
  60. }
  61. }
  62. void eeprom_update_dword(uint32_t *addr, uint32_t value) {
  63. uint32_t orig = eeprom_read_dword(addr);
  64. if (orig != value) {
  65. eeprom_write_dword(addr, value);
  66. }
  67. }