-
Notifications
You must be signed in to change notification settings - Fork 30
/
Adafruit_FRAM_SPI.cpp
421 lines (368 loc) · 11 KB
/
Adafruit_FRAM_SPI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*!
* @file Adafruit_FRAM_SPI.cpp
*
* @mainpage Adafruit SPI FRAM breakout.
*
* @section intro_sec Introduction
*
* This is a library for the Adafruit SPI FRAM breakout.
*
* Designed specifically to work with the Adafruit SPI FRAM breakout.
*
* Pick one up today in the adafruit shop!
* ------> https://www.adafruit.com/product/1897
*
* These module use SPI to communicate, 4 pins are required to interface.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*
* @section author Author
*
* K.Townsend (Adafruit Industries)
*
* @section license License
*
* BSD license, all text above must be included in any redistribution
*
* History
* - FUEL4EP: Added sleep mode support
*/
#include <stdlib.h>
#include "Adafruit_FRAM_SPI.h"
/// Enable debug output
#define FRAM_DEBUG 0
/// Supported flash devices
const struct {
uint8_t manufID; ///< Manufacture ID
uint16_t prodID; ///< Product ID
uint32_t size; ///< Size in bytes
bool support_sleep; ///< Support sleep mode
} _supported_devices[] = {
// Sorted in numerical order
// Fujitsu
{0x04, 0x0101, 2 * 1024UL, false}, // MB85RS16
{0x04, 0x0302, 8 * 1024UL, false}, // MB85RS64V
{0x04, 0x2303, 8 * 1024UL, true}, // MB85RS64T
{0x04, 0x2503, 32 * 1024UL, true}, // MB85RS256TY
{0x04, 0x2703, 128 * 1024UL, true}, // MB85RS1MT
{0x04, 0x4803, 256 * 1024UL, true}, // MB85RS2MTA
{0x04, 0x2803, 256 * 1024UL, true}, // MB85RS2MT
{0x04, 0x4903, 512 * 1024UL, true}, // MB85RS4MT
{0x04, 0x490B, 512 * 1024UL, true}, // MB85RS4MTY
// Cypress
{0x7F, 0x7F7f, 32 * 1024UL, false}, // FM25V02
// (manu = 7F7F7F7F7F7FC2, device = 0x2200)
// Lapis
{0xAE, 0x8305, 8 * 1024UL, false} // MR45V064B
};
/*!
* @brief Check if the flash device is supported
* @param manufID
* ManufactureID to be checked
* @param prodID
* ProductID to be checked
* @return device index, -1 if not supported
*/
static int get_supported_idx(uint8_t manufID, uint16_t prodID) {
for (unsigned int i = 0;
i < sizeof(_supported_devices) / sizeof(_supported_devices[0]); i++) {
if (manufID == _supported_devices[i].manufID &&
prodID == _supported_devices[i].prodID)
return i;
}
return -1;
}
/*!
* @brief Initialize the SPI FRAM class
*/
void Adafruit_FRAM_SPI::init(void) {
_nAddressSizeBytes = 0;
_dev_idx = -1;
}
/*!
* @brief Instantiates a new SPI FRAM class using hardware SPI
* @param cs
* Required chip select pin number
* @param *theSPI
* SPI interface object, defaults to &SPI
* @param freq
* The SPI clock frequency to use, defaults to 1MHz
*/
Adafruit_FRAM_SPI::Adafruit_FRAM_SPI(int8_t cs, SPIClass *theSPI,
uint32_t freq) {
init();
spi_dev = new Adafruit_SPIDevice(cs, freq, SPI_BITORDER_MSBFIRST, SPI_MODE0,
theSPI);
}
/*!
* @brief Instantiates a new SPI FRAM class using bitbang SPI
* @param clk
* CLK pin
* @param miso
* MISO pin
* @param mosi
* MOSI pin
* @param cs
* CS pin
*/
Adafruit_FRAM_SPI::Adafruit_FRAM_SPI(int8_t clk, int8_t miso, int8_t mosi,
int8_t cs) {
init();
spi_dev = new Adafruit_SPIDevice(cs, clk, miso, mosi, 1000000,
SPI_BITORDER_MSBFIRST, SPI_MODE0);
}
Adafruit_FRAM_SPI::~Adafruit_FRAM_SPI(void) {
if (spi_dev) {
delete spi_dev;
}
}
/*!
* @brief Initializes SPI and configures the chip (call this function before
* doing anything else)
* @param nAddressSizeBytes
* sddress size in bytes (default 2)
* @return true if successful
*/
bool Adafruit_FRAM_SPI::begin(uint8_t nAddressSizeBytes) {
// not used anymore, since we will use auto-detect size
(void)nAddressSizeBytes;
/* Configure SPI */
if (!spi_dev->begin()) {
return false;
}
/* Make sure we're actually connected */
uint8_t manufID;
uint16_t prodID;
getDeviceID(&manufID, &prodID);
/* Everything seems to be properly initialised and connected */
_dev_idx = get_supported_idx(manufID, prodID);
if (_dev_idx == -1) {
#if FRAM_DEBUG
Serial.print(F("Unexpected Device: Manufacturer ID = 0x"));
Serial.print(manufID, HEX);
Serial.print(F(", Product ID = 0x"));
Serial.println(prodID, HEX);
#endif
return false;
} else {
uint32_t const fram_size = _supported_devices[_dev_idx].size;
#if FRAM_DEBUG
Serial.print(F("FRAM Size = 0x"));
Serial.println(fram_size, HEX);
#endif
// Detect address size in bytes either 2 or 3 bytes (4 bytes is not
// supported)
if (fram_size > 64UL * 1024) {
setAddressSize(3);
} else {
setAddressSize(2);
}
return true;
}
}
/*!
* @brief Enables or disables writing to the SPI flash
* @param enable
* True enables writes, false disables writes
* @return true if successful
*/
bool Adafruit_FRAM_SPI::writeEnable(bool enable) {
uint8_t cmd;
if (enable) {
cmd = OPCODE_WREN;
} else {
cmd = OPCODE_WRDI;
}
return spi_dev->write(&cmd, 1);
}
/*!
* @brief Writes a byte at the specific FRAM address
* @param addr
* The 32-bit address to write to in FRAM memory
* @param value
* The 8-bit value to write at framAddr
* @return true if successful
*/
bool Adafruit_FRAM_SPI::write8(uint32_t addr, uint8_t value) {
uint8_t buffer[10];
uint8_t i = 0;
buffer[i++] = OPCODE_WRITE;
if (_nAddressSizeBytes > 3) {
buffer[i++] = (uint8_t)(addr >> 24);
}
if (_nAddressSizeBytes > 2) {
buffer[i++] = (uint8_t)(addr >> 16);
}
buffer[i++] = (uint8_t)(addr >> 8);
buffer[i++] = (uint8_t)(addr & 0xFF);
buffer[i++] = value;
return spi_dev->write(buffer, i);
}
/*!
* @brief Writes count bytes starting at the specific FRAM address
* @param addr
* The 32-bit address to write to in FRAM memory
* @param values
* The pointer to an array of 8-bit values to write starting at addr
* @param count
* The number of bytes to write
* @return true if successful
*/
bool Adafruit_FRAM_SPI::write(uint32_t addr, const uint8_t *values,
size_t count) {
uint8_t prebuf[10];
uint8_t i = 0;
prebuf[i++] = OPCODE_WRITE;
if (_nAddressSizeBytes > 3) {
prebuf[i++] = (uint8_t)(addr >> 24);
}
if (_nAddressSizeBytes > 2) {
prebuf[i++] = (uint8_t)(addr >> 16);
}
prebuf[i++] = (uint8_t)(addr >> 8);
prebuf[i++] = (uint8_t)(addr & 0xFF);
return spi_dev->write(values, count, prebuf, i);
}
/*!
* @brief Reads an 8 bit value from the specified FRAM address
* @param addr
* The 32-bit address to read from in FRAM memory
* @return The 8-bit value retrieved at framAddr
*/
uint8_t Adafruit_FRAM_SPI::read8(uint32_t addr) {
uint8_t buffer[10], val;
uint8_t i = 0;
buffer[i++] = OPCODE_READ;
if (_nAddressSizeBytes > 3) {
buffer[i++] = (uint8_t)(addr >> 24);
}
if (_nAddressSizeBytes > 2) {
buffer[i++] = (uint8_t)(addr >> 16);
}
buffer[i++] = (uint8_t)(addr >> 8);
buffer[i++] = (uint8_t)(addr & 0xFF);
spi_dev->write_then_read(buffer, i, &val, 1);
return val;
}
/*!
* @brief Read count bytes starting at the specific FRAM address
* @param addr
* The 32-bit address to write to in FRAM memory
* @param values
* The pointer to an array of 8-bit values to read starting at addr
* @param count
* The number of bytes to read
* @return true if successful
*/
bool Adafruit_FRAM_SPI::read(uint32_t addr, uint8_t *values, size_t count) {
uint8_t buffer[10];
uint8_t i = 0;
buffer[i++] = OPCODE_READ;
if (_nAddressSizeBytes > 3) {
buffer[i++] = (uint8_t)(addr >> 24);
}
if (_nAddressSizeBytes > 2) {
buffer[i++] = (uint8_t)(addr >> 16);
}
buffer[i++] = (uint8_t)(addr >> 8);
buffer[i++] = (uint8_t)(addr & 0xFF);
return spi_dev->write_then_read(buffer, i, values, count);
}
/*!
* @brief Reads the Manufacturer ID and the Product ID from the IC
* @param manufacturerID
* The 8-bit manufacturer ID (Fujitsu = 0x04)
* @param productID
* The memory density (bytes 15..8) and proprietary
* Product ID fields (bytes 7..0). Should be 0x0302 for
* the MB85RS64VPNF-G-JNERE1.
* @return true if successful
*/
bool Adafruit_FRAM_SPI::getDeviceID(uint8_t *manufacturerID,
uint16_t *productID) {
uint8_t cmd = OPCODE_RDID;
uint8_t a[4] = {0, 0, 0, 0};
if (!spi_dev->write_then_read(&cmd, 1, a, 4)) {
return false;
}
if (a[1] == 0x7f) {
// Device with continuation code (0x7F) in their second byte
// Manu ( 1 byte) - 0x7F - Product (2 bytes)
*manufacturerID = (a[0]);
*productID = (a[2] << 8) + a[3];
} else {
// Device without continuation code
// Manu ( 1 byte) - Product (2 bytes)
*manufacturerID = (a[0]);
*productID = (a[1] << 8) + a[2];
}
return true;
}
/*!
@brief Reads the status register
@return register value
*/
uint8_t Adafruit_FRAM_SPI::getStatusRegister() {
uint8_t cmd, val;
cmd = OPCODE_RDSR;
spi_dev->write_then_read(&cmd, 1, &val, 1);
return val;
}
/*!
* @brief Sets the status register
* @param value
* value that will be set
* @return true if successful
*/
bool Adafruit_FRAM_SPI::setStatusRegister(uint8_t value) {
uint8_t cmd[2];
cmd[0] = OPCODE_WRSR;
cmd[1] = value;
return spi_dev->write(cmd, 2);
}
/*!
* @brief Sets adress size to provided value
* @param nAddressSize
* address size in bytes
*/
void Adafruit_FRAM_SPI::setAddressSize(uint8_t nAddressSize) {
_nAddressSizeBytes = nAddressSize;
}
/*!
* @brief Enters the FRAM's low power sleep mode
* @return true if successful
*/
// WARNING: this method has not yet been validated
bool Adafruit_FRAM_SPI::enterSleep(void) {
if (_dev_idx == -1 || !_supported_devices[_dev_idx].support_sleep) {
return false;
}
uint8_t cmd = OPCODE_SLEEP;
return spi_dev->write(&cmd, 1);
}
/*!
* @brief exits the FRAM's low power sleep mode
* @return true if successful
*/
// WARNING: this method has not yet been validated
bool Adafruit_FRAM_SPI::exitSleep(void) {
if (_dev_idx == -1 || !_supported_devices[_dev_idx].support_sleep) {
return false;
}
// Returning to a normal operation from the SLEEP mode is carried out after
// tREC (Max 400 μs) time from the falling edge of CS
spi_dev->beginTransactionWithAssertingCS();
delayMicroseconds(300);
// It is possible to return CS to H level before tREC time. However, it
// is prohibited to bring down CS to L level again during tREC period.
spi_dev->endTransactionWithDeassertingCS();
delayMicroseconds(100);
// MB85RS4MTY requires 450us (extra 50us) to wake from "Hibernate"
if (_supported_devices[_dev_idx].manufID == 0x04 &&
_supported_devices[_dev_idx].prodID == 0x0B) {
delayMicroseconds(50);
}
return true;
}