您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SerialFlash.h 3.8KB

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