Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

429 Zeilen
11KB

  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;
  77. memset(p, 0, len);
  78. f = flags;
  79. b = busy;
  80. if (b) {
  81. if (b < 3) {
  82. // TODO: this may not work on Spansion chips
  83. // which apparently have 2 different suspend
  84. // commands, for program vs erase
  85. SPI.beginTransaction(SPICONFIG);
  86. CSASSERT();
  87. SPI.transfer(0x75); // Suspend program/erase
  88. CSRELEASE();
  89. SPI.endTransaction();
  90. delayMicroseconds(20); // Tsus = 20us
  91. } else {
  92. wait();
  93. b = 0;
  94. }
  95. }
  96. SPI.beginTransaction(SPICONFIG);
  97. do {
  98. uint32_t rdlen = len;
  99. if (f & FLAG_MULTI_DIE) {
  100. if ((addr & 0xFE000000) != ((addr + len - 1) & 0xFE000000)) {
  101. rdlen = 0x2000000 - (addr & 0x1FFFFFF);
  102. }
  103. }
  104. CSASSERT();
  105. // TODO: FIFO optimize....
  106. if (f & FLAG_32BIT_ADDR) {
  107. SPI.transfer(0x03);
  108. SPI.transfer16(addr >> 16);
  109. SPI.transfer16(addr);
  110. } else {
  111. SPI.transfer16(0x0300 | ((addr >> 16) & 255));
  112. SPI.transfer16(addr);
  113. }
  114. SPI.transfer(p, rdlen);
  115. CSRELEASE();
  116. p += rdlen;
  117. addr += rdlen;
  118. len -= rdlen;
  119. } while (len > 0);
  120. SPI.endTransaction();
  121. if (b) {
  122. SPI.beginTransaction(SPICONFIG);
  123. CSASSERT();
  124. SPI.transfer(0x7A); // Resume program/erase
  125. CSRELEASE();
  126. SPI.endTransaction();
  127. }
  128. }
  129. void SerialFlashChip::write(uint32_t addr, const void *buf, uint32_t len)
  130. {
  131. const uint8_t *p = (const uint8_t *)buf;
  132. uint32_t max, pagelen;
  133. //Serial.println("write");
  134. do {
  135. if (busy) wait();
  136. //Serial.printf("pagelen=%d\n", pagelen);
  137. SPI.beginTransaction(SPICONFIG);
  138. CSASSERT();
  139. // write enable command
  140. SPI.transfer(0x06);
  141. CSRELEASE();
  142. max = 256 - (addr & 0xFF);
  143. pagelen = (len <= max) ? len : max;
  144. len -= pagelen;
  145. CSASSERT();
  146. if (flags & FLAG_32BIT_ADDR) {
  147. //Serial.printf("write 32 bit addr %08X %02X\n", addr, addr >> 24);
  148. SPI.transfer(0x02);
  149. SPI.transfer16(addr >> 16);
  150. SPI.transfer16(addr);
  151. } else {
  152. SPI.transfer16(0x0200 | ((addr >> 16) & 255));
  153. SPI.transfer16(addr);
  154. }
  155. // program page command
  156. do {
  157. SPI.transfer(*p++);
  158. } while (--pagelen > 0);
  159. CSRELEASE();
  160. SPI.endTransaction();
  161. busy = 1;
  162. //Serial.printf("busy=%d\n", busy);
  163. } while (len > 0);
  164. }
  165. void SerialFlashChip::eraseAll()
  166. {
  167. if (busy) wait();
  168. uint8_t id[3];
  169. readID(id);
  170. //Serial.printf("ID: %02X %02X %02X\n", id[0], id[1], id[2]);
  171. if (id[0] == 0x20 && id[2] >= 0x20 && id[2] <= 0x22) {
  172. // Micron's multi-die chips require special die erase commands
  173. // N25Q512A 20 BA 20 2 dies 32 Mbyte/die 65 nm transitors
  174. // N25Q00AA 20 BA 21 4 dies 32 Mbyte/die 65 nm transitors
  175. // MT25QL02GC 20 BA 22 2 dies 128 Mbyte/die 45 nm transitors
  176. uint8_t die_count = 2;
  177. if (id[2] == 0x21) die_count = 4;
  178. uint8_t die_index = flags >> 6;
  179. //Serial.printf("Micron die erase %d\n", die_index);
  180. flags &= 0x3F;
  181. if (die_index >= die_count) return; // all dies erased :-)
  182. uint8_t die_size = 2; // in 16 Mbyte units
  183. if (id[2] == 0x22) die_size = 8;
  184. SPI.beginTransaction(SPICONFIG);
  185. CSASSERT();
  186. SPI.transfer(0x06); // write enable command
  187. CSRELEASE();
  188. delayMicroseconds(1);
  189. CSASSERT();
  190. // die erase command
  191. SPI.transfer(0xC4);
  192. SPI.transfer16((die_index * die_size) << 8);
  193. SPI.transfer16(0x0000);
  194. CSRELEASE();
  195. //Serial.printf("Micron erase begin\n");
  196. flags |= (die_index + 1) << 6;
  197. } else {
  198. // All other chips support the bulk erase command
  199. SPI.beginTransaction(SPICONFIG);
  200. CSASSERT();
  201. // write enable command
  202. SPI.transfer(0x06);
  203. CSRELEASE();
  204. delayMicroseconds(1);
  205. CSASSERT();
  206. // bulk erase command
  207. SPI.transfer(0xC7);
  208. CSRELEASE();
  209. SPI.endTransaction();
  210. }
  211. busy = 3;
  212. }
  213. void SerialFlashChip::eraseBlock(uint32_t addr)
  214. {
  215. uint8_t f = flags;
  216. if (busy) wait();
  217. SPI.beginTransaction(SPICONFIG);
  218. CSASSERT();
  219. SPI.transfer(0x06); // write enable command
  220. CSRELEASE();
  221. delayMicroseconds(1);
  222. CSASSERT();
  223. if (f & FLAG_32BIT_ADDR) {
  224. SPI.transfer(0xD8);
  225. SPI.transfer16(addr >> 16);
  226. SPI.transfer16(addr);
  227. } else {
  228. SPI.transfer16(0xD800 | ((addr >> 16) & 255));
  229. SPI.transfer16(addr);
  230. }
  231. CSRELEASE();
  232. SPI.endTransaction();
  233. busy = 2;
  234. }
  235. bool SerialFlashChip::ready()
  236. {
  237. uint32_t status;
  238. if (!busy) return true;
  239. SPI.beginTransaction(SPICONFIG);
  240. CSASSERT();
  241. if (flags & FLAG_STATUS_CMD70) {
  242. // some Micron chips require this different
  243. // command to detect program and erase completion
  244. SPI.transfer(0x70);
  245. status = SPI.transfer(0);
  246. CSRELEASE();
  247. SPI.endTransaction();
  248. //Serial.printf("ready=%02x\n", status & 0xFF);
  249. if ((status & 0x80) == 0) return false;
  250. } else {
  251. // all others work by simply reading the status reg
  252. SPI.transfer(0x05);
  253. status = SPI.transfer(0);
  254. CSRELEASE();
  255. SPI.endTransaction();
  256. //Serial.printf("ready=%02x\n", status & 0xFF);
  257. if ((status & 1)) return false;
  258. }
  259. busy = 0;
  260. if (flags & 0xC0) {
  261. // continue a multi-die erase
  262. eraseAll();
  263. return false;
  264. }
  265. return true;
  266. }
  267. #define ID0_WINBOND 0xEF
  268. #define ID0_SPANSION 0x01
  269. #define ID0_MICRON 0x20
  270. #define ID0_MACRONIX 0xC2
  271. #define ID0_SST 0xBF
  272. //#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
  273. //#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
  274. //#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
  275. //#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
  276. bool SerialFlashChip::begin()
  277. {
  278. uint8_t id[3];
  279. uint8_t f;
  280. uint32_t size;
  281. SPI.begin();
  282. CSCONFIG();
  283. CSRELEASE();
  284. readID(id);
  285. f = 0;
  286. size = capacity(id);
  287. if (size > 16777216) {
  288. // more than 16 Mbyte requires 32 bit addresses
  289. f |= FLAG_32BIT_ADDR;
  290. SPI.beginTransaction(SPICONFIG);
  291. if (id[0] == ID0_SPANSION) {
  292. // spansion uses MSB of bank register
  293. CSASSERT();
  294. SPI.transfer16(0x1780); // bank register write
  295. CSRELEASE();
  296. } else {
  297. // micron & winbond & macronix use command
  298. CSASSERT();
  299. SPI.transfer(0x06); // write enable
  300. CSRELEASE();
  301. delayMicroseconds(1);
  302. CSASSERT();
  303. SPI.transfer(0xB7); // enter 4 byte addr mode
  304. CSRELEASE();
  305. }
  306. SPI.endTransaction();
  307. if (id[0] == ID0_MICRON) f |= FLAG_MULTI_DIE;
  308. }
  309. if (id[0] == ID0_SPANSION) {
  310. // Spansion has separate suspend commands
  311. f |= FLAG_DIFF_SUSPEND;
  312. if (size >= 67108864) {
  313. // Spansion chips >= 512 mbit use 256K sectors
  314. f |= FLAG_256K_BLOCKS;
  315. }
  316. }
  317. if (id[0] == ID0_MICRON) {
  318. // Micron requires busy checks with a different command
  319. f |= FLAG_STATUS_CMD70; // TODO: all or just multi-die chips?
  320. }
  321. flags = f;
  322. readID(id);
  323. return true;
  324. }
  325. void SerialFlashChip::readID(uint8_t *buf)
  326. {
  327. if (busy) wait();
  328. SPI.beginTransaction(SPICONFIG);
  329. CSASSERT();
  330. SPI.transfer(0x9F);
  331. buf[0] = SPI.transfer(0); // manufacturer ID
  332. buf[1] = SPI.transfer(0); // memory type
  333. buf[2] = SPI.transfer(0); // capacity
  334. CSRELEASE();
  335. SPI.endTransaction();
  336. //Serial.printf("ID: %02X %02X %02X\n", buf[0], buf[1], buf[2]);
  337. }
  338. uint32_t SerialFlashChip::capacity(const uint8_t *id)
  339. {
  340. uint32_t n = 1048576; // unknown chips, default to 1 MByte
  341. if (id[2] >= 16 && id[2] <= 31) {
  342. n = 1ul << id[2];
  343. } else
  344. if (id[2] >= 32 && id[2] <= 37) {
  345. n = 1ul << (id[2] - 6);
  346. }
  347. //Serial.printf("capacity %lu\n", n);
  348. return n;
  349. }
  350. uint32_t SerialFlashChip::blockSize()
  351. {
  352. // Spansion chips >= 512 mbit use 256K sectors
  353. if (flags & FLAG_256K_BLOCKS) return 262144;
  354. // everything else seems to have 64K sectors
  355. return 65536;
  356. }
  357. /*
  358. Chip Uniform Sector Erase
  359. 20/21 52 D8/DC
  360. ----- -- -----
  361. W25Q64CV 4 32 64
  362. W25Q128FV 4 32 64
  363. S25FL127S 64
  364. N25Q512A 4 64
  365. N25Q00AA 4 64
  366. S25FL512S 256
  367. SST26VF032 4
  368. */
  369. // size sector busy pgm/erase chip
  370. // Part Mbyte kbyte ID bytes cmd suspend erase
  371. // ---- ---- ----- -------- --- ------- -----
  372. // Winbond W25Q64CV 8 64 EF 40 17
  373. // Winbond W25Q128FV 16 64 EF 40 18 05 single 60 & C7
  374. // Winbond W25Q256FV 32 64 EF 40 19
  375. // Spansion S25FL064A 8 ? 01 02 16
  376. // Spansion S25FL127S 16 64 01 20 18 05
  377. // Spansion S25FL128P 16 64 01 20 18
  378. // Spansion S25FL256S 32 64 01 02 19 05 60 & C7
  379. // Spansion S25FL512S 64 256 01 02 20
  380. // Macronix MX25L12805D 16 ? C2 20 18
  381. // Macronix MX66L51235F 64 C2 20 1A
  382. // Numonyx M25P128 16 ? 20 20 18
  383. // Micron M25P80 1 ? 20 20 14
  384. // Micron N25Q128A 16 64 20 BA 18
  385. // Micron N25Q512A 64 ? 20 BA 20 70 single C4 x2
  386. // Micron N25Q00AA 128 64 20 BA 21 single C4 x4
  387. // Micron MT25QL02GC 256 64 20 BA 22 70 C4 x2
  388. // SST SST25WF010 1/8 ? BF 25 02
  389. // SST SST25WF020 1/4 ? BF 25 03
  390. // SST SST25WF040 1/2 ? BF 25 04
  391. // SST SST25VF016B 1 ? BF 25 41
  392. // SST26VF016 ? BF 26 01
  393. // SST26VF032 ? BF 26 02
  394. // SST25VF032 4 64 BF 25 4A
  395. // SST26VF064 8 ? BF 26 43
  396. // LE25U40CMC 1/2 64 62 06 13
  397. SerialFlashChip SerialFlash;