main() { DestData[0] = 1.0; DestData[1] = 2.0; // write 2 x 8 bytes of the buffer to the 2nd MByte of the Flash Chip first 64KByte block ProgramFlash((volatile char *)DestData,16,FLASH_USER,""); DestData[0] = 3.0; DestData[1] = 4.0; // write 2 x 8 bytes of the buffer to the 2nd MByte of the Flash Chip 2nd 64KByte Block ProgramFlash((volatile char *)DestData,16,FLASH_USER+FLASH_BLOCK_SIZE,""); PrintTwoBlocks(); // change first block DestData[0] = 5.0; DestData[1] = 6.0; // write 2 x 8 bytes of the buffer to the 2nd MByte of the Flash Chip first 64KByte block ProgramFlash((volatile char *)DestData,16,FLASH_USER,""); printf("First block changed\n"); PrintTwoBlocks(); // change 2nd block DestData[0] = 7.0; DestData[1] = 8.0; // write 2 x 8 bytes of the buffer to the 2nd MByte of the Flash Chip 2nd 64KByte Block ProgramFlash((volatile char *)DestData,16,FLASH_USER+FLASH_BLOCK_SIZE,""); printf("2nd block changed\n"); PrintTwoBlocks(); }
int vfs_sync_write(void * xfd, void * buf, unsigned int len) { unsigned int bytes_to_write; unsigned long *fd = xfd; int rc; /* if caller is trying to write past end of backing store */ if ((*fd + len) > (VFS_BK_STORE_BASE_ADDRESS + VFS_BK_STORE_SIZE)) { dprintf("vfs_sync_write() tried to write 0x%x bytes at 0x%lx\n", len,*fd); vfs_sync_close(fd); return 1; } /* while there's data left to write */ while (len) { /* chose a reasonable chunk to write at a time */ bytes_to_write = (len > VFS_MAX_FILE_IO_SIZE) ? VFS_MAX_FILE_IO_SIZE : len; /* write the chunk to the flash driver */ rc = ProgramFlash(*fd, buf, (int) bytes_to_write); /* the flash driver returns TRUE when successful */ if (rc != TRUE) { dprintf("ProgramFlash(0x%lx,buf,0x%x) failed\n",*fd,len); vfs_sync_close(fd); return 2; } /* increase the "file pointer" by the amount written */ *fd += bytes_to_write; /* decrease whats remaining to write by the amount written */ len -= bytes_to_write; } return 0; /* success */ }
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 ); }
/*************************************************************** * void CheckExternalFlashHex(void) ***************************************************************/ void CheckExternalFlashHex(void) { typedef struct { UINT32 mchpSignature; UINT32 mchpCRCData; } CRC_CHECK; CRC_CHECK externalCRC, expectedCRC; WORD textHeight; void *pFont; XCHAR *pStr = NULL; BOOL setProgram = FALSE; XCHAR msgStr1[] = {'P','r','o','g','r','a','m',' ','E','x','t','e','r','n','a','l',' ','D','a','t','a',0}; XCHAR msgStr2[] = {'E','x','t','e','r','n','a','l',' ','d','a','t','a',' ','i','n','v','a','l','i','d','.',0}; XCHAR msgStr3[] = {'P','l','e','a','s','e',' ','s','e','n','d',' ','d','a','t','a',' ','u','s','i','n','g',0}; XCHAR msgStr4[] = {'"','E','x','t','e','r','n','a','l',' ','M','e','m','o','r','y',0}; XCHAR msgStr5[] = {'P','r','o','g','r','a','m','m','e','r','"',' ','u','t','i','l','i','t','y',0}; XCHAR msgStr6[] = {'N','o','w',' ','w','a','i','t','i','n','g',' ','f','o','r',' ','d','a','t','a',0}; XCHAR msgStr7[] = {'v','i','a',' ','U','A','R','T','.','.','.',0}; pFont = (void*) &FONTDEFAULT; SetFont(pFont); textHeight = GetTextHeight(pFont); // check if the CRC matches the data stored in the external flash memory expectedCRC.mchpCRCData = GRC_CRC32_EXTERNAL_MARKER; expectedCRC.mchpSignature = 0x5048434D; // this is "MCHP" // check if programming is prompted if(btnS2 == HW_BUTTON_PRESS) { pStr = msgStr1; setProgram = TRUE; } if (setProgram == FALSE) { ReadArray(GRC_CRC32_EXTERNAL_ADDR, (BYTE *)&externalCRC, 8); if ((expectedCRC.mchpCRCData != externalCRC.mchpCRCData) || \ (expectedCRC.mchpSignature != externalCRC.mchpSignature)) { // expected and read CRC does not match, proceed to programming flash first // run the flash programming pStr = msgStr2; setProgram = TRUE; } } if (setProgram == TRUE) { SetColor(WHITE); ClearDevice(); SetColor(BLACK); OutTextXY(10,10 , pStr); OutTextXY(10,10 + (textHeight*2), msgStr3); OutTextXY(10,10 + (textHeight*3), msgStr4); OutTextXY(10,10 + (textHeight*4), msgStr5); OutTextXY(10,10 + (textHeight*5), msgStr6); OutTextXY(10,10 + (textHeight*6), msgStr7); #ifndef USE_BISTABLE_DISPLAY_GOL_AUTO_REFRESH #if defined( ONE_CYCLE_DRAWING ) // The screen drawing starts and completes during one while(1) cycle loop in main(). // This Demo is one cycle drawing application. if ( GFX_DRIVER_IsUpdateRequested() || (g_UPDATE_FLAGS == GFX_UPDATE_AS_IT_DRAWS) ) { GFX_DRIVER_UpdateEpd( g_UPDATE_FLAGS, 0, 0, GetMaxX(), GetMaxY() ); g_UPDATE_FLAGS = GFX_UPDATE_NO_FLASH | GFX_WAIT_IMAGE_DISPLAYED; } #else // This way can be used when drawing may take few or more cycles of while(1) loop in main(). // See "tick.c" file for details. if ( g_UpdateNow || (g_UPDATE_FLAGS == GFX_UPDATE_AS_IT_DRAWS) ) { g_UpdateNow = 0; GFX_DRIVER_UpdateEpd( g_UPDATE_FLAGS, 0, 0, GetMaxX(), GetMaxY() ); g_UPDATE_FLAGS = GFX_UPDATE_NO_FLASH | GFX_WAIT_IMAGE_DISPLAYED; } #endif #endif // Call the external flash programming routine ProgramFlash(); // check if UART is still busy sending replies to the host while(U2STAbits.TRMT); DelayMs(10); // Force Reset to force the checking of the flash memory if programming was a success Reset(); } }
// // 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; }