Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SerialFlash.h 2.3KB

9 år sedan
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef SerialFlash_h_
  2. #define SerialFlash_h_
  3. #include <Arduino.h>
  4. #include <SPI.h>
  5. class SerialFlashFile;
  6. class SerialFlashChip
  7. {
  8. public:
  9. static bool begin();
  10. static uint32_t capacity();
  11. static uint32_t blockSize();
  12. static void read(void *buf, uint32_t addr, uint32_t len);
  13. static bool ready();
  14. static void wait();
  15. static void write(const void *buf, uint32_t addr, uint32_t len);
  16. static void eraseAll();
  17. static SerialFlashFile open(const char *filename);
  18. static bool create(const char *filename, uint32_t length, uint32_t align = 0);
  19. static bool createErasable(const char *filename, uint32_t length) {
  20. return create(filename, length, blockSize());
  21. }
  22. static void opendir() { dirindex = 0; }
  23. static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
  24. private:
  25. static uint16_t dirindex; // current position for readdir()
  26. static uint8_t fourbytemode; // 0=use 24 bit address, 1=use 32 bit address
  27. static uint8_t busy; // 0 = ready, 1 = suspendable busy, 2 = busy for realz
  28. static uint8_t blocksize; // erasable uniform block size, 1=4K, 2=8K, etc
  29. static uint8_t capacityId; // 3rd byte from 0x9F identification command
  30. };
  31. extern SerialFlashChip SerialFlash;
  32. class SerialFlashFile
  33. {
  34. public:
  35. SerialFlashFile() : address(0) {
  36. }
  37. operator bool() {
  38. if (address > 0) return true;
  39. return false;
  40. }
  41. uint32_t read(uint8_t *buf, uint32_t rdlen) {
  42. if (offset + rdlen > length) {
  43. if (offset >= length) return 0;
  44. rdlen = length - offset;
  45. }
  46. SerialFlash.read(buf, address + offset, rdlen);
  47. offset += rdlen;
  48. return rdlen;
  49. }
  50. uint32_t write(const void *buf, uint32_t wrlen) {
  51. if (offset + wrlen > length) {
  52. if (offset >= length) return 0;
  53. wrlen = length - offset;
  54. }
  55. SerialFlash.write(buf, address + offset, wrlen);
  56. offset += wrlen;
  57. return wrlen;
  58. }
  59. void seek(uint32_t n) {
  60. offset = n;
  61. }
  62. uint32_t position() {
  63. return offset;
  64. }
  65. uint32_t size() {
  66. return length;
  67. }
  68. uint32_t available() {
  69. if (offset >= length) return 0;
  70. return length - offset;
  71. }
  72. void erase();
  73. void flush() {
  74. }
  75. void close() {
  76. }
  77. uint32_t getFlashAddress() {
  78. return address;
  79. }
  80. protected:
  81. friend class SerialFlashChip;
  82. uint32_t address; // where this file's data begins in the Flash, or zero
  83. uint32_t length; // total length of the data in the Flash chip
  84. uint32_t offset; // current read/write offset in the file
  85. };
  86. #endif