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.

469 líneas
13KB

  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. #define CSCONFIG() pinMode(6, OUTPUT)
  29. #define CSASSERT() digitalWriteFast(6, LOW)
  30. #define CSRELEASE() digitalWriteFast(6, HIGH)
  31. #define SPICONFIG SPISettings(50000000, MSBFIRST, SPI_MODE0)
  32. #if !defined(__arm__) || !defined(CORE_TEENSY)
  33. #define digitalWriteFast(pin, state) digitalWrite((pin), (state))
  34. #endif
  35. uint16_t SerialFlashChip::dirindex = 0;
  36. uint8_t SerialFlashChip::flags = 0;
  37. uint8_t SerialFlashChip::busy = 0;
  38. #define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  39. #define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  40. #define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  41. #define FLAG_MULTI_DIE 0x08 // multiple die, don't read cross 32M barrier
  42. #define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  43. #define FLAG_DIE_MASK 0xC0 // top 2 bits count during multi-die erase
  44. void SerialFlashChip::wait(void)
  45. {
  46. uint32_t status;
  47. //Serial.print("wait-");
  48. while (1) {
  49. SPI.beginTransaction(SPICONFIG);
  50. CSASSERT();
  51. if (flags & FLAG_STATUS_CMD70) {
  52. // some Micron chips require this different
  53. // command to detect program and erase completion
  54. SPI.transfer(0x70);
  55. status = SPI.transfer(0);
  56. CSRELEASE();
  57. SPI.endTransaction();
  58. //Serial.printf("b=%02x.", status & 0xFF);
  59. if ((status & 0x80)) break;
  60. } else {
  61. // all others work by simply reading the status reg
  62. SPI.transfer(0x05);
  63. status = SPI.transfer(0);
  64. CSRELEASE();
  65. SPI.endTransaction();
  66. //Serial.printf("b=%02x.", status & 0xFF);
  67. if (!(status & 1)) break;
  68. }
  69. }
  70. busy = 0;
  71. //Serial.println();
  72. }
  73. void SerialFlashChip::read(uint32_t addr, void *buf, uint32_t len)
  74. {
  75. uint8_t *p = (uint8_t *)buf;
  76. uint8_t b, f, status, cmd;
  77. memset(p, 0, len);
  78. f = flags;
  79. SPI.beginTransaction(SPICONFIG);
  80. b = busy;
  81. if (b) {
  82. // read status register ... chip may no longer be busy
  83. CSASSERT();
  84. if (flags & FLAG_STATUS_CMD70) {
  85. SPI.transfer(0x70);
  86. status = SPI.transfer(0);
  87. if ((status & 0x80)) b = 0;
  88. } else {
  89. SPI.transfer(0x05);
  90. status = SPI.transfer(0);
  91. if (!(status & 1)) b = 0;
  92. }
  93. CSRELEASE();
  94. if (b == 0) {
  95. // chip is no longer busy :-)
  96. busy = 0;
  97. } else if (b < 3) {
  98. // TODO: this may not work on Spansion chips
  99. // which apparently have 2 different suspend
  100. // commands, for program vs erase
  101. CSASSERT();
  102. SPI.transfer(0x06); // write enable (Micron req'd)
  103. CSRELEASE();
  104. delayMicroseconds(1);
  105. cmd = 0x75; //Suspend program/erase for almost all chips
  106. // but Spansion just has to be different for program suspend!
  107. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x85;
  108. CSASSERT();
  109. SPI.transfer(cmd); // Suspend command
  110. CSRELEASE();
  111. if (f & FLAG_STATUS_CMD70) {
  112. // Micron chips don't actually suspend until flags read
  113. CSASSERT();
  114. SPI.transfer(0x70);
  115. do {
  116. status = SPI.transfer(0);
  117. } while (!(status & 0x80));
  118. CSRELEASE();
  119. } else {
  120. CSASSERT();
  121. SPI.transfer(0x05);
  122. do {
  123. status = SPI.transfer(0);
  124. } while ((status & 0x01));
  125. CSRELEASE();
  126. }
  127. } else {
  128. // chip is busy with an operation that can not suspend
  129. SPI.endTransaction(); // is this a good idea?
  130. wait(); // should we wait without ending
  131. b = 0; // the transaction??
  132. SPI.beginTransaction(SPICONFIG);
  133. }
  134. }
  135. do {
  136. uint32_t rdlen = len;
  137. if (f & FLAG_MULTI_DIE) {
  138. if ((addr & 0xFE000000) != ((addr + len - 1) & 0xFE000000)) {
  139. rdlen = 0x2000000 - (addr & 0x1FFFFFF);
  140. }
  141. }
  142. CSASSERT();
  143. // TODO: FIFO optimize....
  144. if (f & FLAG_32BIT_ADDR) {
  145. SPI.transfer(0x03);
  146. SPI.transfer16(addr >> 16);
  147. SPI.transfer16(addr);
  148. } else {
  149. SPI.transfer16(0x0300 | ((addr >> 16) & 255));
  150. SPI.transfer16(addr);
  151. }
  152. SPI.transfer(p, rdlen);
  153. CSRELEASE();
  154. p += rdlen;
  155. addr += rdlen;
  156. len -= rdlen;
  157. } while (len > 0);
  158. if (b) {
  159. CSASSERT();
  160. SPI.transfer(0x06); // write enable (Micron req'd)
  161. CSRELEASE();
  162. delayMicroseconds(1);
  163. cmd = 0x7A;
  164. if ((f & FLAG_DIFF_SUSPEND) && (b == 1)) cmd = 0x8A;
  165. CSASSERT();
  166. SPI.transfer(cmd); // Resume program/erase
  167. CSRELEASE();
  168. }
  169. SPI.endTransaction();
  170. }
  171. void SerialFlashChip::write(uint32_t addr, const void *buf, uint32_t len)
  172. {
  173. const uint8_t *p = (const uint8_t *)buf;
  174. uint32_t max, pagelen;
  175. //Serial.printf("WR: addr %08X, len %d\n", addr, len);
  176. do {
  177. if (busy) wait();
  178. SPI.beginTransaction(SPICONFIG);
  179. CSASSERT();
  180. // write enable command
  181. SPI.transfer(0x06);
  182. CSRELEASE();
  183. max = 256 - (addr & 0xFF);
  184. pagelen = (len <= max) ? len : max;
  185. //Serial.printf("WR: addr %08X, pagelen %d\n", addr, pagelen);
  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 = 1;
  202. SPI.endTransaction();
  203. } while (len > 0);
  204. }
  205. void SerialFlashChip::eraseAll()
  206. {
  207. if (busy) wait();
  208. uint8_t id[3];
  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()
  317. {
  318. uint8_t id[3];
  319. uint8_t f;
  320. uint32_t size;
  321. SPI.begin();
  322. CSCONFIG();
  323. CSRELEASE();
  324. readID(id);
  325. f = 0;
  326. size = capacity(id);
  327. if (size > 16777216) {
  328. // more than 16 Mbyte requires 32 bit addresses
  329. f |= FLAG_32BIT_ADDR;
  330. SPI.beginTransaction(SPICONFIG);
  331. if (id[0] == ID0_SPANSION) {
  332. // spansion uses MSB of bank register
  333. CSASSERT();
  334. SPI.transfer16(0x1780); // bank register write
  335. CSRELEASE();
  336. } else {
  337. // micron & winbond & macronix use command
  338. CSASSERT();
  339. SPI.transfer(0x06); // write enable
  340. CSRELEASE();
  341. delayMicroseconds(1);
  342. CSASSERT();
  343. SPI.transfer(0xB7); // enter 4 byte addr mode
  344. CSRELEASE();
  345. }
  346. SPI.endTransaction();
  347. if (id[0] == ID0_MICRON) f |= FLAG_MULTI_DIE;
  348. }
  349. if (id[0] == ID0_SPANSION) {
  350. // Spansion has separate suspend commands
  351. f |= FLAG_DIFF_SUSPEND;
  352. if (size >= 67108864) {
  353. // Spansion chips >= 512 mbit use 256K sectors
  354. f |= FLAG_256K_BLOCKS;
  355. }
  356. }
  357. if (id[0] == ID0_MICRON) {
  358. // Micron requires busy checks with a different command
  359. f |= FLAG_STATUS_CMD70; // TODO: all or just multi-die chips?
  360. }
  361. flags = f;
  362. readID(id);
  363. return true;
  364. }
  365. void SerialFlashChip::readID(uint8_t *buf)
  366. {
  367. if (busy) wait();
  368. SPI.beginTransaction(SPICONFIG);
  369. CSASSERT();
  370. SPI.transfer(0x9F);
  371. buf[0] = SPI.transfer(0); // manufacturer ID
  372. buf[1] = SPI.transfer(0); // memory type
  373. buf[2] = SPI.transfer(0); // capacity
  374. CSRELEASE();
  375. SPI.endTransaction();
  376. //Serial.printf("ID: %02X %02X %02X\n", buf[0], buf[1], buf[2]);
  377. }
  378. uint32_t SerialFlashChip::capacity(const uint8_t *id)
  379. {
  380. uint32_t n = 1048576; // unknown chips, default to 1 MByte
  381. if (id[2] >= 16 && id[2] <= 31) {
  382. n = 1ul << id[2];
  383. } else
  384. if (id[2] >= 32 && id[2] <= 37) {
  385. n = 1ul << (id[2] - 6);
  386. }
  387. //Serial.printf("capacity %lu\n", n);
  388. return n;
  389. }
  390. uint32_t SerialFlashChip::blockSize()
  391. {
  392. // Spansion chips >= 512 mbit use 256K sectors
  393. if (flags & FLAG_256K_BLOCKS) return 262144;
  394. // everything else seems to have 64K sectors
  395. return 65536;
  396. }
  397. /*
  398. Chip Uniform Sector Erase
  399. 20/21 52 D8/DC
  400. ----- -- -----
  401. W25Q64CV 4 32 64
  402. W25Q128FV 4 32 64
  403. S25FL127S 64
  404. N25Q512A 4 64
  405. N25Q00AA 4 64
  406. S25FL512S 256
  407. SST26VF032 4
  408. */
  409. // size sector busy pgm/erase chip
  410. // Part Mbyte kbyte ID bytes cmd suspend erase
  411. // ---- ---- ----- -------- --- ------- -----
  412. // Winbond W25Q64CV 8 64 EF 40 17
  413. // Winbond W25Q128FV 16 64 EF 40 18 05 single 60 & C7
  414. // Winbond W25Q256FV 32 64 EF 40 19
  415. // Spansion S25FL064A 8 ? 01 02 16
  416. // Spansion S25FL127S 16 64 01 20 18 05
  417. // Spansion S25FL128P 16 64 01 20 18
  418. // Spansion S25FL256S 32 64 01 02 19 05 60 & C7
  419. // Spansion S25FL512S 64 256 01 02 20
  420. // Macronix MX25L12805D 16 ? C2 20 18
  421. // Macronix MX66L51235F 64 C2 20 1A
  422. // Numonyx M25P128 16 ? 20 20 18
  423. // Micron M25P80 1 ? 20 20 14
  424. // Micron N25Q128A 16 64 20 BA 18
  425. // Micron N25Q512A 64 ? 20 BA 20 70 single C4 x2
  426. // Micron N25Q00AA 128 64 20 BA 21 single C4 x4
  427. // Micron MT25QL02GC 256 64 20 BA 22 70 C4 x2
  428. // SST SST25WF010 1/8 ? BF 25 02
  429. // SST SST25WF020 1/4 ? BF 25 03
  430. // SST SST25WF040 1/2 ? BF 25 04
  431. // SST SST25VF016B 1 ? BF 25 41
  432. // SST26VF016 ? BF 26 01
  433. // SST26VF032 ? BF 26 02
  434. // SST25VF032 4 64 BF 25 4A
  435. // SST26VF064 8 ? BF 26 43
  436. // LE25U40CMC 1/2 64 62 06 13
  437. SerialFlashChip SerialFlash;