// call this to erase pages // return true on success, else false bool FlashErase(uint32_t startOffset, uint32_t length) { uint32_t offset = startOffset; uint32_t endOffset = startOffset + length; int errors = 0; while (offset < endOffset) { uint32_t * ptr = (void*)FlashData; ptr += offset; void * physicalAddress = VirtualToPhysicalAddress(ptr); // Claim: Microchip has serious bug in their PIC32 peripheral lib - erase page depends on series // from http://www.microchip.com/forums/m772388.aspx // next solution: In "PIC32 Flash Programming Specification" DS61145L, Table 5.1: I finally found the details. // Flash page size 256 Word for PIC32MX1 and MX2. Flash page size 1024 Words for MX3 and higher, and 4096 Words for MZ devices. // shizzle f**k :) // needs PHYSICAL address, not virtual address if (NVMErasePage(physicalAddress) != 0) { // double try if (NVMErasePage(physicalAddress) != 0) { errors++; } } offset += BYTE_PAGE_SIZE/4; // note this size changes by PIC model } if (errors == 0) return true; return false; }
/*** writeEeprom ** ** Parameters: ** address - location to be written ** data - data to be written ** ** Return Value: ** Returns true or false wether the operation was successful ** or not. ** ** Errors: ** none ** ** Description: ** Writes a data to specifed address location */ BOOL writeEeprom(uint32_t address, uint8_t data) { int i; if(address > MAX_ADDRESS) { return fFalse; } //Try writting data to flash for(i=0; i < _EEPROM_PAGE_COUNT; i++) { if(putEeprom(&eedata_addr[i][0], address, data)) { return fTrue; } } for(i=0; i < _EEPROM_PAGE_COUNT; i++) { //Put page to buffer putBuffer(&eedata_addr[i][0], tempBuffer); //Clear page NVMErasePage(&eedata_addr[i][0]); //Put buffer back to page getBuffer(&eedata_addr[i][0], tempBuffer); } //Try writting data to flash again for(i=0; i < _EEPROM_PAGE_COUNT; i++) { if(putEeprom(&eedata_addr[i][0], address, data)) { return fTrue; } } return fFalse; }
/***************************************************************************** * * exoHAL_EraseNVMeta * * \param None * * \return None * * \brief Wipes out meta information - replaces with 0's * *****************************************************************************/ void exoHAL_EraseMeta(void) { NVMErasePage((void *)EXOMETA_ADDR); return; }
void WiFi_WriteConfigToMemory(void) { if(sizeof(DRV_WIFI_CONFIG_DATA) > 512) SYS_ASSERT(true, "p_wifi_ConfigData > 512"); NVMErasePage((void*)NVM_PROGRAM_PAGE); NVMWriteRow((void*)NVM_PROGRAM_PAGE, (void*)p_wifi_ConfigData); }
/*** clearEeprom() ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** Clears all eeprom values to 0xFF */ void clearEeprom() { int i; //Clear page for(i=0; i < _EEPROM_PAGE_COUNT; i++) { NVMErasePage(&eedata_addr[i][0]); } }
void eraseFlash(void){ int temp; uint32_t address = StartAppVectPhysical; //uint32_t erAddr; uint32_t size =EndAppVectPhysical-StartAppVectPhysical; for( temp = 0; temp < (size/FLASH_PAGE_SIZE); temp++ ){ if (address <(EndAppVectPhysical-FLASH_PAGE_SIZE)){ //printfDEBUG("Erasing page at address:"); //printfDEBUG_UL(address); NVMErasePage( (uint32_t*) address ); } address +=FLASH_PAGE_SIZE; } }
/**************************************************************************** Function: void EraseBootRegions(void) Description: This routine will erase the entire application memory space. Precondition: Each boot region is page aligned (Starts on address divisible by FLASH_BLOCK_SIZE and ends on address divisible by FLASH_BLOCK_SIZE) Parameters: None Returns: None ***************************************************************************/ void EraseBootRegions(void) { DWORD address; BYTE i; // Iterate through each boot region for(i = 0;i < NUM_BOOT_REGIONS; i++) { // Iterate through each flash block of this boot region for(address = my_boot_regions[i].addressStart; address < my_boot_regions[i].addressEnd;address += (FLASH_BLOCK_SIZE)) { NVMErasePage((void *)address); // Erase page } } }
void APP_NVM_Write2(void) { int i; uint8_t buf_t[10]; char c_tmp = 0; for(i = 0;i<DRV_NVM_PAGE_SIZE;i++) { NVM_DATA_TEST_BUFF[i] = 0x55; } SYS_CONSOLE_MESSAGE("2. erase\r\n"); NVMErasePage((void*)NVM_WF_CONFIG__ADDRESS); SYS_CONSOLE_MESSAGE("3. Write\r\n"); NVMWriteRow((void*)NVM_WF_CONFIG__ADDRESS, (void*)NVM_DATA_TEST_BUFF); SYS_CONSOLE_MESSAGE("4. Done\r\n"); }
//-------------------------------------------------------------------------------------------- // Fonction : config_ReadIsr // Utilité : écrire dans la zonne de configuration appartire du main //-------------------------------------------------------------------------------------------- void config_Write(char *pBuffer, long Base, int Nbr, long IdConfig) { unsigned int pageStartAddr; unsigned int offset, numLeftInPage; unsigned int index; Base += IdConfig;//SECTEUR_CONFIG; // 2. Calculate Page Start address pageStartAddr = (unsigned int)Base & (~(BYTE_PAGE_SIZE - 1)); offset = (unsigned int)Base & (BYTE_PAGE_SIZE - 1); while(Nbr) { memcpy((unsigned int *)(config_Mem), (void *)(pageStartAddr), BYTE_PAGE_SIZE); NVMErasePage((void *)pageStartAddr); numLeftInPage = BYTE_PAGE_SIZE - offset; if(Nbr <= numLeftInPage) { memcpy((unsigned int *)(config_Mem + offset), (void *)(pBuffer), Nbr); Nbr = 0; } else { memcpy((unsigned int *)(config_Mem + offset), (void *)(pBuffer), numLeftInPage); Nbr -= numLeftInPage; pBuffer += numLeftInPage; } // Program the Page for(index = 0; index < NUM_ROWS_PAGE; index++) { NVMWriteRow((void *)(pageStartAddr), (void *)config_Mem + (index * BYTE_ROW_SIZE)); pageStartAddr += BYTE_ROW_SIZE; } offset = 0; } }
/******************************************************************** * Function: main() * * Precondition: * * Input: None. * * Output: None. * * Side Effects: None. * * Overview: Main entry function. If there is a trigger or * if there is no valid application, the device * stays in firmware upgrade mode. * * * Note: None. ********************************************************************/ INT main(void) { UINT pbClk; UINT bSoftResetFlag = 0; // Setup configuration pbClk = SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); InitLED(); TRISBbits.TRISB15 = 1;//test bSoftResetFlag = *(unsigned int *)(NVM_DATA); if(bSoftResetFlag == 1) { NVMErasePage((void*)NVM_DATA); NVMWriteWord((void*)(NVM_DATA), (unsigned int)0x00); } // Enter firmware upgrade mode if there is a trigger or if the application is not valid if(bSoftResetFlag == 1 || CheckTrigger() || !ValidAppPresent()) { // Initialize the transport layer - UART/USB/Ethernet TRANS_LAYER_Init(pbClk); while(!FRAMEWORK_ExitFirmwareUpgradeMode()) // Be in loop till framework recieves "run application" command from PC { // Enter firmware upgrade mode. // Be in loop, looking for commands from PC TRANS_LAYER_Task(); // Run Transport layer tasks FRAMEWORK_FrameWorkTask(); // Run frame work related tasks (Handling Rx frame, process frame and so on) // Blink LED (Indicates the user that bootloader is running). BlinkLED(); } // Close trasnport layer. TRANS_LAYER_Close(); } // No trigger + valid application = run application. JumpToApp(); return 0; }
void WiFi_EraseConfigFromMemory(void) { NVMErasePage((void*)NVM_PROGRAM_PAGE); }
void DoSerial() // Serial Processing of any input data { char c; uint8 i; if(Serial2Available()) { SerialConnected=1; // Client has connected so we can now send stuff.. (Set until POR as we dont have the equivelent for a disconnect) c = Serial2Read(); if(c==3) // 3. Raw image test (focus setup etc) { while(c!=1 && c!=9) { OC_COUNT(ON); AquireImage(); Serial2Write(3); Serial2Write(99); for(i=0; i<128; i++) Serial2Write(RawData[i]); Serial2Write(99); DelayMicroseconds(100000); if(Serial2Available()) c = Serial2Read(); } } if(c==4) // 4. end processing { PowerSaveSleep(); // END! while(1); } if(c==5) // 5. Send fixed (NVM) settings { Serial2Write(5); Serial2Write(99); Serial2Write(Tmin); Serial2Write(algorithm); Serial2Write(cap); Serial2Write(neck); Serial2Write(SampleRate>>8); Serial2Write(SampleRate); Serial2Write(RejOnTime>>8); Serial2Write(RejOnTime); Serial2Write(tollerance); } if(c==6) SendCounters();// 6. Send current settings if(c==7) // 7. Receive new settings temporary until reset or permanent if written to NVM { Tmin=Serial2Read(); algorithm=Serial2Read(); cap=Serial2Read(); neck=Serial2Read(); SampleRate=Serial2Read()<<8 + Serial2Read(); RejOnTime=Serial2Read()<<8 + Serial2Read(); tollerance=Serial2Read(); NVMChanged=1; Serial2Write(7); } if(c==8 && NVMChanged) // 8. Save to NVM { if( (void*)(NVMPtr+8) >= NVM_END) // used all the flash page so we have to erase whole page and start again (will never happen seriously!) { NVMErasePage(NVM_START); NVMPtr = NVM_START; } // Each variable is stored as a 32 bit integer which is the minimum size programable to flash NVMPtr will always be set at next free FLASH slot after POR or a write NVMWriteWord( (void*) (NVMPtr++) , Tmin ); NVMWriteWord( (void*) (NVMPtr++) , algorithm ); NVMWriteWord( (void*) (NVMPtr++) , cap ); NVMWriteWord( (void*) (NVMPtr++) , neck ); NVMWriteWord( (void*) (NVMPtr++) , SampleRate ); NVMWriteWord( (void*) (NVMPtr++) , RejOnTime); NVMWriteWord( (void*) (NVMPtr++) , tollerance); NVMChanged = 0; // All good so unflag change Serial2Write(8); } if(c==9) SoftReset(); // 9. RESET Sensor! }