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

99 行
2.5KB

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