/* ** 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); }
/******************************************************************************* Function: FlashProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray ) Arguments: udAddr, start address to program pArray, address of the buffer that holds the elements to be programmed udNrOfElementsInArray, number of elements to be programmed, counted in bytes Return Value: Flash_AddressInvalid Flash_MemoryOverflow Flash_OperationTimeOut Flash_Success Description: This function programs a chunk of data into the memory at one go. If the start address and the available space are checked successfully, this function programs data from the buffer(pArray) to the memory sequentially by invoking FlashPageProgram(). This function automatically handles page boundary crossing, if any. Like FlashPageProgram(), this function assumes that the memory to be programmed has been previously erased or that bits are only changed from 1 to 0. Note: This function does not check whether the target memory area is in a Software Protection Mode(SPM) or Hardware Protection Mode(HPM), in which case the PP Instruction will be ignored. The function assumes that the target memory area has previously been unprotected at both the hardware and software levels. To unprotect the memory, please call FlashWriteStatusRegister(ST_uint8 ucStatusRegister), and refer to the datasheet for a proper ucStatusRegister value. Pseudo Code: Step 1: Validate address input Step 2: Check memory space available on the whole memory Step 3: calculte memory space available within the page containing the start address(udAddr) Step 3-1: if the page boundary is crossed, invoke FlashPageProgram() repeatedly Step 3-2: if the page boundary is not crossed, invoke FlashPageProgram() once only *******************************************************************************/ ReturnType FlashProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray ) { ST_uint16 ucMargin; ST_uint16 ucPageCount, ucRemainder; ReturnType typeReturn; // Step 1: Validate address input if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid; // Step 2: Check memory space available on the whole memory if(udAddr + udNrOfElementsInArray > FLASH_SIZE) return Flash_MemoryOverflow; // Step 3: calculte memory space available within the page containing the start address(udAddr) ucMargin = (ST_uint8)(~udAddr) + 1; // Step 3-1: if the page boundary is crossed, invoke FlashPageWrite() repeatedly if(udNrOfElementsInArray > ucMargin) { typeReturn = FlashPageProgram(udAddr, pArray, ucMargin); if(Flash_Success != typeReturn) return typeReturn; // return immediately if Not successful udNrOfElementsInArray -= ucMargin; // re-calculate the number of elements pArray += ucMargin; // modify the pointer to the buffer udAddr += ucMargin; // modify the start address in the memory ucPageCount = udNrOfElementsInArray / FLASH_WRITE_BUFFER_SIZE; // calculate the number of pages to be programmed ucRemainder = udNrOfElementsInArray % FLASH_WRITE_BUFFER_SIZE; // calculate the remainder after filling up one or more whole pages while(ucPageCount--) { typeReturn = FlashPageProgram(udAddr, pArray, FLASH_WRITE_BUFFER_SIZE); if(Flash_Success != typeReturn) return typeReturn; // return immediately if Not successful pArray += FLASH_WRITE_BUFFER_SIZE; udAddr += FLASH_WRITE_BUFFER_SIZE; }; return FlashPageProgram(udAddr, pArray, ucRemainder); } // Step 3-2: if the page boundary is not crossed, invoke FlashPageWrite() once only else { return FlashPageProgram(udAddr, pArray, udNrOfElementsInArray); } }
/* ** 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); }