Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SerialFlash by
Diff: M25PDeviceImpl.cpp
- Revision:
- 2:6f8ab876b516
- Parent:
- 1:385965a14c7c
--- a/M25PDeviceImpl.cpp Wed Mar 02 00:28:03 2011 +0000
+++ b/M25PDeviceImpl.cpp Wed Mar 02 16:09:09 2011 +0000
@@ -1,13 +1,13 @@
-#include "ISPI.h"
+#include "ISPICommand.h"
#include <string.h>
#include "M25PDeviceImpl.h"
-const M25PDeviceImpl::DeviceProperty* M25PDeviceImpl::findMatchDevice(ISPI* pSPI)
+const M25PDeviceImpl::DeviceProperty* M25PDeviceImpl::findMatchDevice(ISPICommand* pSPICommand)
{
int manufacturerId;
int memoryType;
int memoryCapacity;
- readId(pSPI, manufacturerId, memoryType, memoryCapacity);
+ readId(pSPICommand, manufacturerId, memoryType, memoryCapacity);
struct SupportedDevice
{
@@ -46,26 +46,25 @@
return NULL;
}
-bool M25PDeviceImpl::IsSupported(ISPI* pSPI)
+bool M25PDeviceImpl::IsSupported(ISPICommand* pSPICommand)
{
- return findMatchDevice(pSPI) != NULL;
+ return findMatchDevice(pSPICommand) != NULL;
}
-M25PDeviceImpl* M25PDeviceImpl::Create(ISPI* pSPI)
+M25PDeviceImpl* M25PDeviceImpl::Create(ISPICommand* pSPICommand)
{
- const DeviceProperty* property = findMatchDevice(pSPI);
+ const DeviceProperty* property = findMatchDevice(pSPICommand);
if (property == NULL)
{
return NULL;
}
- return new M25PDeviceImpl(pSPI, *property);
+ return new M25PDeviceImpl(pSPICommand, *property);
}
-M25PDeviceImpl::M25PDeviceImpl(ISPI* pSPI, const DeviceProperty& property)
- : _pSPI(pSPI)
+M25PDeviceImpl::M25PDeviceImpl(ISPICommand* pSPICommand, const DeviceProperty& property)
+ : _pSPICommand(pSPICommand)
, _property(property)
{
- pSPI->ChangeCS(ISPI::High);
clearBlockProtection();
}
@@ -74,48 +73,39 @@
}
-void M25PDeviceImpl::readId(ISPI* pSPI, int& manufacturerId, int& memoryType, int& memoryCapacity)
+void M25PDeviceImpl::readId(ISPICommand* pSPICommand, int& manufacturerId, int& memoryType, int& memoryCapacity)
{
const static int DefaultOperationFrequency = 20000000;
- pSPI->SetFrequency(DefaultOperationFrequency);
+ pSPICommand->SetMaxFrequency(DefaultOperationFrequency);
- pSPI->ChangeCS(ISPI::Low);
- pSPI->Write(0x9f);
- manufacturerId = pSPI->Read();
- memoryType = pSPI->Read();
- memoryCapacity = pSPI->Read();
- pSPI->ChangeCS(ISPI::High);
+ unsigned char id[3];
+ pSPICommand->Read(0x9f, id, sizeof(id));
+ manufacturerId = id[0];
+ memoryType = id[1];
+ memoryCapacity = id[2];
}
int M25PDeviceImpl::readStatusRegister(void)
{
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x05);
- int result = _pSPI->Read();
- _pSPI->ChangeCS(ISPI::High);
- return result;
+ unsigned char status;
+ _pSPICommand->Read(0x05, &status, 1);
+ return status;
}
void M25PDeviceImpl::writeStatusRegister(int value)
{
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x01);
- _pSPI->Write(value);
- _pSPI->ChangeCS(ISPI::High);
+ char vb = static_cast<char>(value);
+ _pSPICommand->Write(0x01, &vb, 1);
}
void M25PDeviceImpl::writeEnable()
{
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x06);
- _pSPI->ChangeCS(ISPI::High);
+ _pSPICommand->Write(0x06);
}
void M25PDeviceImpl::writeDisable()
{
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x04);
- _pSPI->ChangeCS(ISPI::High);
+ _pSPICommand->Write(0x04);
}
void M25PDeviceImpl::clearBlockProtection(void)
@@ -152,15 +142,19 @@
return 0;
}
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x0b);
- writeAddress(address);
- _pSPI->Write(0xff);
- _pSPI->Read(buffer, length);
- _pSPI->ChangeCS(ISPI::High);
+ char param[] = { 0, 0, 0, 0xff };
+ fillAddress(param, address);
+ _pSPICommand->Read(0x0b, param, sizeof(param), buffer, length);
return length;
}
+void M25PDeviceImpl::fillAddress(char* pBuffer, int address)
+{
+ *(pBuffer + 0) = (address & 0xff0000) >> 16;
+ *(pBuffer + 1) = (address & 0x00ff00) >> 8;
+ *(pBuffer + 2) = (address & 0x0000ff);
+}
+
int M25PDeviceImpl::Write(int address, const void* buffer, int length)
{
if (address >= GetCapacity())
@@ -221,32 +215,22 @@
{
writeEnable();
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0xc7);
- _pSPI->ChangeCS(ISPI::High);
-
+ _pSPICommand->Write(0xc7);
+
while (readStatusRegister() & 0x01)
;
clearBlockProtection();
}
-void M25PDeviceImpl::writeAddress(int address)
-{
- _pSPI->Write((address & 0xff0000) >> 16);
- _pSPI->Write((address & 0xff00) >> 8);
- _pSPI->Write(address & 0xff);
-}
-
void M25PDeviceImpl::pageProgram(int address, const void* buffer)
{
writeEnable();
- _pSPI->ChangeCS(ISPI::Low);
- _pSPI->Write(0x02);
- writeAddress(address);
- _pSPI->Write(buffer, 256);
- _pSPI->ChangeCS(ISPI::High);
+ char param[256 + 3];
+ fillAddress(param, address);
+ memcpy(param + 3, buffer, 256);
+ _pSPICommand->Write(0x02, param, sizeof(param));
while (readStatusRegister() & 0x01)
;
