PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
  2. // SPI Flash memory library for arduino/moteino.
  3. // This works with 256byte/page SPI flash memory
  4. // For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes)
  5. // Minimal modifications should allow chips that have different page size but modifications
  6. // DEPENDS ON: Arduino SPI library
  7. // > Updated Jan. 5, 2015, TomWS1, modified writeBytes to allow blocks > 256 bytes and handle page misalignment.
  8. // > Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above)
  9. // > Selective merge by Felix after testing in IDE 1.0.6, 1.6.4
  10. // **********************************************************************************
  11. // License
  12. // **********************************************************************************
  13. // This program is free software; you can redistribute it
  14. // and/or modify it under the terms of the GNU General
  15. // Public License as published by the Free Software
  16. // Foundation; either version 3 of the License, or
  17. // (at your option) any later version.
  18. //
  19. // This program is distributed in the hope that it will
  20. // be useful, but WITHOUT ANY WARRANTY; without even the
  21. // implied warranty of MERCHANTABILITY or FITNESS FOR A
  22. // PARTICULAR PURPOSE. See the GNU General Public
  23. // License for more details.
  24. //
  25. // You should have received a copy of the GNU General
  26. // Public License along with this program.
  27. // If not, see <http://www.gnu.org/licenses/>.
  28. //
  29. // Licence can be viewed at
  30. // http://www.gnu.org/licenses/gpl-3.0.txt
  31. //
  32. // Please maintain this license information along with authorship
  33. // and copyright notices in any redistribution of this code
  34. #ifndef _SPIFLASH_H_
  35. #define _SPIFLASH_H_
  36. #if ARDUINO >= 100
  37. #include <Arduino.h>
  38. #else
  39. #include <wiring.h>
  40. #include "pins_arduino.h"
  41. #endif
  42. #include <SPI.h>
  43. /// IMPORTANT: NAND FLASH memory requires erase before write, because
  44. /// it can only transition from 1s to 0s and only the erase command can reset all 0s to 1s
  45. /// See http://en.wikipedia.org/wiki/Flash_memory
  46. /// The smallest range that can be erased is a sector (4K, 32K, 64K); there is also a chip erase command
  47. /// Standard SPI flash commands
  48. /// Assuming the WP pin is pulled up (to disable hardware write protection)
  49. /// To use any write commands the WEL bit in the status register must be set to 1.
  50. /// This is accomplished by sending a 0x06 command before any such write/erase command.
  51. /// The WEL bit in the status register resets to the logical 0 state after a
  52. /// device power-up or reset. In addition, the WEL bit will be reset to the logical 0 state automatically under the following conditions:
  53. /// Write Disable operation completes successfully
  54. /// Write Status Register operation completes successfully or aborts
  55. /// Protect Sector operation completes successfully or aborts
  56. /// Unprotect Sector operation completes successfully or aborts
  57. /// Byte/Page Program operation completes successfully or aborts
  58. /// Sequential Program Mode reaches highest unprotected memory location
  59. /// Sequential Program Mode reaches the end of the memory array
  60. /// Sequential Program Mode aborts
  61. /// Block Erase operation completes successfully or aborts
  62. /// Chip Erase operation completes successfully or aborts
  63. /// Hold condition aborts
  64. #define SPIFLASH_WRITEENABLE 0x06 // write enable
  65. #define SPIFLASH_WRITEDISABLE 0x04 // write disable
  66. #define SPIFLASH_BLOCKERASE_4K 0x20 // erase one 4K block of flash memory
  67. #define SPIFLASH_BLOCKERASE_32K 0x52 // erase one 32K block of flash memory
  68. #define SPIFLASH_BLOCKERASE_64K 0xD8 // erase one 64K block of flash memory
  69. #define SPIFLASH_CHIPERASE 0x60 // chip erase (may take several seconds depending on size)
  70. // but no actual need to wait for completion (instead need to check the status register BUSY bit)
  71. #define SPIFLASH_STATUSREAD 0x05 // read status register
  72. #define SPIFLASH_STATUSWRITE 0x01 // write status register
  73. #define SPIFLASH_ARRAYREAD 0x0B // read array (fast, need to add 1 dummy byte after 3 address bytes)
  74. #define SPIFLASH_ARRAYREADLOWFREQ 0x03 // read array (low frequency)
  75. #define SPIFLASH_SLEEP 0xB9 // deep power down
  76. #define SPIFLASH_WAKE 0xAB // deep power wake up
  77. #define SPIFLASH_BYTEPAGEPROGRAM 0x02 // write (1 to 256bytes)
  78. #define SPIFLASH_IDREAD 0x9F // read JEDEC manufacturer and device ID (2 bytes, specific bytes for each manufacturer and device)
  79. // Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf)
  80. // Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf)
  81. #define SPIFLASH_MACREAD 0x4B // read unique ID number (MAC)
  82. class SPIFlash {
  83. public:
  84. static uint8_t UNIQUEID[8];
  85. SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0);
  86. boolean initialize();
  87. void command(uint8_t cmd, boolean isWrite=false);
  88. uint8_t readStatus();
  89. uint8_t readByte(uint32_t addr);
  90. void readBytes(uint32_t addr, void* buf, uint16_t len);
  91. void writeByte(uint32_t addr, uint8_t byt);
  92. void writeBytes(uint32_t addr, const void* buf, uint16_t len);
  93. boolean busy();
  94. void chipErase();
  95. void blockErase4K(uint32_t address);
  96. void blockErase32K(uint32_t address);
  97. void blockErase64K(uint32_t addr);
  98. uint16_t readDeviceId();
  99. uint8_t* readUniqueId();
  100. void sleep();
  101. void wakeup();
  102. void end();
  103. protected:
  104. void select();
  105. void unselect();
  106. uint8_t _slaveSelectPin;
  107. uint16_t _jedecID;
  108. uint8_t _SPCR;
  109. uint8_t _SPSR;
  110. #ifdef SPI_HAS_TRANSACTION
  111. SPISettings _settings;
  112. #endif
  113. };
  114. #endif