-
Notifications
You must be signed in to change notification settings - Fork 0
/
IS25LP128.cpp
167 lines (149 loc) · 3.91 KB
/
IS25LP128.cpp
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
#include "IS25LP128.h"
IS25LP128::IS25LP128(int csPin) : csPin(csPin) {}
void IS25LP128::begin() {
SPI.begin();
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
}
void IS25LP128::enterQPI() {
digitalWrite(csPin, LOW);
SPI.transfer(0x35);
digitalWrite(csPin, HIGH);
checkError();
}
void IS25LP128::exitQPI() {
digitalWrite(csPin, LOW);
SPI.transfer(0xF5);
digitalWrite(csPin, HIGH);
checkError();
}
void IS25LP128::writeEnable() {
digitalWrite(csPin, LOW);
SPI.transfer(WRITE_ENABLE);
digitalWrite(csPin, HIGH);
}
void IS25LP128::writeDisable() {
digitalWrite(csPin, LOW);
SPI.transfer(WRITE_DISABLE);
digitalWrite(csPin, HIGH);
}
uint8_t IS25LP128::readStatusRegister() {
digitalWrite(csPin, LOW);
SPI.transfer(READ_STATUS_REGISTER);
uint8_t status = SPI.transfer(0);
digitalWrite(csPin, HIGH);
return status;
}
void IS25LP128::writeStatusRegister(uint8_t status) {
digitalWrite(csPin, LOW);
SPI.transfer(WRITE_STATUS_REGISTER);
SPI.transfer(status);
digitalWrite(csPin, HIGH);
checkError();
}
void IS25LP128::writeByte(uint32_t addr, uint8_t data) {
for (int i = 0; i < 3; i++) { // Retry up to 3 times
writeEnable();
digitalWrite(csPin, LOW);
SPI.transfer(PAGE_PROGRAM);
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
SPI.transfer(addr);
SPI.transfer(data);
digitalWrite(csPin, HIGH);
waitUntilNotBusy();
if (checkError()) {
continue; // If there was an error, retry the operation
}
uint8_t readBack = readByte(addr);
if (readBack == data) {
writeDisable();
return; // If the data matches, the operation was successful
}
}
// If we reach this point, the operation failed after 3 attempts
// Handle the error according to your application's requirements
}
uint8_t IS25LP128::readByte(uint32_t addr) {
digitalWrite(csPin, LOW);
SPI.transfer(READ_DATA);
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
SPI.transfer(addr);
uint8_t data = SPI.transfer(0);
digitalWrite(csPin, HIGH);
checkError();
return data;
}
void IS25LP128::eraseSector(uint32_t addr) {
writeEnable();
digitalWrite(csPin, LOW);
SPI.transfer(SECTOR_ERASE);
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
SPI.transfer(addr);
digitalWrite(csPin, HIGH);
waitUntilNotBusy();
checkError();
writeDisable();
}
void IS25LP128::eraseBlock32K(uint32_t addr) {
writeEnable();
digitalWrite(csPin, LOW);
SPI.transfer(BLOCK_ERASE_32K);
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
SPI.transfer(addr);
digitalWrite(csPin, HIGH);
waitUntilNotBusy();
checkError();
writeDisable();
}
void IS25LP128::eraseBlock64K(uint32_t addr) {
writeEnable();
digitalWrite(csPin, LOW);
SPI.transfer(BLOCK_ERASE_64K);
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
SPI.transfer(addr);
digitalWrite(csPin, HIGH);
waitUntilNotBusy();
checkError();
writeDisable();
}
void IS25LP128::eraseChip() {
writeEnable();
digitalWrite(csPin, LOW);
SPI.transfer(CHIP_ERASE);
digitalWrite(csPin, HIGH);
waitUntilNotBusy();
checkError();
writeDisable();
}
uint8_t IS25LP128::readDeviceID() {
digitalWrite(csPin, LOW);
SPI.transfer(DEVICE_ID);
uint8_t id = SPI.transfer(0);
digitalWrite(csPin, HIGH);
checkError();
return id;
}
void IS25LP128::waitUntilNotBusy() {
while (readStatusRegister() & 0x01); // Wait for the operation to finish
}
bool IS25LP128::checkError() {
uint8_t status = readStatusRegister();
if (status & 0x20) {
resetDevice();
return true; // An error occurred
}
return false; // No error
}
void IS25LP128::resetDevice() {
digitalWrite(csPin, LOW);
SPI.transfer(ENABLE_RESET);
digitalWrite(csPin, HIGH);
digitalWrite(csPin, LOW);
SPI.transfer(RESET_DEVICE);
digitalWrite(csPin, HIGH);
}