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.

SerialFlash.h 4.1KB

10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef SerialFlash_h_
  28. #define SerialFlash_h_
  29. #include <Arduino.h>
  30. #include <SPI.h>
  31. class SerialFlashFile;
  32. class SerialFlashChip
  33. {
  34. public:
  35. static bool begin(SPIClass& device, uint8_t pin = 6);
  36. static bool begin(uint8_t pin = 6);
  37. static uint32_t capacity(const uint8_t *id);
  38. static uint32_t blockSize();
  39. static void sleep();
  40. static void wakeup();
  41. static void readID(uint8_t *buf);
  42. static void readSerialNumber(uint8_t *buf);
  43. static void read(uint32_t addr, void *buf, uint32_t len);
  44. static bool ready();
  45. static void wait();
  46. static void write(uint32_t addr, const void *buf, uint32_t len);
  47. static void eraseAll();
  48. static void eraseBlock(uint32_t addr);
  49. static SerialFlashFile open(const char *filename);
  50. static bool create(const char *filename, uint32_t length, uint32_t align = 0);
  51. static bool createErasable(const char *filename, uint32_t length) {
  52. return create(filename, length, blockSize());
  53. }
  54. static bool exists(const char *filename);
  55. static bool remove(const char *filename);
  56. static bool remove(SerialFlashFile &file);
  57. static void opendir() { dirindex = 0; }
  58. static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
  59. private:
  60. static uint16_t dirindex; // current position for readdir()
  61. static uint8_t flags; // chip features
  62. static uint8_t busy; // 0 = ready
  63. // 1 = suspendable program operation
  64. // 2 = suspendable erase operation
  65. // 3 = busy for realz!!
  66. };
  67. extern SerialFlashChip SerialFlash;
  68. class SerialFlashFile
  69. {
  70. public:
  71. constexpr SerialFlashFile() { }
  72. operator bool() {
  73. if (address > 0) return true;
  74. return false;
  75. }
  76. uint32_t read(void *buf, uint32_t rdlen) {
  77. if (offset + rdlen > length) {
  78. if (offset >= length) return 0;
  79. rdlen = length - offset;
  80. }
  81. SerialFlash.read(address + offset, buf, rdlen);
  82. offset += rdlen;
  83. return rdlen;
  84. }
  85. uint32_t write(const void *buf, uint32_t wrlen) {
  86. if (offset + wrlen > length) {
  87. if (offset >= length) return 0;
  88. wrlen = length - offset;
  89. }
  90. SerialFlash.write(address + offset, buf, wrlen);
  91. offset += wrlen;
  92. return wrlen;
  93. }
  94. void seek(uint32_t n) {
  95. offset = n;
  96. }
  97. uint32_t position() {
  98. return offset;
  99. }
  100. uint32_t size() {
  101. return length;
  102. }
  103. uint32_t available() {
  104. if (offset >= length) return 0;
  105. return length - offset;
  106. }
  107. void erase();
  108. void flush() {
  109. }
  110. void close() {
  111. }
  112. uint32_t getFlashAddress() {
  113. return address;
  114. }
  115. protected:
  116. friend class SerialFlashChip;
  117. uint32_t address = 0; // where this file's data begins in the Flash, or zero
  118. uint32_t length = 0; // total length of the data in the Flash chip
  119. uint32_t offset = 0; // current read/write offset in the file
  120. uint16_t dirindex = 0;
  121. };
  122. #endif