spi_flash_sst25vfxx.c 10 KB

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