/* ** Main Function. */ int main(void) { unsigned char choice = 0; /* Enable the clocks for McSPI0 module.*/ McSPI0ModuleClkConfig(); /* Perform Pin-Muxing for SPI0 Instance.*/ McSPIPinMuxSetup(0); /* Perform Pin-Muxing for CS0 of SPI0 Instance.*/ McSPI0CSPinMuxSetup(MCSPI_CH_NUM); /* Initialize the UART utility functions.*/ UARTStdioInit(); /* Enable IRQ in CPSR.*/ IntMasterIRQEnable(); UARTPuts("Here the McSPI controller on the SOC communicates with ",-1); UARTPuts("the McSPI Flash.\r\n\r\n",-1); /* Initialize the EDMA3 instance.*/ EDMA3Initialize(); /* Request EDMA3CC for Tx and Rx channels for SPI0. */ RequestEDMA3Channels(); /* Set up the McSPI instance.*/ McSPISetUp(); /* Enable the SPI Flash for writing to it. */ WriteEnable(); UARTPuts("Do you want to erase a sector of the flash before writing to it ?.", -1); UARTPuts("\r\nInput y(Y)/n(N) to proceed.\r\n", -1); choice = UARTGetc(); UARTPutc(choice); if(('y' == choice) || ('Y' == choice)) { /* Erasing the specified sector of SPI Flash. */ FlashSectorErase(); } /* Enable the SPI Flash for writing to it. */ WriteEnable(); /* Write data of 1 page size into a page of Flash.*/ FlashPageProgram(); /* Read data of 1 page size from a page of flash.*/ ReadFromFlash(); /* Verify the data written to and read from Flash are same or not.*/ VerifyData(); while(1); }
/********************************************************************* * @fn OADTarget_eraseFlash * * @brief Erase selected flash page. * * @param page - the page to erase. * * @return None. */ void OADTarget_eraseFlash(uint8_t page) { uint8_t cacheState; cacheState = OADTarget_disableCache(); FlashSectorErase((uint32_t)FLASH_ADDRESS(page, 0)); OADTarget_enableCache(cacheState); }
//***************************************************************************** // //! Erase a flash sector // //***************************************************************************** uint32_t FlashsafeSectorErase(uint32_t ui32SectorAddress) { uint32_t ui32ErrorReturn; // // Check the arguments. // ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() - FlashsafeSectorSizeGet())); ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00); if(FlashsafeSMPHTryAcquire()) { ui32ErrorReturn = FlashSectorErase(ui32SectorAddress); FlashsafeSMPHRelease(); return(ui32ErrorReturn); } return(FLASHSAFE_ACCESS_DENIED); }
/* ** Main function. */ int main(void) { volatile char originalData[260] = {0}; volatile char readFlash[260] = {0}; unsigned int retVal = 0; unsigned char choice; /* Initializes the UART Instance for serial communication. */ UARTStdioInit(); UARTPuts("Welcome to SPI EDMA application.\r\n", -1); UARTPuts("Here the SPI controller on the SoC communicates with", -1); UARTPuts(" the SPI Flash present on the SoM.\r\n\r\n", -1); /* Initializes the EDMA3 Instance. */ EDMA3Initialize(); /* Initializes the SPI1 Instance. */ SPIInitialize(); /* Request EDMA3CC for Tx and Rx channels for SPI1. */ RequestEDMA3Channels(); /* Enable SPI communication. */ SPIEnable(SOC_SPI_1_REGS); /* Enable the SPI Flash for writing to it. */ WriteEnable(); UARTPuts("Do you want to erase a sector of the flash before writing to it ?.", -1); UARTPuts("\r\nInput y(Y)/n(N) to proceed.\r\n", -1); choice = UARTGetc(); UARTPutc(choice); if(('y' == choice) || ('Y' == choice)) { /* Erasing the specified sector of SPI Flash. */ FlashSectorErase(); } WriteEnable(); /* Program a specified Page of the SPI Flash. */ FlashPageProgram(originalData); /* Read the contents of the page that was previously written to. */ ReadFromFlash(readFlash); /* Verify whether the written and the read contents are equal. */ retVal = verifyData(&originalData[4], &readFlash[4], 256); if(TRUE == retVal) { UARTPuts("\r\nThe data in the Flash and the one written ", -1); UARTPuts("to it are equal.\r\n", -1); } else { UARTPuts("\r\n\r\nThe data in the Flash and the one written to it", -1); UARTPuts(" are not equal.\r\n", -1); } while(1); }
/************************************************************************************************** * @fn HalFlashErase * * @brief This function erases 'cnt' pages of the internal flash. * * input parameters * * @param pg - Valid HAL flash page number (ie < 128) to erase. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void HalFlashErase(uint8 pg) { FlashSectorErase( (uint32)HalFlashGetAddress(pg, 0)); }
//涉及到的地址是Flash的绝对地址 BYTE 不支持跨扇区操作!!! //奇数地址, 则向后移写入(避免删除SECTOR 追加方式) int FlashSaveBuffer(U32 Addr, void *Buffer, U32 Size) { U32 i,SectorStartAddr,StartAddr,EndAddr,SecSize,Erased,ThisSize; U16 *TempBuffer; TempBuffer=(U16 *)malloc(STD_SECTOR_SIZE); //get flash sector index and size i=GetFlashSectorIndex(Addr); SecSize=FlashSectorSizes[i]; //get flash start address SectorStartAddr=GetFlashStartAddress(i); //read from flash to buffer for(i=0;i<SecSize/2;i++) { TempBuffer[i]=((U16*)SectorStartAddr)[i]; //if(i%150==0) printf("%d %x\n", i, SectorStartAddr+i*2); } //Determine the bound of tempbuffer and buffer if(SectorStartAddr < Addr) StartAddr=Addr-SectorStartAddr; else StartAddr=0; EndAddr=StartAddr + Size; if(EndAddr > SecSize) EndAddr = SecSize; ThisSize=EndAddr-StartAddr; //check diff of buffer if(memcmp((U8*)TempBuffer+StartAddr, Buffer, ThisSize)) { //check if erase this section Erased=0; for(i=StartAddr;i<EndAddr;i+=2) { if(!(TempBuffer[i/2]==0xFFFF)) { if (!FlashSectorErase(SectorStartAddr)){ free(TempBuffer); return -1; } Erased=1; break; } } memcpy((U8*)TempBuffer+StartAddr, Buffer, ThisSize); if(Erased) //if erased old data, write back all data of the section { i=0; EndAddr=SecSize; } else { i=(StartAddr/2)*2; //adjust odd address to previous EndAddr=((EndAddr+1)/2)*2; } while(i < EndAddr) { if (!FlashWriteWord(SectorStartAddr + i, TempBuffer[i/2])) { free(TempBuffer); return -2; } i+=2; } } free(TempBuffer); return 1; }
int FlashSectorEraseByIndex(int i) { return FlashSectorErase(GetFlashStartAddress(i)); }
/******************************************************************************* * @fn main * * @brief C-code main function. * * input parameters * * None. * * output parameters * * None. * * @return None. */ void main(void) { uint8_t *pLedgerPg = (uint8_t *)BIM_LEDGER_ADDR; uint16_t i = 0; uint8_t id = LOAD_IMG_A; /* * VIMS disabling should not be necessary as this is disabled by default and * no application has had a chance to modify its state at this point. */ // Check first byte of ledger page. if 0x00, erase. if (pLedgerPg[i] == 0) { // Erase the ledger page. FlashSectorErase((uint32_t)pLedgerPg); } else { // Find last offset unwritten offset in ledger page. for (i=0; i < HAL_FLASH_PAGE_SIZE; i++) { id = pLedgerPg[i]; if(id == 0xFF) { if (i != 0) { id = pLedgerPg[i-1]; } else { id = LOAD_IMG_A; } break; } } } // Check which image to load. if (id == LOAD_IMG_A) { // Load address of label __iar_program_start from the fixed location // of the image's reset vector and jump. asm(" MOV R0, #0x1504 "); asm(" LDR R0, [R0] "); asm(" BX R0 "); } else { // Load address of the entry point from the fixed location // of the image's reset vector and jump. #if defined(__IAR_SYSTEMS_ICC__) asm(" MOV R0, #0x9004 "); #else asm(" MOV R0, #0x8004 "); #endif asm(" LDR R0, [R0] "); asm(" BX R0 "); } }
/******************************************************************************* Function: ReturnType Flash( InstructionType insInstruction, ParameterType *fp ) Arguments: insInstruction is an enum which contains all the available Instructions of the SW driver. fp is a (union) parameter struct for all Flash Instruction parameters Return Value: The function returns the following conditions: Flash_AddressInvalid, Flash_MemoryOverflow, Flash_PageEraseFailed, Flash_PageNrInvalid, Flash_SectorNrInvalid, Flash_FunctionNotSupported, Flash_NoInformationAvailable, Flash_OperationOngoing, Flash_OperationTimeOut, Flash_ProgramFailed, Flash_SpecificError, Flash_Success, Flash_WrongType Description: This function is used to access all functions provided with the current Flash device. Pseudo Code: Step 1: Select the right action using the insInstruction parameter Step 2: Execute the Flash memory Function Step 3: Return the Error Code *******************************************************************************/ ReturnType Flash( InstructionType insInstruction, ParameterType *fp ) { ReturnType rRetVal; ST_uint8 ucStatusRegister; ST_uint16 ucDeviceIdentification; #ifdef USE_JEDEC_STANDARD_TWO_BYTE_SIGNATURE ST_uint8 ucManufacturerIdentification; #endif switch (insInstruction) { case WriteEnable: rRetVal = FlashWriteEnable( ); break; case WriteDisable: rRetVal = FlashWriteDisable( ); break; case ReadDeviceIdentification: rRetVal = FlashReadDeviceIdentification(&ucDeviceIdentification); (*fp).ReadDeviceIdentification.ucDeviceIdentification = ucDeviceIdentification; break; #ifdef USE_JEDEC_STANDARD_TWO_BYTE_SIGNATURE case ReadManufacturerIdentification: rRetVal = FlashReadManufacturerIdentification(&ucManufacturerIdentification); (*fp).ReadManufacturerIdentification.ucManufacturerIdentification = ucManufacturerIdentification; break; #endif case ReadStatusRegister: rRetVal = FlashReadStatusRegister(&ucStatusRegister); (*fp).ReadStatusRegister.ucStatusRegister = ucStatusRegister; break; case WriteStatusRegister: ucStatusRegister = (*fp).WriteStatusRegister.ucStatusRegister; rRetVal = FlashWriteStatusRegister(ucStatusRegister); break; case Read: rRetVal = FlashRead( (*fp).Read.udAddr, (*fp).Read.pArray, (*fp).Read.udNrOfElementsToRead ); break; case FastRead: rRetVal = FlashFastRead( (*fp).Read.udAddr, (*fp).Read.pArray, (*fp).Read.udNrOfElementsToRead ); break; case PageProgram: rRetVal = FlashProgram( (*fp).PageProgram.udAddr, (*fp).PageProgram.pArray, (*fp).PageProgram.udNrOfElementsInArray ); break; case SectorErase: rRetVal = FlashSectorErase((*fp).SectorErase.ustSectorNr ); break; case SubSectorErase: rRetVal = FlashSubSectorErase((*fp).SubSectorErase.ustSectorNr ); break; case BulkErase: rRetVal = FlashBulkErase( ); break; #ifndef NO_DEEP_POWER_DOWN_SUPPORT case DeepPowerDown: rRetVal = FlashDeepPowerDown( ); break; case ReleaseFromDeepPowerDown: rRetVal = FlashReleaseFromDeepPowerDown( ); break; #endif case Program: rRetVal = FlashProgram( (*fp).Program.udAddr, (*fp).Program.pArray, (*fp).Program.udNrOfElementsInArray); break; default: rRetVal = Flash_FunctionNotSupported; break; } /* EndSwitch */ return rRetVal; } /* EndFunction Flash */