spi_flash_sst25vfxx.c 11 KB

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