My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

SPIFlashStorage.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../inc/MarlinConfigPre.h"
  23. #if HAS_TFT_LVGL_UI
  24. #include "../../../inc/MarlinConfig.h"
  25. #include "SPIFlashStorage.h"
  26. #if !HAS_SPI_FLASH
  27. #error "HAS_SPI_FLASH is required with TFT_LVGL_UI."
  28. #endif
  29. extern W25QXXFlash W25QXX;
  30. uint8_t SPIFlashStorage::m_pageData[SPI_FLASH_PageSize];
  31. uint32_t SPIFlashStorage::m_currentPage;
  32. uint16_t SPIFlashStorage::m_pageDataUsed;
  33. uint32_t SPIFlashStorage::m_startAddress;
  34. #if HAS_SPI_FLASH_COMPRESSION
  35. uint8_t SPIFlashStorage::m_compressedData[SPI_FLASH_PageSize];
  36. uint16_t SPIFlashStorage::m_compressedDataUsed;
  37. template <typename T>
  38. static uint32_t rle_compress(T *output, uint32_t outputLength, T *input, uint32_t inputLength, uint32_t& inputProcessed) {
  39. uint32_t count = 0, out = 0, index, i;
  40. T pixel;
  41. // 32767 for uint16_t
  42. // 127 for uint16_t
  43. // calculated at compile time
  44. constexpr T max = (0xFFFFFFFF >> (8 * (4 - sizeof(T)))) / 2;
  45. inputProcessed = 0;
  46. while (count < inputLength && out < outputLength) {
  47. index = count;
  48. pixel = input[index++];
  49. while (index < inputLength && index - count < max && input[index] == pixel)
  50. index++;
  51. if (index - count == 1) {
  52. /*
  53. * Failed to "replicate" the current pixel. See how many to copy.
  54. * Avoid a replicate run of only 2-pixels after a literal run. There
  55. * is no gain in this, and there is a risK of loss if the run after
  56. * the two identical pixels is another literal run. So search for
  57. * 3 identical pixels.
  58. */
  59. while (index < inputLength && index - count < max && (input[index] != input[index - 1] || (index > 1 && input[index] != input[index - 2])))
  60. index++;
  61. /*
  62. * Check why this run stopped. If it found two identical pixels, reset
  63. * the index so we can add a run. Do this twice: the previous run
  64. * tried to detect a replicate run of at least 3 pixels. So we may be
  65. * able to back up two pixels if such a replicate run was found.
  66. */
  67. while (index < inputLength && input[index] == input[index - 1])
  68. index--;
  69. // If the output buffer could overflow, stop at the remaining bytes
  70. NOMORE(index, count + outputLength - out - 1);
  71. output[out++] = (uint16_t)(count - index);
  72. for (i = count; i < index; i++)
  73. output[out++] = input[i];
  74. }
  75. else {
  76. // Need at least more 2 spaces
  77. if (out > outputLength - 2) break;
  78. output[out++] = (uint16_t)(index - count);
  79. output[out++] = pixel;
  80. }
  81. count = index;
  82. }
  83. inputProcessed = count;
  84. // Padding
  85. if (out == outputLength - 1) output[out++] = 0;
  86. return out;
  87. }
  88. template <typename UT, typename T>
  89. static uint32_t rle_uncompress(UT *output, uint32_t outputLength, UT *input, uint32_t inputLength, uint32_t &outputFilled) {
  90. T count;
  91. UT i;
  92. uint32_t processedBytes = 0;
  93. outputFilled = 0;
  94. while (outputLength > 0 && inputLength > 0) {
  95. processedBytes++;
  96. count = static_cast<T>(*input++);
  97. inputLength--;
  98. if (count > 0) { // Replicate run
  99. for (i = 0; i < count && outputLength > i; i++)
  100. output[i] = *input;
  101. outputFilled += i;
  102. // If copy incomplete, change the input buffer to start with remaining data in the next call
  103. if (i < count) {
  104. // Change to process the difference in the next call
  105. *(input - 1) = static_cast<UT>(count - i);
  106. return processedBytes - 1;
  107. }
  108. input++;
  109. inputLength--;
  110. processedBytes++;
  111. }
  112. else if (count < 0) { // literal run
  113. count = static_cast<T>(-count);
  114. // Copy, validating if the output have enough space
  115. for (i = 0; i < count && outputLength > i; i++)
  116. output[i] = input[i];
  117. outputFilled += i;
  118. // If copy incomplete, change the input buffer to start with remaining data in the next call
  119. if (i < count) {
  120. input[i - 1] = static_cast<UT>((count - i) * -1);
  121. // Back one
  122. return processedBytes + i - 1;
  123. }
  124. input += count;
  125. inputLength -= count;
  126. processedBytes += count;
  127. }
  128. output += count;
  129. outputLength -= count;
  130. }
  131. return processedBytes;
  132. }
  133. #endif // HAS_SPI_FLASH_COMPRESSION
  134. void SPIFlashStorage::beginWrite(uint32_t startAddress) {
  135. m_pageDataUsed = 0;
  136. m_currentPage = 0;
  137. m_startAddress = startAddress;
  138. #if HAS_SPI_FLASH_COMPRESSION
  139. // Restart the compressed buffer, keep the pointers of the uncompressed buffer
  140. m_compressedDataUsed = 0;
  141. #endif
  142. }
  143. void SPIFlashStorage::endWrite() {
  144. // Flush remaining data
  145. #if HAS_SPI_FLASH_COMPRESSION
  146. if (m_compressedDataUsed > 0) {
  147. flushPage();
  148. savePage(m_compressedData);
  149. }
  150. #else
  151. if (m_pageDataUsed > 0) flushPage();
  152. #endif
  153. }
  154. void SPIFlashStorage::savePage(uint8_t *buffer) {
  155. W25QXX.SPI_FLASH_BufferWrite(buffer, m_startAddress + (SPI_FLASH_PageSize * m_currentPage), SPI_FLASH_PageSize);
  156. // Test env
  157. // char fname[256];
  158. // snprintf(fname, sizeof(fname), "./pages/page-%03d.data", m_currentPage);
  159. // FILE *fp = fopen(fname, "wb");
  160. // fwrite(buffer, 1, m_compressedDataUsed, fp);
  161. // fclose(fp);
  162. }
  163. void SPIFlashStorage::loadPage(uint8_t *buffer) {
  164. W25QXX.SPI_FLASH_BufferRead(buffer, m_startAddress + (SPI_FLASH_PageSize * m_currentPage), SPI_FLASH_PageSize);
  165. // Test env
  166. // char fname[256];
  167. // snprintf(fname, sizeof(fname), "./pages/page-%03d.data", m_currentPage);
  168. // FILE *fp = fopen(fname, "rb");
  169. // if (fp) {
  170. // fread(buffer, 1, SPI_FLASH_PageSize, fp);
  171. // fclose(fp);
  172. // }
  173. }
  174. void SPIFlashStorage::flushPage() {
  175. #if HAS_SPI_FLASH_COMPRESSION
  176. // Work com with compressed in memory
  177. uint32_t inputProcessed;
  178. uint32_t compressedSize = rle_compress<uint16_t>((uint16_t *)(m_compressedData + m_compressedDataUsed), compressedDataFree() / 2, (uint16_t *)m_pageData, m_pageDataUsed / 2, inputProcessed) * 2;
  179. inputProcessed *= 2;
  180. m_compressedDataUsed += compressedSize;
  181. // Space remaining in the compressed buffer?
  182. if (compressedDataFree() > 0) {
  183. // Free the uncompressed buffer
  184. m_pageDataUsed = 0;
  185. return;
  186. }
  187. // Part of the m_pageData was compressed, so adjust the pointers, freeing what was processed, shift the buffer
  188. // TODO: To avoid this copy, use a circular buffer
  189. memmove(m_pageData, m_pageData + inputProcessed, m_pageDataUsed - inputProcessed);
  190. m_pageDataUsed -= inputProcessed;
  191. // No? So flush page with compressed data!!
  192. uint8_t *buffer = m_compressedData;
  193. #else
  194. uint8_t *buffer = m_pageData;
  195. #endif
  196. savePage(buffer);
  197. #if HAS_SPI_FLASH_COMPRESSION
  198. // Restart the compressed buffer, keep the pointers of the uncompressed buffer
  199. m_compressedDataUsed = 0;
  200. #else
  201. m_pageDataUsed = 0;
  202. #endif
  203. m_currentPage++;
  204. }
  205. void SPIFlashStorage::readPage() {
  206. #if HAS_SPI_FLASH_COMPRESSION
  207. if (compressedDataFree() == 0) {
  208. loadPage(m_compressedData);
  209. m_currentPage++;
  210. m_compressedDataUsed = 0;
  211. }
  212. // Need to uncompress data
  213. if (pageDataFree() == 0) {
  214. m_pageDataUsed = 0;
  215. uint32_t outpuProcessed = 0;
  216. uint32_t inputProcessed = rle_uncompress<uint16_t, int16_t>((uint16_t *)(m_pageData + m_pageDataUsed), pageDataFree() / 2, (uint16_t *)(m_compressedData + m_compressedDataUsed), compressedDataFree() / 2, outpuProcessed);
  217. inputProcessed *= 2;
  218. outpuProcessed *= 2;
  219. if (outpuProcessed < pageDataFree()) {
  220. m_pageDataUsed = SPI_FLASH_PageSize - outpuProcessed;
  221. // TODO: To avoid this copy, use a circular buffer
  222. memmove(m_pageData + m_pageDataUsed, m_pageData, outpuProcessed);
  223. }
  224. m_compressedDataUsed += inputProcessed;
  225. }
  226. #else
  227. loadPage(m_pageData);
  228. m_pageDataUsed = 0;
  229. m_currentPage++;
  230. #endif
  231. }
  232. uint16_t SPIFlashStorage::inData(uint8_t *data, uint16_t size) {
  233. // Don't write more than we can
  234. NOMORE(size, pageDataFree());
  235. memcpy(m_pageData + m_pageDataUsed, data, size);
  236. m_pageDataUsed += size;
  237. return size;
  238. }
  239. void SPIFlashStorage::writeData(uint8_t *data, uint16_t size) {
  240. // Flush a page if needed
  241. if (pageDataFree() == 0) flushPage();
  242. while (size > 0) {
  243. uint16_t written = inData(data, size);
  244. size -= written;
  245. // Need to write more? Flush page and continue!
  246. if (size > 0) {
  247. flushPage();
  248. data += written;
  249. }
  250. }
  251. }
  252. void SPIFlashStorage::beginRead(uint32_t startAddress) {
  253. m_startAddress = startAddress;
  254. m_currentPage = 0;
  255. // Nothing in memory now
  256. m_pageDataUsed = SPI_FLASH_PageSize;
  257. #if HAS_SPI_FLASH_COMPRESSION
  258. m_compressedDataUsed = sizeof(m_compressedData);
  259. #endif
  260. }
  261. uint16_t SPIFlashStorage::outData(uint8_t *data, uint16_t size) {
  262. // Don't read more than we have
  263. NOMORE(size, pageDataFree());
  264. memcpy(data, m_pageData + m_pageDataUsed, size);
  265. m_pageDataUsed += size;
  266. return size;
  267. }
  268. void SPIFlashStorage::readData(uint8_t *data, uint16_t size) {
  269. // Read a page if needed
  270. if (pageDataFree() == 0) readPage();
  271. while (size > 0) {
  272. uint16_t read = outData(data, size);
  273. size -= read;
  274. // Need to write more? Flush page and continue!
  275. if (size > 0) {
  276. readPage();
  277. data += read;
  278. }
  279. }
  280. }
  281. SPIFlashStorage SPIFlash;
  282. #endif // HAS_TFT_LVGL_UI