spi_flash_at45dbxx.c 15 KB

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