Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

133 lines
4.0KB

  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(uint8_t pin = 6);
  36. static uint32_t capacity(const uint8_t *id);
  37. static uint32_t blockSize();
  38. static void sleep();
  39. static void wakeup();
  40. static void readID(uint8_t *buf);
  41. static void readSerialNumber(uint8_t *buf);
  42. static void read(uint32_t addr, void *buf, uint32_t len);
  43. static bool ready();
  44. static void wait();
  45. static void write(uint32_t addr, const void *buf, uint32_t len);
  46. static void eraseAll();
  47. static void eraseBlock(uint32_t addr);
  48. static SerialFlashFile open(const char *filename);
  49. static bool create(const char *filename, uint32_t length, uint32_t align = 0);
  50. static bool createErasable(const char *filename, uint32_t length) {
  51. return create(filename, length, blockSize());
  52. }
  53. static bool exists(const char *filename);
  54. static bool remove(const char *filename);
  55. static bool remove(SerialFlashFile &file);
  56. static void opendir() { dirindex = 0; }
  57. static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
  58. private:
  59. static uint16_t dirindex; // current position for readdir()
  60. static uint8_t flags; // chip features
  61. static uint8_t busy; // 0 = ready
  62. // 1 = suspendable program operation
  63. // 2 = suspendable erase operation
  64. // 3 = busy for realz!!
  65. };
  66. extern SerialFlashChip SerialFlash;
  67. class SerialFlashFile
  68. {
  69. public:
  70. SerialFlashFile() : address(0) {
  71. }
  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; // where this file's data begins in the Flash, or zero
  118. uint32_t length; // total length of the data in the Flash chip
  119. uint32_t offset; // current read/write offset in the file
  120. uint16_t dirindex;
  121. };
  122. #endif