spi_flash_sst25vfxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-12-16 aozima the first version
  9. */
  10. #include <stdint.h>
  11. #include "spi_flash_sst25vfxx.h"
  12. #define FLASH_DEBUG
  13. #ifdef FLASH_DEBUG
  14. #define FLASH_TRACE rt_kprintf
  15. #else
  16. #define FLASH_TRACE(...)
  17. #endif /* #ifdef FLASH_DEBUG */
  18. /* JEDEC Manufacturer’s ID */
  19. #define MF_ID (0xBF)
  20. /* JEDEC Device ID : Memory Type */
  21. #define MT_ID (0x25)
  22. /* JEDEC Device ID: Memory Capacity */
  23. #define MC_ID_SST25VF020B (0x8C) /* 2Mbit */
  24. #define MC_ID_SST25VF040B (0x8D) /* 4Mbit */
  25. #define MC_ID_SST25VF080B (0x8E) /* 8Mbit */
  26. #define MC_ID_SST25VF016B (0x41) /* 16Mbit */
  27. #define MC_ID_SST25VF032B (0x4A) /* 32Mbit */
  28. #define MC_ID_SST25VF064C (0x4B) /* 64Mbit */
  29. /* command list */
  30. #define CMD_RDSR (0x05)
  31. #define CMD_WRSR (0x01)
  32. #define CMD_EWSR (0x50)
  33. #define CMD_WRDI (0x04)
  34. #define CMD_WREN (0x06)
  35. #define CMD_READ (0x03)
  36. #define CMD_FAST_READ (0x0B)
  37. #define CMD_BP (0x02)
  38. #define CMD_AAIP (0xAD)
  39. #define CMD_ERASE_4K (0x20)
  40. #define CMD_ERASE_32K (0x52)
  41. #define CMD_ERASE_64K (0xD8)
  42. #define CMD_ERASE_full (0xC7)
  43. #define CMD_JEDEC_ID (0x9F)
  44. #define CMD_EBSY (0x70)
  45. #define CMD_DBSY (0x80)
  46. #define DUMMY (0xFF)
  47. static struct spi_flash_sst25vfxx spi_flash_sst25vfxx;
  48. static uint8_t sst25vfxx_read_status(struct spi_flash_sst25vfxx * spi_flash)
  49. {
  50. return rt_spi_sendrecv8(spi_flash->rt_spi_device, CMD_RDSR);
  51. }
  52. static void sst25vfxx_wait_busy(struct spi_flash_sst25vfxx * spi_flash)
  53. {
  54. while( sst25vfxx_read_status(spi_flash) & (0x01));
  55. }
  56. /** \brief write N page on [page]
  57. *
  58. * \param page uint32_t unit : byte (4096 * N,1 page = 4096byte)
  59. * \param buffer const uint8_t*
  60. * \param size uint32_t unit : byte ( 4096*N )
  61. * \return uint32_t
  62. *
  63. */
  64. static uint32_t sst25vfxx_page_write(struct spi_flash_sst25vfxx * spi_flash, uint32_t page, const uint8_t * buffer, uint32_t size)
  65. {
  66. uint32_t index;
  67. uint32_t need_wirte = size;
  68. uint8_t send_buffer[6];
  69. page &= ~0xFFF; // page size = 4096byte
  70. send_buffer[0] = CMD_WREN;
  71. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  72. send_buffer[0] = CMD_ERASE_4K;
  73. send_buffer[1] = (page >> 16);
  74. send_buffer[2] = (page >> 8);
  75. send_buffer[3] = (page);
  76. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 4);
  77. sst25vfxx_wait_busy(spi_flash); // wait erase done.
  78. send_buffer[0] = CMD_WREN;
  79. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  80. send_buffer[0] = CMD_AAIP;
  81. send_buffer[1] = (uint8_t)(page >> 16);
  82. send_buffer[2] = (uint8_t)(page >> 8);
  83. send_buffer[3] = (uint8_t)(page);
  84. send_buffer[4] = *buffer++;
  85. send_buffer[5] = *buffer++;
  86. need_wirte -= 2;
  87. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 6);
  88. sst25vfxx_wait_busy(spi_flash);
  89. for(index=0; index < need_wirte/2; index++)
  90. {
  91. send_buffer[0] = CMD_AAIP;
  92. send_buffer[1] = *buffer++;
  93. send_buffer[2] = *buffer++;
  94. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 3);
  95. sst25vfxx_wait_busy(spi_flash);
  96. }
  97. send_buffer[0] = CMD_WRDI;
  98. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  99. return size;
  100. }
  101. /* RT-Thread device interface */
  102. static rt_err_t sst25vfxx_flash_init(rt_device_t dev)
  103. {
  104. return RT_EOK;
  105. }
  106. static rt_err_t sst25vfxx_flash_open(rt_device_t dev, rt_uint16_t oflag)
  107. {
  108. rt_err_t result;
  109. uint8_t send_buffer[2];
  110. struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev;
  111. /* lock spi flash */
  112. result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER);
  113. if(result != RT_EOK)
  114. {
  115. return result;
  116. }
  117. send_buffer[0] = CMD_DBSY;
  118. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  119. send_buffer[0] = CMD_EWSR;
  120. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  121. send_buffer[0] = CMD_WRSR;
  122. send_buffer[1] = 0;
  123. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 2);
  124. /* release lock */
  125. rt_mutex_release(&(spi_flash->lock));
  126. return RT_EOK;
  127. }
  128. static rt_err_t sst25vfxx_flash_close(rt_device_t dev)
  129. {
  130. return RT_EOK;
  131. }
  132. static rt_err_t sst25vfxx_flash_control(rt_device_t dev, int cmd, void *args)
  133. {
  134. struct spi_flash_sst25vfxx * spi_flash;
  135. spi_flash = (struct spi_flash_sst25vfxx *)dev;
  136. RT_ASSERT(dev != RT_NULL);
  137. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  138. {
  139. struct rt_device_blk_geometry *geometry;
  140. geometry = (struct rt_device_blk_geometry *)args;
  141. if (geometry == RT_NULL) return -RT_ERROR;
  142. geometry->bytes_per_sector = spi_flash->geometry.bytes_per_sector;
  143. geometry->sector_count = spi_flash->geometry.sector_count;
  144. geometry->block_size = spi_flash->geometry.block_size;
  145. }
  146. return RT_EOK;
  147. }
  148. static rt_size_t sst25vfxx_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  149. {
  150. rt_err_t result;
  151. uint8_t send_buffer[4];
  152. struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev;
  153. uint32_t offset = pos * spi_flash->geometry.bytes_per_sector;
  154. /* lock spi flash */
  155. result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER);
  156. if(result != RT_EOK)
  157. {
  158. return 0;
  159. }
  160. send_buffer[0] = CMD_WRDI;
  161. rt_spi_send(spi_flash->rt_spi_device, send_buffer, 1);
  162. send_buffer[0] = CMD_READ;
  163. send_buffer[1] = (uint8_t)(offset>>16);
  164. send_buffer[2] = (uint8_t)(offset>>8);
  165. send_buffer[3] = (uint8_t)(offset);
  166. rt_spi_send_then_recv(spi_flash->rt_spi_device, send_buffer, 4, buffer, size * spi_flash->geometry.bytes_per_sector);
  167. /* release lock */
  168. rt_mutex_release(&(spi_flash->lock));
  169. return size;
  170. }
  171. static rt_size_t sst25vfxx_flash_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  172. {
  173. uint32_t i;
  174. rt_err_t result;
  175. const uint8_t * write_buffer = buffer;
  176. struct spi_flash_sst25vfxx * spi_flash = (struct spi_flash_sst25vfxx *)dev;
  177. /* lock spi flash */
  178. result = rt_mutex_take(&(spi_flash->lock), RT_WAITING_FOREVER);
  179. if(result != RT_EOK)
  180. {
  181. return 0;
  182. }
  183. for(i=0; i<size; i++)
  184. {
  185. sst25vfxx_page_write(spi_flash,
  186. (pos + i) * spi_flash->geometry.bytes_per_sector,
  187. write_buffer,
  188. spi_flash->geometry.bytes_per_sector);
  189. write_buffer += spi_flash->geometry.bytes_per_sector;
  190. }
  191. /* release lock */
  192. rt_mutex_release(&(spi_flash->lock));
  193. return size;
  194. }
  195. #ifdef RT_USING_DEVICE_OPS
  196. const static struct rt_device_ops sst25vfxx_device_ops =
  197. {
  198. sst25vfxx_flash_init,
  199. sst25vfxx_flash_open,
  200. sst25vfxx_flash_close,
  201. sst25vfxx_flash_read,
  202. sst25vfxx_flash_write,
  203. sst25vfxx_flash_control
  204. };
  205. #endif
  206. rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_name)
  207. {
  208. struct rt_spi_device * rt_spi_device;
  209. struct spi_flash_sst25vfxx * spi_flash = &spi_flash_sst25vfxx;
  210. rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  211. if(rt_spi_device == RT_NULL)
  212. {
  213. FLASH_TRACE("spi device %s not found!\r\n", spi_device_name);
  214. return -RT_ENOSYS;
  215. }
  216. spi_flash->rt_spi_device = rt_spi_device;
  217. /* config spi */
  218. {
  219. struct rt_spi_configuration cfg;
  220. cfg.data_width = 8;
  221. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
  222. cfg.max_hz = 50000000; /* 50M */
  223. rt_spi_configure(spi_flash->rt_spi_device, &cfg);
  224. }
  225. /* init flash */
  226. {
  227. rt_uint8_t cmd;
  228. rt_uint8_t id_recv[3];
  229. cmd = CMD_WRDI;
  230. rt_spi_send(spi_flash->rt_spi_device, &cmd, 1);
  231. /* read flash id */
  232. cmd = CMD_JEDEC_ID;
  233. rt_spi_send_then_recv(spi_flash->rt_spi_device, &cmd, 1, id_recv, 3);
  234. if(id_recv[0] != MF_ID || id_recv[1] != MT_ID)
  235. {
  236. FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n");
  237. FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]);
  238. return -RT_ENOSYS;
  239. }
  240. spi_flash->geometry.bytes_per_sector = 4096;
  241. spi_flash->geometry.block_size = 4096; /* block erase: 4k */
  242. if(id_recv[2] == MC_ID_SST25VF020B)
  243. {
  244. FLASH_TRACE("SST25VF020B detection\r\n");
  245. spi_flash->geometry.sector_count = 64;
  246. }
  247. else if(id_recv[2] == MC_ID_SST25VF040B)
  248. {
  249. FLASH_TRACE("SST25VF040B detection\r\n");
  250. spi_flash->geometry.sector_count = 128;
  251. }
  252. else if(id_recv[2] == MC_ID_SST25VF080B)
  253. {
  254. FLASH_TRACE("SST25VF080B detection\r\n");
  255. spi_flash->geometry.sector_count = 256;
  256. }
  257. else if(id_recv[2] == MC_ID_SST25VF016B)
  258. {
  259. FLASH_TRACE("SST25VF016B detection\r\n");
  260. spi_flash->geometry.sector_count = 512;
  261. }
  262. else if(id_recv[2] == MC_ID_SST25VF032B)
  263. {
  264. FLASH_TRACE("SST25VF032B detection\r\n");
  265. spi_flash->geometry.sector_count = 1024;
  266. }
  267. else if(id_recv[2] == MC_ID_SST25VF064C)
  268. {
  269. FLASH_TRACE("SST25VF064C detection\r\n");
  270. spi_flash->geometry.sector_count = 2048;
  271. }
  272. else
  273. {
  274. FLASH_TRACE("Memory Capacity error!\r\n");
  275. return -RT_ENOSYS;
  276. }
  277. }
  278. /* initialize mutex lock */
  279. rt_mutex_init(&spi_flash->lock, flash_device_name, RT_IPC_FLAG_PRIO);
  280. /* register device */
  281. spi_flash->flash_device.type = RT_Device_Class_Block;
  282. #ifdef RT_USING_DEVICE_OPS
  283. spi_flash->flash_device.ops = &sst25vfxx_device_ops;
  284. #else
  285. spi_flash->flash_device.init = sst25vfxx_flash_init;
  286. spi_flash->flash_device.open = sst25vfxx_flash_open;
  287. spi_flash->flash_device.close = sst25vfxx_flash_close;
  288. spi_flash->flash_device.read = sst25vfxx_flash_read;
  289. spi_flash->flash_device.write = sst25vfxx_flash_write;
  290. spi_flash->flash_device.control = sst25vfxx_flash_control;
  291. #endif
  292. /* no private */
  293. spi_flash->flash_device.user_data = RT_NULL;
  294. rt_device_register(&spi_flash->flash_device, flash_device_name,
  295. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  296. return RT_EOK;
  297. }