//***************************************************************************** // This is an example code demonstrating F021 Flash API usage. // This code is in Flash //***************************************************************************** void main(void) { // Step 1. Initialize System Control: // Enable Peripheral Clocks // This example function is found in the F28M35x_SysCtrl.c file. InitSysCtrl(); // Unlock CSM // // If the API functions are going to run in unsecured RAM // then the CSM must be unlocked in order for the flash // API functions to access the flash. // If the flash API functions are executed from secure memory // then this step is not required. CsmUnlock(); // Step 2. Initalize GPIO: // This example function is found in the F28M35x_Gpio.c file and // illustrates how to set the GPIO to it's default state. // InitGpio(); // Skipped for this example // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize the PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the F28M35x_PieCtrl.c file. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in DSP2802x_DefaultIsr.c. // This function is found in F28M35x_PieVect.c. InitPieVectTable(); // Copy time critical code and Flash setup code to RAM // This includes InitFlash(), Flash API functions and any functions that are // assigned to ramfuncs section. // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart // symbols are created by the linker. Refer to the device .cmd file. memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize); // Call Flash Initialization to setup flash waitstates // This function must reside in RAM InitFlash(); // Gain pump semaphore FlashGainPump(); // Jump to RAM and call the Flash API functions Example_CallFlashAPI(); }
void CopyData() { struct HEADER { Uint16 BlockSize; Uint32 DestAddr; Uint32 ProgBuffAddr; } BlockHeader; Uint16 wordData; Uint16 status; Uint16 i,j; //Make sure code security is disabled CsmUnlock(); EALLOW; Flash_CPUScaleFactor = SCALE_FACTOR; Flash_CallbackPtr = NULL; EDIS; status = Flash_Erase((SECTORA | SECTORB | SECTORC | SECTORD), &FlashStatus); if(status != STATUS_SUCCESS) { //TODO fix so that it returns a serial error and reboot device return; } // After Flash Erase, send the checksum to PC program. SendCheckSum(); // Get the size in words of the first block BlockHeader.BlockSize = (*GetOnlyWordData)(); // While the block size is > 0 copy the data // to the DestAddr. There is no error checking // as it is assumed the DestAddr is a valid // memory location while(BlockHeader.BlockSize != (Uint16)0x0000) { if(BlockHeader.BlockSize > PROG_BUFFER_LENGTH){ //Block is to big to fit into our buffer so we must program it in chunks BlockHeader.DestAddr = GetLongData(); //Program as many full buffers as possible for(j = 0; j < (BlockHeader.BlockSize / PROG_BUFFER_LENGTH); j++){ BlockHeader.ProgBuffAddr = (Uint32)progBuf; for(i = 1; i <= PROG_BUFFER_LENGTH; i++) { wordData = (*GetOnlyWordData)(); *(Uint16 *)BlockHeader.ProgBuffAddr++ = wordData; } status = Flash_Program((Uint16 *) BlockHeader.DestAddr, (Uint16 *)progBuf, PROG_BUFFER_LENGTH, &FlashStatus); if(status != STATUS_SUCCESS) { return; } BlockHeader.DestAddr += PROG_BUFFER_LENGTH; // After Flash program, send the checksum to PC program. SendCheckSum(); } //Program the leftovers BlockHeader.ProgBuffAddr = (Uint32)progBuf; for(i = 1; i <= (BlockHeader.BlockSize % PROG_BUFFER_LENGTH); i++) { wordData = (*GetOnlyWordData)(); *(Uint16 *)BlockHeader.ProgBuffAddr++ = wordData; } status = Flash_Program((Uint16 *) BlockHeader.DestAddr, (Uint16 *)progBuf, (BlockHeader.BlockSize % PROG_BUFFER_LENGTH), &FlashStatus); if(status != STATUS_SUCCESS) { return; } // After Flash program, send the checksum to PC program. SendCheckSum(); }else{ //Block will fit into our buffer so we'll program it all at once BlockHeader.DestAddr = GetLongData(); BlockHeader.ProgBuffAddr = (Uint32)progBuf; for(i = 1; i <= BlockHeader.BlockSize; i++) { wordData = (*GetOnlyWordData)(); *(Uint16 *)BlockHeader.ProgBuffAddr++ = wordData; } status = Flash_Program((Uint16 *) BlockHeader.DestAddr, (Uint16 *)progBuf, BlockHeader.BlockSize, &FlashStatus); if(status != STATUS_SUCCESS) { return; } // After Flash program, send the checksum to PC program. SendCheckSum(); } // Get the size of the next block BlockHeader.BlockSize = (*GetOnlyWordData)(); } return; }