Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mem.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * mem.c
  3. *
  4. * Copyright (c) 2023 - 2025 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <string.h>
  19. #include "pico/flash.h"
  20. #include "config.h"
  21. #include "log.h"
  22. #include "sequence.h"
  23. #include "mem.h"
  24. struct mem_contents {
  25. uint8_t version;
  26. uint32_t checksum;
  27. struct mem_data data;
  28. };
  29. #define MEM_CONTENTS_INIT { \
  30. .version = MEM_VERSION, \
  31. .checksum = 0, \
  32. .data = MEM_DATA_INIT, \
  33. }
  34. static const struct mem_contents data_defaults = MEM_CONTENTS_INIT;
  35. static struct mem_contents data_ram = data_defaults;
  36. static const uint8_t *data_flash = (const uint8_t *)(XIP_BASE + EEPROM_FLASH_OFFSET);
  37. static uint8_t page_buffer[FLASH_PAGE_SIZE] = {0};
  38. #define MEM_DATA_SIZE sizeof(struct mem_contents)
  39. static_assert(MEM_DATA_SIZE < FLASH_SECTOR_SIZE,
  40. "Config needs to fit inside a flash sector");
  41. static uint32_t calc_checksum(const struct mem_contents *data) {
  42. uint32_t c = 0xFFFFFFFF;
  43. const uint8_t *d = (const uint8_t *)data;
  44. const size_t offset_checksum = offsetof(struct mem_contents, checksum);
  45. const size_t size_checksum = sizeof(data->checksum);
  46. for (size_t i = 0; i < sizeof(struct mem_contents); i++) {
  47. if ((i >= offset_checksum) && (i < (offset_checksum + size_checksum))) {
  48. continue;
  49. }
  50. // adapted from "Hacker's Delight"
  51. c ^= d[i];
  52. for (size_t j = 0; j < 8; j++) {
  53. uint32_t mask = -(c & 1);
  54. c = (c >> 1) ^ (0xEDB88320 & mask);
  55. }
  56. }
  57. return ~c;
  58. }
  59. void mem_load_defaults(void) {
  60. data_ram = data_defaults;
  61. for (uint32_t i = 0; i < NUM_CHANNELS; i++) {
  62. data_ram.data.ch_timings[i] = CH_GPIO_DEFAULT_MS;
  63. }
  64. }
  65. void mem_load(void) {
  66. mem_load_defaults();
  67. if (!flash_safe_execute_core_init()) {
  68. debug("error calling flash_safe_execute_core_init");
  69. }
  70. const struct mem_contents *flash_ptr = (const struct mem_contents *)data_flash;
  71. if (flash_ptr->version == MEM_VERSION) {
  72. debug("found matching config (0x%02"PRIX8")", flash_ptr->version);
  73. uint32_t checksum = calc_checksum(flash_ptr);
  74. if (checksum != flash_ptr->checksum) {
  75. debug("invalid checksum (0x%08"PRIX32" != 0x%08"PRIX32")", flash_ptr->checksum, checksum);
  76. } else {
  77. if (flash_ptr->data.ch_count != NUM_CHANNELS) {
  78. debug("invalid channel count (0x%"PRIu32" != 0x%d)", flash_ptr->data.ch_count, NUM_CHANNELS);
  79. } else {
  80. debug("loading from flash (0x%08"PRIX32")", checksum);
  81. data_ram = *flash_ptr;
  82. }
  83. }
  84. } else {
  85. debug("invalid config (0x%02"PRIX8" != 0x%02X)", flash_ptr->version, MEM_VERSION);
  86. }
  87. }
  88. static void mem_write_flash(void *param) {
  89. // we erase a 4K sector (FLASH_SECTOR_SIZE), but...
  90. flash_range_erase(EEPROM_FLASH_OFFSET, FLASH_SECTOR_SIZE);
  91. // ...can write in 256 byte pages (FLASH_PAGE_SIZE)
  92. for (uint32_t off = 0; off < MEM_DATA_SIZE; off += FLASH_PAGE_SIZE) {
  93. memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
  94. memcpy(page_buffer, param + off, MIN(FLASH_PAGE_SIZE, MEM_DATA_SIZE - off));
  95. flash_range_program(EEPROM_FLASH_OFFSET + off, page_buffer, FLASH_PAGE_SIZE);
  96. }
  97. }
  98. void mem_write(void) {
  99. if (memcmp(&data_ram, data_flash, sizeof(struct mem_contents)) == 0) {
  100. debug("no change, skip write");
  101. return;
  102. }
  103. data_ram.checksum = calc_checksum(&data_ram);
  104. debug("writing new data (0x%08"PRIX32")", data_ram.checksum);
  105. int r = flash_safe_execute(mem_write_flash, &data_ram, FLASH_LOCK_TIMEOUT_MS);
  106. if (r != PICO_OK) {
  107. debug("error calling mem_write_flash: %d", r);
  108. }
  109. }
  110. struct mem_data *mem_data(void) {
  111. return &data_ram.data;
  112. }