-
Notifications
You must be signed in to change notification settings - Fork 2
/
mem-at45db041.h
319 lines (235 loc) · 9.54 KB
/
mem-at45db041.h
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
/*
IBEX UK LTD http://www.ibexuk.com
Electronic Product Design Specialists
RELEASED SOFTWARE
The MIT License (MIT)
Copyright (c) 2013, IBEX UK Ltd, http://ibexuk.com
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//Project Name: ATMEL AT45DB041 DRIVER
//*************************************
//*************************************
//********** DRIVER REVISION **********
//*************************************
//*************************************
//
//V1.00
//- Original release
//*********************************
//*********************************
//********** USAGE NOTES **********
//*********************************
//*********************************
//Include this header file in any .c files within your project from which you wish to use it's functions.
//---------------------------------------
//----- SPI PORT SETUP REQUIREMENTS -----
//---------------------------------------
//Up to 33MHz SPI port (for continuous read which is limiting speed spec of this device)
//Data latched on rising edge of SCK, data is output on the falling edge of SCK.
/*
Example setup for PIC18 family:-
//----- SETUP SSP1 -----
//Used for: Flash memory IC
//CLK idle in low state
//AT45DB041 clocks in data on the rising edge, outputs data on the falling edge
SSPSTAT = 0b01000000;
SSPCON1 = 0b00100000; //Fosc / 4 = 10MHz
*/
/*
Example setup for DSPIC33FJ family:-
//----- SETUP SPI 1 -----
//Used for: Flash memory IC
w_temp = SPI1BUF;
SPI1STAT = 0;
SPI1CON1 = 0x0320; //SPI in master mode
//Data is valid on the rising edge of the clock (Transmit occurs on transition from active to idle clock state)
//Clock low in idle bus state
SPI1CON1bits.PPRE1 = 1; //Prescallers 4:1 1:1 = 10MHz (max possible from this device)
SPI1CON1bits.PPRE0 = 0;
SPI1CON1bits.SPRE2 = 1;
SPI1CON1bits.SPRE1 = 1;
SPI1CON1bits.SPRE0 = 1;
SPI1CON2 = 0;
SPI1STATbits.SPIEN = 1; //Enable the port
*/
/*
Example setup for PIC24HJ family:-
//----- SETUP SPI 1 -----
//Used for: Flash memory IC
w_temp = SPI1BUF;
SPI1STAT = 0;
SPI1CON1 = 0b0000001100100001; //SPI in master mode (SPI1STATbits.SPIEN must be 0 to write to this register)
//Data is valid on the rising edge of the clock (Transmit occurs on transition from active to idle clock state)
//Clock low in idle bus state
SPI1CON1bits.PPRE1 = 1; //Prescallers 4:1 1:1 = 10MHz (max possible from this device)
SPI1CON1bits.PPRE0 = 0;
SPI1CON1bits.SPRE2 = 1;
SPI1CON1bits.SPRE1 = 1;
SPI1CON1bits.SPRE0 = 1;
SPI1CON2 = 0;
SPI1STATbits.SPIEN = 1; //Enable the port
*/
/*
Example setup for PIC32 family:-
//----- SETUP SPI 1 -----
//Used for: Flash Memory
//Idle low, active high
//Transmit on active to idle (falling edge)
SpiChnOpen(SPI_CHANNEL1, (SPI_CON_MSTEN | SPI_OPEN_CKE_REV | SPI_CON_MODE8), 2); //40MHz fpb / 2 = 20MHz
*/
//##### ADDING A FUNCTION TO READ INDVIDIUAL BYTES USING A LOCAL BUFFER FOR A PAGE OF MEMORY#####
//Add to INTERNAL ONLY MEMORY DEFINITIONS:-
/*
DWORD flash_buffer_address = 0xffffffff;
BYTE flash_buffer[256];
*/
//Add to fucntion definitions
/*
BYTE flash_read_byte (DWORD address);
extern BYTE flash_read_byte (DWORD address);
*/
//Add this function:-
//*******************************
//*******************************
//********** READ BYTE **********
//*******************************
//*******************************
/*
BYTE flash_read_byte (DWORD address)
{
//LOAD PAGE IF NECESSARY
if ((flash_buffer_address & 0xffffff00) != (address & 0xffffff00))
{
flash_buffer_address = (address & 0xffffff00);
flash_read_page(flash_buffer_address, &flash_buffer[0]);
}
return(flash_buffer[(address & 0x000000ff)]);
}
*/
//*****************************
//*****************************
//********** DEFINES **********
//*****************************
//*****************************
#ifndef FLASH_C_INIT //Do only once the first time this file is used
#define FLASH_C_INIT
//----------------------------
//----- FLASH MEMORY MAP -----
//----------------------------
//AT45DB041, FLASH MEMORY
//524288bytes x 8 (512KB)
//2,048 pages of 256 bytes (To avoid non binary complexities this driver only uses 256 bytes of each page instead of the 264 which are actually available)
//0x00000 - 0x7ffff
#define FLASH_END_ADDRESS 0x7ffff
#define FLASH_BLOCK_SIZE 0x800 //A block is 8 pages
//##################
//##### PIC 18 #####
//##################
/*
#define FLASH_CLR_WDT ClrWdt() //Clear watchdog timer
//----- PINS -----
#define FLASH_CS(state) LATCbits.LATC0 = state
//----- SPI BUS READ WRITE DEFINES -----
#define FLASH_SPI_BUF_FULL PIR1bits.SSPIF //Flag that the SPI receive buffer contains a received byte, also signifying that transmit is complete
#define FLASH_SPI_READ_BUFFER SSPBUF //Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data) SSPBUF = data //Write byte to SPI bus
#define DO_CS_HIGH_DELAY Nop(); Nop() //Delay of at least 50nS
*/
//#####################
//##### DSPIC33FJ #####
//#####################
/*
#define FLASH_CLR_WDT ClrWdt() //Clear watchdog timer
//----- PINS -----
#define FLASH_CS(state) _LATF5 = state
//----- SPI BUS READ WRITE DEFINES -----
#define FLASH_SPI_BUF_FULL IFS0bits.SPI1IF //Flag that the SPI receive buffer contains a received byte, also signifying that transmit is complete
#define FLASH_SPI_READ_BUFFER SPI1BUF //Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data) IFS0bits.SPI1IF = 0; SPI1BUF = data //Write byte to SPI bus
#define DO_CS_HIGH_DELAY Nop(); Nop() //Delay of at least 50nS
*/
//###################
//##### PIC24HJ #####
//###################
#define FLASH_CLR_WDT ClrWdt() //Clear watchdog timer
//----- PINS -----
#define FLASH_CS(state) _LATG15 = state
//----- SPI BUS READ WRITE DEFINES -----
#define FLASH_SPI_BUF_FULL IFS0bits.SPI1IF //Flag that the SPI receive buffer contains a received byte, also signifying that transmit is complete
#define FLASH_SPI_READ_BUFFER SPI1BUF //Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data) IFS0bits.SPI1IF = 0; SPI1BUF = data //Write byte to SPI bus
#define DO_CS_HIGH_DELAY Nop(); Nop() //Delay of at least 50nS
//#################
//##### PIC32 #####
//#################
/*
#define FLASH_CLR_WDT ClearWDT() //Clear watchdog timer
//----- PINS -----
#define FLASH_CS(state) (state ? mPORTCSetBits(BIT_14) : mPORTCClearBits(BIT_14))
//----- SPI BUS READ WRITE DEFINES -----
#define FLASH_SPI_BUF_FULL SpiChnDataRdy(SPI_CHANNEL1) //>0 when the SPI receive buffer contains a received byte, also signifying that transmit is complete
#define FLASH_SPI_READ_BUFFER (BYTE)SpiChnReadC(SPI_CHANNEL1) //Register to read last received byte from
#define FLASH_SPI_WRITE_BUFFER(data) SpiChnWriteC(SPI_CHANNEL1, (unsigned int)data) //Macro to write a byte and start transmission over the SPI bus
#define DO_CS_HIGH_DELAY Nop(); Nop() //Delay of at least 50nS
*/
#endif //#ifndef FLASH_C_INIT
//*******************************
//*******************************
//********** FUNCTIONS **********
//*******************************
//*******************************
#ifdef FLASH_C
//-----------------------------------
//----- INTERNAL ONLY FUNCTIONS -----
//-----------------------------------
BYTE flash_read_status (void);
//-----------------------------------------
//----- INTERNAL & EXTERNAL FUNCTIONS -----
//-----------------------------------------
//(Also defined below as extern)
BYTE flash_check_present (void);
void flash_erase_all (void);
void flash_write_page (DWORD address, BYTE *buffer, BYTE erase_first);
void flash_read_page (DWORD address, BYTE *buffer);
BYTE flash_test_memory (void);
#else
//------------------------------
//----- EXTERNAL FUNCTIONS -----
//------------------------------
extern BYTE flash_check_present (void);
extern void flash_erase_all (void);
extern void flash_write_page (DWORD address, BYTE *buffer, BYTE erase_first);
extern void flash_read_page (DWORD address, BYTE *buffer);
extern BYTE flash_test_memory (void);
#endif
//****************************
//****************************
//********** MEMORY **********
//****************************
//****************************
#ifdef FLASH_C
//--------------------------------------------
//----- INTERNAL ONLY MEMORY DEFINITIONS -----
//--------------------------------------------
//--------------------------------------------------
//----- INTERNAL & EXTERNAL MEMORY DEFINITIONS -----
//--------------------------------------------------
//(Also defined below as extern)
#else
//---------------------------------------
//----- EXTERNAL MEMORY DEFINITIONS -----
//---------------------------------------
#endif