Skip to content

Commit 9281805

Browse files
author
erik.nyquist
committed
Add Paul Stoffregen's SerialFlash library
1 parent 8dea7b4 commit 9281805

File tree

11 files changed

+2363
-0
lines changed

11 files changed

+2363
-0
lines changed

libraries/SerialFlash/SerialFlash.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
28+
#ifndef SerialFlash_h_
29+
#define SerialFlash_h_
30+
31+
#include <Arduino.h>
32+
#include <SPI.h>
33+
34+
class SerialFlashFile;
35+
36+
class SerialFlashChip
37+
{
38+
public:
39+
static bool begin(uint8_t pin = 6);
40+
static uint32_t capacity(const uint8_t *id);
41+
static uint32_t blockSize();
42+
static void sleep();
43+
static void wakeup();
44+
static void readID(uint8_t *buf);
45+
static void readSerialNumber(uint8_t *buf);
46+
static void read(uint32_t addr, void *buf, uint32_t len);
47+
static bool ready();
48+
static void wait();
49+
static void write(uint32_t addr, const void *buf, uint32_t len);
50+
static void eraseAll();
51+
static void eraseBlock(uint32_t addr);
52+
53+
static SerialFlashFile open(const char *filename);
54+
static bool create(const char *filename, uint32_t length, uint32_t align = 0);
55+
static bool createErasable(const char *filename, uint32_t length) {
56+
return create(filename, length, blockSize());
57+
}
58+
static bool exists(const char *filename);
59+
static bool remove(const char *filename);
60+
static bool remove(SerialFlashFile &file);
61+
static void opendir() { dirindex = 0; }
62+
static bool readdir(char *filename, uint32_t strsize, uint32_t &filesize);
63+
private:
64+
static uint16_t dirindex; // current position for readdir()
65+
static uint8_t flags; // chip features
66+
static uint8_t busy; // 0 = ready
67+
// 1 = suspendable program operation
68+
// 2 = suspendable erase operation
69+
// 3 = busy for realz!!
70+
};
71+
72+
extern SerialFlashChip SerialFlash;
73+
74+
75+
class SerialFlashFile
76+
{
77+
public:
78+
SerialFlashFile() : address(0) {
79+
}
80+
operator bool() {
81+
if (address > 0) return true;
82+
return false;
83+
}
84+
uint32_t read(void *buf, uint32_t rdlen) {
85+
if (offset + rdlen > length) {
86+
if (offset >= length) return 0;
87+
rdlen = length - offset;
88+
}
89+
SerialFlash.read(address + offset, buf, rdlen);
90+
offset += rdlen;
91+
return rdlen;
92+
}
93+
uint32_t write(const void *buf, uint32_t wrlen) {
94+
if (offset + wrlen > length) {
95+
if (offset >= length) return 0;
96+
wrlen = length - offset;
97+
}
98+
SerialFlash.write(address + offset, buf, wrlen);
99+
offset += wrlen;
100+
return wrlen;
101+
}
102+
void seek(uint32_t n) {
103+
offset = n;
104+
}
105+
uint32_t position() {
106+
return offset;
107+
}
108+
uint32_t size() {
109+
return length;
110+
}
111+
uint32_t available() {
112+
if (offset >= length) return 0;
113+
return length - offset;
114+
}
115+
void erase();
116+
void flush() {
117+
}
118+
void close() {
119+
}
120+
uint32_t getFlashAddress() {
121+
return address;
122+
}
123+
protected:
124+
friend class SerialFlashChip;
125+
uint32_t address; // where this file's data begins in the Flash, or zero
126+
uint32_t length; // total length of the data in the Flash chip
127+
uint32_t offset; // current read/write offset in the file
128+
uint16_t dirindex;
129+
};
130+
131+
132+
#endif

0 commit comments

Comments
 (0)