-
Notifications
You must be signed in to change notification settings - Fork 20
/
eeprom25AA1024.c
133 lines (92 loc) · 2.66 KB
/
eeprom25AA1024.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
#include <xc.h>
#include "eeprom25AA1024.h"
//read status register
unsigned char read_aa_status(){
nCS = 0;
unsigned char status;
writeSPIByte(AA_READ_STATUS);
status = readSPIByte();
nCS = 1;
return status;
}
//direct write to SR
void write_aa_status(unsigned char setting){
nCS = 0;
unsigned char status;
writeSPIByte(AA_WRITE_STATUS);
writeSPIByte(setting);
nCS = 1;
}
//set the WEL in SR
void enable_aa_write(){
nCS = 0;
writeSPIByte(AA_WREN);
nCS = 1;
}
//clear the WEL is SR
void disable_aa_write(){
nCS = 0;
writeSPIByte(AA_WRDI);
nCS = 1;
}
//initiate write sequence starting at 24 bit address location
void initiate_aa_write(unsigned short long address){
//need to set WEL is status
enable_aa_write();
nCS = 0;
writeSPIByte(AA_WRITE);
writeSPI24bits(address);
}
//set nCS to start write sequence and poll WEP in SR
void finalise_aa_write(){
nCS = 1;
while(read_aa_status() == 0x03){} //wait for SR WIP to clear
}
bool checkPageEdge(unsigned short long currentAddress){
if((currentAddress) % 0xFF == 0){ //page edge detected
finalise_aa_write();
return true;
}
else{
return false;
}
}
//write data to write buffer, initiate write sequence at page edges, repeat untill all data written
//this is a 16 bit write
void write_aa(unsigned int* data, unsigned char length, unsigned short long address){
initiate_aa_write(address);
//keep track of page edges and memory location as 16 bit writes are two memory locations on EEPROM but only one index change in data array
unsigned short long page_address, memory_location = 0;
for(int data_location = 0; data_location < length; data_location++){
int dataToWrite = data[data_location];
unsigned char b1, b2;
//write first half of data
b1 = writeSPIByte(dataToWrite >> 8);
//check page edge
if(checkPageEdge(address+memory_location)){
initiate_aa_write(address+memory_location+1); //restart write
}
//increase location
memory_location++;
//write 2nd half
b2 = writeSPIByte(dataToWrite);
//check page edge
if(checkPageEdge(address+memory_location)){
initiate_aa_write(address+memory_location+1); //restart write
}
//increase location
memory_location++;
}
finalise_aa_write();
}
//read from memory in 16 bit reads
void read_aa(unsigned int* data, unsigned char length, unsigned short long address){
nCS = 0;
writeSPIByte(AA_READ);
writeSPI24bits(address);
//count in data for length of array - as reads are 16 bit don't need to worry about double incrementing in the memory
for(int i = 0; i < length; i++){
data[i] = readSPIWord();
}
nCS = 1;
}