logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

nvm_via.c (2616B)


  1. // Copyright 2024 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "eeprom.h"
  4. #include "util.h"
  5. #include "via.h"
  6. #include "nvm_via.h"
  7. #include "nvm_eeprom_eeconfig_internal.h"
  8. #include "nvm_eeprom_via_internal.h"
  9. void nvm_via_erase(void) {
  10. // No-op, nvm_eeconfig_erase() will have already erased EEPROM if necessary.
  11. }
  12. void nvm_via_read_magic(uint8_t *magic0, uint8_t *magic1, uint8_t *magic2) {
  13. if (magic0) {
  14. *magic0 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0);
  15. }
  16. if (magic1) {
  17. *magic1 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1);
  18. }
  19. if (magic2) {
  20. *magic2 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2);
  21. }
  22. }
  23. void nvm_via_update_magic(uint8_t magic0, uint8_t magic1, uint8_t magic2) {
  24. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, magic0);
  25. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, magic1);
  26. eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, magic2);
  27. }
  28. uint32_t nvm_via_read_layout_options(void) {
  29. uint32_t value = 0;
  30. // Start at the most significant byte
  31. void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
  32. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  33. value = value << 8;
  34. value |= eeprom_read_byte(source);
  35. source++;
  36. }
  37. return value;
  38. }
  39. void nvm_via_update_layout_options(uint32_t val) {
  40. // Start at the least significant byte
  41. void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
  42. for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
  43. eeprom_update_byte(target, val & 0xFF);
  44. val = val >> 8;
  45. target--;
  46. }
  47. }
  48. uint32_t nvm_via_read_custom_config(void *buf, uint32_t offset, uint32_t length) {
  49. #if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0
  50. void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset);
  51. void *ee_end = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length));
  52. eeprom_read_block(buf, ee_start, ee_end - ee_start);
  53. return ee_end - ee_start;
  54. #else
  55. return 0;
  56. #endif
  57. }
  58. uint32_t nvm_via_update_custom_config(const void *buf, uint32_t offset, uint32_t length) {
  59. #if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0
  60. void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset);
  61. void *ee_end = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length));
  62. eeprom_update_block(buf, ee_start, ee_end - ee_start);
  63. return ee_end - ee_start;
  64. #else
  65. return 0;
  66. #endif
  67. }