1
0

spi_flash_w25qxx.c 11 KB

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