選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

130 行
3.9KB

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