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 10.0KB

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