void Flash_Write(U16 sector){ U32 addr = ADDR + sector*SECTOR_SIZE; __disable_irq(); Flash_Erase(sector); Flash_Program(sector,DATA_NUM,flashData); __enable_irq(); }
UINT8 FWriteByte(UINT8 u8Data) { /* Validation */ if (su16CurrSize <= (su16CurrAddr - F_START_ADDR)) { return F_EOF; } temp = Flash_Program(su16CurrAddr, u8Data); if (temp == 0x00) /* If correctly programmed word */ { su16CurrAddr++; return F_OK; } else { return F_ERR; } }
void Example_CallFlashAPI(void) { Uint16 i; Uint16 Status; Uint16 *Flash_ptr; // Pointer to a location in flash Uint32 Length; // Number of 16-bit values to be programmed float32 Version; // Version of the API in floating point Uint16 VersionHex; // Version of the API in decimal encoded hex /*------------------------------------------------------------------ Toggle Test The toggle test is run to verify the frequency configuration of the API functions. The selected pin will toggle at 10kHz (100uS cycle time) if the API is configured correctly. Example_ToggleTest() supports common output pins. Other pins can be used by modifying the Example_ToggleTest() function or calling the Flash_ToggleTest() function directly. Select a pin that makes sense for the hardware platform being used. This test will run forever and not return, thus only run this test to confirm frequency configuration and not during normal API use. ------------------------------------------------------------------*/ // Example: Toggle GPIO0 // Example_ToggleTest(0); // Example: Toggle GPIO10 // Example_ToggleTest(10); // Example: Toggle GPIO15 // Example_ToggleTest(15); // Example: Toggle GPIO31 // Example_ToggleTest(31); // Example: Toggle GPIO34 // Example_ToggleTest(34); /*------------------------------------------------------------------ Check the version of the API Flash_APIVersion() returns the version in floating point. FlashAPIVersionHex() returns the version as a decimal encoded hex. FlashAPIVersionHex() can be used to avoid processing issues associated with floating point values. ------------------------------------------------------------------*/ VersionHex = Flash_APIVersionHex(); if(VersionHex != 0x0100) { // Unexpected API version // Make a decision based on this info. asm(" ESTOP0"); } Version = Flash_APIVersion(); if(Version != (float32)1.00) { // Unexpected API version // Make a decision based on this info. asm(" ESTOP0"); } /*------------------------------------------------------------------ Before programming make sure the sectors are Erased. ------------------------------------------------------------------*/ // Example: Erase Sector B - Sector H // Sectors A has example code so leave them unerased // SECTORA-SECTORH are defined in Flash2803x_API_Library.h Status = Flash_Erase((SECTORB|SECTORC|SECTORD|SECTORE|SECTORF|SECTORG|SECTORH),&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } /*------------------------------------------------------------------ Program Flash Examples ------------------------------------------------------------------*/ // A buffer can be supplied to the program function. Each word is // programmed until the whole buffer is programmed or a problem is // found. If the buffer goes outside of the range of OTP or Flash // then nothing is done and an error is returned. // Example: Program 0x400 values in Flash SectorG // In this case just fill a buffer with data to program into the flash. for(i=0;i<WORDS_IN_FLASH_BUFFER;i++) { Buffer[i] = 0x100+i; } Flash_ptr = Sector[1].StartAddr; Length = 0x400; Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Verify the values programmed. The Program step itself does a verify // as it goes. This verify is a 2nd verification that can be done. Status = Flash_Verify(Flash_ptr,Buffer,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // -------------- // Example: Program 0x199 values in Flash SectorG for(i=0;i<WORDS_IN_FLASH_BUFFER;i++) { Buffer[i] = 0x4500+i; } Flash_ptr = (Uint16 *)Sector[1].StartAddr+0x450; Length = 0x199; Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Verify the values programmed. The Program step itself does a verify // as it goes. This verify is a 2nd verification that can be done. Status = Flash_Verify(Flash_ptr,Buffer,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // -------------- // You can program a single bit in a memory location and then go back to // program another bit in the same memory location. // Example: Program bit 0 in location in Flash SectorF. // That is program the value 0xFFFE Flash_ptr = Sector[2].StartAddr; i = 0xFFFE; Length = 1; Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Example: Program bit 1 in the same location. Remember // that bit 0 was already programmed so the value will be 0xFFFC // (bit 0 and bit 1 will both be 0) i = 0xFFFC; Length = 1; Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Verify the value. This first verify should fail. i = 0xFFFE; Status = Flash_Verify(Flash_ptr,&i,Length,&FlashStatus); if(Status != STATUS_FAIL_VERIFY) { Example_Error(Status); } // This is the correct value and will pass. i = 0xFFFC; Status = Flash_Verify(Flash_ptr,&i,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // -------------- // If a bit has already been programmed, it cannot be brought back to a 1 by // the program function. The only way to bring a bit back to a 1 is by erasing // the entire sector that the bit belongs to. This example shows the error // that program will return if a bit is specified as a 1 when it has already // been programmed to 0. // Example: Program a single 16-bit value 0x0002, in Flash Sector B Flash_ptr = Sector[2].StartAddr+1; i = 0x0002; Length = 1; Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Example: This will return an error!! Can't program 0x0001 // because bit 0 in the the location was previously programmed // to zero! i = 0x0001; Length = 1; Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus); // This should return a STATUS_FAIL_ZERO_BIT_ERROR if(Status != STATUS_FAIL_ZERO_BIT_ERROR) { Example_Error(Status); } // -------------- // Example: This will return an error!! The location specified // is outside of the Flash and OTP! Flash_ptr = (Uint16 *)0x00340000; i = 0x0001; Length = 1; Status = Flash_Program(Flash_ptr,&i,Length,&FlashStatus); // This should return a STATUS_FAIL_ADDR_INVALID error if(Status != STATUS_FAIL_ADDR_INVALID) { Example_Error(Status); } // -------------- // Example: This will return an error!! Can't program 1 // because bit 0 in the the location was previously programmed // to zero! for(i=0;i<WORDS_IN_FLASH_BUFFER;i++) { Buffer[i] = 0xFFFF; } Flash_ptr = Sector[0].EndAddr; Length = 13; Status = Flash_Program(Flash_ptr,Buffer,Length,&FlashStatus); if(Status != STATUS_FAIL_ZERO_BIT_ERROR)//STATUS_FAIL_ADDR_INVALID) { Example_Error(Status); } /*------------------------------------------------------------------ More Erase Sectors Examples - Clean up the sectors we wrote to: ------------------------------------------------------------------*/ // Example: Erase Sector G // SECTORB is defined in Flash2803x_API_Library.h Status = Flash_Erase(SECTORG,&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Example: Erase Sector F // SECTORC is defined in Flash2803x_API_Library.h Status = Flash_Erase((SECTORF),&FlashStatus); if(Status != STATUS_SUCCESS) { Example_Error(Status); } // Example: This will return an error. No valid sector is specified. Status = Flash_Erase(0,&FlashStatus); // Should return STATUS_FAIL_NO_SECTOR_SPECIFIED if(Status != STATUS_FAIL_NO_SECTOR_SPECIFIED) { Example_Error(Status); } Example_Done(); }
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; }
int main(void) { Flash_Program(); return 0; }
void FContextChange() { UINT8 u8ByteCount = 0; UINT16 u16Addr = 0x0000; UINT8 *pu8srcData = (UINT8 *)F_START_ADDR; UINT8 *pu8dstData = (UINT8 *) 0; UINT16 u16Index = 0; UINT8 i=0; /* Let's first erase all the pages to which the image references */ while (u16Index <= su16CurrSize) { /*Find addr*/ u16Addr = (UINT16)pu8srcData[u16Index]<<8; u16Addr |= (UINT16)pu8srcData[++u16Index]; pu8dstData = (UINT8 *) u16Addr; u8ByteCount = pu8srcData[++u16Index]; if (u8ByteCount == 0xFF) break; // End of file reached for (i=0;i<u8ByteCount;i++) { if (*(pu8dstData) != 0xFF) { (void)Flash_Erase(pu8dstData); break; } pu8dstData++; } pu8dstData = (UINT8 *) u16Addr; u16Index += u8ByteCount; u16Index++; u16Index++; } u16Index = 0; i=0; /* Now let's program the addresses referenced by the image */ while (u16Index <= su16CurrSize) { /*Find addr*/ u16Addr = (UINT16)pu8srcData[u16Index]<<8; u16Addr |= (UINT16)pu8srcData[++u16Index]; pu8dstData = (UINT8 *) u16Addr; u8ByteCount = pu8srcData[++u16Index]; if (u8ByteCount == 0xFF) break; // End of file reached pu8dstData = (UINT8 *) u16Addr; for (i=0;i<u8ByteCount;i++, pu8dstData++) { (void)Flash_Program(pu8dstData, pu8srcData[++u16Index]); } //Flash_Burst(pu8dstData, u8ByteCount, &pu8srcData[++u16Index]); //u16Index += u8ByteCount; u16Index++; u16Index++; } __asm SWI; }