spi_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "at32f403a_407.h"
  2. #include "spi_flash.h"
  3. #include "spi_common.h"
  4. #include "FreeRTOS.h"
  5. #include "task.h"
  6. #include "common_config.h"
  7. #include <string.h>
  8. #include <stdio.h>
  9. #define SPI_FLASH SPI3
  10. #define INCLUDE_SPI_TEST 0
  11. #define CMD_WREN 0x06 // +
  12. #define CMD_WRDI 0x04 // +
  13. #define CMD_WRSR 0x01 // +
  14. #define CMD_RDSR 0x05 // +
  15. #define CMD_READ 0x03 // +
  16. #define CMD_SE 0x20 // + Block Erse 4 Kbytes
  17. #define CMD_BE 0x52 // Block Erase 32 Kbytes
  18. #define CMD_CE 0x60 // + Chip Erase
  19. #define CMD_PP 0x02 // + Byte/Page Program (1 to 256 Bytes)
  20. #define CMD_RDSFDP 0x5A // - not used for FLASH_TYPE_AT25SF161
  21. #define SR_WIP (1 << 0)
  22. #define SR_WEL (1 << 1)
  23. #define SR_SRWD (1 << 7)
  24. static spi_flash_desc_t spi_flash_desc;
  25. //
  26. static uint8_t spi_tx_rx(uint8_t byte)
  27. {
  28. return common_spi_tx_rx(SPI_FLASH, byte);
  29. }
  30. //
  31. static inline void wait_write_enable(void)
  32. {
  33. uint8_t status;
  34. SPI_FLASH_CS_L();
  35. spi_tx_rx(CMD_RDSR);
  36. do {
  37. status = spi_tx_rx(0);
  38. } while (!(status & SR_WEL));
  39. SPI_FLASH_CS_H();
  40. }
  41. //
  42. static inline void wait_write_end(void)
  43. {
  44. uint8_t status;
  45. SPI_FLASH_CS_L();
  46. spi_tx_rx(CMD_RDSR);
  47. do {
  48. status = spi_tx_rx(0);
  49. } while (status & SR_WIP);
  50. SPI_FLASH_CS_H();
  51. }
  52. //
  53. static inline void send_addr(int addr)
  54. {
  55. spi_tx_rx((addr >> 16) & 0xFF);
  56. spi_tx_rx((addr >> 8) & 0xFF);
  57. spi_tx_rx(addr & 0xFF);
  58. }
  59. //
  60. static int spi_flash_read_sfdp(int addr, void *buf, size_t len)
  61. {
  62. uint8_t *ptr = (uint8_t*)buf;
  63. //xSemaphoreTake(spi_mutex, portMAX_DELAY);
  64. SPI_FLASH_CS_L();
  65. spi_tx_rx(CMD_RDSFDP);
  66. send_addr(addr);
  67. spi_tx_rx(0);
  68. while (len--)
  69. *ptr++ = spi_tx_rx(0);
  70. SPI_FLASH_CS_H();
  71. //xSemaphoreGive(spi_mutex);
  72. return 0;
  73. }
  74. //
  75. ssize_t spi_flash_read(int addr, void *buf, size_t len, uint32_t timeout)
  76. {
  77. uint8_t *ptr = (uint8_t*)buf;
  78. (void)timeout;
  79. //xSemaphoreTake(spi_mutex, portMAX_DELAY);
  80. SPI_FLASH_CS_L();
  81. spi_tx_rx(CMD_READ);
  82. send_addr(addr);
  83. while (len--)
  84. *ptr++ = spi_tx_rx(0);
  85. SPI_FLASH_CS_H();
  86. //xSemaphoreGive(spi_mutex);
  87. return len;
  88. }
  89. #define TIMEOUT 10000
  90. uint16_t spi_flash_pp(int addr, const void *buf, size_t len, uint32_t timeout)
  91. {
  92. uint8_t *ptr = (uint8_t*)buf;
  93. (void)timeout;
  94. ssize_t ret = 0;
  95. if ((addr & 0xFF) + len > 0xFF)
  96. len = 0x100 - (addr & 0xFF);
  97. ret = len;
  98. //xSemaphoreTake(spi_mutex, portMAX_DELAY);
  99. SPI_FLASH_CS_L();
  100. spi_tx_rx(CMD_WREN);
  101. SPI_FLASH_CS_H();
  102. wait_write_enable();
  103. SPI_FLASH_CS_L();
  104. spi_tx_rx(CMD_PP);
  105. send_addr(addr);
  106. while (len--)
  107. spi_tx_rx(*ptr++);
  108. SPI_FLASH_CS_H();
  109. wait_write_end();
  110. //xSemaphoreGive(spi_mutex);
  111. return ret;
  112. }
  113. //
  114. ssize_t spi_flash_write(int addr, const void *buf, size_t len, uint32_t timeout)
  115. {
  116. (void)timeout;
  117. int ret = 0, offset = 0;
  118. do {
  119. ret = spi_flash_pp(addr + offset, (uint8_t*)buf + offset, len - offset, 0);
  120. offset += ret;
  121. } while (len - offset);
  122. return 0;
  123. }
  124. //
  125. int spi_flash_chip_erase(uint32_t timeout)
  126. {
  127. (void)timeout;
  128. //xSemaphoreTake(spi_mutex, portMAX_DELAY);
  129. SPI_FLASH_CS_L();
  130. spi_tx_rx(CMD_WREN);
  131. SPI_FLASH_CS_H();
  132. wait_write_enable();
  133. SPI_FLASH_CS_L();
  134. spi_tx_rx(CMD_CE);
  135. SPI_FLASH_CS_H();
  136. wait_write_end();
  137. //xSemaphoreGive(spi_mutex);
  138. return 0;
  139. }
  140. //
  141. int spi_flash_erase_sector(int addr, uint32_t timeout)
  142. {
  143. (void)timeout;
  144. //xSemaphoreTake(spi_mutex, portMAX_DELAY);
  145. SPI_FLASH_CS_L();
  146. spi_tx_rx(CMD_WREN);
  147. SPI_FLASH_CS_H();
  148. wait_write_enable();
  149. SPI_FLASH_CS_L();
  150. spi_tx_rx(CMD_SE);
  151. send_addr(addr);
  152. SPI_FLASH_CS_H();
  153. wait_write_end();
  154. //xSemaphoreGive(spi_mutex);
  155. return 0;
  156. }
  157. //
  158. unsigned int spi_flash_get_sector_size(void) {
  159. return spi_flash_desc.sector_size;
  160. }
  161. //
  162. unsigned int spi_flash_get_sector_count(void) {
  163. return spi_flash_desc.sector_count;
  164. }
  165. //
  166. unsigned int spi_flash_get_total_size(void) {
  167. return spi_flash_desc.sector_count * spi_flash_desc.sector_size;
  168. }
  169. //
  170. bool spi_flash_is_init(void) {
  171. return spi_flash_desc.present;
  172. }
  173. //
  174. bool spi_flash_init(void)
  175. {
  176. bool ret = false;
  177. uint8_t tmp[4] = {0};
  178. SPI_FLASH_CS_H();
  179. spi_flash_get_id(tmp);
  180. if (!(tmp[0] == 0x1F && tmp[1] == 0x86 && tmp[2] == 0x01))
  181. {
  182. ret = false;
  183. spi_flash_read_sfdp(0, tmp, 4);
  184. if (!(tmp[0] == 0x53 && tmp[1] == 0x46 && tmp[2] == 0x44 && tmp[3] == 0x50))
  185. ret = false;
  186. else
  187. ret = true;
  188. }
  189. else
  190. ret = true;
  191. if (ret)
  192. {
  193. spi_flash_desc.sector_size = SPI_FLASH_SECTOR_SIZE;
  194. spi_flash_desc.sector_erase_op = 32;
  195. spi_flash_desc.sector_count = 512;
  196. spi_flash_desc.present = true;
  197. return true;
  198. }
  199. else
  200. return false;
  201. #if 0
  202. uint32_t i, ptable, bitsize = 0;
  203. uint8_t tmp[4];
  204. spi_flash_desc.present = false;
  205. // check SFDP magic
  206. SPI_FLASH_CS_H();
  207. spi_flash_read_sfdp(0, tmp, 4);
  208. if (!(tmp[0] == 0x53 && tmp[1] == 0x46 &&
  209. tmp[2] == 0x44 && tmp[3] == 0x50))
  210. return 0;
  211. // get parameter headers count
  212. spi_flash_read_sfdp(0x06, tmp, 1);
  213. // find first jedec pheader (with ID == 0)
  214. for (ptable = 0x08, i = 0; i <= tmp[0]; i++, ptable += 8) {
  215. spi_flash_read_sfdp(ptable, tmp, 1);
  216. if (tmp[0] == 0)
  217. break;
  218. }
  219. // read ptable pointer from pheader
  220. spi_flash_read_sfdp(ptable + 4, &ptable, 3);
  221. // get flash density (size in bits)
  222. if (spi_flash_read_sfdp(ptable + 4, &bitsize, 4) < 0 || !bitsize)
  223. return 0;
  224. // find smallest available sector
  225. for (i = 0; i < 4; i++) {
  226. tmp[0] = 0;
  227. if (spi_flash_read_sfdp(ptable + 0x1C + i*2, &tmp, 2) >= 0 &&
  228. tmp[0]) {
  229. spi_flash_desc.sector_size = 1 << tmp[0];
  230. spi_flash_desc.sector_erase_op = tmp[1];
  231. spi_flash_desc.sector_count = (bitsize + 1) >> (3 + tmp[0]);
  232. break;
  233. }
  234. }
  235. if (!spi_flash_desc.sector_size)
  236. return 0;
  237. spi_flash_desc.present = true;
  238. #endif
  239. return 1;
  240. }
  241. /*
  242. bool spi_flash_init(void){
  243. //spi_init_();
  244. spi_flash_desc.sector_size = SPI_FLASH_SECTOR_SIZE;
  245. spi_flash_desc.sector_erase_op = 32;
  246. spi_flash_desc.sector_count = 512;
  247. spi_flash_desc.present = true;
  248. return 1;
  249. }
  250. */
  251. /*
  252. void init_spi_mutex(void)
  253. {
  254. spi_mutex = xSemaphoreCreateMutex();
  255. }
  256. */
  257. // -------------------------------------------------------------------------- //
  258. void spi_flash_get_id(uint8_t *buf)
  259. {
  260. SPI_FLASH_CS_L();
  261. spi_tx_rx(0x9F);
  262. *buf++ = spi_tx_rx(0);
  263. *buf++ = spi_tx_rx(0);
  264. *buf++ = spi_tx_rx(0);
  265. SPI_FLASH_CS_H();
  266. }
  267. // -------------------------------------------------------------------------- //
  268. #if INCLUDE_SPI_TEST
  269. const uint8_t txbuf1[] = "This film came out on DVD yesterday and I rushed to buy it. \
  270. This version is the first to render all the detail and perfection \
  271. of Jack Cardiff's amazing compositions and brilliant, varied photography. \
  272. As a collection of memorable images, this film is better than any comparable \
  273. historical epic of the period and even gives GWTW a run for its money. \
  274. King Vidor's direction is a series of 'tableaux vivants' where the \
  275. characters are not posing but acting in a very natural, period-specific way. \
  276. I have never had a problem with this adaptation of Tolstoy's novel. \
  277. I think it is a wonderful introduction to the period and the novel and that \
  278. it is a very poetic, very original work in its own right. Henry Fonda's \
  279. characterization is especially moving, including great memorable interactions \
  280. with/reations to Mel Ferrer, Audrey Hepburn, Helmut Dantine and John Mills, but \
  281. all members of the cast are actually perfect. The harrowing last 45 minutes of \
  282. the film manage to convey a sense of history, a sense of grandeur as well as to \
  283. communicate very clearly Tolstoy's ideas about the meaning of life, by relying \
  284. mostly on the power of memorable images. The most conspicuous handicap of this movie, \
  285. in my opinion, is its soundtrack (in glorious mono). The barely hi-fi recording of \
  286. dialogues and music sounds pinched, hollow and tinny and it always has in very version \
  287. I have ever seen: in the theatres, on TV and on video. Even the soundtrack album is \
  288. an atrocity. In some scenes, before the necessary adjustments of bass and treble, \
  289. Audrey Hepburn's and Mel Ferrer's voices actually hurt your ear. Nino Rota's very \
  290. Russian-sounding score is serviceable and melodic, although rather sparse in its \
  291. orchestration and in the number of players. One can only wonder what 'War and Peace' \
  292. could have sounded like with a cohort of Hollywood arrangers, decent recording facilities \
  293. and lavish, varied orchestrations in true high fidelity and stereophonic sound. \
  294. According to Lukas Kendall of 'Film Score Monthly', the original recording elements \
  295. of the soundtrack have long ago disappeared, which is the common lot of international, \
  296. independent co-productions of the era. Someone somewhere is certainly guilty of \
  297. skimping on quality or embezzlement for this 1956 movie to sound so much worse \
  298. than a 1939, pre-hi-fi epic like GWTW. Like all VistaVision films, this one was \
  299. meant to be shown in Perspecta Stereophonic Sound where the mono dialog track was \
  300. meant to be channelled to three different directions, making it directional, while \
  301. the separate mono music + sound effects track was generally directed to all three \
  302. speakers at the same time. The results fooled the viewers into thinking everything \
  303. was in true stereo and the reproduction of the music was usually in very high fidelity. \
  304. Maybe the soundtrack used on the DVD is a mono reduction of those two separate tracks \
  305. that has squandered that fidelity and maybe the DVD can be issued again with better \
  306. results in some kind of 4.0 presentation. When they do, very little electronic \
  307. restoration work will be needed to make the image absolutely perfect. \
  308. But let's concentrate on the positive: This film is a summit of visual \
  309. splendour and its sets, costumes, colour photography, composition and lighting \
  310. achieve, in every single scene, wonders of artistry, creativity and delicacy \
  311. But let's concentrate on the positive: This film is a summit of visual \
  312. splendour and its sets, costumes, colour photography, composition and lighting \
  313. achieve, in every single scene, wonders of artistry, creativity and delicacy \
  314. that will probably never be equalled. Suffice it to say that it has, \
  315. among many other treasures, a sunrise duel scene in the snow that still has viewers \
  316. wondering whether it was shot outdoors or in a studio and that will have them wondering foreverhsgkhgkshgu.\r\n";
  317. #define countof(a) (sizeof(a) / sizeof(*(a)))
  318. #define bufsize1 (countof(txbuf1)-1)
  319. uint8_t rxbuf1[bufsize1] = {0};
  320. bool spi_flash_test(void) {
  321. if (!spi_flash_is_init()) {
  322. return false;
  323. }
  324. const unsigned int sector_count = spi_flash_get_sector_count();
  325. const unsigned int sector_size = spi_flash_get_sector_size();
  326. const unsigned int total_size = spi_flash_get_total_size();
  327. printf("Present: %u sectors, %u bytes per sector (%u bytes total)\r\n",
  328. sector_count, sector_size, total_size);
  329. for (uint16_t i = 0; i < sector_count; i++) {
  330. printf("sector: %u/%u ", i, sector_count);
  331. spi_flash_erase_sector(i * sector_size, 0);
  332. spi_flash_write(i * sector_size, txbuf1, bufsize1, 0);
  333. memset(rxbuf1, 0, bufsize1);
  334. spi_flash_read(i * sector_size, rxbuf1, bufsize1, 0);
  335. if (memcmp(txbuf1, rxbuf1, bufsize1) != 0) {
  336. printf("fail\r\n");
  337. return false;
  338. }
  339. printf("ok\r\n");
  340. }
  341. printf("\r\nDone\r\n");
  342. return true;
  343. }
  344. #endif