Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SerialFlash.h 4.1KB

9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. SerialFlashFile() : address(0) {
  72. }
  73. operator bool() {
  74. if (address > 0) return true;
  75. return false;
  76. }
  77. uint32_t read(void *buf, uint32_t rdlen) {
  78. if (offset + rdlen > length) {
  79. if (offset >= length) return 0;
  80. rdlen = length - offset;
  81. }
  82. SerialFlash.read(address + offset, buf, rdlen);
  83. offset += rdlen;
  84. return rdlen;
  85. }
  86. uint32_t write(const void *buf, uint32_t wrlen) {
  87. if (offset + wrlen > length) {
  88. if (offset >= length) return 0;
  89. wrlen = length - offset;
  90. }
  91. SerialFlash.write(address + offset, buf, wrlen);
  92. offset += wrlen;
  93. return wrlen;
  94. }
  95. void seek(uint32_t n) {
  96. offset = n;
  97. }
  98. uint32_t position() {
  99. return offset;
  100. }
  101. uint32_t size() {
  102. return length;
  103. }
  104. uint32_t available() {
  105. if (offset >= length) return 0;
  106. return length - offset;
  107. }
  108. void erase();
  109. void flush() {
  110. }
  111. void close() {
  112. }
  113. uint32_t getFlashAddress() {
  114. return address;
  115. }
  116. protected:
  117. friend class SerialFlashChip;
  118. uint32_t address; // where this file's data begins in the Flash, or zero
  119. uint32_t length; // total length of the data in the Flash chip
  120. uint32_t offset; // current read/write offset in the file
  121. uint16_t dirindex;
  122. };
  123. #endif