++

Fork of SerialFlash by tom dunigan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialFlash.h Source File

SerialFlash.h

00001 /* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
00002  * https://github.com/PaulStoffregen/SerialFlash
00003  * Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
00004  *
00005  * Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
00006  * Please support PJRC's efforts to develop open source software by purchasing
00007  * Teensy or other genuine PJRC products.
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice, development funding notice, and this permission
00017  * notice shall be included in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  */
00027 
00028 #ifndef SerialFlash_h_
00029 #define SerialFlash_h_
00030 #include "mbed.h"
00031 
00032 class SerialFlashFile;
00033 
00034 class SerialFlashChip
00035 {
00036 public:
00037     static bool begin(uint8_t pin = 6);
00038     static uint32_t capacity(const uint8_t *id);
00039     static uint32_t blockSize();
00040     static void sleep();
00041     static void wakeup();
00042     static void readID(uint8_t *buf);
00043     static void readSerialNumber(uint8_t *buf);
00044     static void read(uint32_t addr, void *buf, uint32_t len);
00045     static bool ready();
00046     static void wait();
00047     static void write(uint32_t addr, const void *buf, uint32_t len);
00048     static void eraseAll();
00049     static void eraseBlock(uint32_t addr);
00050 
00051     static SerialFlashFile open(const char *filename);
00052     static bool create(const char *filename, uint32_t length, uint32_t align = 0);
00053     static bool createErasable(const char *filename, uint32_t length) {
00054         return create(filename, length, blockSize());
00055     }
00056     static bool exists(const char *filename);
00057     static bool remove(const char *filename);
00058     static bool remove(SerialFlashFile &file);
00059     static void opendir() { dirindex = 0; }
00060     static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
00061 private:
00062     static uint16_t dirindex; // current position for readdir()
00063     static uint8_t flags;   // chip features
00064     static uint8_t busy;    // 0 = ready
00065                 // 1 = suspendable program operation
00066                 // 2 = suspendable erase operation
00067                 // 3 = busy for realz!!
00068 
00069   //  SPI spi(D11,D12,D13); // mosi, miso, sclk
00070 };
00071 
00072 extern SerialFlashChip SerialFlash;
00073 
00074 
00075 class SerialFlashFile
00076 {
00077 public:
00078     SerialFlashFile() : address(0) {
00079     }
00080     operator bool() {
00081         if (address > 0) return true;
00082         return false;
00083     }
00084     uint32_t read(void *buf, uint32_t rdlen) {
00085         if (offset + rdlen > length) {
00086             if (offset >= length) return 0;
00087             rdlen = length - offset;
00088         }
00089         SerialFlash.read(address + offset, buf, rdlen);
00090         offset += rdlen;
00091         return rdlen;
00092     }
00093     uint32_t write(const void *buf, uint32_t wrlen) {
00094         if (offset + wrlen > length) {
00095             if (offset >= length) return 0;
00096             wrlen = length - offset;
00097         }
00098         SerialFlash.write(address + offset, buf, wrlen);
00099         offset += wrlen;
00100         return wrlen;
00101     }
00102     void seek(uint32_t n) {
00103         offset = n;
00104     }
00105     uint32_t position() {
00106         return offset;
00107     }
00108     uint32_t size() {
00109         return length;
00110     }
00111     uint32_t available() {
00112         if (offset >= length) return 0;
00113         return length - offset;
00114     }
00115     void erase();
00116     void flush() {
00117     }
00118     void close() {
00119     }
00120     uint32_t getFlashAddress() {
00121         return address;
00122     }
00123 protected:
00124     friend class SerialFlashChip;
00125     uint32_t address;  // where this file's data begins in the Flash, or zero
00126     uint32_t length;   // total length of the data in the Flash chip
00127     uint32_t offset; // current read/write offset in the file
00128     uint16_t dirindex;
00129 };
00130 
00131 
00132 #endif