SPI Flash Library for AT45
Dependents: AT45_spi_flash_example AT45_spi_flash_example PLC1608-V1 RL0201-V1
Fork of AT45 by
Diff: AT45.cpp
- Revision:
- 1:f8e562ae5cc3
- Parent:
- 0:2e9d45485414
--- a/AT45.cpp Tue Jan 10 09:20:38 2012 +0000
+++ b/AT45.cpp Tue Nov 14 14:53:37 2017 +0000
@@ -27,7 +27,7 @@
// Public functions
//=============================================================================
-AT45::AT45(SPI& spi, PinName ncs) : _spi(spi), _ncs(ncs)
+AT45::AT45(SPI *spi, PinName ncs) : _spi(spi), _ncs(ncs)
{
_pages = -1; // number of pages
@@ -86,13 +86,13 @@
// this is done directly as we are reading back an entire buffer, and this can be done more optimally than using _sramread
_select();
- _spi.write(0xd4);
+ _spi->write(0xd4);
_sendaddr (0x0);
- _spi.write (0x0); // dont care byte
+ _spi->write (0x0); // dont care byte
for(int i=0; i<_pagesize ;i++)
{
- data[i] = _spi.write (0x0);
+ data[i] = _spi->write (0x0);
}
_deselect();
@@ -194,12 +194,12 @@
}
_select();
- _spi.write(0x84); // writing to buffer #1
+ _spi->write(0x84); // writing to buffer #1
_sendaddr (0); // we are writing to the entire buffer
for(int i=0; i<_pagesize ;i++)
{
- _spi.write (data[i]);
+ _spi->write (data[i]);
}
_deselect();
@@ -207,7 +207,7 @@
// issue command to write buffer 1 to the appropraite flash page
_select();
- _spi.write (0x83);
+ _spi->write (0x83);
_sendaddr (_getpaddr(address));
_deselect();
@@ -276,13 +276,13 @@
// this is done directly as we are reading back an entire buffer, and this can be done more optimally than using _sramread
_select();
- _spi.write(0xd4);
+ _spi->write(0xd4);
_sendaddr (0x0);
- _spi.write (0x0); // dont care byte
+ _spi->write (0x0); // dont care byte
for(int i=0;i<256;i++)
{
- data[i] = _spi.write (0x0);
+ data[i] = _spi->write (0x0);
}
_deselect();
@@ -292,13 +292,13 @@
// Now the second page is loaded, pull this out into the second half of the data buffer
// this is done directly as we are reading back an entire buffer, and this can be done more optimally than using _sramread
_select();
- _spi.write(0xd4);
+ _spi->write(0xd4);
_sendaddr (0x0);
- _spi.write (0x0); // dont care byte
+ _spi->write (0x0); // dont care byte
for(int i=0;i<256;i++)
{
- data[256+i] = _spi.write (0x0);
+ data[256+i] = _spi->write (0x0);
}
_deselect();
return (0);
@@ -316,13 +316,13 @@
// Now the page has loaded, simply transfer it from the sram buffer to the data array
// this is done directly as we are reading back an entire buffer, and this can be done more optimally than using _sramread
_select();
- _spi.write(0xd4);
+ _spi->write(0xd4);
_sendaddr (0x0);
- _spi.write (0x0); // dont care byte
+ _spi->write (0x0); // dont care byte
for(int i=0;i<512;i++)
{
- data[i] = _spi.write (0x0);
+ data[i] = _spi->write (0x0);
}
_deselect();
return (0);
@@ -343,7 +343,7 @@
// this is done directly as we are reading back an entire buffer, and this can be done more optimally than using _sramread
_select();
- _spi.write(0xd4);
+ _spi->write(0xd4);
if (page %2) // odd numbered block, read from adress 0x200
{
@@ -354,11 +354,11 @@
_sendaddr (0x0);
}
- _spi.write (0x0); // dont care byte
+ _spi->write (0x0); // dont care byte
for(int i=0;i<512;i++)
{
- data[i] = _spi.write (0x0);
+ data[i] = _spi->write (0x0);
}
_deselect();
return (0);
@@ -378,11 +378,11 @@
// fill the first buffer with the first half of the block
// do this directly, for better performance
_select();
- _spi.write(0x84); // writing to buffer #1
+ _spi->write(0x84); // writing to buffer #1
_sendaddr (0); // we are writing to the entire buffer
for(int i=0;i<256;i++) {
- _spi.write (data[i]);
+ _spi->write (data[i]);
}
_deselect();
@@ -391,11 +391,11 @@
// fill the buffer with the second half of the block
// do this directly, for better performance
_select();
- _spi.write(0x84); // writing to buffer #1
+ _spi->write(0x84); // writing to buffer #1
_sendaddr (0); // we are writing to the entire buffer
for(int i=0;i<256;i++) {
- _spi.write (data[256+i]);
+ _spi->write (data[256+i]);
}
_deselect();
@@ -409,11 +409,11 @@
// fill the first buffer with the block data
// do this directly, for better performance
_select();
- _spi.write(0x84); // writing to buffer #1
+ _spi->write(0x84); // writing to buffer #1
_sendaddr (0); // we are writing to the entire buffer
for(int i=0;i<512;i++) {
- _spi.write (data[i]);
+ _spi->write (data[i]);
}
_deselect();
@@ -421,7 +421,7 @@
// issue command to write buffer 1 to the appropraite flash page
_select();
- _spi.write (0x83);
+ _spi->write (0x83);
_sendaddr (_getpaddr(page * 512));
_deselect();
}
@@ -444,7 +444,7 @@
// Overwrite the appropriate half
// do this directly, for better performance
_select();
- _spi.write(0x84); // writing to buffer #1
+ _spi->write(0x84); // writing to buffer #1
if(page%2) // this is an odd block number, overwrite second half of buffer
{
@@ -457,7 +457,7 @@
for(int i=0;i<512;i++)
{
- _spi.write (data[i]);
+ _spi->write (data[i]);
}
_deselect();
@@ -482,10 +482,10 @@
_select();
// 4 byte command sequence
- _spi.write(0xc7);
- _spi.write(0x94);
- _spi.write(0x80);
- _spi.write(0x9a);
+ _spi->write(0xc7);
+ _spi->write(0x94);
+ _spi->write(0x80);
+ _spi->write(0x9a);
_deselect();
_busy(); // Make erase a blocking function
@@ -526,7 +526,7 @@
_busy();
_select();
- _spi.write(0x50);
+ _spi->write(0x50);
_sendaddr (address);
_deselect();
_busy();
@@ -572,7 +572,7 @@
_busy();
_select();
- _spi.write(0x81);
+ _spi->write(0x81);
_sendaddr (address);
_deselect();
_busy();
@@ -608,10 +608,10 @@
_select();
// 4 byte command sequence
- _spi.write(0x3d);
- _spi.write(0x2a);
- _spi.write(0x80);
- _spi.write(0xa6);
+ _spi->write(0x3d);
+ _spi->write(0x2a);
+ _spi->write(0x80);
+ _spi->write(0xa6);
_deselect();
_busy(); // Make erase a blocking function
@@ -628,9 +628,9 @@
{
int id = 0;
_select();
- _spi.write(0x9f);
- id = (_spi.write(0x00) << 8);
- id |= _spi.write(0x00);
+ _spi->write(0x9f);
+ id = (_spi->write(0x00) << 8);
+ id |= _spi->write(0x00);
_deselect();
return id;
}
@@ -640,8 +640,8 @@
{
int status = 0;
_select();
- _spi.write(0xd7);
- status = (_spi.write(0x00));
+ _spi->write(0xd7);
+ status = (_spi->write(0x00));
_deselect();
return status;
}
@@ -657,7 +657,7 @@
if(_deep_down_onoff == false) // Wake up from deep power down
{
_select();
- _spi.write(0xab);
+ _spi->write(0xab);
_deselect();
_deep_down = true;
// remenber to want 35uS before using the device.
@@ -666,7 +666,7 @@
{
_busy();
_select();
- _spi.write(0xb9);
+ _spi->write(0xb9);
_deselect();
_deep_down = false;
}
@@ -824,9 +824,9 @@
else
{cmd = 0x87;}
- _spi.write(cmd);
+ _spi->write(cmd);
_sendaddr (baddr);
- _spi.write (data);
+ _spi->write (data);
_deselect();
}
@@ -849,10 +849,10 @@
else
{cmd = 0xd6;}
- _spi.write(cmd);
+ _spi->write(cmd);
_sendaddr (baddr);
- _spi.write (0x0); // dont care byte
- bufdata = _spi.write (0x0);
+ _spi->write (0x0); // dont care byte
+ bufdata = _spi->write (0x0);
_deselect();
@@ -875,7 +875,7 @@
else
{cmd = 0x86;}
- _spi.write (cmd);
+ _spi->write (cmd);
_sendaddr (paddr);
_deselect();
@@ -897,7 +897,7 @@
else
{cmd = 0x55;}
- _spi.write (cmd);
+ _spi->write (cmd);
_sendaddr (paddr);
_deselect();
}
@@ -915,17 +915,17 @@
_select();
- _spi.write (0xd2); // Direct read command
+ _spi->write (0xd2); // Direct read command
_sendaddr (addr);
// 4 dont care bytes
- _spi.write (0x00);
- _spi.write (0x00);
- _spi.write (0x00);
- _spi.write (0x00);
+ _spi->write (0x00);
+ _spi->write (0x00);
+ _spi->write (0x00);
+ _spi->write (0x00);
// this one clocks the data
- data = _spi.write (0x00);
+ data = _spi->write (0x00);
_deselect();
_busy();
@@ -980,7 +980,7 @@
// Sends the three lest significant bytes of the supplied address
void AT45::_sendaddr (int address)
{
- _spi.write(address >> 16);
- _spi.write(address >> 8);
- _spi.write(address);
+ _spi->write(address >> 16);
+ _spi->write(address >> 8);
+ _spi->write(address);
}
\ No newline at end of file
