/* Main */ int main(void) { uint32_t u32ByteCount, u32FlashAddress, u32PageNumber; uint32_t nError = 0; uint16_t u16ID; /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); /* Configure SPI_FLASH_PORT as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 2MHz */ SPI_Open(SPI_FLASH_PORT, SPI_MASTER, SPI_MODE_0, 8, 2000000); /* Enable the automatic hardware slave select function. Select the SS0 pin and configure as low-active. */ SPI_EnableAutoSS(SPI_FLASH_PORT, SPI_SS0, SPI_SS_ACTIVE_LOW); SPI_TRIGGER(SPI_FLASH_PORT); printf("\n+------------------------------------------------------------------------+\n"); printf("| NUC472/NUC442 SPI Dual Mode with Flash Sample Code |\n"); printf("+------------------------------------------------------------------------+\n"); /* Wait ready */ SpiFlash_WaitReady(); if((u16ID = SpiFlash_ReadMidDid()) != 0x1C14) { printf("Wrong ID, 0x%x\n", u16ID); return -1; } else printf("Flash found: EN25QH16 ...\n"); printf("Erase chip ..."); /* Erase SPI flash */ SpiFlash_ChipErase(); /* Wait ready */ SpiFlash_WaitReady(); printf("[OK]\n"); /* init source data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { SrcArray[u32ByteCount] = u32ByteCount; } printf("Start to normal write data to Flash ..."); /* Program SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page program */ SpiFlash_NormalPageProgram(u32FlashAddress, SrcArray); SpiFlash_WaitReady(); u32FlashAddress += 0x100; } printf("[OK]\n"); /* clear destination data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { DestArray[u32ByteCount] = 0; } printf("Dual Read & Compare ..."); /* Read SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page read */ SpiFlash_DualFastRead(u32FlashAddress, DestArray); u32FlashAddress += 0x100; for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { if(DestArray[u32ByteCount] != SrcArray[u32ByteCount]) nError ++; } } if(nError == 0) printf("[OK]\n"); else printf("[FAIL]\n"); while(1); }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { unsigned int u32ByteCount; unsigned int u32PageNumber; unsigned int u32ProgramFlashAddress = 0; unsigned int u32VerifyFlashAddress = 0; unsigned int MidDid; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("Hello World.\n"); printf("PLL Clock = %d Hz\n", CLK_GetPLLClockFreq()); printf("Core Clock = %d Hz\n\n", CLK_GetHCLKFreq()); printf("+-------------------------------------------------------+\n"); printf("| M451 Series SPI_Flash Sample Code |\n"); printf("+-------------------------------------------------------+\n"); /* Open 7-Seg */ Open_Seven_Segment(); /* Open SPI for Serial Flash */ Open_SPI_Flash(); /* Read MID & DID */ MidDid = SpiFlash_ReadMidDid(); printf("\nMID and DID = %x", MidDid); /* Erase SPI Flash */ SpiFlash_ChipErase(); printf("\nFlash Erasing... "); /* Wait ready */ SpiFlash_WaitReady(); printf("Done!"); /* Fill the Source Data and clear Destination Data Buffer */ for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++) { SrcArray[u32ByteCount] = u32ByteCount; DestArray[u32ByteCount] = 0; } u32ProgramFlashAddress = 0; u32VerifyFlashAddress = 0; for(u32PageNumber = 0; u32PageNumber < TEST_NUMBER; u32PageNumber++) { printf("\n\nTest Page Number = %d", u32PageNumber); Show_Seven_Segment(u32PageNumber, 1); CLK_SysTickDelay(200000); /*=== Program SPI Flash ===*/ printf("\n Flash Programming... "); /* Page Program */ SpiFlash_PageProgram(SrcArray, u32ProgramFlashAddress, 256); SpiFlash_WaitReady(); u32ProgramFlashAddress += 0x100; printf("Done!"); /*=== Read Back and Compare Data ===*/ printf("\n Flash Verifying... "); /* Page Read */ SpiFlash_ReadData(DestArray, u32VerifyFlashAddress, 256); u32VerifyFlashAddress += 0x100; for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++) { if(DestArray[u32ByteCount] != u32ByteCount) { /* Error */ printf("SPI Flash R/W Fail!"); while(1); } } /* Clear Destination Data Buffer */ for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++) DestArray[u32ByteCount] = 0; printf("Done!"); } printf("\n\nSPI Flash Test Ok!"); printf("\n\n"); while(1); }
int32_t main(void) { uint16_t u16ID; uint32_t u32ByteCount, u32FlashAddress, u32PageNumber; uint32_t nError = 0; outpw(REG_CLK_HCLKEN, 0x0527); outpw(REG_CLK_PCLKEN0, 0); outpw(REG_CLK_PCLKEN1, 0); sysDisableCache(); sysFlushCache(I_D_CACHE); sysEnableCache(CACHE_WRITE_BACK); sysInitializeUART(); /* Configure multi function pins to SPI0 */ outpw(REG_SYS_GPB_MFPL, (inpw(REG_SYS_GPB_MFPL) & ~0xff000000) | 0xBB000000); outpw(REG_SYS_GPB_MFPH, (inpw(REG_SYS_GPB_MFPH) & ~0xff) | 0xBB); spiInit(0); spiOpen(0); // set spi interface speed to 2MHz spiIoctl(0, SPI_IOC_SET_SPEED, 2000000, 0); // set transfer length to 8-bit spiIoctl(0, SPI_IOC_SET_TX_BITLEN, 8, 0); // set transfer mode to mode-0 spiIoctl(0, SPI_IOC_SET_MODE, 0, 0); // check flash id if((u16ID = SpiFlash_ReadMidDid()) == 0xEF17) { sysprintf("Flash found: W25Q128BV ...\n"); } else sysprintf("Flash ID, 0x%x\n", u16ID); sysprintf("Erase chip ..."); /* Erase SPI flash */ SpiFlash_ChipErase(); /* Wait ready */ SpiFlash_WaitReady(); sysprintf("[OK]\n"); /* init source data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { SrcArray[u32ByteCount] = u32ByteCount; } sysprintf("Start to normal write data to Flash ..."); /* Program SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page program */ SpiFlash_NormalPageProgram(u32FlashAddress, SrcArray); SpiFlash_WaitReady(); u32FlashAddress += 0x100; } sysprintf("[OK]\n"); /* clear destination data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { DestArray[u32ByteCount] = 0; } sysprintf("Normal Read & Compare ..."); /* Read SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page read */ SpiFlash_NormalRead(u32FlashAddress, DestArray); u32FlashAddress += 0x100; for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { if(DestArray[u32ByteCount] != SrcArray[u32ByteCount]) nError ++; } } if(nError == 0) sysprintf("[OK]\n"); else sysprintf("[FAIL]\n"); return 1; }