spi_flash_at45dbxx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_at45dbxx.h"
  12. #define FLASH_DEBUG
  13. #define DMA_BUFFER_SIZE 512
  14. #ifdef FLASH_DEBUG
  15. #define FLASH_TRACE rt_kprintf
  16. #else
  17. #define FLASH_TRACE(...)
  18. #endif /**< #ifdef FLASH_DEBUG */
  19. /* JEDEC Manufacturer’s ID */
  20. #define MF_ID (0x1F) /* atmel */
  21. #define DENSITY_CODE_011D (0x02) /* AT45DB011D Density Code : 00010 = 1-Mbit */
  22. #define DENSITY_CODE_021D (0x03) /* AT45DB021D Density Code : 00011 = 2-Mbit */
  23. #define DENSITY_CODE_041D (0x04) /* AT45DB041D Density Code : 00100 = 4-Mbit */
  24. #define DENSITY_CODE_081D (0x05) /* AT45DB081D Density Code : 00101 = 8-Mbit */
  25. #define DENSITY_CODE_161D (0x06) /* AT45DB161D Density Code : 00110 = 16-Mbit */
  26. #define DENSITY_CODE_321D (0x07) /* AT45DB321D Density Code : 00111 = 32-Mbit */
  27. #define DENSITY_CODE_642D (0x08) /* AT45DB642D Density Code : 01000 = 64-Mbit */
  28. struct JEDEC_ID
  29. {
  30. uint8_t manufacturer_id; /* Manufacturer ID */
  31. uint8_t density_code:5; /* Density Code */
  32. uint8_t family_code:3; /* Family Code */
  33. uint8_t version_code:5; /* Product Version Code */
  34. uint8_t mlc_code:3; /* MLC Code */
  35. uint8_t byte_count; /* Byte Count */
  36. };
  37. #define AT45DB_BUFFER_1_WRITE 0x84
  38. #define AT45DB_BUFFER_2_WRITE 0x87
  39. #define AT45DB_BUFFER_1_READ 0xD4
  40. #define AT45DB_BUFFER_2_READ 0xD6
  41. #define AT45DB_B1_TO_MM_PAGE_PROG_WITH_ERASE 0x83
  42. #define AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86
  43. #define AT45DB_MM_PAGE_TO_B1_XFER 0x53
  44. #define AT45DB_MM_PAGE_TO_B2_XFER 0x55
  45. #define AT45DB_PAGE_ERASE 0x81
  46. #define AT45DB_SECTOR_ERASE 0x7C
  47. #define AT45DB_READ_STATE_REGISTER 0xD7
  48. #define AT45DB_MM_PAGE_READ 0xD2
  49. #define AT45DB_MM_PAGE_PROG_THRU_BUFFER1 0x82
  50. #define AT45DB_CMD_JEDEC_ID 0x9F
  51. static struct spi_flash_at45dbxx spi_flash_at45dbxx;
  52. /*****************************************************************************/
  53. /*Status Register Format: */
  54. /* ------------------------------------------------------------------------- */
  55. /* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
  56. /* |--------|--------|--------|--------|--------|--------|--------|--------| */
  57. /* |RDY/BUSY| COMP | device density | X | X | */
  58. /* ------------------------------------------------------------------------- */
  59. /* 0:busy | | AT45DB041:0111 | protect|page size */
  60. /* 1:ready | | AT45DB161:1011 | */
  61. /* --------------------------------------------------------------------------*/
  62. /*****************************************************************************/
  63. static uint8_t AT45DB_StatusRegisterRead(void)
  64. {
  65. return rt_spi_sendrecv8(spi_flash_at45dbxx.rt_spi_device, AT45DB_READ_STATE_REGISTER);
  66. }
  67. static void wait_busy(void)
  68. {
  69. uint16_t i = 0;
  70. while (i++ < 10000)
  71. {
  72. if (AT45DB_StatusRegisterRead() & 0x80)
  73. {
  74. return;
  75. }
  76. }
  77. FLASH_TRACE("\r\nSPI_FLASH timeout!!!\r\n");
  78. }
  79. /* RT-Thread Device Driver Interface */
  80. static rt_err_t AT45DB_flash_init(rt_device_t dev)
  81. {
  82. return RT_EOK;
  83. }
  84. static rt_err_t AT45DB_flash_open(rt_device_t dev, rt_uint16_t oflag)
  85. {
  86. return RT_EOK;
  87. }
  88. static rt_err_t AT45DB_flash_close(rt_device_t dev)
  89. {
  90. return RT_EOK;
  91. }
  92. static rt_err_t AT45DB_flash_control(rt_device_t dev, int cmd, void *args)
  93. {
  94. RT_ASSERT(dev != RT_NULL);
  95. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  96. {
  97. struct rt_device_blk_geometry *geometry;
  98. geometry = (struct rt_device_blk_geometry *)args;
  99. if (geometry == RT_NULL) return -RT_ERROR;
  100. geometry->bytes_per_sector = 512;
  101. geometry->sector_count = 4096;
  102. geometry->block_size = 4096; /* block erase: 4k */
  103. }
  104. return RT_EOK;
  105. }
  106. static rt_size_t AT45DB_flash_read_page_256(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  107. {
  108. uint32_t index, nr;
  109. uint8_t * read_buffer = buffer;
  110. uint32_t page = pos;
  111. nr = size;
  112. for (index = 0; index < nr; index++)
  113. {
  114. uint8_t send_buffer[8];
  115. uint32_t i;
  116. for(i=0; i<sizeof(send_buffer); i++)
  117. {
  118. send_buffer[i] = 0;
  119. }
  120. send_buffer[0] = AT45DB_MM_PAGE_READ;
  121. send_buffer[1] = (uint8_t)(page >> 7);
  122. send_buffer[2] = (uint8_t)(page << 1);
  123. rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 256);
  124. read_buffer += 256;
  125. page++;
  126. }
  127. return size;
  128. }
  129. static rt_size_t AT45DB_flash_read_page_512(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  130. {
  131. uint32_t index, nr;
  132. uint8_t * read_buffer = buffer;
  133. uint32_t page = pos;
  134. nr = size;
  135. for (index = 0; index < nr; index++)
  136. {
  137. uint8_t send_buffer[8];
  138. uint32_t i;
  139. for(i=0; i<sizeof(send_buffer); i++)
  140. {
  141. send_buffer[i] = 0;
  142. }
  143. send_buffer[0] = AT45DB_MM_PAGE_READ;
  144. send_buffer[1] = (uint8_t)(page >> 6);
  145. send_buffer[2] = (uint8_t)(page << 2);
  146. rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 512);
  147. read_buffer += 512;
  148. page++;
  149. }
  150. return size;
  151. }
  152. static rt_size_t AT45DB_flash_read_page_1024(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  153. {
  154. uint32_t index, nr;
  155. uint8_t * read_buffer = buffer;
  156. uint32_t page = pos;
  157. nr = size;
  158. for (index = 0; index < nr; index++)
  159. {
  160. uint8_t send_buffer[8];
  161. uint32_t i;
  162. for(i=0; i<sizeof(send_buffer); i++)
  163. {
  164. send_buffer[i] = 0;
  165. }
  166. send_buffer[0] = AT45DB_MM_PAGE_READ;
  167. send_buffer[1] = (uint8_t)(page >> 5);
  168. send_buffer[2] = (uint8_t)(page << 3);
  169. rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, send_buffer, 8, read_buffer, 1024);
  170. read_buffer += 1024;
  171. page++;
  172. }
  173. return size;
  174. }
  175. static rt_size_t AT45DB_flash_write_page_256(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  176. {
  177. rt_uint32_t index, nr;
  178. const uint8_t * write_buffer = buffer;
  179. uint32_t page = pos;
  180. nr = size;
  181. for (index = 0; index < nr; index++)
  182. {
  183. uint8_t send_buffer[4];
  184. send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1;
  185. send_buffer[1] = (uint8_t) (page >> 7);
  186. send_buffer[2] = (uint8_t) (page << 1);
  187. send_buffer[3] = 0;
  188. rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 256);
  189. write_buffer += 256;
  190. page++;
  191. wait_busy();
  192. }
  193. return size;
  194. }
  195. static rt_size_t AT45DB_flash_write_page_512(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  196. {
  197. rt_uint32_t index, nr;
  198. const uint8_t * write_buffer = buffer;
  199. uint32_t page = pos;
  200. nr = size;
  201. for (index = 0; index < nr; index++)
  202. {
  203. uint8_t send_buffer[4];
  204. send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1;
  205. send_buffer[1] = (uint8_t) (page >> 6);
  206. send_buffer[2] = (uint8_t) (page << 2);
  207. send_buffer[3] = 0;
  208. rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 512);
  209. write_buffer += 512;
  210. page++;
  211. wait_busy();
  212. }
  213. return size;
  214. }
  215. static rt_size_t AT45DB_flash_write_page_1024(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  216. {
  217. rt_uint32_t index, nr;
  218. const uint8_t * write_buffer = buffer;
  219. uint32_t page = pos;
  220. nr = size;
  221. for (index = 0; index < nr; index++)
  222. {
  223. uint8_t send_buffer[4];
  224. send_buffer[0] = AT45DB_MM_PAGE_PROG_THRU_BUFFER1;
  225. send_buffer[1] = (uint8_t) (page >> 5);
  226. send_buffer[2] = (uint8_t) (page << 3);
  227. send_buffer[3] = 0;
  228. rt_spi_send_then_send(spi_flash_at45dbxx.rt_spi_device, send_buffer, 4, write_buffer, 1024);
  229. write_buffer += 1024;
  230. page++;
  231. wait_busy();
  232. }
  233. return size;
  234. }
  235. rt_err_t at45dbxx_init(const char * flash_device_name, const char * spi_device_name)
  236. {
  237. struct rt_spi_device * rt_spi_device;
  238. struct JEDEC_ID * JEDEC_ID;
  239. rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  240. if(rt_spi_device == RT_NULL)
  241. {
  242. FLASH_TRACE("spi device %s not found!\r\n", spi_device_name);
  243. return -RT_ENOSYS;
  244. }
  245. spi_flash_at45dbxx.rt_spi_device = rt_spi_device;
  246. /* config spi */
  247. {
  248. struct rt_spi_configuration cfg;
  249. cfg.data_width = 8;
  250. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 and 3 */
  251. cfg.max_hz = 66000000; /* Atmel RapidS Serial Interface: 66MHz Maximum Clock Frequency */
  252. rt_spi_configure(spi_flash_at45dbxx.rt_spi_device, &cfg);
  253. }
  254. /* read JEDEC ID */
  255. {
  256. uint8_t cmd;
  257. uint8_t id_recv[6];
  258. JEDEC_ID = (struct JEDEC_ID *)id_recv;
  259. cmd = AT45DB_CMD_JEDEC_ID;
  260. rt_spi_send_then_recv(spi_flash_at45dbxx.rt_spi_device, &cmd, 1, id_recv, 6);
  261. /**< 1FH = Atmel */
  262. /**< 001 = Atmel DataFlash */
  263. if(JEDEC_ID->manufacturer_id != 0x1F || JEDEC_ID->family_code != 0x01)
  264. {
  265. FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n");
  266. FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]);
  267. return -RT_ENOSYS;
  268. }
  269. if(JEDEC_ID->density_code == DENSITY_CODE_011D)
  270. {
  271. /**< AT45DB011D Density Code : 00010 = 1-Mbit */
  272. FLASH_TRACE("AT45DB011D detection\r\n");
  273. spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */
  274. spi_flash_at45dbxx.geometry.sector_count = 512; /* 1-Mbit / 8 / 256 = 512 */
  275. spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */
  276. }
  277. else if(JEDEC_ID->density_code == DENSITY_CODE_021D)
  278. {
  279. /**< AT45DB021D Density Code : 00011 = 2-Mbit */
  280. FLASH_TRACE("AT45DB021D detection\r\n");
  281. spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */
  282. spi_flash_at45dbxx.geometry.sector_count = 512*2; /* 2-Mbit / 8 / 256 = 1024 */
  283. spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */
  284. }
  285. else if(JEDEC_ID->density_code == DENSITY_CODE_041D)
  286. {
  287. /**< AT45DB041D Density Code : 00100 = 4-Mbit */
  288. FLASH_TRACE("AT45DB041D detection\r\n");
  289. spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */
  290. spi_flash_at45dbxx.geometry.sector_count = 512*4; /* 4-Mbit / 8 / 256 = 2048 */
  291. spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */
  292. }
  293. else if(JEDEC_ID->density_code == DENSITY_CODE_081D)
  294. {
  295. /**< AT45DB081D Density Code : 00101 = 8-Mbit */
  296. FLASH_TRACE("AT45DB081D detection\r\n");
  297. spi_flash_at45dbxx.geometry.bytes_per_sector = 256; /* Page Erase (256 Bytes) */
  298. spi_flash_at45dbxx.geometry.sector_count = 512*8; /* 8-Mbit / 8 / 256 = 4096 */
  299. spi_flash_at45dbxx.geometry.block_size = 1024*2; /* Block Erase (2-Kbytes) */
  300. }
  301. else if(JEDEC_ID->density_code == DENSITY_CODE_161D)
  302. {
  303. /**< AT45DB161D Density Code : 00110 = 16-Mbit */
  304. FLASH_TRACE("AT45DB161D detection\r\n");
  305. spi_flash_at45dbxx.geometry.bytes_per_sector = 512; /* Page Erase (512 Bytes) */
  306. spi_flash_at45dbxx.geometry.sector_count = 256*16; /* 16-Mbit / 8 / 512 = 4096 */
  307. spi_flash_at45dbxx.geometry.block_size = 1024*4; /* Block Erase (4-Kbytes) */
  308. }
  309. else if(JEDEC_ID->density_code == DENSITY_CODE_321D)
  310. {
  311. /**< AT45DB321D Density Code : 00111 = 32-Mbit */
  312. FLASH_TRACE("AT45DB321D detection\r\n");
  313. spi_flash_at45dbxx.geometry.bytes_per_sector = 512; /* Page Erase (512 Bytes) */
  314. spi_flash_at45dbxx.geometry.sector_count = 256*32; /* 32-Mbit / 8 / 512 = 8192 */
  315. spi_flash_at45dbxx.geometry.block_size = 1024*4; /* Block Erase (4-Kbytes) */
  316. }
  317. else if(JEDEC_ID->density_code == DENSITY_CODE_642D)
  318. {
  319. /**< AT45DB642D Density Code : 01000 = 64-Mbit */
  320. FLASH_TRACE("AT45DB642D detection\r\n");
  321. spi_flash_at45dbxx.geometry.bytes_per_sector = 1024; /* Page Erase (1 Kbyte) */
  322. spi_flash_at45dbxx.geometry.sector_count = 128*64; /* 64-Mbit / 8 / 1024 = 8192 */
  323. spi_flash_at45dbxx.geometry.block_size = 1024*8; /* Block Erase (8 Kbytes) */
  324. }
  325. else
  326. {
  327. FLASH_TRACE("Memory Capacity error!\r\n");
  328. return -RT_ENOSYS;
  329. }
  330. }
  331. /* register device */
  332. spi_flash_at45dbxx.flash_device.type = RT_Device_Class_Block;
  333. spi_flash_at45dbxx.flash_device.init = AT45DB_flash_init;
  334. spi_flash_at45dbxx.flash_device.open = AT45DB_flash_open;
  335. spi_flash_at45dbxx.flash_device.close = AT45DB_flash_close;
  336. spi_flash_at45dbxx.flash_device.control = AT45DB_flash_control;
  337. if(JEDEC_ID->density_code == DENSITY_CODE_642D)
  338. {
  339. spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_1024;
  340. spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_1024;
  341. }
  342. else if(JEDEC_ID->density_code == DENSITY_CODE_161D
  343. || JEDEC_ID->density_code == DENSITY_CODE_321D )
  344. {
  345. spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_512;
  346. spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_512;
  347. }
  348. else
  349. {
  350. spi_flash_at45dbxx.flash_device.read = AT45DB_flash_read_page_256;
  351. spi_flash_at45dbxx.flash_device.write = AT45DB_flash_write_page_256;
  352. }
  353. /* no private */
  354. spi_flash_at45dbxx.flash_device.user_data = RT_NULL;
  355. rt_device_register(&spi_flash_at45dbxx.flash_device, flash_device_name,
  356. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  357. return RT_EOK;
  358. }