Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123 rindas
3.7KB

  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();
  37. static uint32_t blockSize();
  38. static void readID(uint8_t *buf);
  39. static void read(void *buf, uint32_t addr, uint32_t len);
  40. static bool ready();
  41. static void wait();
  42. static void write(const void *buf, uint32_t addr, 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 fourbytemode; // 0=use 24 bit address, 1=use 32 bit address
  55. static uint8_t busy; // 0 = ready, 1 = suspendable busy, 2 = busy for realz
  56. };
  57. extern SerialFlashChip SerialFlash;
  58. class SerialFlashFile
  59. {
  60. public:
  61. SerialFlashFile() : address(0) {
  62. }
  63. operator bool() {
  64. if (address > 0) return true;
  65. return false;
  66. }
  67. uint32_t read(uint8_t *buf, uint32_t rdlen) {
  68. if (offset + rdlen > length) {
  69. if (offset >= length) return 0;
  70. rdlen = length - offset;
  71. }
  72. SerialFlash.read(buf, address + offset, rdlen);
  73. offset += rdlen;
  74. return rdlen;
  75. }
  76. uint32_t write(const void *buf, uint32_t wrlen) {
  77. if (offset + wrlen > length) {
  78. if (offset >= length) return 0;
  79. wrlen = length - offset;
  80. }
  81. SerialFlash.write(buf, address + offset, wrlen);
  82. offset += wrlen;
  83. return wrlen;
  84. }
  85. void seek(uint32_t n) {
  86. offset = n;
  87. }
  88. uint32_t position() {
  89. return offset;
  90. }
  91. uint32_t size() {
  92. return length;
  93. }
  94. uint32_t available() {
  95. if (offset >= length) return 0;
  96. return length - offset;
  97. }
  98. void erase();
  99. void flush() {
  100. }
  101. void close() {
  102. }
  103. uint32_t getFlashAddress() {
  104. return address;
  105. }
  106. protected:
  107. friend class SerialFlashChip;
  108. uint32_t address; // where this file's data begins in the Flash, or zero
  109. uint32_t length; // total length of the data in the Flash chip
  110. uint32_t offset; // current read/write offset in the file
  111. };
  112. #endif