forked from biomimetics/imageproc-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfmem.c
651 lines (528 loc) · 20.1 KB
/
dfmem.c
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
* Copyright (c) 2008-2013, 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.1.0
*
* 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.
* Humphrey Hu 2012-1-22 Enabled DMA on SPI port.
* Andrew Pullin 2012-4-8 Adding auto flash geometry and
* some telemetry helper functions.
*
* Notes:
* - Uses an SPI port for communicating with the memory chip and DMA
* channels 4 and 5 with spi_controller.c
*/
// TODO (humhu) : Divide into generic nvmem (non-volatile memory) device class interface and
// DFMEM-specific driver to match radio class/driver
// TODO (humhu) : Add a rudimentary filesystem
#include <stdlib.h>
#include <string.h>
#include <xc.h>
#include "spi.h"
#include "dfmem.h"
#include "spi_controller.h" // For DMA
#include "utils.h"
// Flash geometry
// 8 Mbit
#define FLASH_8MBIT_MAX_SECTOR 16
#define FLASH_8MBIT_MAX_PAGES 4096
#define FLASH_8MBIT_BUFFERSIZE 264
#define FLASH_8MBIT_BYTES_PER_PAGE 264
#define FLASH_8MBIT_PAGES_PER_BLOCK 8
#define FLASH_8MBIT_BLOCKS_PER_SECTOR 32
#define FLASH_8MBIT_PAGES_PER_SECTOR 256 //Calculated; not directly in datasheet
#define FLASH_8MBIT_BYTE_ADDRESS_BITS 9
// 16 Mbit
#define FLASH_16MBIT_MAX_SECTOR 16
#define FLASH_16MBIT_MAX_PAGES 4096
#define FLASH_16MBIT_BUFFERSIZE 528
#define FLASH_16MBIT_BYTES_PER_PAGE 528
#define FLASH_16MBIT_PAGES_PER_BLOCK 8
#define FLASH_16MBIT_BLOCKS_PER_SECTOR 32
#define FLASH_16MBIT_PAGES_PER_SECTOR 256 //Calculated; not directly in datasheet
#define FLASH_16MBIT_BYTE_ADDRESS_BITS 10
// 32 Mbit
#define FLASH_32MBIT_MAX_SECTOR 64
#define FLASH_32MBIT_MAX_PAGES 8192
#define FLASH_32MBIT_BUFFERSIZE 528
#define FLASH_32MBIT_BYTES_PER_PAGE 528
#define FLASH_32MBIT_PAGES_PER_BLOCK 8
#define FLASH_32MBIT_BLOCKS_PER_SECTOR 16 // --> THIS VALUE IS WRONG IN THE DATASHEET! 16 IS CORRECT.
#define FLASH_32MBIT_PAGES_PER_SECTOR 128 //Calculated; not directly in datasheet
#define FLASH_32MBIT_BYTE_ADDRESS_BITS 10
// 64 Mbit
#define FLASH_64MBIT_MAX_SECTOR 32
#define FLASH_64MBIT_MAX_PAGES 8192
#define FLASH_64MBIT_BUFFERSIZE 1056
#define FLASH_64MBIT_BYTES_PER_PAGE 1056
#define FLASH_64MBIT_PAGES_PER_BLOCK 8
#define FLASH_64MBIT_BLOCKS_PER_SECTOR 32
#define FLASH_64MBIT_PAGES_PER_SECTOR 256 //Calculated; not directly in datasheet
#define FLASH_64MBIT_BYTE_ADDRESS_BITS 11
// 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
-----------------------------------------------------------------------------*/
// Memory geometry
static DfmemGeometryStruct dfmem_geo;
// Placeholders
static unsigned int currentBuffer = 0;
static unsigned int currentBufferOffset = 0;
static unsigned int nextPage = 0;
enum FlashSizeType {
DFMEM_8MBIT = 0b00101,
DFMEM_16MBIT = 0b00110,
DFMEM_32MBIT = 0b00111,
DFMEM_64MBIT = 0b01000
};
union {
unsigned long address;
unsigned char chr_addr[4];
} MemAddr;
unsigned char userSecRegID[64];
unsigned char factorySecRegID[64];
/*----------------------------------------------------------------------------
* Declaration of private functions
---------------------------------------------------------------------------*/
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);
static void dfmemGeometrySetup(void);
static void dfmemReadSecurityRegister(void);
static void spiCallback(unsigned int irq_source);
/*-----------------------------------------------------------------------------
* Public functions
-----------------------------------------------------------------------------*/
void dfmemSetup(void)
{
dfmemSetupPeripheral();
spic2SetCallback(DFMEM_CS, &spiCallback);
while(!dfmemIsReady());
dfmemGeometrySetup();
//Readback security register, which gets chip-unique identifiers
// ID is stored in a static var here, and dfmem has a public getter
dfmemReadSecurityRegister();
}
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) << dfmem_geo.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]);
spic2MassTransmit(length, data, 2*length);
//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]);
spic2MassTransmit(length, data, 2*length);
}
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) << dfmem_geo.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]);
currentBufferOffset = 0;
dfmemDeselectChip();
}
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) << dfmem_geo.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);
unsigned int read_bytes;
read_bytes = spic2MassTransmit(length, NULL, 2*length);
dfmemSelectChip(); // Busy wait
spic2ReadBuffer(read_bytes, data); // reads DMA buffer
//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) << dfmem_geo.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) << dfmem_geo.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) << dfmem_geo.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) << dfmem_geo.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 de-selecting 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();
}
void dfmemSave(unsigned char* data, unsigned int length)
{ //If this write will not fit into the buffer, then
if (currentBufferOffset + length > dfmem_geo.buffer_size)
{ dfmemSync(); // i) write current buffer to memory, and ii) switch to new buffer
}
/* dfmemWriteBuffer2MemoryNoErase(nextPage, currentBuffer);
currentBuffer = (currentBuffer) ? 0 : 1; // toggle buffer
currentBufferOffset = 0; // reset to beginning
nextPage++;
*/
// write data into buffer
dfmemWriteBuffer(data, length, currentBufferOffset, currentBuffer);
currentBufferOffset += length;
}
// write last buffer to memory
void dfmemSync()
{
//if currentBufferOffset == 0, then we don't need to write anything to be sync'd
if(currentBufferOffset != 0){
dfmemWriteBuffer2MemoryNoErase(nextPage, currentBuffer);
currentBuffer = (currentBuffer) ? 0 : 1; //Toggle buffer number
currentBufferOffset = 0; // reset to beginning of buffer
nextPage++;
}
}
void dfmemGetGeometryParams(DfmemGeometry geo)
{
if(geo == NULL) { return; }
memcpy(geo, &dfmem_geo, sizeof(DfmemGeometryStruct));
}
void dfmemZeroIndex()
{
currentBuffer = 0;
currentBufferOffset = 0;
nextPage = 0;
}
uint64_t dfmemGetUnqiueID(){
//Grab first 8 bytes of 64-byte factory value
uint64_t id = *((uint64_t*)(factorySecRegID));
return id;
}
/*-----------------------------------------------------------------------------
* Private functions
-----------------------------------------------------------------------------*/
void spiCallback(unsigned int irq_source) {
if(irq_source == SPIC_TRANS_SUCCESS) {
dfmemDeselectChip();
} else if(irq_source == SPIC_TRANS_TIMEOUT) {
spic2Reset(); // Reset hardware
}
}
// 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)
{
spic2Transmit(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 spic2Receive();
}
// Selects the memory chip.
static inline void dfmemSelectChip(void) { spic2BeginTransaction(DFMEM_CS); }
// De-selects the memory chip.
static inline void dfmemDeselectChip(void) { spic2EndTransaction(); }
// 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)
{
spicSetupChannel2(DFMEM_CS,
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);
}
// Figures out memory geometry by querying its size
static void dfmemGeometrySetup(void)
{
unsigned char size;
size = dfmemGetChipSize();
switch(size){
case DFMEM_8MBIT:
dfmem_geo.max_sector = FLASH_8MBIT_MAX_SECTOR;
dfmem_geo.max_pages = FLASH_8MBIT_MAX_PAGES;
dfmem_geo.buffer_size = FLASH_8MBIT_BUFFERSIZE;
dfmem_geo.bytes_per_page = FLASH_8MBIT_BYTES_PER_PAGE;
dfmem_geo.pages_per_block = FLASH_8MBIT_PAGES_PER_BLOCK;
dfmem_geo.blocks_per_sector = FLASH_8MBIT_BLOCKS_PER_SECTOR;
dfmem_geo.pages_per_sector = FLASH_8MBIT_PAGES_PER_SECTOR;
dfmem_geo.byte_address_bits = FLASH_8MBIT_BYTE_ADDRESS_BITS;
break;
case DFMEM_16MBIT:
dfmem_geo.max_sector = FLASH_16MBIT_MAX_SECTOR;
dfmem_geo.max_pages = FLASH_16MBIT_MAX_PAGES;
dfmem_geo.buffer_size = FLASH_16MBIT_BUFFERSIZE;
dfmem_geo.bytes_per_page = FLASH_16MBIT_BYTES_PER_PAGE;
dfmem_geo.pages_per_block = FLASH_16MBIT_PAGES_PER_BLOCK;
dfmem_geo.blocks_per_sector = FLASH_16MBIT_BLOCKS_PER_SECTOR;
dfmem_geo.pages_per_sector = FLASH_16MBIT_PAGES_PER_SECTOR;
dfmem_geo.byte_address_bits = FLASH_16MBIT_BYTE_ADDRESS_BITS;
break;
case DFMEM_32MBIT:
dfmem_geo.max_sector = FLASH_32MBIT_MAX_SECTOR;
dfmem_geo.max_pages = FLASH_32MBIT_MAX_PAGES;
dfmem_geo.buffer_size = FLASH_32MBIT_BUFFERSIZE;
dfmem_geo.bytes_per_page = FLASH_32MBIT_BYTES_PER_PAGE;
dfmem_geo.pages_per_block = FLASH_32MBIT_PAGES_PER_BLOCK;
dfmem_geo.blocks_per_sector = FLASH_32MBIT_BLOCKS_PER_SECTOR;
dfmem_geo.pages_per_sector = FLASH_32MBIT_PAGES_PER_SECTOR;
dfmem_geo.byte_address_bits = FLASH_32MBIT_BYTE_ADDRESS_BITS;
break;
case DFMEM_64MBIT:
dfmem_geo.max_sector = FLASH_64MBIT_MAX_SECTOR;
dfmem_geo.max_pages = FLASH_64MBIT_MAX_PAGES;
dfmem_geo.buffer_size = FLASH_64MBIT_BUFFERSIZE;
dfmem_geo.bytes_per_page = FLASH_64MBIT_BYTES_PER_PAGE;
dfmem_geo.pages_per_block = FLASH_64MBIT_PAGES_PER_BLOCK;
dfmem_geo.blocks_per_sector = FLASH_64MBIT_BLOCKS_PER_SECTOR;
dfmem_geo.pages_per_sector = FLASH_64MBIT_PAGES_PER_SECTOR;
dfmem_geo.byte_address_bits = FLASH_64MBIT_BYTE_ADDRESS_BITS;
break;
default:
// TODO (apullin, fgb) : Do something. Probably communicate back with user.
break;
}
}
static void dfmemReadSecurityRegister(void){
int i; // Loop variable, MPLABX is not C99 standard
dfmemSelectChip();
dfmemWriteByte(0x77); //Read security register opcode
dfmemWriteByte(0x77); //Dummy write, 3 bytes required for opcode 0x77
dfmemWriteByte(0x77); //Dummy write, 3 bytes required for opcode 0x77
dfmemWriteByte(0x77); //Dummy write, 3 bytes required for opcode 0x77
//User ID is the first 64 bytes
for(i = 0; i < 64; i++){
userSecRegID[i] = dfmemReadByte();
}
//Factory ID is the second 64 bytes
for(i = 0; i < 64; i++){
factorySecRegID[i] = dfmemReadByte();
}
dfmemDeselectChip();
}