-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_flash_memory.c
402 lines (361 loc) · 15.5 KB
/
spi_flash_memory.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
/*
* spi_flash_memory.c
*This file contain the function definition for flash initialization & reading & writing to flash memory
* Created: 10/3/2018 1:40:40 PM
* Author: Sarthak
*/
#include "spi_flash_memory.h"
struct spi_device flash_ce ={
.id=DF_SS
};
/***********************************************************************************************************************************/
/*
* Function Name: spi_flash_init
* Description: Initializes the SPI & configures the CS pin.
*/
/***********************************************************************************************************************************/
void spi_flash_init(void)
{
spi_master_init(&SPIC);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // setting it to 100Khz.
spi_enable(&SPIC);
flash_memory_write_enable();
flash_write_status_reg(0x00); // the user can access the whole flash memory of 4MB
}
/******************************************************************************************************************************/
/*
* Read SPI Data register
*/
/******************************************************************************************************************************/
uint8_t spi_flash_read(void)
{
uint8_t data;
while(!(SPIC.STATUS & 0x80));
data=SPIC_DATA;
return data;
}
/******************************************************************************************************************************/
/*
* Write to SPI Data register
*/
/******************************************************************************************************************************/
void spi_flash_write(uint8_t value)
{
SPIC_DATA = value;
while(!(SPIC.STATUS & 0x80));
}
/**************************************************************************************************************************************/
/*
* Function name: flash_jdec_read_id
* Description: Reads Manufacturer Id, Memory type & Memory Capacity of Flash memory
*/
/*************************************************************************************************************************************/
void flash_jdec_read_id(void)
{
uint8_t data;
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); \
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x9F); // read id command for flash for getting deviceid
for(int i=0;i<4;i++)
{
data=spi_flash_read();
spi_flash_write(0xff);
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
}
/**********************************************************************************************************************************/
/*
* Function name: flash_memory_write_enable
* Description: it allows to perform any changes in the flash memory like
* writing to memory,status reg,erase,etc.
*/
/**********************************************************************************************************************************/
void flash_memory_write_enable(void)
{
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x06); // OPCODE FOR ENABLING WRITE TO MEMORY
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
}
/************************************************************************************************************************************/
/*
* Function name: flash_memory_write_disable
* Description: It does not allow to perform any changes in the flash memory like
* writing to memory,status reg,erase,etc.
*/
/************************************************************************************************************************************/
void flash_memory_write_disable(void)
{
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x04); // OPCODE FOR DISABLING WRITES TO MEMORY
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
}
/************************************************************************************************************************************/
/*
* Function name: flash_status_reg_enable
* Description: it allows to write to status reg only if memory write enable is enabled
*/
/************************************************************************************************************************************/
void flash_status_reg_enable(void)
{
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x50); // OPCODE FOR ENABLING TO WRITE TO STATUS REG.
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
}
/*********************************************************************************************************************************/
/*
* Function name: flash_read_status_reg_1
* Description: it gets the status of flash memory
*/
/**********************************************************************************************************************************/
uint8_t flash_read_status_reg_1(void)
{
uint8_t data;
uint8_t check=0x01;
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x05); // OPCODE FOR READING STATUS REG
spi_flash_read();
while(check)
{
spi_flash_write(0XFF);
data=spi_flash_read();
check=data & 0x01;
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
return data;
}
/*********************************************************************************************************************************/
/*
* Function name: flash_read_status_reg_2
* Description: it gets the status of flash memory
*/
/**********************************************************************************************************************************/
uint8_t flash_read_status_reg_2(void)
{
uint8_t data;
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x35); // OPCODE FOR READING STATUS REG
spi_flash_read();
spi_flash_write(0XFF);
data=spi_flash_read();
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
return data;
}
/**************************************************************************************************************************************/
/*
* Function name: flash_write_status_reg
* Description: writes the data to status reg. it internally calls flash_memory_write_enable(); & flash_status_reg_enable();
*/
/****************************************************************************************************************************************/
void flash_write_status_reg(uint8_t data)
{
flash_memory_write_enable();
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce); // high to low transition of CE
spi_flash_write(0x01); // OPCODE FOR WRITING TO STATUS REG
spi_flash_write(data);
spi_flash_write(data);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1); // low to high transition of CE
}
/**********************************************************************************************************************************************/
/*
* Function name : flash_clear_memory
* Description : It clears the flash memory as per the parameter send.
* Parameter : ERASE_4KB / ERASE_32KB / ERASE_64KB / ERASE_ALL
* : address
*/
/**********************************************************************************************************************************************/
void flash_clear_memory(uint8_t size,uint32_t address)
{
ds1343_interrupt_disable();
flash_memory_write_enable();
delay_ms(1);
switch(size)
{
case ERASE_4KB:
/* erasing only 4KB of memory */
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(ERASE_4KB);
spi_flash_write((address &0x00ff0000)>>16);
spi_flash_write((address &0x0000ff00)>>8);
spi_flash_write(address &0x000000ff);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
delay_ms(400);
break;
case ERASE_32KB:
/* erasing only 32KB of memory */
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(ERASE_32KB);
spi_flash_write((address &0x00ff0000)>>16);
spi_flash_write((address &0x0000ff00)>>8);
spi_flash_write(address &0x000000ff);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
break;
case ERASE_64KB:
/* erasing only 64KB of memory */
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(ERASE_64KB);
spi_flash_write((address &0x00ff0000)>>16);
spi_flash_write((address &0x0000ff00)>>8);
spi_flash_write(address &0x000000ff);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
break;
case ERASE_ALL:
/* erasing whole 4MB of memory */
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(ERASE_ALL);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
break;
default:
break;
}
flash_read_status_reg_1();
flash_memory_write_disable();
ds1343_interrupt_init();
}
/**********************************************************************************************************************************************/
/*
* Function name : flash_byte_program
* Description : it is used to write single byte of data to flash memory
* Parameter : address & data.
*/
/*********************************************************************************************************************************************/
void flash_byte_program(uint32_t address,uint8_t value)
{
flash_memory_write_enable();
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(0x02); // OPCODE FOR WRITING SINGLE BYTE
spi_flash_write((address & 0x00ff0000)>>16);
spi_flash_write((address & 0x0000ff00)>>8);
spi_flash_write(address & 0x000000ff);
spi_flash_write(value);
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
}
/************************************************************************/
/*
* Function name: flash_aai_word_program
* Description: it is used to write number of byte of data to flash memory
* Parameter: address, data & length (no of bytes).
* Note: length should be always even numbers.
*/
/************************************************************************/
void flash_aai_word_program(uint32_t address,uint8_t *value,uint8_t length)
{
uint8_t loc_len=0;
ds1343_interrupt_disable();
loc_len = (address+length)%256;
if(loc_len>=length)
{
loc_len=0;
}
length=length-loc_len;
flash_memory_write_enable();
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(0x02); // OPCODE FOR ENABLING AAI MODE WRITING
spi_flash_write((address & 0x00ff0000)>>16);
spi_flash_write((address & 0x0000ff00)>>8);
spi_flash_write(address & 0x000000ff);
for(int i=0;i<length;i++)
{
spi_flash_write(value[i]);
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
delay_ms(5);
if(loc_len>0)
{
address=address+length;
flash_memory_write_enable();
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(0x02); // OPCODE FOR ENABLING AAI MODE WRITING
spi_flash_write((address & 0x00ff0000)>>16);
spi_flash_write((address & 0x0000ff00)>>8);
spi_flash_write(address & 0x000000ff);
for(int i=length;i<(loc_len+length);i++)
{
spi_flash_write(value[i]);
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
}
flash_memory_write_disable();
ds1343_interrupt_init();
}
/**********************************************************************************************************************************************/
/*
* Function name: flash_read
* Description: it gets the data from flash memory. it runs at max 25mhz
* Parameter: address,length (no of bytes),array or pointer for storing a read data.
*/
/**********************************************************************************************************************************************/
void flash_read(uint32_t address,uint16_t length,uint8_t *value)
{
ds1343_interrupt_disable();
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(0x03); // OPCODE FOR READ MEMORY
spi_flash_write((address &0x00ff0000)>>16);
spi_flash_write((address &0x0000ff00)>>8);
spi_flash_write(address &0x000000ff);
spi_flash_read();
spi_flash_write(0xff);
//delay_s(1);
for(int i=0;i<length;i++)
{
value[i]=spi_flash_read();
spi_flash_write(0xff);
delay_ms(1);
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
ds1343_interrupt_init();
}
/*********************************************************************************************************************************************/
/*
* Function name: flash_high_speed_read
* Description: it gets the data from flash memory. it runs at max 80mhz
* Parameter: address,length (no of bytes),array or pointer for storing a read data.
*/
/*********************************************************************************************************************************************/
void flash_high_speed_read(uint32_t address,uint8_t length,uint8_t *value)
{
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
spi_select_device(&SPIC, &flash_ce);
spi_flash_write(0x0B); // OPCODE FOR HIGH SPEED READ
spi_flash_write((address & 0x00ff0000)>>16);
spi_flash_write((address & 0x0000ff00)>>8);
spi_flash_write(address & 0x000000ff);
spi_flash_write(0xff);
spi_flash_read();
for(int i=0;i<length;i++)
{
spi_flash_write(0xff);
delay_ms(1);
value[i]=spi_flash_read();
}
spi_deselect_device(&SPIC, &flash_ce);
spi_master_setup_device(&SPIC, &flash_ce,SPI_MODE_3,1000000,1);
}