My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SPIFlashStorage.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. extern W25QXXFlash W25QXX;
  27. uint8_t SPIFlashStorage::m_pageData[SPI_FLASH_PageSize];
  28. uint32_t SPIFlashStorage::m_currentPage;
  29. uint16_t SPIFlashStorage::m_pageDataUsed;
  30. uint32_t SPIFlashStorage::m_startAddress;
  31. #if HAS_SPI_FLASH_COMPRESSION
  32. uint8_t SPIFlashStorage::m_compressedData[SPI_FLASH_PageSize];
  33. uint16_t SPIFlashStorage::m_compressedDataUsed;
  34. template <typename T>
  35. static uint32_t rle_compress(T *output, uint32_t outputLength, T *input, uint32_t inputLength, uint32_t& inputProcessed) {
  36. uint32_t count = 0, out = 0, index, i;
  37. T pixel;
  38. //32767 for uint16_t
  39. //127 for uint16_t
  40. //calculated at compile time
  41. constexpr T max = (0xFFFFFFFF >> (8 * (4 - sizeof(T)))) / 2;
  42. inputProcessed = 0;
  43. while (count < inputLength && out < outputLength) {
  44. index = count;
  45. pixel = input[index++];
  46. while (index < inputLength && index - count < max && input[index] == pixel)
  47. index++;
  48. if (index - count == 1) {
  49. /*
  50. * Failed to "replicate" the current pixel. See how many to copy.
  51. * Avoid a replicate run of only 2-pixels after a literal run. There
  52. * is no gain in this, and there is a risK of loss if the run after
  53. * the two identical pixels is another literal run. So search for
  54. * 3 identical pixels.
  55. */
  56. while (index < inputLength && index - count < max && (input[index] != input[index - 1] || (index > 1 && input[index] != input[index - 2])))
  57. index++;
  58. /*
  59. * Check why this run stopped. If it found two identical pixels, reset
  60. * the index so we can add a run. Do this twice: the previous run
  61. * tried to detect a replicate run of at least 3 pixels. So we may be
  62. * able to back up two pixels if such a replicate run was found.
  63. */
  64. while (index < inputLength && input[index] == input[index - 1])
  65. index--;
  66. // If the output buffer could overflow, stop at the remaining bytes
  67. NOMORE(index, count + outputLength - out - 1);
  68. output[out++] = (uint16_t)(count - index);
  69. for (i = count; i < index; i++)
  70. output[out++] = input[i];
  71. }
  72. else {
  73. // Need at least more 2 spaces
  74. if (out > outputLength - 2) break;
  75. output[out++] = (uint16_t)(index - count);
  76. output[out++] = pixel;
  77. }
  78. count = index;
  79. }
  80. inputProcessed = count;
  81. // Padding
  82. if (out == outputLength - 1) output[out++] = 0;
  83. return out;
  84. }
  85. template <typename UT, typename T>
  86. static uint32_t rle_uncompress(UT *output, uint32_t outputLength, UT *input, uint32_t inputLength, uint32_t &outputFilled) {
  87. T count;
  88. UT i;
  89. uint32_t processedBytes = 0;
  90. outputFilled = 0;
  91. while (outputLength > 0 && inputLength > 0) {
  92. processedBytes++;
  93. count = static_cast<T>(*input++);
  94. inputLength--;
  95. if (count > 0) { // Replicate run
  96. for (i = 0; i < count && outputLength > i; i++)
  97. output[i] = *input;
  98. outputFilled += i;
  99. // If copy incomplete, change the input buffer to start with remaining data in the next call
  100. if (i < count) {
  101. // Change to process the difference in the next call
  102. *(input - 1) = static_cast<UT>(count - i);
  103. return processedBytes - 1;
  104. }
  105. input++;
  106. inputLength--;
  107. processedBytes++;
  108. }
  109. else if (count < 0) { // literal run
  110. count = static_cast<T>(-count);
  111. // Copy, validating if the output have enough space
  112. for (i = 0; i < count && outputLength > i; i++)
  113. output[i] = input[i];
  114. outputFilled += i;
  115. // If copy incomplete, change the input buffer to start with remaining data in the next call
  116. if (i < count) {
  117. input[i - 1] = static_cast<UT>((count - i) * -1);
  118. // Back one
  119. return processedBytes + i - 1;
  120. }
  121. input += count;
  122. inputLength -= count;
  123. processedBytes += count;
  124. }
  125. output += count;
  126. outputLength -= count;
  127. }
  128. return processedBytes;
  129. }
  130. #endif // HAS_SPI_FLASH_COMPRESSION
  131. void SPIFlashStorage::beginWrite(uint32_t startAddress) {
  132. m_pageDataUsed = 0;
  133. m_currentPage = 0;
  134. m_startAddress = startAddress;
  135. #if HAS_SPI_FLASH_COMPRESSION
  136. // Restart the compressed buffer, keep the pointers of the uncompressed buffer
  137. m_compressedDataUsed = 0;
  138. #endif
  139. }
  140. void SPIFlashStorage::endWrite() {
  141. // Flush remaining data
  142. #if HAS_SPI_FLASH_COMPRESSION
  143. if (m_compressedDataUsed > 0) {
  144. flushPage();
  145. savePage(m_compressedData);
  146. }
  147. #else
  148. if (m_pageDataUsed > 0) flushPage();
  149. #endif
  150. }
  151. void SPIFlashStorage::savePage(uint8_t *buffer) {
  152. W25QXX.SPI_FLASH_BufferWrite(buffer, m_startAddress + (SPI_FLASH_PageSize * m_currentPage), SPI_FLASH_PageSize);
  153. // Test env
  154. // char fname[256];
  155. // snprintf(fname, sizeof(fname), "./pages/page-%03d.data", m_currentPage);
  156. // FILE *fp = fopen(fname, "wb");
  157. // fwrite(buffer, 1, m_compressedDataUsed, fp);
  158. // fclose(fp);
  159. }
  160. void SPIFlashStorage::loadPage(uint8_t *buffer) {
  161. W25QXX.SPI_FLASH_BufferRead(buffer, m_startAddress + (SPI_FLASH_PageSize * m_currentPage), SPI_FLASH_PageSize);
  162. // Test env
  163. // char fname[256];
  164. // snprintf(fname, sizeof(fname), "./pages/page-%03d.data", m_currentPage);
  165. // FILE *fp = fopen(fname, "rb");
  166. // if (fp) {
  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 adjust 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. #else
  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