📄 main.c
字号:
#include "../../Include/type.h"#define UART1_BASE 0x00206000#define UART2_BASE 0x00207000#define _reg_URXD (*((volatile U32 *)(gUartBase+0x00)))#define _reg_UTXD (*((volatile U32 *)(gUartBase+0x40)))#define _reg_USR2 (*((volatile U32 *)(gUartBase+0x98)))#define ADET_MASK 0x8000 // auto-baud detection #define TXFE_MASK 0x4000 // Tx buffer empty#define RDR_MASK 0x0001 // receive data readyU32 gUartBase;const U8 gMsgGreet[] = "\n\rMX1ADS Sync-flash Programming Utility v0.5 2002/08/21\n\r";const U8 gMsgSource[] = "Source address (stored in 0x0AFE0000): 0x";const U8 gMsgTarget[] = "Target address (stored in 0x0AFE0004): 0x";const U8 gMsgSize[] = "Size (stored in 0x0AFE0008): 0x";const U8 gMsgStart[] = "Press any key to start programming ...";const U8 gMsgErasing[] = "\n\rErasing flash ...";const U8 gMsgProgramming[] = "\n\rProgramming ...";const U8 gMsgFinish[] = "\n\rProgramming finished.\n\rPress 'a' to continue ....";U8 DataReady(){ return (_reg_USR2 & RDR_MASK); // check RDR bit}void PutChar(U8 data){ while (!(_reg_USR2 & TXFE_MASK)); // wait until TXFE bit set _reg_UTXD = (U16)data;}U8 GetChar(){ while (!DataReady()); // wait until RDR bit set return (U8)_reg_URXD;}void PutHex(U8 data){ U8 d; // print first digit d = data >> 4; if (d > 9) d += 55; else d += '0'; PutChar(d); // print second digit d = data & 0xF; if (d > 9) d += 55; else d += '0'; PutChar(d);}U8 Verify(U32 source, U32 destin, U32 count){ U32 *pSource, *pDestin, i; EUARTputString((U8 *)"\n\rVerifying flash ..."); pSource = (U32 *)source; pDestin = (U32 *)destin; for (i=0; i<count; i+=4) { if (*(pSource++)!=*(pDestin++)) return 0; // not equal } return 1; // all verified okay}U8 CheckBlank(U32 destin, U32 count){ U32 *pDestin, i;EUARTputString((U8 *)"\n\rBlank checking ..."); pDestin = (U32 *)destin; for (i=0; i<count; i+=4) { if (*(pDestin++) != 0xFFFFFFFF) return 0; // not blank } return 1; // all blankcd}int main(void){ U32 SourceAddress; U32 TargetAddress; U32 ByteSize; U8 tmp; U32 i; U32 j=1; long size; U32 blockStart; char c; U16 status; // see if UART1 is the current port to use gUartBase = UART1_BASE; if (!(_reg_USR2 & 0x8000)) // ADET bit not set ? { gUartBase = UART2_BASE; } EUARTputString((U8 *)&gMsgGreet); SourceAddress = *((U32 *)0x0AFE0000); EUARTputString((U8 *)&gMsgSource); PutHex((U8)(SourceAddress >> 24)); PutHex((U8)(SourceAddress >> 16)); PutHex((U8)(SourceAddress >> 8)); PutHex((U8)SourceAddress); PutChar('\n'); PutChar('\r'); TargetAddress = *((U32 *)0x0AFE0004); EUARTputString((U8 *)&gMsgTarget); PutHex((U8)(TargetAddress >> 24)); PutHex((U8)(TargetAddress >> 16)); PutHex((U8)(TargetAddress >> 8)); PutHex((U8)TargetAddress); PutChar('\n'); PutChar('\r'); ByteSize = *((U32 *)0x0AFE0008); EUARTputString((U8 *)&gMsgSize); PutHex((U8)(ByteSize >> 24)); PutHex((U8)(ByteSize >> 16)); PutHex((U8)(ByteSize >> 8)); PutHex((U8)ByteSize); PutChar('\n'); PutChar('\r'); // clear input buffer while (DataReady()) c = GetChar(); EUARTputString((U8 *)&gMsgStart); c = GetChar(); SyncFlashInit();// SyncFlashNvmodeErase();// SyncFlashNvmodeWrite(); SyncFlashNormal(); do { EUARTputString((U8 *)"\nErasing ..."); size = ByteSize; blockStart = TargetAddress; do { SyncFlashErase(blockStart); size -= 0x100000; blockStart += 0x100000; } while (size > 0); SyncFlashNormal(); } while (!CheckBlank(TargetAddress, ByteSize)); EUARTputString((U8 *)"\nProgramming ..."); SyncFlashWrite(SourceAddress, TargetAddress, ByteSize); if (Verify(SourceAddress, TargetAddress, ByteSize)) { EUARTputString((U8 *)" succeed.\n"); } else { EUARTputString((U8 *)" failed !\n"); } SyncFlashNormal(); /* if(Compare(SourceAddress, TargetAddress, ByteSize)) Error(); while (1); // success*/ EUARTputString((U8 *)&gMsgFinish); JumpToAddr(0); // go back to boot-strap return 0; // dummy}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -