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.

56 lines
1.7 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. #include "eeprom_transient.h"
  20. __attribute__((aligned(4))) static uint8_t transientBuffer[TRANSIENT_EEPROM_SIZE] = {0};
  21. size_t clamp_length(intptr_t offset, size_t len) {
  22. if (offset + len > TRANSIENT_EEPROM_SIZE) {
  23. len = TRANSIENT_EEPROM_SIZE - offset;
  24. }
  25. return len;
  26. }
  27. void eeprom_driver_init(void) {
  28. eeprom_driver_erase();
  29. }
  30. void eeprom_driver_erase(void) {
  31. memset(transientBuffer, 0x00, TRANSIENT_EEPROM_SIZE);
  32. }
  33. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  34. intptr_t offset = (intptr_t)addr;
  35. memset(buf, 0x00, len);
  36. len = clamp_length(offset, len);
  37. if (len > 0) {
  38. memcpy(buf, &transientBuffer[offset], len);
  39. }
  40. }
  41. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  42. intptr_t offset = (intptr_t)addr;
  43. len = clamp_length(offset, len);
  44. if (len > 0) {
  45. memcpy(&transientBuffer[offset], buf, len);
  46. }
  47. }