spi_flash_at45dbxx.c 14 KB

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