Hi Jeremy,
It is the sample code: lpcspifilib
First i run this code, which seems fine:
static void RunExample(void)
{
uint32_t idx;
uint32_t spifiBaseClockRate;
uint32_t maxSpifiClock;
uint16_t libVersion;
uint32_t pageAddress;
uint32_t loopBytes;
uint32_t bytesRemaining;
uint32_t deviceByteCount;
SPIFI_HANDLE_T *pSpifi;
SPIFI_ERR_T errCode;
static uint32_t buffer[TEST_BUFFSIZE / sizeof(uint32_t)];
/* Report the library version to start with */
libVersion = spifiGetLibVersion();
DEBUGOUT("\r\n\r\nSPIFI Lib Version %02d%02d\r\n", ((libVersion >> 8) & 0xff), (libVersion & 0xff));
/* set the blink rate while testing */
setLedBlinkRate(500);
/* Setup SPIFI FLASH pin muxing (QUAD) */
Chip_SCU_SetPinMuxing(spifipinmuxing, sizeof(spifipinmuxing) / sizeof(PINMUX_GRP_T));
/* SPIFI base clock will be based on the main PLL rate and a divider */
spifiBaseClockRate = Chip_Clock_GetClockInputHz(CLKIN_MAINPLL);
/* Setup SPIFI clock to run around 1Mhz. Use divider E for this, as it allows
higher divider values up to 256 maximum) */
Chip_Clock_SetDivider(CLK_IDIV_E, CLKIN_MAINPLL, ((spifiBaseClockRate / 1000000) + 1));
Chip_Clock_SetBaseClock(CLK_BASE_SPIFI, CLKIN_IDIVE, true, false);
DEBUGOUT("SPIFI clock rate %d\r\n", Chip_Clock_GetClockInputHz(CLKIN_IDIVE));
/* Initialize the spifi library. This registers the device family and detects the part */
pSpifi = initializeSpifi();
/* Get some info needed for the application */
maxSpifiClock = spifiDevGetInfo(pSpifi, SPIFI_INFO_MAXCLOCK);
/* Get info */
DEBUGOUT("Device family = %s\r\n", spifiDevGetFamilyName(pSpifi));
DEBUGOUT("Capabilities = 0x%x\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_CAPS));
DEBUGOUT("Device size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_DEVSIZE));
DEBUGOUT("Max Clock Rate = %d\r\n", maxSpifiClock);
DEBUGOUT("Erase blocks = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKS));
DEBUGOUT("Erase block size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE));
DEBUGOUT("Erase sub-blocks = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_SUBBLOCKS));
DEBUGOUT("Erase sub-blocksize = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_SUBBLOCKSIZE));
DEBUGOUT("Write page size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE));
DEBUGOUT("Max single readsize = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_MAXREADSIZE));
DEBUGOUT("Current dev status = 0x%x\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_STATUS));
DEBUGOUT("Current options = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_OPTIONS));
/* Setup SPIFI clock to at the maximum interface rate the detected device
can use. This should be done after device init. */
Chip_Clock_SetDivider(CLK_IDIV_E, CLKIN_MAINPLL, ((spifiBaseClockRate / maxSpifiClock) + 1));
DEBUGOUT("SPIFI final Rate = %d\r\n", Chip_Clock_GetClockInputHz(CLKIN_IDIVE));
DEBUGOUT("\r\n");
/* start by unlocking the device */
DEBUGOUT("Unlocking device...\r\n");
errCode = spifiDevUnlockDevice(pSpifi);
if (errCode != SPIFI_ERR_NONE) {
fatalError("unlockDevice", errCode);
}
/* Next erase everything */
DEBUGOUT("Erasing device...\r\n");
errCode = spifiErase(pSpifi, 0, spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKS));
if (errCode != SPIFI_ERR_NONE) {
fatalError("EraseBlocks", errCode);
}
pageAddress = SPIFLASH_BASE_ADDRESS;
deviceByteCount = spifiDevGetInfo(pSpifi, SPIFI_INFO_DEVSIZE);
/* Enable quad. If not supported it will be ignored */
spifiDevSetOpts(pSpifi, SPIFI_OPT_USE_QUAD, true);
/* Enter memMode */
spifiDevSetMemMode(pSpifi, true);
DEBUGOUT("Verifying device erased...\r\n");
for (idx = 0; idx < deviceByteCount; idx += sizeof(uint32_t)) {
if ( ((uint32_t *) pageAddress)[(idx >> 2)] != 0xffffffff) {
fatalError("EraseDevice verify", SPIFI_ERR_GEN);
}
}
spifiDevSetMemMode(pSpifi, false);
/* fill the buffer with 0x5a bytes */
for (idx = 0; idx < TEST_BUFFSIZE; ++idx) {
((uint8_t *) buffer)[idx] = 0x5a;
}
/* Get the maximum amount we can write and check against our buffer.
If larger, restrict to our buffer size */
loopBytes = spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE);
if (loopBytes > TEST_BUFFSIZE) {
loopBytes = TEST_BUFFSIZE;
}
pageAddress = spifiGetAddrFromBlock(pSpifi, 0);
bytesRemaining = spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE);
/* Write the Sector using the buffer */
DEBUGOUT("Writing Sector 0...\r\n");
while (bytesRemaining) {
if (loopBytes > bytesRemaining) {
loopBytes = bytesRemaining;
}
errCode = spifiDevPageProgram(pSpifi, pageAddress, buffer, loopBytes);
if (errCode != SPIFI_ERR_NONE) {
fatalError("WriteBlock 0", errCode);
}
bytesRemaining -= loopBytes;
pageAddress += loopBytes;
}
/* Get the maximum amount we can write and check against our buffer.
If larger, restrict to our buffer size */
loopBytes = spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE);
if (loopBytes > TEST_BUFFSIZE) {
loopBytes = TEST_BUFFSIZE;
}
pageAddress = spifiGetAddrFromBlock(pSpifi, 0);
bytesRemaining = spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE);
/* Read the sector and validate it using DevRead api*/
DEBUGOUT("Verifying Sector 0...\r\n");
while (bytesRemaining) {
if (loopBytes > bytesRemaining) {
loopBytes = bytesRemaining;
}
errCode = spifiDevRead(pSpifi, pageAddress, buffer, loopBytes);
if (errCode != SPIFI_ERR_NONE) {
fatalError("WriteBlock 0", errCode);
}
/* Read the buffer and make sure it is programmed */
for (idx = 0; idx < loopBytes; ++idx) {
if (((uint8_t *) buffer)[idx] != 0x5a) {
fatalError("Verify block 0", SPIFI_ERR_GEN);
}
}
bytesRemaining -= loopBytes;
pageAddress += loopBytes;
}
/* Done, de-init will enter memory mode */
spifiDevDeInit(pSpifi);
/* indicate test complete serialy and with solid ON led */
DEBUGOUT("Complete.\r\n");
setLedBlinkRate(0);
while (1) {
__WFI();
}
}
Then I run the same code, but with commented erasing and writing functions. When I check the buffer after reading, it is full of 0xFF instead of 0x5A.
static void RunExample(void)
{
uint32_t idx;
uint32_t spifiBaseClockRate;
uint32_t maxSpifiClock;
uint16_t libVersion;
uint32_t pageAddress;
uint32_t loopBytes;
uint32_t bytesRemaining;
uint32_t deviceByteCount;
SPIFI_HANDLE_T *pSpifi;
SPIFI_ERR_T errCode;
static uint32_t buffer[TEST_BUFFSIZE / sizeof(uint32_t)];
/* Report the library version to start with */
libVersion = spifiGetLibVersion();
DEBUGOUT("\r\n\r\nSPIFI Lib Version %02d%02d\r\n", ((libVersion >> 8) & 0xff), (libVersion & 0xff));
/* set the blink rate while testing */
setLedBlinkRate(500);
/* Setup SPIFI FLASH pin muxing (QUAD) */
Chip_SCU_SetPinMuxing(spifipinmuxing, sizeof(spifipinmuxing) / sizeof(PINMUX_GRP_T));
/* SPIFI base clock will be based on the main PLL rate and a divider */
spifiBaseClockRate = Chip_Clock_GetClockInputHz(CLKIN_MAINPLL);
/* Setup SPIFI clock to run around 1Mhz. Use divider E for this, as it allows
higher divider values up to 256 maximum) */
Chip_Clock_SetDivider(CLK_IDIV_E, CLKIN_MAINPLL, ((spifiBaseClockRate / 1000000) + 1));
Chip_Clock_SetBaseClock(CLK_BASE_SPIFI, CLKIN_IDIVE, true, false);
DEBUGOUT("SPIFI clock rate %d\r\n", Chip_Clock_GetClockInputHz(CLKIN_IDIVE));
/* Initialize the spifi library. This registers the device family and detects the part */
pSpifi = initializeSpifi();
/* Get some info needed for the application */
maxSpifiClock = spifiDevGetInfo(pSpifi, SPIFI_INFO_MAXCLOCK);
/* Get info */
DEBUGOUT("Device family = %s\r\n", spifiDevGetFamilyName(pSpifi));
DEBUGOUT("Capabilities = 0x%x\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_CAPS));
DEBUGOUT("Device size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_DEVSIZE));
DEBUGOUT("Max Clock Rate = %d\r\n", maxSpifiClock);
DEBUGOUT("Erase blocks = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKS));
DEBUGOUT("Erase block size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE));
DEBUGOUT("Erase sub-blocks = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_SUBBLOCKS));
DEBUGOUT("Erase sub-blocksize = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_SUBBLOCKSIZE));
DEBUGOUT("Write page size = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE));
DEBUGOUT("Max single readsize = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_MAXREADSIZE));
DEBUGOUT("Current dev status = 0x%x\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_STATUS));
DEBUGOUT("Current options = %d\r\n", spifiDevGetInfo(pSpifi, SPIFI_INFO_OPTIONS));
/* Setup SPIFI clock to at the maximum interface rate the detected device
can use. This should be done after device init. */
Chip_Clock_SetDivider(CLK_IDIV_E, CLKIN_MAINPLL, ((spifiBaseClockRate / maxSpifiClock) + 1));
DEBUGOUT("SPIFI final Rate = %d\r\n", Chip_Clock_GetClockInputHz(CLKIN_IDIVE));
DEBUGOUT("\r\n");
/* start by unlocking the device */
DEBUGOUT("Unlocking device...\r\n");
errCode = spifiDevUnlockDevice(pSpifi);
if (errCode != SPIFI_ERR_NONE) {
fatalError("unlockDevice", errCode);
}
/* Next erase everything */
/*DEBUGOUT("Erasing device...\r\n");
errCode = spifiErase(pSpifi, 0, spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKS));
if (errCode != SPIFI_ERR_NONE) {
fatalError("EraseBlocks", errCode);
}*/
pageAddress = SPIFLASH_BASE_ADDRESS;
deviceByteCount = spifiDevGetInfo(pSpifi, SPIFI_INFO_DEVSIZE);
/* Enable quad. If not supported it will be ignored */
spifiDevSetOpts(pSpifi, SPIFI_OPT_USE_QUAD, true);
/* Enter memMode */
spifiDevSetMemMode(pSpifi, true);
/*DEBUGOUT("Verifying device erased...\r\n");
for (idx = 0; idx < deviceByteCount; idx += sizeof(uint32_t)) {
if ( ((uint32_t *) pageAddress)[(idx >> 2)] != 0xffffffff) {
fatalError("EraseDevice verify", SPIFI_ERR_GEN);
}
}*/
spifiDevSetMemMode(pSpifi, false);
/* fill the buffer with 0x5a bytes */
for (idx = 0; idx < TEST_BUFFSIZE; ++idx) {
((uint8_t *) buffer)[idx] = 0x5a;
}
/* Get the maximum amount we can write and check against our buffer.
If larger, restrict to our buffer size */
loopBytes = spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE);
if (loopBytes > TEST_BUFFSIZE) {
loopBytes = TEST_BUFFSIZE;
}
pageAddress = spifiGetAddrFromBlock(pSpifi, 0);
bytesRemaining = spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE);
/* Write the Sector using the buffer */
DEBUGOUT("Writing Sector 0...\r\n");
while (bytesRemaining) {
if (loopBytes > bytesRemaining) {
loopBytes = bytesRemaining;
}
//errCode = spifiDevPageProgram(pSpifi, pageAddress, buffer, loopBytes);
if (errCode != SPIFI_ERR_NONE) {
fatalError("WriteBlock 0", errCode);
}
bytesRemaining -= loopBytes;
pageAddress += loopBytes;
}
/* Get the maximum amount we can write and check against our buffer.
If larger, restrict to our buffer size */
loopBytes = spifiDevGetInfo(pSpifi, SPIFI_INFO_PAGESIZE);
if (loopBytes > TEST_BUFFSIZE) {
loopBytes = TEST_BUFFSIZE;
}
pageAddress = spifiGetAddrFromBlock(pSpifi, 0);
bytesRemaining = spifiDevGetInfo(pSpifi, SPIFI_INFO_ERASE_BLOCKSIZE);
/* Read the sector and validate it using DevRead api*/
DEBUGOUT("Verifying Sector 0...\r\n");
while (bytesRemaining) {
if (loopBytes > bytesRemaining) {
loopBytes = bytesRemaining;
}
errCode = spifiDevRead(pSpifi, pageAddress, buffer, loopBytes);
if (errCode != SPIFI_ERR_NONE) {
fatalError("WriteBlock 0", errCode);
}
/* Read the buffer and make sure it is programmed */
for (idx = 0; idx < loopBytes; ++idx) {
if (((uint8_t *) buffer)[idx] != 0x5a) {
fatalError("Verify block 0", SPIFI_ERR_GEN);
}
}
bytesRemaining -= loopBytes;
pageAddress += loopBytes;
}
/* Done, de-init will enter memory mode */
spifiDevDeInit(pSpifi);
/* indicate test complete serialy and with solid ON led */
DEBUGOUT("Complete.\r\n");
setLedBlinkRate(0);
while (1) {
__WFI();
}
}
Thank you so much for help.