参考:https://github.com/OpenNuvoton/N9H20_emWin_NonOS/tree/master/Tools/MassProduction_Tools/SpiWriter
int usiEraseSector(UINT32 addr, UINT32 secCount)
{
int volatile i;
if ((addr % (64*1024)) != 0)
return -1;
if(!g_4byte_adderss)
{
if(addr & 0xFF000000)
{
Enter4ByteMode();
g_4byte_adderss = TRUE;
PRINTF("SPI 4 Byte Address Mode Enable (R)\n");
}
else if ((addr+secCount*64*1024-1) & 0xFF000000)
{
Enter4ByteMode();
g_4byte_adderss = TRUE;
PRINTF("SPI 4 Byte Address Mode Enable (W1)\n");
}
}
for (i=0; i<secCount; i++)
{
usiWriteEnable();
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) | 0x01); /* CS0 */
/* erase command */
outpw(REG_SPI0_TX0, 0xd8);
spiTxLen(0, 0, 8);
spiActive(0);
/* address */
outpw(REG_SPI0_TX0, addr+i*64*1024);
if(g_4byte_adderss)
spiTxLen(0, 0, 32);
else
spiTxLen(0, 0, 24);
spiActive(0);
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) & 0xfe); /* CS0 */
/* check status */
usiCheckBusy();
}
return Successful;
}
int usiEraseAll(void)
{
usiWriteEnable();
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) | 0x01); /* CS0 */
outpw(REG_SPI0_TX0, 0xc7);
spiTxLen(0, 0, 8);
spiActive(0);
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) & 0xfe); /* CS0 */
/* check status */
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) | 0x01); /* CS0 */
outpw(REG_SPI0_TX0, 0x05);
spiTxLen(0, 0, 8);
spiActive(0);
/* get status */
outpw(REG_SPI0_TX0, 0xff);
spiTxLen(0, 0, 8);
spiActive(0);
if (((inpw(REG_SPI0_RX0) & 0xff) & 0x01) != 0x01)
{
outpw(REG_SPI0_SSR, inpw(REG_SPI0_SSR) & 0xfe); /* CS0 */
return 0; /* ready */
}
else
return 0xFF; /* busy */
} |