forked from biomimetics/imageproc-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfmem.c
More file actions
executable file
·491 lines (383 loc) · 13.5 KB
/
dfmem.c
File metadata and controls
executable file
·491 lines (383 loc) · 13.5 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright (c) 2008-2011, Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the University of California, Berkeley nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* ATMEL DataFlash Memory (dfmem) Interface
*
* by Fernando L. Garcia Bermudez
*
* v.beta
*
* Revisions:
* Fernando L. Garcia Bermudez 2008-7-23 Initial release
* 2010-7-19 Blocking read/writes tested
* Stanley S. Baek 2010-8-30 Added buffer read/writes and sector
* erase for improving writing speeds.
* Andrew Pullin 2011-6-7 Added ability to query for chip
* w/Fernando L. Garcia Bermudez size and flags to handle them.
* Andrew Pullin 2011-9-23 Added ability for deep power-down.
*
* Notes:
* - Uses an SPI port for communicating with the memory chip.
*/
#include "p33Fxxxx.h"
#include "spi.h"
#include "dfmem.h"
#if (defined(__IMAGEPROC1) || defined(__IMAGEPROC2) || defined(__MIKRO) || defined(__EXP16DEV))
// MIKRO & EXP16DEV has no FLASHMEM, but needs this for compile
// SPIx pins
#define SPI_CS _LATG9
// SPIx Registers
#define SPI_BUF SPI2BUF
#define SPI_CON1 SPI2CON1
#define SPI_CON2 SPI2CON2
#define SPI_STAT SPI2STAT
#define SPI_STATbits SPI2STATbits
#endif
// Handle different chip sizes
#if (defined(__DFMEM_8MBIT))
#define BYTE_ADDRESS_BITS 9
#elif (defined(__DFMEM_16MBIT) || defined(__DFMEM_32MBIT))
#define BYTE_ADDRESS_BITS 10
#elif (defined(__DFMEM_64MBIT))
#define BYTE_ADDRESS_BITS 11
#else
#error "You need to specify the size of your memory chip by defining one of the following flags: __DFMEM_8MBIT, __DFMEM_16MBIT, or __DFMEM_32MBIT."
#endif
// Commands
#define WRITE_PAGE_VIA_BUFFER1 0x82
#define WRITE_PAGE_VIA_BUFFER2 0x85
#define WRITE_TO_BUFFER1 0x84
#define WRITE_TO_BUFFER2 0x87
#define WRITE_BUFFER1_TO_PAGE_NO_ERASE 0x88
#define WRITE_BUFFER2_TO_PAGE_NO_ERASE 0x89
#define WRITE_BUFFER1_TO_PAGE_WITH_ERASE 0x83
#define WRITE_BUFFER2_TO_PAGE_WITH_ERASE 0x86
#define READ_PAGE 0xD2
#define READ_PAGE_TO_BUFFER_1 0x53
#define READ_PAGE_TO_BUFFER_2 0x55
#define ERASE_PAGE 0x81
#define ERASE_BLOCK 0x50
#define ERASE_SECTOR 0x7C
/*-----------------------------------------------------------------------------
* Private variables
-----------------------------------------------------------------------------*/
union {
unsigned long address;
unsigned char chr_addr[4];
} MemAddr;
/*----------------------------------------------------------------------------
* Declaration of private functions
---------------------------------------------------------------------------*/
static inline unsigned char dfmemExchangeByte (unsigned char byte);
static inline void dfmemWriteByte (unsigned char byte);
static inline unsigned char dfmemReadByte (void);
static inline void dfmemSelectChip(void);
static inline void dfmemDeselectChip(void);
static void dfmemSetupPeripheral(void);
/*-----------------------------------------------------------------------------
* Public functions
-----------------------------------------------------------------------------*/
void dfmemSetup(void)
{
dfmemSetupPeripheral();
dfmemDeselectChip();
}
void dfmemWrite (unsigned char *data, unsigned int length, unsigned int page,
unsigned int byte, unsigned char buffer)
{
unsigned char command;
while(!dfmemIsReady());
// Choose command dependent on buffer choice
if (buffer == 1) {
command = WRITE_PAGE_VIA_BUFFER1;
} else {
command = WRITE_PAGE_VIA_BUFFER2;
}
// Restructure page/byte addressing
// 1 don't care bit + 13 page address bits + byte address bits
MemAddr.address = (((unsigned long)page) << BYTE_ADDRESS_BITS) + byte;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(command);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
while (length--) { dfmemWriteByte(*data++); }
dfmemDeselectChip();
}
void dfmemWriteBuffer (unsigned char *data, unsigned int length,
unsigned int byte, unsigned char buffer)
{
unsigned char command;
// Choose command dependent on buffer choice
if (buffer == 1) {
command = WRITE_TO_BUFFER1;
} else {
command = WRITE_TO_BUFFER2;
}
// Restructure page/byte addressing
// 14 don't care bit + byte address bits
MemAddr.address = (unsigned long)byte;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(command);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
while (length--) { dfmemWriteByte(*data++); }
dfmemDeselectChip();
}
void dfmemWriteBuffer2MemoryNoErase (unsigned int page, unsigned char buffer)
{
unsigned char command;
while(!dfmemIsReady());
// Choose command dependent on buffer choice
if (buffer == 1) {
command = WRITE_BUFFER1_TO_PAGE_NO_ERASE;
} else {
command = WRITE_BUFFER2_TO_PAGE_NO_ERASE;
}
// Restructure page/byte addressing
// 1 don't care bit + 13 page address bits + don't care bits
MemAddr.address = ((unsigned long)page) << BYTE_ADDRESS_BITS;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(command);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemDeselectChip();
}
void dfmemPush (unsigned char *data, unsigned int length, unsigned int page_reset)
{
static unsigned int page = 0;
static unsigned int byte = 0;
static unsigned char buffer = 0;
if (page_reset != 0xffff) {
page = page_reset;
}
if (length > 512 || length == 0) return;
if (length + byte > 512) {
dfmemWriteBuffer2MemoryNoErase(page++, buffer);
buffer ^= 0x01; // toggle buffer
byte = 0;
}
dfmemWriteBuffer(data, length, byte, buffer);
byte += length;
}
void dfmemRead (unsigned int page, unsigned int byte, unsigned int length,
unsigned char *data)
{
while(!dfmemIsReady());
// Restructure page/byte addressing
// 1 don't care bit + 13 page address bits + byte address bits
MemAddr.address = (((unsigned long)page) << BYTE_ADDRESS_BITS) + byte;
// Read data from memory
dfmemSelectChip();
dfmemWriteByte(READ_PAGE);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemWriteByte(0x00); // 4 don't care bytes
dfmemWriteByte(0x00);
dfmemWriteByte(0x00);
dfmemWriteByte(0x00);
while (length--) { *data++ = dfmemReadByte(); }
dfmemDeselectChip();
}
void dfmemReadPage2Buffer (unsigned int page, unsigned char buffer)
{
unsigned char command;
while(!dfmemIsReady());
// Choose command dependent on buffer choice
if (buffer == 1) {
command = READ_PAGE_TO_BUFFER_1;
} else {
command = READ_PAGE_TO_BUFFER_2;
}
// 1 don't care bit + 13 page address bits + don't care bits
MemAddr.address = ((unsigned long)page) << BYTE_ADDRESS_BITS;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(command);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemDeselectChip();
}
void dfmemErasePage (unsigned int page)
{
while(!dfmemIsReady());
// Restructure page/byte addressing
MemAddr.address = ((unsigned long)page) << BYTE_ADDRESS_BITS;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(ERASE_PAGE);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemDeselectChip();
}
void dfmemEraseBlock (unsigned int page)
{
while(!dfmemIsReady());
// Restructure page/byte addressing
MemAddr.address = ((unsigned long)page) << BYTE_ADDRESS_BITS;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(ERASE_BLOCK);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemDeselectChip();
}
void dfmemEraseSector (unsigned int page)
{
while(!dfmemIsReady());
// Restructure page/byte addressing
MemAddr.address = ((unsigned long)page) << BYTE_ADDRESS_BITS;
// Write data to memory
dfmemSelectChip();
dfmemWriteByte(ERASE_SECTOR);
dfmemWriteByte(MemAddr.chr_addr[2]);
dfmemWriteByte(MemAddr.chr_addr[1]);
dfmemWriteByte(MemAddr.chr_addr[0]);
dfmemDeselectChip();
}
void dfmemEraseChip (void)
{
while(!dfmemIsReady());
dfmemSelectChip();
dfmemWriteByte(0xC7);
dfmemWriteByte(0x94);
dfmemWriteByte(0x80);
dfmemWriteByte(0x9A);
dfmemDeselectChip();
}
unsigned char dfmemIsReady (void)
{
return (dfmemGetStatus() >> 7);
}
unsigned char dfmemGetStatus (void)
{
unsigned char byte;
dfmemSelectChip();
dfmemWriteByte(0xD7);
byte = dfmemReadByte();
dfmemDeselectChip();
return byte;
}
// The manufacturer and device id command (0x9F) returns 4 bytes normally
// (including info on id, family, density, etc.), but this functions returns
// just the manufacturer id and discards the rest when deselecting the chip.
unsigned char dfmemGetManufacturerID (void)
{
unsigned char byte;
dfmemSelectChip();
dfmemWriteByte(0x9F);
byte = dfmemReadByte();
dfmemDeselectChip();
return byte;
}
// The manufacturer and device id command (0x9F) returns 4 bytes normally
// (including info on id, family, density, etc.), but this functions returns
// only the 5 bits pertaining to the memory density.
unsigned char dfmemGetChipSize (void)
{
unsigned char byte;
dfmemSelectChip();
dfmemWriteByte(0x9F);
byte = dfmemReadByte(); // Manufacturer ID, not needed
byte = dfmemReadByte() & 0b00011111;
dfmemDeselectChip();
return byte;
}
void dfmemDeepSleep()
{
dfmemSelectChip();
dfmemWriteByte(0xB9);
dfmemDeselectChip();
}
void dfmemResumeFromDeepSleep()
{
dfmemSelectChip();
dfmemWriteByte(0xAB);
dfmemDeselectChip();
}
/*-----------------------------------------------------------------------------
* Private functions
-----------------------------------------------------------------------------*/
// Sends a byte to the memory chip and returns the byte read from it
//
// Parameters : byte to send.
// Returns : received byte.
static inline unsigned char dfmemExchangeByte (unsigned char byte)
{
SPI_BUF = byte;
while(SPI_STATbits.SPITBF);
while(!SPI_STATbits.SPIRBF);
SPI_STATbits.SPIROV = 0;
return SPI_BUF;
}
// Sends a byte to the memory chip.
//
// It discards the byte it receives when transmitting this one as it should
// not be important and so that it doesn't stay in the received queue.
//
// Parameters : byte to send.
static inline void dfmemWriteByte (unsigned char byte)
{
dfmemExchangeByte(byte);
}
// Receives a byte from the memory chip.
//
// It sends a null byte so as to issue the required clock cycles for receiving
// one from the memory.
//
// Returns : received byte.
static inline unsigned char dfmemReadByte (void)
{
return dfmemExchangeByte(0x00);
}
// Selects the memory chip.
static inline void dfmemSelectChip(void) { SPI_CS = 0; }
// Deselects the memory chip.
static inline void dfmemDeselectChip(void) { SPI_CS = 1; }
// Initializes the SPIx bus for communicating with the memory.
//
// The MCU is the SPI master and the clock isn't continuous.
static void dfmemSetupPeripheral(void)
{
SPI_CON1 = ENABLE_SCK_PIN & ENABLE_SDO_PIN & SPI_MODE16_OFF & SPI_SMP_OFF &
SPI_CKE_ON & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_HIGH &
MASTER_ENABLE_ON & PRI_PRESCAL_1_1 & SEC_PRESCAL_4_1;
SPI_CON2 = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT & FRAME_POL_ACTIVE_HIGH &
FRAME_SYNC_EDGE_PRECEDE;
SPI_STAT = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR;
}