S&B Volcano vaporizer remote control with Pi Pico W
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mem.c 4.9KB

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