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ů.

SerialFlash.h 4.0KB

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