No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

SerialFlashChip.cpp 14KB

hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
hace 10 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 CSASSERT() DIRECT_WRITE_LOW(cspin_basereg, cspin_bitmask)
  30. #define CSRELEASE() DIRECT_WRITE_HIGH(cspin_basereg, cspin_bitmask)
  31. #define SPICONFIG SPISettings(50000000, MSBFIRST, SPI_MODE0)
  32. uint16_t SerialFlashChip::dirindex = 0;
  33. uint8_t SerialFlashChip::flags = 0;
  34. uint8_t SerialFlashChip::busy = 0;
  35. static volatile IO_REG_TYPE *cspin_basereg;
  36. static IO_REG_TYPE cspin_bitmask;
  37. #define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  38. #define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  39. #define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  40. #define FLAG_MULTI_DIE 0x08 // multiple die, don't read cross 32M barrier
  41. #define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  42. #define FLAG_DIE_MASK 0xC0 // top 2 bits count during multi-die erase
  43. void SerialFlashChip::wait(void)
  44. {
  45. uint32_t status;
  46. //Serial.print("wait-");
  47. while (1) {
  48. SPI.beginTransaction(SPICONFIG);
  49. CSASSERT();
  50. if (flags & FLAG_STATUS_CMD70) {
  51. // some Micron chips require this different
  52. // command to detect program and erase completion
  53. SPI.transfer(0x70);
  54. status = SPI.transfer(0);
  55. CSRELEASE();
  56. SPI.endTransaction();
  57. //Serial.printf("b=%02x.", status & 0xFF);
  58. if ((status & 0x80)) break;
  59. } else {
  60. // all others work by simply reading the status reg
  61. SPI.transfer(0x05);
  62. status = SPI.transfer(0);
  63. CSRELEASE();
  64. SPI.endTransaction();
  65. //Serial.printf("b=%02x.", status & 0xFF);
  66. if (!(status & 1)) break;
  67. }
  68. }
  69. busy = 0;
  70. //Serial.println();
  71. }
  72. void SerialFlashChip::read(uint32_t addr, void *buf, uint32_t len)
  73. {
  74. uint8_t *p = (uint8_t *)buf;
  75. uint8_t b, f, status, cmd;
  76. memset(p, 0, len);
  77. f = flags;
  78. SPI.beginTransaction(SPICONFIG);
  79. b = busy;
  80. if (b) {
  81. // read status register ... chip may no longer be busy
  82. CSASSERT();
  83. if (flags & FLAG_STATUS_CMD70) {
  84. SPI.transfer(0x70);
  85. status = SPI.transfer(0);
  86. if ((status & 0x80)) b = 0;
  87. } else {
  88. SPI.transfer(0x05);
  89. status = SPI.transfer(0);
  90. if (!(status & 1)) b = 0;
  91. }
  92. CSRELEASE();
  93. if (b == 0) {
  94. // chip is no longer busy :-)
  95. busy = 0;
  96. } else if (b < 3) {
  97. // TODO: this may not work on Spansion chips
  98. // which apparently have 2 different suspend
  99. // commands, for program vs erase
  100. CSASSERT();
  101. SPI.transfer(0x06); // write enable (Micron req'd)
  102. CSRELEASE();
  103. delayMicroseconds(1);
  104. cmd = 0x75; //Suspend program/erase for almost all chips
  105. // but Spansion just has to be different for program suspend!
  106. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x85;
  107. CSASSERT();
  108. SPI.transfer(cmd); // Suspend command
  109. CSRELEASE();
  110. if (f & FLAG_STATUS_CMD70) {
  111. // Micron chips don't actually suspend until flags read
  112. CSASSERT();
  113. SPI.transfer(0x70);
  114. do {
  115. status = SPI.transfer(0);
  116. } while (!(status & 0x80));
  117. CSRELEASE();
  118. } else {
  119. CSASSERT();
  120. SPI.transfer(0x05);
  121. do {
  122. status = SPI.transfer(0);
  123. } while ((status & 0x01));
  124. CSRELEASE();
  125. }
  126. } else {
  127. // chip is busy with an operation that can not suspend
  128. SPI.endTransaction(); // is this a good idea?
  129. wait(); // should we wait without ending
  130. b = 0; // the transaction??
  131. SPI.beginTransaction(SPICONFIG);
  132. }
  133. }
  134. do {
  135. uint32_t rdlen = len;
  136. if (f & FLAG_MULTI_DIE) {
  137. if ((addr & 0xFE000000) != ((addr + len - 1) & 0xFE000000)) {
  138. rdlen = 0x2000000 - (addr & 0x1FFFFFF);
  139. }
  140. }
  141. CSASSERT();
  142. // TODO: FIFO optimize....
  143. if (f & FLAG_32BIT_ADDR) {
  144. SPI.transfer(0x03);
  145. SPI.transfer16(addr >> 16);
  146. SPI.transfer16(addr);
  147. } else {
  148. SPI.transfer16(0x0300 | ((addr >> 16) & 255));
  149. SPI.transfer16(addr);
  150. }
  151. SPI.transfer(p, rdlen);
  152. CSRELEASE();
  153. p += rdlen;
  154. addr += rdlen;
  155. len -= rdlen;
  156. } while (len > 0);
  157. if (b) {
  158. CSASSERT();
  159. SPI.transfer(0x06); // write enable (Micron req'd)
  160. CSRELEASE();
  161. delayMicroseconds(1);
  162. cmd = 0x7A;
  163. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x8A;
  164. CSASSERT();
  165. SPI.transfer(cmd); // Resume program/erase
  166. CSRELEASE();
  167. }
  168. SPI.endTransaction();
  169. }
  170. void SerialFlashChip::write(uint32_t addr, const void *buf, uint32_t len)
  171. {
  172. const uint8_t *p = (const uint8_t *)buf;
  173. uint32_t max, pagelen;
  174. //Serial.printf("WR: addr %08X, len %d\n", addr, len);
  175. do {
  176. if (busy) wait();
  177. SPI.beginTransaction(SPICONFIG);
  178. CSASSERT();
  179. // write enable command
  180. SPI.transfer(0x06);
  181. CSRELEASE();
  182. max = 256 - (addr & 0xFF);
  183. pagelen = (len <= max) ? len : max;
  184. //Serial.printf("WR: addr %08X, pagelen %d\n", addr, pagelen);
  185. delayMicroseconds(1); // TODO: reduce this, but prefer safety first
  186. CSASSERT();
  187. if (flags & FLAG_32BIT_ADDR) {
  188. SPI.transfer(0x02); // program page command
  189. SPI.transfer16(addr >> 16);
  190. SPI.transfer16(addr);
  191. } else {
  192. SPI.transfer16(0x0200 | ((addr >> 16) & 255));
  193. SPI.transfer16(addr);
  194. }
  195. addr += pagelen;
  196. len -= pagelen;
  197. do {
  198. SPI.transfer(*p++);
  199. } while (--pagelen > 0);
  200. CSRELEASE();
  201. busy = 4;
  202. SPI.endTransaction();
  203. } while (len > 0);
  204. }
  205. void SerialFlashChip::eraseAll()
  206. {
  207. if (busy) wait();
  208. uint8_t id[5];
  209. readID(id);
  210. //Serial.printf("ID: %02X %02X %02X\n", id[0], id[1], id[2]);
  211. if (id[0] == 0x20 && id[2] >= 0x20 && id[2] <= 0x22) {
  212. // Micron's multi-die chips require special die erase commands
  213. // N25Q512A 20 BA 20 2 dies 32 Mbyte/die 65 nm transitors
  214. // N25Q00AA 20 BA 21 4 dies 32 Mbyte/die 65 nm transitors
  215. // MT25QL02GC 20 BA 22 2 dies 128 Mbyte/die 45 nm transitors
  216. uint8_t die_count = 2;
  217. if (id[2] == 0x21) die_count = 4;
  218. uint8_t die_index = flags >> 6;
  219. //Serial.printf("Micron die erase %d\n", die_index);
  220. flags &= 0x3F;
  221. if (die_index >= die_count) return; // all dies erased :-)
  222. uint8_t die_size = 2; // in 16 Mbyte units
  223. if (id[2] == 0x22) die_size = 8;
  224. SPI.beginTransaction(SPICONFIG);
  225. CSASSERT();
  226. SPI.transfer(0x06); // write enable command
  227. CSRELEASE();
  228. delayMicroseconds(1);
  229. CSASSERT();
  230. // die erase command
  231. SPI.transfer(0xC4);
  232. SPI.transfer16((die_index * die_size) << 8);
  233. SPI.transfer16(0x0000);
  234. CSRELEASE();
  235. //Serial.printf("Micron erase begin\n");
  236. flags |= (die_index + 1) << 6;
  237. } else {
  238. // All other chips support the bulk erase command
  239. SPI.beginTransaction(SPICONFIG);
  240. CSASSERT();
  241. // write enable command
  242. SPI.transfer(0x06);
  243. CSRELEASE();
  244. delayMicroseconds(1);
  245. CSASSERT();
  246. // bulk erase command
  247. SPI.transfer(0xC7);
  248. CSRELEASE();
  249. SPI.endTransaction();
  250. }
  251. busy = 3;
  252. }
  253. void SerialFlashChip::eraseBlock(uint32_t addr)
  254. {
  255. uint8_t f = flags;
  256. if (busy) wait();
  257. SPI.beginTransaction(SPICONFIG);
  258. CSASSERT();
  259. SPI.transfer(0x06); // write enable command
  260. CSRELEASE();
  261. delayMicroseconds(1);
  262. CSASSERT();
  263. if (f & FLAG_32BIT_ADDR) {
  264. SPI.transfer(0xD8);
  265. SPI.transfer16(addr >> 16);
  266. SPI.transfer16(addr);
  267. } else {
  268. SPI.transfer16(0xD800 | ((addr >> 16) & 255));
  269. SPI.transfer16(addr);
  270. }
  271. CSRELEASE();
  272. SPI.endTransaction();
  273. busy = 2;
  274. }
  275. bool SerialFlashChip::ready()
  276. {
  277. uint32_t status;
  278. if (!busy) return true;
  279. SPI.beginTransaction(SPICONFIG);
  280. CSASSERT();
  281. if (flags & FLAG_STATUS_CMD70) {
  282. // some Micron chips require this different
  283. // command to detect program and erase completion
  284. SPI.transfer(0x70);
  285. status = SPI.transfer(0);
  286. CSRELEASE();
  287. SPI.endTransaction();
  288. //Serial.printf("ready=%02x\n", status & 0xFF);
  289. if ((status & 0x80) == 0) return false;
  290. } else {
  291. // all others work by simply reading the status reg
  292. SPI.transfer(0x05);
  293. status = SPI.transfer(0);
  294. CSRELEASE();
  295. SPI.endTransaction();
  296. //Serial.printf("ready=%02x\n", status & 0xFF);
  297. if ((status & 1)) return false;
  298. }
  299. busy = 0;
  300. if (flags & 0xC0) {
  301. // continue a multi-die erase
  302. eraseAll();
  303. return false;
  304. }
  305. return true;
  306. }
  307. #define ID0_WINBOND 0xEF
  308. #define ID0_SPANSION 0x01
  309. #define ID0_MICRON 0x20
  310. #define ID0_MACRONIX 0xC2
  311. #define ID0_SST 0xBF
  312. //#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  313. //#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  314. //#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  315. //#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  316. bool SerialFlashChip::begin(uint8_t pin)
  317. {
  318. uint8_t id[5];
  319. uint8_t f;
  320. uint32_t size;
  321. cspin_basereg = PIN_TO_BASEREG(pin);
  322. cspin_bitmask = PIN_TO_BITMASK(pin);
  323. SPI.begin();
  324. pinMode(pin, OUTPUT);
  325. CSRELEASE();
  326. readID(id);
  327. f = 0;
  328. size = capacity(id);
  329. if (size > 16777216) {
  330. // more than 16 Mbyte requires 32 bit addresses
  331. f |= FLAG_32BIT_ADDR;
  332. SPI.beginTransaction(SPICONFIG);
  333. if (id[0] == ID0_SPANSION) {
  334. // spansion uses MSB of bank register
  335. CSASSERT();
  336. SPI.transfer16(0x1780); // bank register write
  337. CSRELEASE();
  338. } else {
  339. // micron & winbond & macronix use command
  340. CSASSERT();
  341. SPI.transfer(0x06); // write enable
  342. CSRELEASE();
  343. delayMicroseconds(1);
  344. CSASSERT();
  345. SPI.transfer(0xB7); // enter 4 byte addr mode
  346. CSRELEASE();
  347. }
  348. SPI.endTransaction();
  349. if (id[0] == ID0_MICRON) f |= FLAG_MULTI_DIE;
  350. }
  351. if (id[0] == ID0_SPANSION) {
  352. // Spansion has separate suspend commands
  353. f |= FLAG_DIFF_SUSPEND;
  354. if (!id[4]) {
  355. // Spansion chips with id[4] == 0 use 256K sectors
  356. f |= FLAG_256K_BLOCKS;
  357. }
  358. }
  359. if (id[0] == ID0_MICRON) {
  360. // Micron requires busy checks with a different command
  361. f |= FLAG_STATUS_CMD70; // TODO: all or just multi-die chips?
  362. }
  363. flags = f;
  364. readID(id);
  365. return true;
  366. }
  367. // chips tested: https://github.com/PaulStoffregen/SerialFlash/pull/12#issuecomment-169596992
  368. //
  369. void SerialFlashChip::sleep()
  370. {
  371. if (busy) wait();
  372. SPI.beginTransaction(SPICONFIG);
  373. CSASSERT();
  374. SPI.transfer(0xB9); // Deep power down command
  375. CSRELEASE();
  376. }
  377. void SerialFlashChip::wakeup()
  378. {
  379. SPI.beginTransaction(SPICONFIG);
  380. CSASSERT();
  381. SPI.transfer(0xAB); // Wake up from deep power down command
  382. CSRELEASE();
  383. }
  384. void SerialFlashChip::readID(uint8_t *buf)
  385. {
  386. if (busy) wait();
  387. SPI.beginTransaction(SPICONFIG);
  388. CSASSERT();
  389. SPI.transfer(0x9F);
  390. buf[0] = SPI.transfer(0); // manufacturer ID
  391. buf[1] = SPI.transfer(0); // memory type
  392. buf[2] = SPI.transfer(0); // capacity
  393. if (buf[0] == ID0_SPANSION) {
  394. buf[3] = SPI.transfer(0); // ID-CFI
  395. buf[4] = SPI.transfer(0); // sector size
  396. }
  397. CSRELEASE();
  398. SPI.endTransaction();
  399. //Serial.printf("ID: %02X %02X %02X\n", buf[0], buf[1], buf[2]);
  400. }
  401. void SerialFlashChip::readSerialNumber(uint8_t *buf) //needs room for 8 bytes
  402. {
  403. if (busy) wait();
  404. SPI.beginTransaction(SPICONFIG);
  405. CSASSERT();
  406. SPI.transfer(0x4B);
  407. SPI.transfer16(0);
  408. SPI.transfer16(0);
  409. for (int i=0; i<8; i++) {
  410. buf[i] = SPI.transfer(0);
  411. }
  412. CSRELEASE();
  413. SPI.endTransaction();
  414. // Serial.printf("Serial Number: %02X %02X %02X %02X %02X %02X %02X %02X\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  415. }
  416. uint32_t SerialFlashChip::capacity(const uint8_t *id)
  417. {
  418. uint32_t n = 1048576; // unknown chips, default to 1 MByte
  419. if (id[2] >= 16 && id[2] <= 31) {
  420. n = 1ul << id[2];
  421. } else
  422. if (id[2] >= 32 && id[2] <= 37) {
  423. n = 1ul << (id[2] - 6);
  424. }
  425. //Serial.printf("capacity %lu\n", n);
  426. return n;
  427. }
  428. uint32_t SerialFlashChip::blockSize()
  429. {
  430. // Spansion chips >= 512 mbit use 256K sectors
  431. if (flags & FLAG_256K_BLOCKS) return 262144;
  432. // everything else seems to have 64K sectors
  433. return 65536;
  434. }
  435. /*
  436. Chip Uniform Sector Erase
  437. 20/21 52 D8/DC
  438. ----- -- -----
  439. W25Q64CV 4 32 64
  440. W25Q128FV 4 32 64
  441. S25FL127S 64
  442. N25Q512A 4 64
  443. N25Q00AA 4 64
  444. S25FL512S 256
  445. SST26VF032 4
  446. */
  447. // size sector busy pgm/erase chip
  448. // Part Mbyte kbyte ID bytes cmd suspend erase
  449. // ---- ---- ----- -------- --- ------- -----
  450. // Winbond W25Q64CV 8 64 EF 40 17
  451. // Winbond W25Q128FV 16 64 EF 40 18 05 single 60 & C7
  452. // Winbond W25Q256FV 32 64 EF 40 19
  453. // Spansion S25FL064A 8 ? 01 02 16
  454. // Spansion S25FL127S 16 64 01 20 18 05
  455. // Spansion S25FL128P 16 64 01 20 18
  456. // Spansion S25FL256S 32 64 01 02 19 05 60 & C7
  457. // Spansion S25FL512S 64 256 01 02 20
  458. // Macronix MX25L12805D 16 ? C2 20 18
  459. // Macronix MX66L51235F 64 C2 20 1A
  460. // Numonyx M25P128 16 ? 20 20 18
  461. // Micron M25P80 1 ? 20 20 14
  462. // Micron N25Q128A 16 64 20 BA 18
  463. // Micron N25Q512A 64 ? 20 BA 20 70 single C4 x2
  464. // Micron N25Q00AA 128 64 20 BA 21 single C4 x4
  465. // Micron MT25QL02GC 256 64 20 BA 22 70 C4 x2
  466. // SST SST25WF010 1/8 ? BF 25 02
  467. // SST SST25WF020 1/4 ? BF 25 03
  468. // SST SST25WF040 1/2 ? BF 25 04
  469. // SST SST25VF016B 1 ? BF 25 41
  470. // SST26VF016 ? BF 26 01
  471. // SST26VF032 ? BF 26 02
  472. // SST25VF032 4 64 BF 25 4A
  473. // SST26VF064 8 ? BF 26 43
  474. // LE25U40CMC 1/2 64 62 06 13
  475. SerialFlashChip SerialFlash;