void * vfs_sync_open(int clear, int * p_error) { unsigned short sector; int rc; /* clear != 0 => clear backing store */ if (clear) { /* so erase the flash */ for (sector = VFS_BK_STORE_FIRST_SECTOR; sector <= VFS_BK_STORE_LAST_SECTOR; ++sector) { rc = EraseFlash(sector); if (rc != TRUE) { /* the AMD flash driver doesn't tell you why a failure occured, so this is the best we can do */ *p_error = 1; dprintf("EraseFlash(%d) failed\n",sector); return NULL; } } } /* put the "file" pointer at the beginning of the flash backing store */ vfs_flash_address = VFS_BK_STORE_BASE_ADDRESS; /* return the address of the file pointer */ return (void *) &vfs_flash_address; }
UINT_T FinalizeMRD(void) { UINT_T Retval = NoError; // no valid MRD, fatal error if (pMRD_valid == NULL) { serial_outstr("No valid MRD\n"); zimi_force_minisys(NOVALIDMRD); FatalError(NOVALIDMRD); } // both of the MRDs are OK, no problem if (pMRD_invalid == NULL) { serial_outstr("Primary/Backup MRD are both OK\n"); // reload valid MRD Retval = ReadFlash(pMRD_valid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_valid->ImageSize, BOOT_FLASH); if (Retval != NoError) { zimi_force_minisys(Retval); FatalError(Retval); } return NoError; } serial_outstr("Start to write valid MRD to corrupted MRD\n"); // one MRD is OK, the other MRD has problem // we don't know which one is valid, so, we need to reload valid MRD Retval = ReadFlash(pMRD_valid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_valid->ImageSize, BOOT_FLASH); if (Retval != NoError) { zimi_force_minisys(Retval); FatalError(Retval); } // and then erase invalid MRD Retval = EraseFlash(pMRD_invalid->FlashEntryAddr, pMRD_invalid->ImageSize, BOOT_FLASH); if (Retval != NoError) { zimi_force_minisys(Retval); FatalError(Retval); } // and then write valid MRD to flash Retval = WriteFlash(pMRD_invalid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_invalid->ImageSize, BOOT_FLASH); if (Retval != NoError) { zimi_force_minisys(Retval); FatalError(Retval); } serial_outstr("Write valid MRD to corrupted MRD Done\n"); return NoError; }
void EraseAllFlash(void) { for(ErasePageTracker = BeginPageToErase; ErasePageTracker < (MaxPageToErase + 1); ErasePageTracker++) { EraseFlash(); } NVMCONbits.WREN = 0; //Good practice to clear WREN bit anytime we are not expecting to do erase/write operations, further reducing probability of accidental activation. }
void BOOTLDR_Run() { volatile UINT i; FILEHANDLE *myFile; FILEHANDLE *currentFile; FW_VERSION newVersion; CHAR buffer[64]; newVersion.major = 0; newVersion.minor = 0; newVersion.build = 0; //if(!ForcedFirmwareExists() && !NewerFirmwareExists() && ValidAppPresent()) if(!ShouldFlashFirmware(sTargetFile, &newVersion) && ValidAppPresent()) { // No new firmware to load. Jump // directly to the application JumpToApp(); } // At this point sTargetFile will have the name of the file we should use //strcpy(sTargetFile, "fw/force/v0_0_2.hex"); myFile = FATFS_fopen(sTargetFile,"r"); if(myFile == NULL)// Make sure the file is present. { //Indicate error and stay in while loop. ErrorFunction(); } // Erase Flash (Block Erase the program Flash) EraseFlash(); // Initialize the state-machine to read the records. record.status = REC_NOT_FOUND; while(1) { // For a faster read, read 512 bytes at a time and buffer it. readBytes = FATFS_fread((void *)&asciiBuffer[pointer], 1, 512, myFile); //readBytes = FSfread((void *)&asciiBuffer[pointer],1,512,myFile); if(readBytes == 0) { // Nothing to read. Come out of this loop // break; FATFS_fclose(myFile); // Something fishy. The hex file has ended abruptly, looks like there was no "end of hex record". //Indicate error and stay in while loop. ErrorFunction(); } for(i = 0; i < (readBytes + pointer); i ++) { // This state machine seperates-out the valid hex records from the read 512 bytes. switch(record.status) { case REC_FLASHED: case REC_NOT_FOUND: if(asciiBuffer[i] == ':') { // We have a record found in the 512 bytes of data in the buffer. record.start = &asciiBuffer[i]; record.len = 0; record.status = REC_FOUND_BUT_NOT_FLASHED; } break; case REC_FOUND_BUT_NOT_FLASHED: if((asciiBuffer[i] == 0x0A) || (asciiBuffer[i] == 0xFF)) { // We have got a complete record. (0x0A is new line feed and 0xFF is End of file) // Start the hex conversion from element // 1. This will discard the ':' which is // the start of the hex record. ConvertAsciiToHex(&record.start[1],hexRec); // We're done so let's close the file and update current.txt if(hexRec[3] == END_OF_FILE_RECORD) { FATFS_fclose(myFile); currentFile = FATFS_fopen("fw/current.txt", "wo"); if(currentFile != NULL) { sprintf(buffer, "v%d_%d_%d.hex", newVersion.major, newVersion.minor, newVersion.build); FATFS_fwrite(buffer, 1, strlen(buffer), currentFile); FATFS_fclose(currentFile); } } WriteHexRecord2Flash(hexRec); record.status = REC_FLASHED; // Blink the green LED to indicate programming in progress mLED_Green_Toggle(); } break; } // Move to next byte in the buffer. record.len ++; } if(record.status == REC_FOUND_BUT_NOT_FLASHED) { // We still have a half read record in the buffer. The next half part of the record is read // when we read 512 bytes of data from the next file read. memcpy(asciiBuffer, record.start, record.len); pointer = record.len; record.status = REC_NOT_FOUND; } else { pointer = 0; } }//while(1) }// end BOOTLDR_Run function
u32 adi_pdd_Control( ADI_DEV_PDD_HANDLE PDDHandle, u32 Command, void *pArg) { ERROR_CODE ErrorCode = NO_ERR; COMMAND_STRUCT *pCmdStruct = (COMMAND_STRUCT *)pArg; // switch on the command switch ( Command ) { // erase all case CNTRL_ERASE_ALL: ErrorCode = EraseFlash(pCmdStruct->SEraseAll.ulFlashStartAddr); break; // erase sector case CNTRL_ERASE_SECT: ErrorCode = EraseBlock( pCmdStruct->SEraseSect.nSectorNum, pCmdStruct->SEraseSect.ulFlashStartAddr ); break; // get manufacturer and device codes case CNTRL_GET_CODES: ErrorCode = GetCodes((int *)pCmdStruct->SGetCodes.pManCode, (int *)pCmdStruct->SGetCodes.pDevCode, (unsigned long)pCmdStruct->SGetCodes.ulFlashStartAddr); break; case CNTRL_GET_DESC: //Filling the contents with data pCmdStruct->SGetDesc.pDesc = pFlashDesc; pCmdStruct->SGetDesc.pFlashCompany = pDeviceCompany; break; // get sector number based on address case CNTRL_GET_SECTNUM: ErrorCode = GetSectorNumber( pCmdStruct->SGetSectNum.ulOffset, (int *)pCmdStruct->SGetSectNum.pSectorNum ); break; // get sector number start and end offset case CNTRL_GET_SECSTARTEND: ErrorCode = GetSectorStartEnd( pCmdStruct->SSectStartEnd.pStartOffset, pCmdStruct->SSectStartEnd.pEndOffset, pCmdStruct->SSectStartEnd.nSectorNum ); break; // get the number of sectors case CNTRL_GETNUM_SECTORS: pCmdStruct->SGetNumSectors.pnNumSectors[0] = gNumSectors; break; // reset case CNTRL_RESET: ErrorCode = ResetFlash(pCmdStruct->SReset.ulFlashStartAddr); break; // turn on dataflow (command required of all device drivers) case ADI_DEV_CMD_SET_DATAFLOW: ErrorCode = NO_ERR; break; case ADI_DEV_CMD_SET_DATAFLOW_METHOD: // Do nothing & simply return back for these commands break; // get peripheral DMA support (command required of all device drivers) case ADI_DEV_CMD_GET_PERIPHERAL_DMA_SUPPORT: (*(u32 *)pArg) = FALSE; // no, this device is not supported by peripheral DMA break; // no command or unknown command do nothing default: // set our error ErrorCode = UNKNOWN_COMMAND; break; } // return return(ErrorCode); }
int BootFromFlash( void ) { long int HexFile_p = 0; /* datafile descriptor */ long int HexFileSize = 0; /* size in bytes of the file */ int ErrorCount = 0; ST_ErrorCode_t ReturnError = ST_NO_ERROR; char Filename[] = "flash.hex"; U8 Block; U32 BlockStart; STFLASH_Print(( "\n" )); STFLASH_Print(( "============================================================\n" )); STFLASH_Print(( "Commencing Boot From Flash Test ..\n" )); STFLASH_Print(( "============================================================\n" )); /* Init Bank 0, Vpp 0 */ InitParams_s.DeviceType = DEVICE_TYPE; InitParams_s.BaseAddress = (U32*)STFLASH_BANK_0_BASE; InitParams_s.VppAddress = (U32*)STFLASH_VPP_0_ENABLE; InitParams_s.MinAccessWidth = MIN_ACCESS_WIDTH; InitParams_s.MaxAccessWidth = MAX_ACCESS_WIDTH; InitParams_s.NumberOfBlocks = NUM_BLOCKS; InitParams_s.Blocks = BlockData_s; #ifdef ST_OS21 InitParams_s.DriverPartition = system_partition; #else InitParams_s.DriverPartition = TEST_PARTITION_1; #endif STFLASH_Print(( "FLASH_BANK_0_BASE = %x\n", STFLASH_BANK_0_BASE )); STFLASH_Print(( "Calling STFLASH_Init() Bank0 ..........\n" )); ReturnError = STFLASH_Init( "Bank0", &InitParams_s ); ErrorReport( &ErrorCount, ReturnError, ST_NO_ERROR ); /* Open Bank 0 */ STFLASH_Print(( "Calling STFLASH_Open() Bank0 ..........\n" )); ReturnError = STFLASH_Open( "Bank0", &OpenParams_s, &FLASHHndl[0] ); ErrorReport( &ErrorCount, ReturnError, ST_NO_ERROR ); /* GetParams for Bank 0 */ GetParams_s.InitParams.Blocks = GetBlkDat_s; STFLASH_Print(( "Calling STFLASH_GetParams() Bank 0 ....\n" )); ReturnError = STFLASH_GetParams( FLASHHndl[0], &GetParams_s ); ErrorReport( &ErrorCount, ReturnError, ST_NO_ERROR ); ParamsReport( &GetParams_s ); BlockStart = BaseAddress[BANK0] = (U32) InitParams_s.BaseAddress; for ( Block = 0; Block < InitParams_s.NumberOfBlocks; Block ++) { BlockInfo[Block].Bank = 0; BlockInfo[Block].Address = BlockStart; BlockInfo[Block].Length = InitParams_s.Blocks[Block].Length; BlockStart += InitParams_s.Blocks[Block].Length; } /* Open and Read file into memory */ HexFile_p = debugopen(Filename, "rb"); STFLASH_Print(("HexFile_p = 0x%8x\n",HexFile_p)); if (HexFile_p < 0) { STFLASH_Print(("Error opening file \'%s\'\n", Filename )); return (0); } else { HexFileSize = debugfilesize(HexFile_p); STFLASH_Print(("HexFileSize = 0x%8x\n",HexFileSize)); /* allocate File data buffer */ FlashData_p = (char*) memory_allocate( system_partition, (U32) HexFileSize ); if ( FlashData_p != NULL ) { STFLASH_Print(("Loading \'%s\' into memory, wait .. ", Filename )); debugread(HexFile_p, FlashData_p, (size_t) HexFileSize); STFLASH_Print(("%d bytes\n", HexFileSize )); } else { STFLASH_Print(("Not enough memory for HEX file (%d bytes)\n", HexFileSize)); HexFileSize = 0; } debugclose(HexFile_p); } if ( HexFileSize > 0 ) { /* convert buffer to binary and resize memory */ STFLASH_Print(("Converting file in memory, wait .. ")); FlashSize = ConvertMemory( HexFileSize ); if ( FlashSize > 0 ) { STFLASH_Print(("%d bytes\n", FlashSize )); FlashData_p = (char*) memory_reallocate( system_partition, FlashData_p, FlashSize ); } else { STFLASH_Print(("Invalid file\n")); } } if ( EraseFlash(FALSE) == FALSE ) { if ( ProgramFlash() == FALSE ) { VerifyFlash(); } } /* Close Bank 0 */ STFLASH_Print(( "Calling STFLASH_Close() Bank 0 ........\n" )); ReturnError = STFLASH_Close( FLASHHndl[0] ); ErrorReport( &ErrorCount, ReturnError, ST_NO_ERROR ); /* Term Bank 0 */ TermParams_s.ForceTerminate = FALSE; STFLASH_Print(( "Calling STFLASH_Term() Bank 0 .........\n" )); ReturnError = STFLASH_Term( "Bank0", &TermParams_s ); ErrorReport( &ErrorCount, ReturnError, ST_NO_ERROR ); return( ErrorCount ); }
u32 adi_pdd_Control( ADI_DEV_PDD_HANDLE PDDHandle, u32 Command, void *pArg) { ERROR_CODE ErrorCode = NO_ERR; COMMAND_STRUCT *pCmdStruct = (COMMAND_STRUCT *)pArg; // switch on the command switch ( Command ) { // erase all case CNTRL_ERASE_ALL: ErrorCode = EraseFlash(pCmdStruct->SEraseAll.ulFlashStartAddr); break; // erase sector case CNTRL_ERASE_SECT: ErrorCode = EraseBlock( pCmdStruct->SEraseSect.nSectorNum, pCmdStruct->SEraseSect.ulFlashStartAddr ); break; // get manufacturer and device codes case CNTRL_GET_CODES: ErrorCode = GetCodes((int *)pCmdStruct->SGetCodes.pManCode, (int *)pCmdStruct->SGetCodes.pDevCode, (unsigned long)pCmdStruct->SGetCodes.ulFlashStartAddr); break; //Filling the contents with data case CNTRL_GET_DESC: pCmdStruct->SGetDesc.pTitle = pEzKitTitle; pCmdStruct->SGetDesc.pDesc = pFlashDesc; pCmdStruct->SGetDesc.pFlashCompany = pDeviceCompany; break; // get sector number based on address case CNTRL_GET_SECTNUM: ErrorCode = GetSectorNumber( pCmdStruct->SGetSectNum.ulOffset, (int *)pCmdStruct->SGetSectNum.pSectorNum ); break; // get sector number start and end offset case CNTRL_GET_SECSTARTEND: ErrorCode = GetSectorStartEnd( pCmdStruct->SSectStartEnd.pStartOffset, pCmdStruct->SSectStartEnd.pEndOffset, pCmdStruct->SSectStartEnd.nSectorNum ); break; // get the number of sectors case CNTRL_GETNUM_SECTORS: pCmdStruct->SGetNumSectors.pnNumSectors[0] = NUM_SECTORS; break; // reset case CNTRL_RESET: ErrorCode = ResetFlash(pCmdStruct->SReset.ulFlashStartAddr); break; // no command or unknown command do nothing default: // set our error ErrorCode = UNKNOWN_COMMAND; break; } // return return(ErrorCode); }
// // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; int wmId, wmEvent; PAINTSTRUCT ps; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_HELP_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_FILE_EXIT: SendMessage (hWnd, WM_CLOSE, 0, 0); break; case IDM_FLASH_ERASE: if (gSystemErrorHasOccured != TRUE){ LoadString(hInst, IDS_FLASH_ERASE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); EraseFlash(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); } else SystemErrorAlert(hWnd); break; case IDM_FLASH_TEST_TIMED: LoadString(hInst, IDS_FLASH_TEST_TIMED, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); gTimerID = SetTimer(hWnd,BB_TEST_FLASH,50,NULL); break; case IDM_FLASH_TEST_STRAIGHT: TestFlash(hWnd); break; case IDM_FLASH_SAVE: LoadString(hInst, IDS_FLASH_SAVE_BL, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); SaveFlash(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); break; case IDM_FLASH_SAVE_WINCE: LoadString(hInst, IDS_FLASH_SAVE_WINCE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); SaveFlashWince(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); break; case IDM_FLASH_PROTECT_WINCE: LoadString(hInst, IDS_FLASH_PROTECT_WINCE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); ProtectWince(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); break; case IDM_FLASH_SAVE_GZ: LoadString(hInst, IDS_FLASH_SAVE_BL_GZ, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); SaveFlashGZ(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); break; case IDM_FLASH_PROGRAM: if (gSystemErrorHasOccured != TRUE){ if (LoadImage(hWnd) != TRUE) //loaded, so program it return 0; LoadString(hInst, IDS_FLASH_PROTECT_WINCE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); ProtectWince(hWnd); if (gSystemErrorHasOccured == TRUE) return 0; LoadString(hInst, IDS_FLASH_ERASE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); EraseFlash(hWnd); LoadString(hInst, IDS_FLASH_PROGRAM, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,FALSE); UpdateWindow(hWnd); Sleep(100); if (ProgramFlash(hWnd) == TRUE) VerifyFlash(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); } else SystemErrorAlert(hWnd); break; case IDM_FLASH_PROGRAM_XPLE: if (gSystemErrorHasOccured != TRUE){ if (LoadImage(hWnd) != TRUE) //loaded, so program it return 0; for (int i=0; i < 50; i++){ LoadString(hInst, IDS_FLASH_PROTECT_WINCE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); ProtectWince(hWnd); if (gSystemErrorHasOccured == TRUE) return 0; LoadString(hInst, IDS_FLASH_ERASE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); EraseFlash(hWnd); LoadString(hInst, IDS_FLASH_PROGRAM, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,FALSE); UpdateWindow(hWnd); Sleep(100); ProgramFlash(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); UpdateWindow(hWnd); Sleep(100); } VerifyFlash(hWnd); } else SystemErrorAlert(hWnd); break; case IDM_FLASH_SHOW: ShowFlash(hWnd); break; case IDM_FLASH_VERIFY: VerifyFlash(hWnd); break; case IDOK: //SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd); //SendMessage (hWnd, WM_CLOSE, 0, 0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_TIMER: // this method isn;t used anymore, really KillTimer(hWnd,wParam); if (wParam == BB_TEST_FLASH){ TestFlash(hWnd); LoadString(hInst, IDS_IDLE, szStatus, MAX_LOADSTRING); InvalidateRect(hWnd,NULL,TRUE); } else{} case WM_CREATE: hwndCB = CreateRpCommandBar(hWnd); break; case WM_PAINT: RECT rt; hdc = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rt); DrawText(hdc, szStatus, _tcslen(szStatus), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: CommandBar_Destroy(hwndCB); PostQuitMessage(0); break; case WM_SETTINGCHANGE: SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
/** * ProcessIO: **/ static void ProcessIO(void) { uint16_t address; uint16_t erase_length; uint8_t length; uint8_t checksum; uint8_t cmd; uint8_t rc = CH_ERROR_NONE; /* reset the LED state */ led_counter--; if (led_counter == 0) { CHugSetLEDsInternal(led_color); led_color *= 2; if (led_color > CH_STATUS_LED_BLUE) led_color = CH_STATUS_LED_GREEN; led_counter = BOOTLOADER_FLASH_INTERVAL; } /* User Application USB tasks */ if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) return; /* no data was received */ if(HIDRxHandleBusy(USBOutHandle)) { if (idle_counter++ == 0xff && idle_command != 0x00) CHugDeviceIdle(); return; } /* got data, reset idle counter */ idle_counter = 0; /* clear for debugging */ memset (TxBuffer, 0xff, sizeof (TxBuffer)); cmd = RxBuffer[CH_BUFFER_INPUT_CMD]; switch(cmd) { case CH_CMD_GET_HARDWARE_VERSION: TxBuffer[CH_BUFFER_OUTPUT_DATA] = PORTB & 0x0f; break; case CH_CMD_RESET: /* only reset when USB stack is not busy */ idle_command = CH_CMD_RESET; break; case CH_CMD_GET_FIRMWARE_VERSION: memcpy (&TxBuffer[CH_BUFFER_OUTPUT_DATA], &FirmwareVersion, 2 * 3); break; case CH_CMD_ERASE_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); /* allow to erase any address but not the bootloader */ if (address < CH_EEPROM_ADDR_RUNCODE) { rc = CH_ERROR_INVALID_ADDRESS; break; } memcpy (&erase_length, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+2], 2); EraseFlash(address, address + erase_length); break; case CH_CMD_READ_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* allow to read any address */ memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); length = RxBuffer[CH_BUFFER_INPUT_DATA+2]; if (length > 60) { rc = CH_ERROR_INVALID_LENGTH; break; } ReadFlash(address, length, (unsigned char *) &TxBuffer[CH_BUFFER_OUTPUT_DATA+1]); checksum = CHugCalculateChecksum (&TxBuffer[CH_BUFFER_OUTPUT_DATA+1], length); TxBuffer[CH_BUFFER_OUTPUT_DATA+0] = checksum; break; case CH_CMD_WRITE_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* write to flash that's not the bootloader */ memcpy (&address, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0], 2); if (address < CH_EEPROM_ADDR_RUNCODE) { rc = CH_ERROR_INVALID_ADDRESS; break; } length = RxBuffer[CH_BUFFER_INPUT_DATA+2]; if (length > CH_FLASH_TRANSFER_BLOCK_SIZE) { rc = CH_ERROR_INVALID_LENGTH; break; } checksum = CHugCalculateChecksum(&RxBuffer[CH_BUFFER_INPUT_DATA+4], length); if (checksum != RxBuffer[CH_BUFFER_INPUT_DATA+3]) { rc = CH_ERROR_INVALID_CHECKSUM; break; } /* copy low 32 bytes into flash buffer, and only write * in 64 byte chunks as this is a limitation of the * hardware */ if ((address & CH_FLASH_TRANSFER_BLOCK_SIZE) == 0) { memset (FlashBuffer, 0xff, CH_FLASH_WRITE_BLOCK_SIZE); memcpy (FlashBuffer, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4], length); } else { memcpy (FlashBuffer + CH_FLASH_TRANSFER_BLOCK_SIZE, (const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4], length); WriteBytesFlash(address - CH_FLASH_TRANSFER_BLOCK_SIZE, CH_FLASH_WRITE_BLOCK_SIZE, (unsigned char *) FlashBuffer); } break; case CH_CMD_BOOT_FLASH: /* are we lost or stolen */ if (flash_success == 0xff) { rc = CH_ERROR_DEVICE_DEACTIVATED; break; } /* only boot when USB stack is not busy */ idle_command = CH_CMD_BOOT_FLASH; break; case CH_CMD_SET_FLASH_SUCCESS: if (RxBuffer[CH_BUFFER_INPUT_DATA] != 0x00) { rc = CH_ERROR_INVALID_VALUE; break; } flash_success = RxBuffer[CH_BUFFER_INPUT_DATA]; EraseFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, CH_EEPROM_ADDR_FLASH_SUCCESS + 1); WriteBytesFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, 1, (unsigned char *) &RxBuffer[CH_BUFFER_INPUT_DATA]); break; case CH_CMD_SELF_TEST: rc = CHugSelfTest(); break; default: rc = CH_ERROR_UNKNOWN_CMD_FOR_BOOTLOADER; break; } /* always send return code */ if(!HIDTxHandleBusy(USBInHandle)) { TxBuffer[CH_BUFFER_OUTPUT_RETVAL] = rc; TxBuffer[CH_BUFFER_OUTPUT_CMD] = cmd; USBInHandle = HIDTxPacket(HID_EP, (BYTE*)&TxBuffer[0], CH_USB_HID_EP_SIZE); } /* re-arm the OUT endpoint for the next packet */ USBOutHandle = HIDRxPacket(HID_EP, (BYTE*)&RxBuffer, CH_USB_HID_EP_SIZE); }
char BootApplication(FSFILE * pFile) { // Read the first packet header size_t sBytesRead = FSfread(&g_packetHeader, 8, 1, pFile); if( sBytesRead != 8 || g_packetHeader.PacketSize > 56) { if ( sBytesRead == 2 && g_packetHeader.PacketSize == 0) return 0; else return 2; } // Read the data if ( g_packetHeader.PacketSize ) { sBytesRead = FSfread(&g_DataBuffer, g_packetHeader.PacketSize, 1, pFile); if( sBytesRead != g_packetHeader.PacketSize ) return 2; } // Next action will depend on the type of packet we have switch( g_packetHeader.Type ) { case 0: { // FINISHED! return 0; break; } case UNLOCK_CONFIG: { MaxPageToErase = MaxPageToEraseWithConfigs; //Assume we will not allow erase/programming of config words (unless host sends override command) ProgramMemStopAddress = ProgramMemStopWithConfigs; ConfigsProtected = UNLOCKCONFIG; // There is one more page to erase then... ErasePageTracker = MaxPageToEraseWithConfigs; EraseFlash(); break; } case PROGRAM_COMPLETE: { WriteFlashSubBlock(); ProgrammedPointer = InvalidAddress; //Reinitialize pointer to an invalid range, so we know the next PROGRAM_DEVICE will be the start address of a contiguous section. break; } case PROGRAM_DEVICE: { if(ProgrammedPointer == (unsigned long)InvalidAddress) ProgrammedPointer = (unsigned long)g_packetHeader.Address; if(ProgrammedPointer == (unsigned long)g_packetHeader.Address) { unsigned char i; for(i = 0; i < (g_packetHeader.PacketSize/WORDSIZE); i++) { //unsigned int index; //index = (RequestDataBlockSize-g_packetHeader.PacketSize)/WORDSIZE+i; //ProgrammingBuffer[BufferedDataIndex] = g_DataBuffer[(RequestDataBlockSize-g_packetHeader.PacketSize)/WORDSIZE+i]; //Data field is right justified. Need to put it in the buffer left justified. ProgrammingBuffer[BufferedDataIndex] = g_DataBuffer[(i*2)]; ProgrammingBuffer[BufferedDataIndex] += (g_DataBuffer[(i*2)+1] << 8) & 0xFF00; BufferedDataIndex++; ProgrammedPointer++; if(BufferedDataIndex == (RequestDataBlockSize/WORDSIZE)) //Need to make sure it doesn't call WriteFlashSubBlock() unless BufferedDataIndex/2 is an integer { WriteFlashSubBlock(); } } } //else host sent us a non-contiguous packet address... to make this firmware simpler, host should not do this without sending a PROGRAM_COMPLETE command in between program sections. break; } } return 1; }//End BootApplication()
void SafeEraseFlash(unsigned long startaddr, unsigned long endaddr){ if (startaddr >= PROG_START && endaddr >= PROG_START) EraseFlash(startaddr,endaddr); }
/********************************************************************* * Function: INT8U MRF24J40Init(void) * * PreCondition: BoardInit (or other initialzation code is required) * * Input: None * * Output: Success/error * * Side Effects: MRF24J40 is initialized * * Overview: This function initializes the MRF24J40 ********************************************************************/ INT8U MRF24J40Init(void) { volatile INT8U i; volatile INT16U j; #if PROCESSOR == COLDFIRE_V1 iNesting++; #endif PHY_RESETn_LOW; for(j=0;j<7000;j++) { }; PHY_RESETn_HIGH; for(j=0;j<7000;j++) { }; /* do a soft reset */ PHYSetShortRAMAddr(WRITE_SOFTRST,0x07); for(j=0;j<1000;j++){}; PHYSetShortRAMAddr(WRITE_RFCTL,0x04); PHYSetShortRAMAddr(WRITE_RFCTL,0x00); for(j=0;j<1000;j++){}; // Enable FIFO and TX Enable PHYSetShortRAMAddr(WRITE_FFOEN, 0x98); PHYSetShortRAMAddr(WRITE_TXPEMISP, 0x95); /* program the RF and Baseband Register */ //PHYSetLongRAMAddr(RFCTRL4,0x02); /* Enable the RX */ //PHYSetLongRAMAddr(RFRXCTRL,0x01); /* setup */ PHYSetLongRAMAddr(RFCTRL0,0x03); PHYSetLongRAMAddr(RFCTRL1,RFCTRL1_VAL); /* enable the RF-PLL */ PHYSetLongRAMAddr(RFCTRL2,0x80); /* set TX output power */ PHYSetLongRAMAddr(RFCTRL3,TX_POWER_LEVEL); /* program RSSI ADC with 2.5 MHz clock */ //PHYSetLongRAMAddr(RFCTRL6,0x04); PHYSetLongRAMAddr(RFCTRL6,0x90); //PHYSetLongRAMAddr(RFCTRL7,0b00000000); //PHYSetLongRAMAddr(RFCTRL7,0b10011000); PHYSetLongRAMAddr(RFCTRL7,0x80); PHYSetLongRAMAddr(RFCTRL8,0x10); // Disable clockout PHYSetLongRAMAddr(SCLKDIV,0x21); #if (DEVICE_TYPE != PAN_COORDINATOR) // Join network as router // and Automatic Acknowledgment enabled PHYSetShortRAMAddr(WRITE_RXMCR, 0x04); #else // Join network as PAN Coordinator // and Automatic Acknowledgment enabled PHYSetShortRAMAddr(WRITE_RXMCR, 0x0C); #endif /* flush the RX fifo */ PHYSetShortRAMAddr(WRITE_RXFLUSH,0x01); // Configure Unslotted CSMA-CA Mode i = PHYGetShortRAMAddr(READ_TXMCR); i = i & 0xDF; PHYSetShortRAMAddr(WRITE_TXMCR,i); // Define Nonbeacon-Enabled Network PHYSetShortRAMAddr(WRITE_ORDER,0xFF); /* Program CCA mode using RSSI */ // 0x80 é o valor default, 0xB8 é o recomendado PHYSetShortRAMAddr(WRITE_BBREG2,0xB8); //PHYSetShortRAMAddr(WRITE_BBREG2,0x80); /* Program CCA, RSSI threshold values */ // Valor Default é 0x00, recomendado é 0x60 // Determina o níve de sinal que indica o canal como ocupado // 0x60 = -69 dBm PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x60); //PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x00); /* Enable the packet RSSI */ PHYSetShortRAMAddr(WRITE_BBREG6,0x40); /* Program the short MAC Address, 0xffff */ #if (DEVICE_TYPE != PAN_COORDINATOR) PHYSetShortRAMAddr(WRITE_SADRL,0xFF); PHYSetShortRAMAddr(WRITE_SADRH,0xFF); #else PHYSetShortRAMAddr(WRITE_SADRL,0x00); PHYSetShortRAMAddr(WRITE_SADRH,0x00); #endif /* and the short PanId Identifier, 0xffff */ #if (DEVICE_TYPE != PAN_COORDINATOR) PHYSetShortRAMAddr(WRITE_PANIDL,0xFF); PHYSetShortRAMAddr(WRITE_PANIDH,0xFF); #else PHYSetShortRAMAddr(WRITE_PANIDL,(PANID_INIT_VALUE & 0xFF)); PHYSetShortRAMAddr(WRITE_PANIDH,((PANID_INIT_VALUE>>8) & 0xFF)); #endif /* Test radio */ do { PHYSetShortRAMAddr(WRITE_EADR0,0xCA); i = PHYGetShortRAMAddr(READ_EADR0); if(i!= 0xCA) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR1,0xF1); i = PHYGetShortRAMAddr(READ_EADR1); if(i!= 0xF1) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR2,0x05); i = PHYGetShortRAMAddr(READ_EADR2); if(i!= 0x05) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR3,0xBA); i = PHYGetShortRAMAddr(READ_EADR3); if(i!= 0xBA) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR4,0xFF); i = PHYGetShortRAMAddr(READ_EADR4); if(i!= 0xFF) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR5,0x10); i = PHYGetShortRAMAddr(READ_EADR5); if(i!= 0x10) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR6,0x4E); i = PHYGetShortRAMAddr(READ_EADR6); if(i!= 0x4E) goto EXIT_ERROR; PHYSetShortRAMAddr(WRITE_EADR7,0x11); i = PHYGetShortRAMAddr(READ_EADR7); if(i!= 0x11) goto EXIT_ERROR; break; EXIT_ERROR: #if PROCESSOR == COLDFIRE_V1 iNesting--; #endif return RADIO_ERROR; }while(0); // Inicializa o módulo de memória FLASH InitFlash(); #if (PROCESSOR == ARM_Cortex_M0) if ((mac64Address[0] == 0xFF) && (mac64Address[1] == 0xFF) && (mac64Address[2] == 0xFF) && (mac64Address[3] == 0xFF)) { INT8U tmp_mac64[8] = {EUI_0,EUI_1,EUI_2,EUI_3,EUI_4,EUI_5,EUI_6,EUI_7}; EraseFlash(0x0001F000, 1024); WriteToFlash((INT8U*)&tmp_mac64, MAC64_MEM_ADDRESS, 8); } #endif /* Program Long MAC Address*/ for(i=0;i<8;i++) { PHYSetShortRAMAddr(WRITE_EADR0+i*2,mac64Address[7-i]); } SetChannel(CHANNEL_INIT_VALUE); for(j=0;j<1000;j++){}; //i = PHYGetShortRAMAddr(READ_ISRSTS); PHYSetShortRAMAddr(WRITE_FFOEN, 0x98); // Habilita interrupções PHYSetShortRAMAddr(WRITE_INTMSK,0xF6); // bypass the errata to make RF communication stable PHYSetLongRAMAddr(RFCTRL1, RFCTRL1_VAL); #if PROCESSOR == COLDFIRE_V1 iNesting--; #endif return RADIO_OK; }
HAL_StatusTypeDef WriteFlash(int32_t* table, uint32_t Address, uint32_t lenth) { HAL_StatusTypeDef res; HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); /* Erase the user Flash area (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ EraseFlash(Address); for(int i=0;i<lenth;i++) HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address+4*i,*(table+i)); res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address+4*lenth-4,table[lenth-1]); HAL_FLASH_Lock(); return res; }