spi_flash_w25qxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * File : spi_flash_w25qxx.c
  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. * 2012-05-06 aozima can page write.
  14. * 2012-08-23 aozima add flash lock.
  15. * 2012-08-24 aozima fixed write status register BUG.
  16. * 2015-05-13 bernard add GD25Q flash ID.
  17. */
  18. #include <stdint.h>
  19. #include "spi_flash_w25qxx.h"
  20. #define FLASH_DEBUG
  21. #ifdef FLASH_DEBUG
  22. #define FLASH_TRACE rt_kprintf
  23. #else
  24. #define FLASH_TRACE(...)
  25. #endif /* #ifdef FLASH_DEBUG */
  26. #define PAGE_SIZE 4096
  27. /* JEDEC Manufacturer��s ID */
  28. #define MF_ID (0xEF)
  29. #define GD_ID (0xC8)
  30. /* JEDEC Device ID: Memory type and Capacity */
  31. #define MTC_W25Q16_BV_CL_CV (0x4015) /* W25Q16BV W25Q16CL W25Q16CV */
  32. #define MTC_W25Q16_DW (0x6015) /* W25Q16DW */
  33. #define MTC_W25Q32_BV (0x4016) /* W25Q32BV */
  34. #define MTC_W25Q32_DW (0x6016) /* W25Q32DW */
  35. #define MTC_W25Q64_BV_CV (0x4017) /* W25Q64BV W25Q64CV */
  36. #define MTC_W25Q64_DW (0x4017) /* W25Q64DW */
  37. #define MTC_W25Q128_BV (0x4018) /* W25Q128BV */
  38. #define MTC_W25Q256_FV (TBD) /* W25Q256FV */
  39. /* command list */
  40. #define CMD_WRSR (0x01) /* Write Status Register */
  41. #define CMD_PP (0x02) /* Page Program */
  42. #define CMD_READ (0x03) /* Read Data */
  43. #define CMD_WRDI (0x04) /* Write Disable */
  44. #define CMD_RDSR1 (0x05) /* Read Status Register-1 */
  45. #define CMD_WREN (0x06) /* Write Enable */
  46. #define CMD_FAST_READ (0x0B) /* Fast Read */
  47. #define CMD_ERASE_4K (0x20) /* Sector Erase:4K */
  48. #define CMD_RDSR2 (0x35) /* Read Status Register-2 */
  49. #define CMD_ERASE_32K (0x52) /* 32KB Block Erase */
  50. #define CMD_JEDEC_ID (0x9F) /* Read JEDEC ID */
  51. #define CMD_ERASE_full (0xC7) /* Chip Erase */
  52. #define CMD_ERASE_64K (0xD8) /* 64KB Block Erase */
  53. #define DUMMY (0xFF)
  54. static struct spi_flash_device spi_flash_device;
  55. static void flash_lock(struct spi_flash_device * flash_device)
  56. {
  57. rt_mutex_take(&flash_device->lock, RT_WAITING_FOREVER);
  58. }
  59. static void flash_unlock(struct spi_flash_device * flash_device)
  60. {
  61. rt_mutex_release(&flash_device->lock);
  62. }
  63. static uint8_t w25qxx_read_status(void)
  64. {
  65. return rt_spi_sendrecv8(spi_flash_device.rt_spi_device, CMD_RDSR1);
  66. }
  67. static void w25qxx_wait_busy(void)
  68. {
  69. while( w25qxx_read_status() & (0x01));
  70. }
  71. /** \brief read [size] byte from [offset] to [buffer]
  72. *
  73. * \param offset uint32_t unit : byte
  74. * \param buffer uint8_t*
  75. * \param size uint32_t unit : byte
  76. * \return uint32_t byte for read
  77. *
  78. */
  79. static uint32_t w25qxx_read(uint32_t offset, uint8_t * buffer, uint32_t size)
  80. {
  81. uint8_t send_buffer[4];
  82. send_buffer[0] = CMD_WRDI;
  83. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1);
  84. send_buffer[0] = CMD_READ;
  85. send_buffer[1] = (uint8_t)(offset>>16);
  86. send_buffer[2] = (uint8_t)(offset>>8);
  87. send_buffer[3] = (uint8_t)(offset);
  88. rt_spi_send_then_recv(spi_flash_device.rt_spi_device,
  89. send_buffer, 4,
  90. buffer, size);
  91. return size;
  92. }
  93. /** \brief write N page on [page]
  94. *
  95. * \param page_addr uint32_t unit : byte (4096 * N,1 page = 4096byte)
  96. * \param buffer const uint8_t*
  97. * \return uint32_t
  98. *
  99. */
  100. uint32_t w25qxx_page_write(uint32_t page_addr, const uint8_t* buffer)
  101. {
  102. uint32_t index;
  103. uint8_t send_buffer[4];
  104. RT_ASSERT((page_addr&0xFF) == 0); /* page addr must align to 256byte. */
  105. send_buffer[0] = CMD_WREN;
  106. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1);
  107. send_buffer[0] = CMD_ERASE_4K;
  108. send_buffer[1] = (page_addr >> 16);
  109. send_buffer[2] = (page_addr >> 8);
  110. send_buffer[3] = (page_addr);
  111. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 4);
  112. w25qxx_wait_busy(); // wait erase done.
  113. for(index=0; index < (PAGE_SIZE / 256); index++)
  114. {
  115. send_buffer[0] = CMD_WREN;
  116. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1);
  117. send_buffer[0] = CMD_PP;
  118. send_buffer[1] = (uint8_t)(page_addr >> 16);
  119. send_buffer[2] = (uint8_t)(page_addr >> 8);
  120. send_buffer[3] = (uint8_t)(page_addr);
  121. rt_spi_send_then_send(spi_flash_device.rt_spi_device,
  122. send_buffer,
  123. 4,
  124. buffer,
  125. 256);
  126. buffer += 256;
  127. page_addr += 256;
  128. w25qxx_wait_busy();
  129. }
  130. send_buffer[0] = CMD_WRDI;
  131. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1);
  132. return PAGE_SIZE;
  133. }
  134. /* RT-Thread device interface */
  135. static rt_err_t w25qxx_flash_init(rt_device_t dev)
  136. {
  137. return RT_EOK;
  138. }
  139. static rt_err_t w25qxx_flash_open(rt_device_t dev, rt_uint16_t oflag)
  140. {
  141. uint8_t send_buffer[3];
  142. flash_lock((struct spi_flash_device *)dev);
  143. send_buffer[0] = CMD_WREN;
  144. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 1);
  145. send_buffer[0] = CMD_WRSR;
  146. send_buffer[1] = 0;
  147. send_buffer[2] = 0;
  148. rt_spi_send(spi_flash_device.rt_spi_device, send_buffer, 3);
  149. w25qxx_wait_busy();
  150. flash_unlock((struct spi_flash_device *)dev);
  151. return RT_EOK;
  152. }
  153. static rt_err_t w25qxx_flash_close(rt_device_t dev)
  154. {
  155. return RT_EOK;
  156. }
  157. static rt_err_t w25qxx_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  158. {
  159. RT_ASSERT(dev != RT_NULL);
  160. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  161. {
  162. struct rt_device_blk_geometry *geometry;
  163. geometry = (struct rt_device_blk_geometry *)args;
  164. if (geometry == RT_NULL) return -RT_ERROR;
  165. geometry->bytes_per_sector = spi_flash_device.geometry.bytes_per_sector;
  166. geometry->sector_count = spi_flash_device.geometry.sector_count;
  167. geometry->block_size = spi_flash_device.geometry.block_size;
  168. }
  169. return RT_EOK;
  170. }
  171. static rt_size_t w25qxx_flash_read(rt_device_t dev,
  172. rt_off_t pos,
  173. void* buffer,
  174. rt_size_t size)
  175. {
  176. flash_lock((struct spi_flash_device *)dev);
  177. w25qxx_read(pos*spi_flash_device.geometry.bytes_per_sector,
  178. buffer,
  179. size*spi_flash_device.geometry.bytes_per_sector);
  180. flash_unlock((struct spi_flash_device *)dev);
  181. return size;
  182. }
  183. static rt_size_t w25qxx_flash_write(rt_device_t dev,
  184. rt_off_t pos,
  185. const void* buffer,
  186. rt_size_t size)
  187. {
  188. rt_size_t i = 0;
  189. rt_size_t block = size;
  190. const uint8_t * ptr = buffer;
  191. flash_lock((struct spi_flash_device *)dev);
  192. while(block--)
  193. {
  194. w25qxx_page_write((pos + i)*spi_flash_device.geometry.bytes_per_sector,
  195. ptr);
  196. ptr += PAGE_SIZE;
  197. i++;
  198. }
  199. flash_unlock((struct spi_flash_device *)dev);
  200. return size;
  201. }
  202. rt_err_t w25qxx_init(const char * flash_device_name, const char * spi_device_name)
  203. {
  204. struct rt_spi_device * rt_spi_device;
  205. /* initialize mutex */
  206. if (rt_mutex_init(&spi_flash_device.lock, spi_device_name, RT_IPC_FLAG_FIFO) != RT_EOK)
  207. {
  208. rt_kprintf("init sd lock mutex failed\n");
  209. return -RT_ENOSYS;
  210. }
  211. rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  212. if(rt_spi_device == RT_NULL)
  213. {
  214. FLASH_TRACE("spi device %s not found!\r\n", spi_device_name);
  215. return -RT_ENOSYS;
  216. }
  217. spi_flash_device.rt_spi_device = rt_spi_device;
  218. /* config spi */
  219. {
  220. struct rt_spi_configuration cfg;
  221. cfg.data_width = 8;
  222. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
  223. cfg.max_hz = 50 * 1000 * 1000; /* 50M */
  224. rt_spi_configure(spi_flash_device.rt_spi_device, &cfg);
  225. }
  226. /* init flash */
  227. {
  228. rt_uint8_t cmd;
  229. rt_uint8_t id_recv[3];
  230. uint16_t memory_type_capacity;
  231. flash_lock(&spi_flash_device);
  232. cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */
  233. rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1);
  234. cmd = CMD_WRDI;
  235. rt_spi_send(spi_flash_device.rt_spi_device, &cmd, 1);
  236. /* read flash id */
  237. cmd = CMD_JEDEC_ID;
  238. rt_spi_send_then_recv(spi_flash_device.rt_spi_device, &cmd, 1, id_recv, 3);
  239. flash_unlock(&spi_flash_device);
  240. if(id_recv[0] != MF_ID && id_recv[0] != GD_ID)
  241. {
  242. FLASH_TRACE("Manufacturers ID error!\r\n");
  243. FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]);
  244. return -RT_ENOSYS;
  245. }
  246. spi_flash_device.geometry.bytes_per_sector = 4096;
  247. spi_flash_device.geometry.block_size = 4096; /* block erase: 4k */
  248. /* get memory type and capacity */
  249. memory_type_capacity = id_recv[1];
  250. memory_type_capacity = (memory_type_capacity << 8) | id_recv[2];
  251. if(memory_type_capacity == MTC_W25Q128_BV)
  252. {
  253. FLASH_TRACE("W25Q128BV detection\r\n");
  254. spi_flash_device.geometry.sector_count = 4096;
  255. }
  256. else if(memory_type_capacity == MTC_W25Q64_BV_CV)
  257. {
  258. FLASH_TRACE("W25Q64BV or W25Q64CV detection\r\n");
  259. spi_flash_device.geometry.sector_count = 2048;
  260. }
  261. else if(memory_type_capacity == MTC_W25Q64_DW)
  262. {
  263. FLASH_TRACE("W25Q64DW detection\r\n");
  264. spi_flash_device.geometry.sector_count = 2048;
  265. }
  266. else if(memory_type_capacity == MTC_W25Q32_BV)
  267. {
  268. FLASH_TRACE("W25Q32BV detection\r\n");
  269. spi_flash_device.geometry.sector_count = 1024;
  270. }
  271. else if(memory_type_capacity == MTC_W25Q32_DW)
  272. {
  273. FLASH_TRACE("W25Q32DW detection\r\n");
  274. spi_flash_device.geometry.sector_count = 1024;
  275. }
  276. else if(memory_type_capacity == MTC_W25Q16_BV_CL_CV)
  277. {
  278. FLASH_TRACE("W25Q16BV or W25Q16CL or W25Q16CV detection\r\n");
  279. spi_flash_device.geometry.sector_count = 512;
  280. }
  281. else if(memory_type_capacity == MTC_W25Q16_DW)
  282. {
  283. FLASH_TRACE("W25Q16DW detection\r\n");
  284. spi_flash_device.geometry.sector_count = 512;
  285. }
  286. else
  287. {
  288. FLASH_TRACE("Memory Capacity error!\r\n");
  289. return -RT_ENOSYS;
  290. }
  291. }
  292. /* register device */
  293. spi_flash_device.flash_device.type = RT_Device_Class_Block;
  294. spi_flash_device.flash_device.init = w25qxx_flash_init;
  295. spi_flash_device.flash_device.open = w25qxx_flash_open;
  296. spi_flash_device.flash_device.close = w25qxx_flash_close;
  297. spi_flash_device.flash_device.read = w25qxx_flash_read;
  298. spi_flash_device.flash_device.write = w25qxx_flash_write;
  299. spi_flash_device.flash_device.control = w25qxx_flash_control;
  300. /* no private */
  301. spi_flash_device.flash_device.user_data = RT_NULL;
  302. rt_device_register(&spi_flash_device.flash_device, flash_device_name,
  303. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  304. return RT_EOK;
  305. }