++

Fork of SerialFlash by tom dunigan

Committer:
lzbpli
Date:
Mon Jul 11 07:49:22 2016 +0000
Revision:
1:5f1a6c63fa57
Parent:
0:e5c9fd5789d7
053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manitou 0:e5c9fd5789d7 1 /* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
manitou 0:e5c9fd5789d7 2 * https://github.com/PaulStoffregen/SerialFlash
manitou 0:e5c9fd5789d7 3 * Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
manitou 0:e5c9fd5789d7 4 *
manitou 0:e5c9fd5789d7 5 * Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
manitou 0:e5c9fd5789d7 6 * Please support PJRC's efforts to develop open source software by purchasing
manitou 0:e5c9fd5789d7 7 * Teensy or other genuine PJRC products.
manitou 0:e5c9fd5789d7 8 *
manitou 0:e5c9fd5789d7 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
manitou 0:e5c9fd5789d7 10 * of this software and associated documentation files (the "Software"), to deal
manitou 0:e5c9fd5789d7 11 * in the Software without restriction, including without limitation the rights
manitou 0:e5c9fd5789d7 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
manitou 0:e5c9fd5789d7 13 * copies of the Software, and to permit persons to whom the Software is
manitou 0:e5c9fd5789d7 14 * furnished to do so, subject to the following conditions:
manitou 0:e5c9fd5789d7 15 *
manitou 0:e5c9fd5789d7 16 * The above copyright notice, development funding notice, and this permission
manitou 0:e5c9fd5789d7 17 * notice shall be included in all copies or substantial portions of the Software.
manitou 0:e5c9fd5789d7 18 *
manitou 0:e5c9fd5789d7 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
manitou 0:e5c9fd5789d7 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
manitou 0:e5c9fd5789d7 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
manitou 0:e5c9fd5789d7 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
manitou 0:e5c9fd5789d7 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
manitou 0:e5c9fd5789d7 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
manitou 0:e5c9fd5789d7 25 * THE SOFTWARE.
manitou 0:e5c9fd5789d7 26 */
manitou 0:e5c9fd5789d7 27
manitou 0:e5c9fd5789d7 28 #ifndef SerialFlash_h_
manitou 0:e5c9fd5789d7 29 #define SerialFlash_h_
manitou 0:e5c9fd5789d7 30 #include "mbed.h"
manitou 0:e5c9fd5789d7 31
manitou 0:e5c9fd5789d7 32 class SerialFlashFile;
manitou 0:e5c9fd5789d7 33
manitou 0:e5c9fd5789d7 34 class SerialFlashChip
manitou 0:e5c9fd5789d7 35 {
manitou 0:e5c9fd5789d7 36 public:
manitou 0:e5c9fd5789d7 37 static bool begin(uint8_t pin = 6);
manitou 0:e5c9fd5789d7 38 static uint32_t capacity(const uint8_t *id);
manitou 0:e5c9fd5789d7 39 static uint32_t blockSize();
manitou 0:e5c9fd5789d7 40 static void sleep();
manitou 0:e5c9fd5789d7 41 static void wakeup();
manitou 0:e5c9fd5789d7 42 static void readID(uint8_t *buf);
manitou 0:e5c9fd5789d7 43 static void readSerialNumber(uint8_t *buf);
manitou 0:e5c9fd5789d7 44 static void read(uint32_t addr, void *buf, uint32_t len);
manitou 0:e5c9fd5789d7 45 static bool ready();
manitou 0:e5c9fd5789d7 46 static void wait();
manitou 0:e5c9fd5789d7 47 static void write(uint32_t addr, const void *buf, uint32_t len);
manitou 0:e5c9fd5789d7 48 static void eraseAll();
manitou 0:e5c9fd5789d7 49 static void eraseBlock(uint32_t addr);
manitou 0:e5c9fd5789d7 50
manitou 0:e5c9fd5789d7 51 static SerialFlashFile open(const char *filename);
manitou 0:e5c9fd5789d7 52 static bool create(const char *filename, uint32_t length, uint32_t align = 0);
manitou 0:e5c9fd5789d7 53 static bool createErasable(const char *filename, uint32_t length) {
manitou 0:e5c9fd5789d7 54 return create(filename, length, blockSize());
manitou 0:e5c9fd5789d7 55 }
manitou 0:e5c9fd5789d7 56 static bool exists(const char *filename);
manitou 0:e5c9fd5789d7 57 static bool remove(const char *filename);
manitou 0:e5c9fd5789d7 58 static bool remove(SerialFlashFile &file);
manitou 0:e5c9fd5789d7 59 static void opendir() { dirindex = 0; }
manitou 0:e5c9fd5789d7 60 static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
manitou 0:e5c9fd5789d7 61 private:
manitou 0:e5c9fd5789d7 62 static uint16_t dirindex; // current position for readdir()
manitou 0:e5c9fd5789d7 63 static uint8_t flags; // chip features
manitou 0:e5c9fd5789d7 64 static uint8_t busy; // 0 = ready
manitou 0:e5c9fd5789d7 65 // 1 = suspendable program operation
manitou 0:e5c9fd5789d7 66 // 2 = suspendable erase operation
manitou 0:e5c9fd5789d7 67 // 3 = busy for realz!!
manitou 0:e5c9fd5789d7 68
manitou 0:e5c9fd5789d7 69 // SPI spi(D11,D12,D13); // mosi, miso, sclk
manitou 0:e5c9fd5789d7 70 };
manitou 0:e5c9fd5789d7 71
manitou 0:e5c9fd5789d7 72 extern SerialFlashChip SerialFlash;
manitou 0:e5c9fd5789d7 73
manitou 0:e5c9fd5789d7 74
manitou 0:e5c9fd5789d7 75 class SerialFlashFile
manitou 0:e5c9fd5789d7 76 {
manitou 0:e5c9fd5789d7 77 public:
manitou 0:e5c9fd5789d7 78 SerialFlashFile() : address(0) {
manitou 0:e5c9fd5789d7 79 }
manitou 0:e5c9fd5789d7 80 operator bool() {
manitou 0:e5c9fd5789d7 81 if (address > 0) return true;
manitou 0:e5c9fd5789d7 82 return false;
manitou 0:e5c9fd5789d7 83 }
manitou 0:e5c9fd5789d7 84 uint32_t read(void *buf, uint32_t rdlen) {
manitou 0:e5c9fd5789d7 85 if (offset + rdlen > length) {
manitou 0:e5c9fd5789d7 86 if (offset >= length) return 0;
manitou 0:e5c9fd5789d7 87 rdlen = length - offset;
manitou 0:e5c9fd5789d7 88 }
manitou 0:e5c9fd5789d7 89 SerialFlash.read(address + offset, buf, rdlen);
manitou 0:e5c9fd5789d7 90 offset += rdlen;
manitou 0:e5c9fd5789d7 91 return rdlen;
manitou 0:e5c9fd5789d7 92 }
manitou 0:e5c9fd5789d7 93 uint32_t write(const void *buf, uint32_t wrlen) {
manitou 0:e5c9fd5789d7 94 if (offset + wrlen > length) {
manitou 0:e5c9fd5789d7 95 if (offset >= length) return 0;
manitou 0:e5c9fd5789d7 96 wrlen = length - offset;
manitou 0:e5c9fd5789d7 97 }
manitou 0:e5c9fd5789d7 98 SerialFlash.write(address + offset, buf, wrlen);
manitou 0:e5c9fd5789d7 99 offset += wrlen;
manitou 0:e5c9fd5789d7 100 return wrlen;
manitou 0:e5c9fd5789d7 101 }
manitou 0:e5c9fd5789d7 102 void seek(uint32_t n) {
manitou 0:e5c9fd5789d7 103 offset = n;
manitou 0:e5c9fd5789d7 104 }
manitou 0:e5c9fd5789d7 105 uint32_t position() {
manitou 0:e5c9fd5789d7 106 return offset;
manitou 0:e5c9fd5789d7 107 }
manitou 0:e5c9fd5789d7 108 uint32_t size() {
manitou 0:e5c9fd5789d7 109 return length;
manitou 0:e5c9fd5789d7 110 }
manitou 0:e5c9fd5789d7 111 uint32_t available() {
manitou 0:e5c9fd5789d7 112 if (offset >= length) return 0;
manitou 0:e5c9fd5789d7 113 return length - offset;
manitou 0:e5c9fd5789d7 114 }
manitou 0:e5c9fd5789d7 115 void erase();
manitou 0:e5c9fd5789d7 116 void flush() {
manitou 0:e5c9fd5789d7 117 }
manitou 0:e5c9fd5789d7 118 void close() {
manitou 0:e5c9fd5789d7 119 }
manitou 0:e5c9fd5789d7 120 uint32_t getFlashAddress() {
manitou 0:e5c9fd5789d7 121 return address;
manitou 0:e5c9fd5789d7 122 }
manitou 0:e5c9fd5789d7 123 protected:
manitou 0:e5c9fd5789d7 124 friend class SerialFlashChip;
manitou 0:e5c9fd5789d7 125 uint32_t address; // where this file's data begins in the Flash, or zero
manitou 0:e5c9fd5789d7 126 uint32_t length; // total length of the data in the Flash chip
manitou 0:e5c9fd5789d7 127 uint32_t offset; // current read/write offset in the file
manitou 0:e5c9fd5789d7 128 uint16_t dirindex;
manitou 0:e5c9fd5789d7 129 };
manitou 0:e5c9fd5789d7 130
manitou 0:e5c9fd5789d7 131
manitou 0:e5c9fd5789d7 132 #endif