Skip to content

Commit e1f258c

Browse files
committed
v1.0 final
1 parent 5519d95 commit e1f258c

5 files changed

Lines changed: 272 additions & 167 deletions

File tree

SPIFlash.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define SUSPEND 0x75
3737
#define ID 0x90
3838
#define RESUME 0x7A
39-
#define JEDECID 0x9F
39+
#define JEDECID 0x9f
4040
#define RELEASE 0xAB
4141
#define POWERDOWN 0xB9
4242
#define BLOCK64ERASE 0xD8
@@ -47,25 +47,38 @@
4747
// Currently library works only with W25Q80BV
4848
#define CAPACITY 1L * 1024L * 1024L
4949

50-
#define CHIP_SELECT *cs_port &= ~cs_mask;
51-
#define CHIP_DESELECT *cs_port |= cs_mask;
50+
//#define CHIP_SELECT *cs_port |= ~cs_mask;
51+
//#define CHIP_DESELECT *cs_port &= cs_mask;
5252
#define xfer(n) SPI.transfer(n)
5353

5454
// Constructor
5555
SPIFlash::SPIFlash(uint8_t cs) {
56-
cs_mask = digitalPinToBitMask(cs);
56+
//cs_mask = digitalPinToBitMask(cs);
5757
SPI.begin();
5858
SPI.setDataMode(0);
5959
SPI.setBitOrder(MSBFIRST);
60+
chipSelect = cs;
61+
pinMode(cs, OUTPUT);
6062
}
6163

6264
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
6365
// Private functions used by read, write and erase operations //
6466
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
6567

68+
//Selects chip
69+
void SPIFlash::_chipSelect() {
70+
digitalWrite(chipSelect, HIGH);
71+
digitalWrite(chipSelect, LOW);
72+
}
73+
74+
//Deselects chip
75+
void SPIFlash::_chipDeselect() {
76+
digitalWrite(chipSelect, HIGH);
77+
}
78+
6679
// Select chip and issue command - data to follow
6780
void SPIFlash::_cmd(uint8_t c) {
68-
CHIP_SELECT
81+
_chipSelect();
6982
(void)xfer(c);
7083
}
7184

@@ -77,7 +90,7 @@ boolean SPIFlash::_notBusy(uint32_t timeout) {
7790
do {
7891
_cmd(READSTAT1);
7992
state = xfer(0);
80-
CHIP_DESELECT
93+
_chipDeselect();
8194
if((millis()-startTime) > timeout)
8295
return false;
8396
} while(state & BUSY);
@@ -89,12 +102,12 @@ boolean SPIFlash::_writeEnable(void) {
89102
uint8_t state;
90103

91104
_cmd(WRITEENABLE);
92-
CHIP_DESELECT
105+
_chipDeselect();
93106

94107
//verifies that WRITE is enabled
95108
_cmd(READSTAT1);
96109
state = xfer(0);
97-
CHIP_DESELECT
110+
_chipDeselect();
98111
return (state & WRTEN) ? true : false;
99112
}
100113

@@ -105,7 +118,7 @@ boolean SPIFlash::_writeEnable(void) {
105118
// Erase Security Register and Program Security register
106119
boolean SPIFlash::_writeDisable(void) {
107120
_cmd(WRITEDISABLE);
108-
CHIP_DESELECT
121+
_chipDeselect();
109122
}
110123

111124

@@ -115,7 +128,7 @@ boolean SPIFlash::_getJedecId (byte *b1, byte *b2, byte *b3) {
115128
*b1 = xfer(0); // manufacturer id
116129
*b2 = xfer(0); // memory type
117130
*b3 = xfer(0); // capacity
118-
CHIP_DESELECT
131+
_chipDeselect();
119132
if (!_notBusy())
120133
return false;
121134
return true;
@@ -134,7 +147,7 @@ boolean SPIFlash::_readPage(uint16_t page_number, uint8_t *page_buffer) {
134147
for (int a = 0; a < 256; ++a) {
135148
page_buffer[a] = xfer(0);
136149
}
137-
CHIP_DESELECT
150+
_chipDeselect();
138151

139152
return true;
140153
}
@@ -151,7 +164,7 @@ boolean SPIFlash::_writePage(uint16_t page_number, uint8_t *page_buffer) {
151164
for (int a = 0; a < 256; ++a) {
152165
xfer(page_buffer[a]);
153166
}
154-
CHIP_DESELECT
167+
_chipDeselect();
155168
delay(3); // Maximum page program time - Ref. datasheet
156169

157170
if(!_notBusy())
@@ -186,8 +199,9 @@ uint32_t SPIFlash::getID(void) {
186199

187200
byte b1, b2, b3;
188201
_getJedecId(&b1, &b2, &b3);
189-
uint32_t combined = ((b1<<16)|(b2<<8)|b3);
190-
return combined;
202+
char buffer[128];
203+
sprintf(buffer, "Manufacturer ID: %02xh\nMemory Type: %02xh\nCapacity: %02xh", b1, b2, b3);
204+
Serial.println(buffer);
191205
}
192206

193207

@@ -213,7 +227,7 @@ uint8_t SPIFlash::readNextByte(void) {
213227

214228
//Stops read operation. Should be called after all the required data is read from repeated readNextByte() calls
215229
void SPIFlash::endRead(void) {
216-
CHIP_DESELECT
230+
_chipDeselect();
217231
}
218232

219233
//Reads a byte of data from a specific location in a page. Takes two arguements -
@@ -247,25 +261,15 @@ boolean SPIFlash::writePage(uint16_t page_number, uint8_t *page_buffer) {
247261
uint8_t temp_buffer[256];
248262
char buffer[80];
249263

250-
sprintf(buffer, "Reading page (%04x)", page_number);
251-
Serial.println(buffer);
252-
_readPage(page_number, temp_buffer);
253-
_printPageBytes(temp_buffer);
254-
255264
_writePage(page_number, page_buffer);
256-
sprintf(buffer, "Writing page (%04x) done", page_number);
257-
Serial.println(buffer);
258-
259-
sprintf(buffer, "Reading page (%04x)", page_number);
260-
Serial.println(buffer);
261265
_readPage(page_number, temp_buffer);
262-
_printPageBytes(temp_buffer);
263266

264267
for(int a=0; a<256; ++a) {
265268
if(!temp_buffer[a] == page_buffer[a])
266269
return false;
267270
}
268-
271+
sprintf(buffer, "Writing page (%04x) done", page_number);
272+
Serial.println(buffer);
269273
return true;
270274
}
271275

@@ -278,7 +282,7 @@ boolean SPIFlash::eraseSector(uint32_t address) {
278282
(void)xfer(address >> 16);
279283
(void)xfer(address >> 8);
280284
(void)xfer(0);
281-
CHIP_DESELECT
285+
_chipDeselect();
282286

283287
if(!_notBusy(1000L))
284288
return false; //Datasheet says erasing a sector takes 400ms max
@@ -300,7 +304,7 @@ boolean SPIFlash::eraseBlock32K(uint32_t address) {
300304
(void)xfer(address >> 16);
301305
(void)xfer(address >> 8);
302306
(void)xfer(0);
303-
CHIP_DESELECT
307+
_chipDeselect();
304308

305309
if(!_notBusy(1000L))
306310
return false; //Datasheet says erasing a sector takes 400ms max
@@ -322,7 +326,7 @@ boolean SPIFlash::eraseBlock64K(uint32_t address) {
322326
(void)xfer(address >> 16);
323327
(void)xfer(address >> 8);
324328
(void)xfer(0);
325-
CHIP_DESELECT
329+
_chipDeselect();
326330

327331
if(!_notBusy(1000L))
328332
return false; //Datasheet says erasing a sector takes 400ms max
@@ -341,7 +345,7 @@ boolean SPIFlash::eraseChip(void) {
341345
return false;
342346

343347
_cmd(CHIPERASE);
344-
CHIP_DESELECT
348+
_chipDeselect();
345349

346350
if(!_notBusy(10000L))
347351
return false; //Datasheet says erasing chip takes 6s max
@@ -364,7 +368,7 @@ boolean SPIFlash::suspendProg(void) {
364368
return false;
365369

366370
_cmd(SUSPEND);
367-
CHIP_DESELECT
371+
_chipDeselect();
368372

369373
delay(20); //Max suspend enable time according to the Datasheet
370374

@@ -378,7 +382,7 @@ boolean SPIFlash::resumeProg(void) {
378382
return false;
379383

380384
_cmd(RESUME);
381-
CHIP_DESELECT
385+
_chipDeselect();
382386

383387
if(_notBusy())
384388
return false;
@@ -395,7 +399,7 @@ boolean SPIFlash::powerDown(void) {
395399
return false;
396400

397401
_cmd(POWERDOWN);
398-
CHIP_DESELECT
402+
_chipDeselect();
399403

400404
delay(3); //Max powerDown enable time according to the Datasheet
401405

@@ -414,7 +418,7 @@ boolean SPIFlash::powerUp(void) {
414418
return false;
415419

416420
_cmd(RELEASE);
417-
CHIP_DESELECT
421+
_chipDeselect();
418422

419423
delay(3); //Max release enable time according to the Datasheet
420424

SPIFlash.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class SPIFlash {
5050

5151

5252
private:
53-
void _cmd(uint8_t c),
53+
void _chipSelect(void),
54+
_chipDeselect(void),
55+
_cmd(uint8_t c),
5456
_printPageBytes(uint8_t *page_buffer);
5557
boolean _notBusy(uint32_t timeout = 100L),
5658
_writeEnable(void),
@@ -61,6 +63,7 @@ class SPIFlash {
6163

6264
volatile uint8_t *cs_port;
6365
uint8_t cs_mask;
66+
uint8_t chipSelect;
6467
};
6568

6669
#endif // _SPIFLASH_H_

0 commit comments

Comments
 (0)