You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
преди 10 години
преди 9 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
  2. * https://github.com/PaulStoffregen/SerialFlash
  3. * Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
  4. *
  5. * Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
  6. * Please support PJRC's efforts to develop open source software by purchasing
  7. * Teensy or other genuine PJRC products.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice, development funding notice, and this permission
  17. * notice shall be included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "SerialFlash.h"
  28. #include "util/SerialFlash_directwrite.h"
  29. #define CSCONFIG() pinMode(6, OUTPUT)
  30. #define CSASSERT() digitalWriteFast(6, LOW)
  31. #define CSRELEASE() digitalWriteFast(6, HIGH)
  32. #define SPICONFIG SPISettings(50000000, MSBFIRST, SPI_MODE0)
  33. #if !defined(__arm__) || !defined(CORE_TEENSY)
  34. #define digitalWriteFast(pin, state) digitalWrite((pin), (state))
  35. #endif
  36. uint16_t SerialFlashChip::dirindex = 0;
  37. uint8_t SerialFlashChip::flags = 0;
  38. uint8_t SerialFlashChip::busy = 0;
  39. #define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  40. #define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  41. #define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  42. #define FLAG_MULTI_DIE 0x08 // multiple die, don't read cross 32M barrier
  43. #define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  44. #define FLAG_DIE_MASK 0xC0 // top 2 bits count during multi-die erase
  45. void SerialFlashChip::wait(void)
  46. {
  47. uint32_t status;
  48. //Serial.print("wait-");
  49. while (1) {
  50. SPI.beginTransaction(SPICONFIG);
  51. CSASSERT();
  52. if (flags & FLAG_STATUS_CMD70) {
  53. // some Micron chips require this different
  54. // command to detect program and erase completion
  55. SPI.transfer(0x70);
  56. status = SPI.transfer(0);
  57. CSRELEASE();
  58. SPI.endTransaction();
  59. //Serial.printf("b=%02x.", status & 0xFF);
  60. if ((status & 0x80)) break;
  61. } else {
  62. // all others work by simply reading the status reg
  63. SPI.transfer(0x05);
  64. status = SPI.transfer(0);
  65. CSRELEASE();
  66. SPI.endTransaction();
  67. //Serial.printf("b=%02x.", status & 0xFF);
  68. if (!(status & 1)) break;
  69. }
  70. }
  71. busy = 0;
  72. //Serial.println();
  73. }
  74. void SerialFlashChip::read(uint32_t addr, void *buf, uint32_t len)
  75. {
  76. uint8_t *p = (uint8_t *)buf;
  77. uint8_t b, f, status, cmd;
  78. memset(p, 0, len);
  79. f = flags;
  80. SPI.beginTransaction(SPICONFIG);
  81. b = busy;
  82. if (b) {
  83. // read status register ... chip may no longer be busy
  84. CSASSERT();
  85. if (flags & FLAG_STATUS_CMD70) {
  86. SPI.transfer(0x70);
  87. status = SPI.transfer(0);
  88. if ((status & 0x80)) b = 0;
  89. } else {
  90. SPI.transfer(0x05);
  91. status = SPI.transfer(0);
  92. if (!(status & 1)) b = 0;
  93. }
  94. CSRELEASE();
  95. if (b == 0) {
  96. // chip is no longer busy :-)
  97. busy = 0;
  98. } else if (b < 3) {
  99. // TODO: this may not work on Spansion chips
  100. // which apparently have 2 different suspend
  101. // commands, for program vs erase
  102. CSASSERT();
  103. SPI.transfer(0x06); // write enable (Micron req'd)
  104. CSRELEASE();
  105. delayMicroseconds(1);
  106. cmd = 0x75; //Suspend program/erase for almost all chips
  107. // but Spansion just has to be different for program suspend!
  108. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x85;
  109. CSASSERT();
  110. SPI.transfer(cmd); // Suspend command
  111. CSRELEASE();
  112. if (f & FLAG_STATUS_CMD70) {
  113. // Micron chips don't actually suspend until flags read
  114. CSASSERT();
  115. SPI.transfer(0x70);
  116. do {
  117. status = SPI.transfer(0);
  118. } while (!(status & 0x80));
  119. CSRELEASE();
  120. } else {
  121. CSASSERT();
  122. SPI.transfer(0x05);
  123. do {
  124. status = SPI.transfer(0);
  125. } while ((status & 0x01));
  126. CSRELEASE();
  127. }
  128. } else {
  129. // chip is busy with an operation that can not suspend
  130. SPI.endTransaction(); // is this a good idea?
  131. wait(); // should we wait without ending
  132. b = 0; // the transaction??
  133. SPI.beginTransaction(SPICONFIG);
  134. }
  135. }
  136. do {
  137. uint32_t rdlen = len;
  138. if (f & FLAG_MULTI_DIE) {
  139. if ((addr & 0xFE000000) != ((addr + len - 1) & 0xFE000000)) {
  140. rdlen = 0x2000000 - (addr & 0x1FFFFFF);
  141. }
  142. }
  143. CSASSERT();
  144. // TODO: FIFO optimize....
  145. if (f & FLAG_32BIT_ADDR) {
  146. SPI.transfer(0x03);
  147. SPI.transfer16(addr >> 16);
  148. SPI.transfer16(addr);
  149. } else {
  150. SPI.transfer16(0x0300 | ((addr >> 16) & 255));
  151. SPI.transfer16(addr);
  152. }
  153. SPI.transfer(p, rdlen);
  154. CSRELEASE();
  155. p += rdlen;
  156. addr += rdlen;
  157. len -= rdlen;
  158. } while (len > 0);
  159. if (b) {
  160. CSASSERT();
  161. SPI.transfer(0x06); // write enable (Micron req'd)
  162. CSRELEASE();
  163. delayMicroseconds(1);
  164. cmd = 0x7A;
  165. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x8A;
  166. CSASSERT();
  167. SPI.transfer(cmd); // Resume program/erase
  168. CSRELEASE();
  169. }
  170. SPI.endTransaction();
  171. }
  172. void SerialFlashChip::write(uint32_t addr, const void *buf, uint32_t len)
  173. {
  174. const uint8_t *p = (const uint8_t *)buf;
  175. uint32_t max, pagelen;
  176. //Serial.printf("WR: addr %08X, len %d\n", addr, len);
  177. do {
  178. if (busy) wait();
  179. SPI.beginTransaction(SPICONFIG);
  180. CSASSERT();
  181. // write enable command
  182. SPI.transfer(0x06);
  183. CSRELEASE();
  184. max = 256 - (addr & 0xFF);
  185. pagelen = (len <= max) ? len : max;
  186. //Serial.printf("WR: addr %08X, pagelen %d\n", addr, pagelen);
  187. CSASSERT();
  188. if (flags & FLAG_32BIT_ADDR) {
  189. SPI.transfer(0x02); // program page command
  190. SPI.transfer16(addr >> 16);
  191. SPI.transfer16(addr);
  192. } else {
  193. SPI.transfer16(0x0200 | ((addr >> 16) & 255));
  194. SPI.transfer16(addr);
  195. }
  196. addr += pagelen;
  197. len -= pagelen;
  198. do {
  199. SPI.transfer(*p++);
  200. } while (--pagelen > 0);
  201. CSRELEASE();
  202. busy = 1;
  203. SPI.endTransaction();
  204. } while (len > 0);
  205. }
  206. void SerialFlashChip::eraseAll()
  207. {
  208. if (busy) wait();
  209. uint8_t id[3];
  210. readID(id);
  211. //Serial.printf("ID: %02X %02X %02X\n", id[0], id[1], id[2]);
  212. if (id[0] == 0x20 && id[2] >= 0x20 && id[2] <= 0x22) {
  213. // Micron's multi-die chips require special die erase commands
  214. // N25Q512A 20 BA 20 2 dies 32 Mbyte/die 65 nm transitors
  215. // N25Q00AA 20 BA 21 4 dies 32 Mbyte/die 65 nm transitors
  216. // MT25QL02GC 20 BA 22 2 dies 128 Mbyte/die 45 nm transitors
  217. uint8_t die_count = 2;
  218. if (id[2] == 0x21) die_count = 4;
  219. uint8_t die_index = flags >> 6;
  220. //Serial.printf("Micron die erase %d\n", die_index);
  221. flags &= 0x3F;
  222. if (die_index >= die_count) return; // all dies erased :-)
  223. uint8_t die_size = 2; // in 16 Mbyte units
  224. if (id[2] == 0x22) die_size = 8;
  225. SPI.beginTransaction(SPICONFIG);
  226. CSASSERT();
  227. SPI.transfer(0x06); // write enable command
  228. CSRELEASE();
  229. delayMicroseconds(1);
  230. CSASSERT();
  231. // die erase command
  232. SPI.transfer(0xC4);
  233. SPI.transfer16((die_index * die_size) << 8);
  234. SPI.transfer16(0x0000);
  235. CSRELEASE();
  236. //Serial.printf("Micron erase begin\n");
  237. flags |= (die_index + 1) << 6;
  238. } else {
  239. // All other chips support the bulk erase command
  240. SPI.beginTransaction(SPICONFIG);
  241. CSASSERT();
  242. // write enable command
  243. SPI.transfer(0x06);
  244. CSRELEASE();
  245. delayMicroseconds(1);
  246. CSASSERT();
  247. // bulk erase command
  248. SPI.transfer(0xC7);
  249. CSRELEASE();
  250. SPI.endTransaction();
  251. }
  252. busy = 3;
  253. }
  254. void SerialFlashChip::eraseBlock(uint32_t addr)
  255. {
  256. uint8_t f = flags;
  257. if (busy) wait();
  258. SPI.beginTransaction(SPICONFIG);
  259. CSASSERT();
  260. SPI.transfer(0x06); // write enable command
  261. CSRELEASE();
  262. delayMicroseconds(1);
  263. CSASSERT();
  264. if (f & FLAG_32BIT_ADDR) {
  265. SPI.transfer(0xD8);
  266. SPI.transfer16(addr >> 16);
  267. SPI.transfer16(addr);
  268. } else {
  269. SPI.transfer16(0xD800 | ((addr >> 16) & 255));
  270. SPI.transfer16(addr);
  271. }
  272. CSRELEASE();
  273. SPI.endTransaction();
  274. busy = 2;
  275. }
  276. bool SerialFlashChip::ready()
  277. {
  278. uint32_t status;
  279. if (!busy) return true;
  280. SPI.beginTransaction(SPICONFIG);
  281. CSASSERT();
  282. if (flags & FLAG_STATUS_CMD70) {
  283. // some Micron chips require this different
  284. // command to detect program and erase completion
  285. SPI.transfer(0x70);
  286. status = SPI.transfer(0);
  287. CSRELEASE();
  288. SPI.endTransaction();
  289. //Serial.printf("ready=%02x\n", status & 0xFF);
  290. if ((status & 0x80) == 0) return false;
  291. } else {
  292. // all others work by simply reading the status reg
  293. SPI.transfer(0x05);
  294. status = SPI.transfer(0);
  295. CSRELEASE();
  296. SPI.endTransaction();
  297. //Serial.printf("ready=%02x\n", status & 0xFF);
  298. if ((status & 1)) return false;
  299. }
  300. busy = 0;
  301. if (flags & 0xC0) {
  302. // continue a multi-die erase
  303. eraseAll();
  304. return false;
  305. }
  306. return true;
  307. }
  308. #define ID0_WINBOND 0xEF
  309. #define ID0_SPANSION 0x01
  310. #define ID0_MICRON 0x20
  311. #define ID0_MACRONIX 0xC2
  312. #define ID0_SST 0xBF
  313. //#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  314. //#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  315. //#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  316. //#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  317. bool SerialFlashChip::begin()
  318. {
  319. uint8_t id[3];
  320. uint8_t f;
  321. uint32_t size;
  322. SPI.begin();
  323. CSCONFIG();
  324. CSRELEASE();
  325. readID(id);
  326. f = 0;
  327. size = capacity(id);
  328. if (size > 16777216) {
  329. // more than 16 Mbyte requires 32 bit addresses
  330. f |= FLAG_32BIT_ADDR;
  331. SPI.beginTransaction(SPICONFIG);
  332. if (id[0] == ID0_SPANSION) {
  333. // spansion uses MSB of bank register
  334. CSASSERT();
  335. SPI.transfer16(0x1780); // bank register write
  336. CSRELEASE();
  337. } else {
  338. // micron & winbond & macronix use command
  339. CSASSERT();
  340. SPI.transfer(0x06); // write enable
  341. CSRELEASE();
  342. delayMicroseconds(1);
  343. CSASSERT();
  344. SPI.transfer(0xB7); // enter 4 byte addr mode
  345. CSRELEASE();
  346. }
  347. SPI.endTransaction();
  348. if (id[0] == ID0_MICRON) f |= FLAG_MULTI_DIE;
  349. }
  350. if (id[0] == ID0_SPANSION) {
  351. // Spansion has separate suspend commands
  352. f |= FLAG_DIFF_SUSPEND;
  353. if (size >= 67108864) {
  354. // Spansion chips >= 512 mbit use 256K sectors
  355. f |= FLAG_256K_BLOCKS;
  356. }
  357. }
  358. if (id[0] == ID0_MICRON) {
  359. // Micron requires busy checks with a different command
  360. f |= FLAG_STATUS_CMD70; // TODO: all or just multi-die chips?
  361. }
  362. flags = f;
  363. readID(id);
  364. return true;
  365. }
  366. void SerialFlashChip::readID(uint8_t *buf)
  367. {
  368. if (busy) wait();
  369. SPI.beginTransaction(SPICONFIG);
  370. CSASSERT();
  371. SPI.transfer(0x9F);
  372. buf[0] = SPI.transfer(0); // manufacturer ID
  373. buf[1] = SPI.transfer(0); // memory type
  374. buf[2] = SPI.transfer(0); // capacity
  375. CSRELEASE();
  376. SPI.endTransaction();
  377. //Serial.printf("ID: %02X %02X %02X\n", buf[0], buf[1], buf[2]);
  378. }
  379. uint32_t SerialFlashChip::capacity(const uint8_t *id)
  380. {
  381. uint32_t n = 1048576; // unknown chips, default to 1 MByte
  382. if (id[2] >= 16 && id[2] <= 31) {
  383. n = 1ul << id[2];
  384. } else
  385. if (id[2] >= 32 && id[2] <= 37) {
  386. n = 1ul << (id[2] - 6);
  387. }
  388. //Serial.printf("capacity %lu\n", n);
  389. return n;
  390. }
  391. uint32_t SerialFlashChip::blockSize()
  392. {
  393. // Spansion chips >= 512 mbit use 256K sectors
  394. if (flags & FLAG_256K_BLOCKS) return 262144;
  395. // everything else seems to have 64K sectors
  396. return 65536;
  397. }
  398. /*
  399. Chip Uniform Sector Erase
  400. 20/21 52 D8/DC
  401. ----- -- -----
  402. W25Q64CV 4 32 64
  403. W25Q128FV 4 32 64
  404. S25FL127S 64
  405. N25Q512A 4 64
  406. N25Q00AA 4 64
  407. S25FL512S 256
  408. SST26VF032 4
  409. */
  410. // size sector busy pgm/erase chip
  411. // Part Mbyte kbyte ID bytes cmd suspend erase
  412. // ---- ---- ----- -------- --- ------- -----
  413. // Winbond W25Q64CV 8 64 EF 40 17
  414. // Winbond W25Q128FV 16 64 EF 40 18 05 single 60 & C7
  415. // Winbond W25Q256FV 32 64 EF 40 19
  416. // Spansion S25FL064A 8 ? 01 02 16
  417. // Spansion S25FL127S 16 64 01 20 18 05
  418. // Spansion S25FL128P 16 64 01 20 18
  419. // Spansion S25FL256S 32 64 01 02 19 05 60 & C7
  420. // Spansion S25FL512S 64 256 01 02 20
  421. // Macronix MX25L12805D 16 ? C2 20 18
  422. // Macronix MX66L51235F 64 C2 20 1A
  423. // Numonyx M25P128 16 ? 20 20 18
  424. // Micron M25P80 1 ? 20 20 14
  425. // Micron N25Q128A 16 64 20 BA 18
  426. // Micron N25Q512A 64 ? 20 BA 20 70 single C4 x2
  427. // Micron N25Q00AA 128 64 20 BA 21 single C4 x4
  428. // Micron MT25QL02GC 256 64 20 BA 22 70 C4 x2
  429. // SST SST25WF010 1/8 ? BF 25 02
  430. // SST SST25WF020 1/4 ? BF 25 03
  431. // SST SST25WF040 1/2 ? BF 25 04
  432. // SST SST25VF016B 1 ? BF 25 41
  433. // SST26VF016 ? BF 26 01
  434. // SST26VF032 ? BF 26 02
  435. // SST25VF032 4 64 BF 25 4A
  436. // SST26VF064 8 ? BF 26 43
  437. // LE25U40CMC 1/2 64 62 06 13
  438. SerialFlashChip SerialFlash;