S&B Volcano vaporizer remote control with Pi Pico W
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.

mem.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * mem.c
  3. *
  4. * Copyright (c) 2023 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 "hardware/flash.h"
  20. #include "pico/flash.h"
  21. #include "pico/btstack_flash_bank.h"
  22. #include "config.h"
  23. #include "log.h"
  24. #include "mem.h"
  25. #define FLASH_LOCK_TIMEOUT_MS 500
  26. /*
  27. * Last two flash pages are used by BTstack.
  28. * So we use the third-last page for our persistent storage.
  29. * This is kept clear by our custom linker script.
  30. */
  31. #define FLASH_OFFSET (PICO_FLASH_BANK_STORAGE_OFFSET - FLASH_SECTOR_SIZE)
  32. struct mem_contents {
  33. uint8_t version;
  34. uint32_t checksum;
  35. struct mem_data data;
  36. };
  37. #define MEM_CONTENTS_INIT { \
  38. .version = MEM_VERSION, \
  39. .checksum = 0, \
  40. .data = MEM_DATA_INIT, \
  41. }
  42. static const struct mem_contents data_defaults = MEM_CONTENTS_INIT;
  43. static struct mem_contents data_ram = data_defaults;
  44. static const uint8_t *data_flash = (const uint8_t *)(XIP_BASE + FLASH_OFFSET);
  45. static_assert(sizeof(struct mem_contents) < FLASH_SECTOR_SIZE,
  46. "Config needs to fit inside a flash sector");
  47. static uint32_t calc_checksum(const struct mem_contents *data) {
  48. uint32_t c = 0xFFFFFFFF;
  49. const uint8_t *d = (const uint8_t *)data;
  50. const size_t offset_checksum = offsetof(struct mem_contents, checksum);
  51. const size_t size_checksum = sizeof(data->checksum);
  52. for (size_t i = 0; i < sizeof(struct mem_contents); i++) {
  53. if ((i >= offset_checksum) && (i < (offset_checksum + size_checksum))) {
  54. continue;
  55. }
  56. // adapted from "Hacker's Delight"
  57. c ^= d[i];
  58. for (size_t j = 0; j < 8; j++) {
  59. uint32_t mask = -(c & 1);
  60. c = (c >> 1) ^ (0xEDB88320 & mask);
  61. }
  62. }
  63. return ~c;
  64. }
  65. void mem_load_defaults(void) {
  66. data_ram = data_defaults;
  67. data_ram.data.wf_count = wf_default_count;
  68. for (uint16_t i = 0; i < wf_default_count; i++) {
  69. data_ram.data.wf[i] = wf_default_data[i];
  70. }
  71. // TODO better way to pre-define WiFi credentials
  72. #if defined(DEFAULT_WIFI_SSID) && defined(DEFAULT_WIFI_PASS)
  73. data_ram.data.net_count = 1;
  74. strcpy(data_ram.data.net[0].name, DEFAULT_WIFI_SSID);
  75. strcpy(data_ram.data.net[0].pass, DEFAULT_WIFI_PASS);
  76. #else
  77. data_ram.data.net_count = 0;
  78. #endif
  79. }
  80. void mem_load(void) {
  81. mem_load_defaults();
  82. if (!flash_safe_execute_core_init()) {
  83. debug("error calling flash_safe_execute_core_init");
  84. }
  85. const struct mem_contents *flash_ptr = (const struct mem_contents *)data_flash;
  86. if (flash_ptr->version == MEM_VERSION) {
  87. debug("found matching config (0x%02X)", flash_ptr->version);
  88. uint32_t checksum = calc_checksum(flash_ptr);
  89. if (checksum != flash_ptr->checksum) {
  90. debug("invalid checksum (0x%08lX != 0x%08lX)", flash_ptr->checksum, checksum);
  91. } else {
  92. debug("loading from flash (0x%08lX)", checksum);
  93. data_ram = *flash_ptr;
  94. debug("%s", data_ram.data.net[0].pass);
  95. }
  96. } else {
  97. debug("invalid config (0x%02X != 0x%02X)", flash_ptr->version, MEM_VERSION);
  98. }
  99. #if defined(DEFAULT_WIFI_SSID) && defined(DEFAULT_WIFI_PASS)
  100. // add default WiFi from #define to flash config, if it is not there yet
  101. bool found = false;
  102. for (uint16_t i = 0; i < data_ram.data.net_count; i++) {
  103. if (strcmp(data_ram.data.net[i].name, DEFAULT_WIFI_SSID) == 0) {
  104. if (strcmp(data_ram.data.net[i].pass, DEFAULT_WIFI_PASS) != 0) {
  105. debug("warning: restoring wifi password for '%s'", DEFAULT_WIFI_SSID);
  106. strcpy(data_ram.data.net[i].pass, DEFAULT_WIFI_PASS);
  107. }
  108. found = true;
  109. break;
  110. }
  111. }
  112. if ((!found) && (data_ram.data.net_count < WIFI_MAX_NET_COUNT)) {
  113. debug("info: adding wifi password for '%s'", DEFAULT_WIFI_SSID);
  114. strcpy(data_ram.data.net[data_ram.data.net_count].name, DEFAULT_WIFI_SSID);
  115. strcpy(data_ram.data.net[data_ram.data.net_count].pass, DEFAULT_WIFI_PASS);
  116. data_ram.data.net_count++;
  117. }
  118. #endif
  119. }
  120. static void mem_write_flash(void *param) {
  121. flash_range_erase(FLASH_OFFSET, FLASH_SECTOR_SIZE);
  122. // TODO only need to write with length multiple of FLASH_PAGE_SIZE
  123. flash_range_program(FLASH_OFFSET, param, FLASH_SECTOR_SIZE);
  124. }
  125. void mem_write(void) {
  126. if (memcmp(&data_ram, data_flash, sizeof(struct mem_contents)) == 0) {
  127. debug("no change, skip write");
  128. return;
  129. }
  130. data_ram.checksum = calc_checksum(&data_ram);
  131. debug("writing new data (0x%08lX)", data_ram.checksum);
  132. int r = flash_safe_execute(mem_write_flash, &data_ram, FLASH_LOCK_TIMEOUT_MS);
  133. if (r != PICO_OK) {
  134. debug("error calling mem_write_flash: %d", r);
  135. }
  136. }
  137. struct mem_data *mem_data(void) {
  138. return &data_ram.data;
  139. }