📄 zmport.c
字号:
#include "evm50100.h"#include "ks32c50.h"#include "zmodem.h"// #define ZM_IO_INT#ifdef ZM_IO_INT#define ZM_PUTS(chan, str) i_puts((chan), (str)) #define ZM_PUTC(chan, ch) i_putc((chan), (ch))#define ZM_GETC(chan, ch) i_getc((chan), (ch))#define ZM_FLUSH(chan) i_flush((chan)) #else #define ZM_PUTS(chan, str) put_str(chan, str) #define ZM_PUTC(chan, ch) put_char(chan ,ch)#define ZM_GETC(chan, ch) rcv_char(chan, ch)#define ZM_FLUSH(chan) #endif// static void * ZmMemoryBase = (void *) 0x0800000; // load base address 8Mbstatic U32 ZmMemorySize = 0x0800000; // load memory size 8Mbstatic U32 ZmMemoryPos = 0; // load memory position static char ZmFileName[32] = ""; // last opened file static int ZmUartConsole= 0; // console port static int ZmUartData = 1; // data port static int ZmVerbose = 0; // verbose flag static int ZmWin = 4096;static int ZmPacketSize = 0;static int ZmAscii = 0; /* translate line endings */static int ZmBinary = 1; /* don't translate line endings */static int ZmResume = 0;static int ZmDoCancel = 0; /* signal received */static int ZmDoLogging = 1;static int ZmXferType = 0;static ZModem zmInfo;//extern void StartDownPgm(void *);//////////////////////////////////////////////////////////////////////////////// // programm ROM bank 0 ///////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// void ZmFlashPgm(U8 * base, int size){ U32 romaddr; // ROM0 bank base address int pos; // memory position U8 ch; // /* [19:10] ROM/SRAM/Flash bank # base pointer */ romaddr = (((ROMCON0 & ROMCON_BASE_MASK) >> ROMCON_BASE_OFFSET) << 16);// ROM0 erase ////////////////////////////////////////////////////////////////// Print("\nErase ROM bank 0. Base address 0x%08x", romaddr); *((volatile U8 *)(romaddr + 0x555))=0xAA; // 1 Chip erase write command 555/AA *((volatile U8 *)(romaddr + 0x2AA))=0x55; // 2 Chip erase write command 2AA/55 *((volatile U8 *)(romaddr + 0x555))=0x80; // 3 Chip erase write command 555/80 *((volatile U8 *)(romaddr + 0x555))=0xAA; // 4 Chip erase write command 555/AA *((volatile U8 *)(romaddr + 0x2AA))=0x55; // 5 Chip erase write command 2AA/55 *((volatile U8 *)(romaddr + 0x555))=0x10; // 6 Chip erase write command 555/10 do { ch = *((volatile U8 *)(romaddr + 0x00)); ch &= 0x80; } while(ch == 0);// Print("\nProgramm ROM bank 0 (#-1K writed)."); Print("\n"); for(pos = 0; pos < size; ++pos) { U8 chr;// programm byte //////////////////////////////////////////////////////////////// ch = *(base + pos); *((volatile U8 *)(romaddr + 0x555)) = 0xAA; *((volatile U8 *)(romaddr + 0x2AA)) = 0x55; *((volatile U8 *)(romaddr + 0x555)) = 0xA0; *((volatile U8 *)(romaddr + pos)) = ch; do { chr = *((volatile U8 *)(romaddr + pos)); if((chr & 0x80) == (ch & 0x80)) { break; } } while((chr & 0x20) == 0); chr = *((volatile U8 *)(romaddr + pos)); if((chr & 0x80) != (ch & 0x80)) { Print("\nFail. Address = [0x%08x]", romaddr + pos);; break; } if((pos % 1000) == 0) { Print("#"); } }}void ZmReset(void){ Print("\nReset.\n"); IOPDATA &= ~(1<<17); /* Output 0 to P17(TOUT1) */ IOPMOD |= (1<<17); /* P17 output mode enable */ IOPDATA &= ~(1<<17); /* Output 0 to P17(TOUT1) */} ////////////////////////////////////////////////////////////////////////////////// Chip Erase //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void ZmFlash(void) { U32 addr; U32 defsize; U32 size;// input memory base address /////////////////////////////////////////////////// Print("\nInput Memory base address (0x%08x) 0x", ZmMemoryBase); addr = get_number(16,8); if(addr == 0x0) { addr = (U32) ZmMemoryBase; // default base address }// input bytes count /////////////////////////////////////////////////////////// if(ZmMemoryPos != 0) { defsize = ZmMemoryPos; } else { defsize = ZmMemorySize; } if(get_last() == KEY_CR) { Print("\n"); } Print("Input Memory size (%d) ", defsize); size = get_number(16,8); if(size == 0x0) { size = defsize; // default bytes count }// Disable_Int(nGLOBAL_INT); ZmFlashPgm((U8 *)addr,size); ZmReset();}/* Read characters from remote end and pass them to ZmRcv() until ZmRcv() returns ZmDone or an error.*/static int ZmDoIO(ZModem * info){ static u_char buffer[1024]; // receive buffer TIME tm0; // start time TIME tm1; // current time U32 tu0; // start time in seconds U32 tu1; // current time in seconds int done = 0; U8 ch; GetSysTime(0, & tm0); // get time from Timer 0 tu0 = tm0.tm_sec + tm0.tm_min * 60; // conver minute to seconds while(done == 0) { int pos; if( ZmDoCancel ) {// cancel => send abort message, put to console diagnostic message ///////////// ZmAbort(info); if(ZmVerbose && ZmUartConsole != ZmUartData) { ZM_PUTS(ZmUartConsole, "cancelled by user\n\r"); ZM_FLUSH(ZmUartConsole); } return(0); } pos = 0; if(ZM_GETC(ZmUartData, & ch) != 0) {// UART Rx buffer not empty => read until empty //////////////////////////////// buffer[pos++] = ch; // write character to buffer while(ZM_GETC(ZmUartData, & ch) != 0)// while UART Rx buffer not empty { buffer[pos++] = ch; // write readed character to buffer if(pos >= 1024) { // out of buffer break; } }// process UART input ////////////////////////////////////////////////////////// done = ZmRcv(buffer, pos, info);// reset start start time ////////////////////////////////////////////////////// GetSysTime(0, & tm0); // read system time tu0 = tm0.tm_sec + tm0.tm_min*60; // convert to seconds } else {// UART Rx buffer empty //////////////////////////////////////////////////////// GetSysTime(0, & tm1); // get time from Timer 0 tu1 = tm1.tm_sec + tm1.tm_min*60; // convert to seconds if(tu1 >= tu0 + zmInfo.timeout) { tu0 = tu1; // reset tu0 done = ZmTimeout(info); } } } return(done);}//////////////////////////////////////////////////////////////////////////////// // tx memory content /////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// int ZmMemTx(void) { int speed; // saved data port speed int done; int txerr=0; int f0; int f1;// zmInfo.ifd = -1; zmInfo.ofd = -1; zmInfo.zrinitflags = 0; zmInfo.zsinitflags = 0; zmInfo.attn = NULL; zmInfo.packetsize = ZmPacketSize; zmInfo.windowsize = ZmWin; zmInfo.bufsize = 0;// select ZMODEM data port ///////////////////////////////////////////////////// while(1) { U8 ch; Print("\nSelect port UART0/UART1 [0/1/Q]"); ch = get_upper(); if(ch == 'Q') { return(1); } else if(ch >= '0' && ch <= '1') { ZmUartData = ch - '0'; break; } } if(ZmUartData == 0) { ZmUartConsole = 1; ZmVerbose = 0; // verbose off ZmDoLogging = 0; // logging off } else { ZmUartConsole = 0; ZmVerbose = 1; // verbose on ZmDoLogging = 1; // logging on }// input memory base address /////////////////////////////////////////////////// { U32 addr; Print("\nInput Memory base address (0x%08x) 0x", ZmMemoryBase); addr = get_number(16,8); if(addr == 0x0) { addr = (U32) ZmMemoryBase; } ZmMemoryBase = (U32 *) addr; }// { U32 defsize; U32 size; if(ZmMemoryPos != 0) { defsize = ZmMemoryPos; } else { defsize = ZmMemorySize; } if(get_last() == KEY_CR) { Print("\n"); } Print("Input Memory size (%d) ", defsize); size = get_number(16,8); if(size == 0x0) { size = defsize; } ZmMemorySize = size; }////////////////////////////////////////////////////////////////////////////////// select data port speed ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// speed = UARTGetSpeed(ZmUartData); while(1) { U8 ch; Print("\nSelect Speed 38400/57600/115200 [3/5/1/Q]"); ch = get_upper();// if(ch == '3') { UARTSetSpeed(ZmUartData, 38400); break; } else if(ch == '5') { UARTSetSpeed(ZmUartData, 57600); break; } else if(ch == '1') { UARTSetSpeed(ZmUartData,115200); break; } else if(ch == 'Q') { return(1); } } // TIMER 1 Initialized for mesuring the test time ////////////////////////////// TimerReset(0); // reset timer 0 TimerInit(0,(ONE_SECOND/TICKS_PER_SECOND)); // init timer 0 TimerStart(0); // start timer 0#ifdef ZM_IO_INT UARTTxIntOn(0); // init UART0 Tx UARTRxIntOn(0); // init UART0 Rx UARTTxIntOn(1); // init UART1 Tx UARTRxIntOn(1); // init UART1 Rx #endif Enable_Int(nGLOBAL_INT);// -new xferType = ZMNEW// -newl xferType = ZMNEWL// -diff xferType = ZMDIFF// -crc xferType = ZMCRC// -apnd xferType = ZMAPND// -clob xferType = ZMCLOB// -prot xferType = ZMPROT// -chng xferType = ZMCHNG/* 2. Call ZmTInit() to begin protocol. */ done = ZmTInit(& zmInfo);/* 3. Read characters from remote end and pass them to */ if(done == 0) { done = ZmDoIO(& zmInfo); } if(done != ZmDone) {// transmit init error ///////////////////////////////////////////////////////// TimerStop(0); // stop timer 0 #ifdef ZM_IO_INT UARTTxIntOff(0); // UART0 Tx Int off UARTRxIntOff(0); // UART0 Rx Int Off UARTTxIntOff(1); // UART1 Tx Int off UARTRxIntOff(1); // UART1 Rx Int off#endif Disable_Int(nGLOBAL_INT); // disbale global int Print("connect failed [%d]\n\r", done);// UARTSetSpeed(ZmUartData, speed); // restore data port speed return(0); } if( ZmAscii ) { f0 = ZCNL; } else if( ZmBinary ) { f0 = ZCBIN; } else if( ZmResume ) { f0 = ZCRESUM;} else f0 = 0; f1 = ZmXferType;/* 4 Call ZmTFile() to begin transfer of a file. */ done = ZmTFile ( "mem" , "mem" , f0 , f1 , 0 , 0 , 0 , 0 , & zmInfo ); switch( done ) { case 0://////////////////////////////////////////////////////////////////////////////// if(ZmVerbose != 0) { ZM_PUTS(ZmUartConsole, "\n\rSending:"); ZM_PUTS(ZmUartConsole, "mem"); ZM_PUTS(ZmUartConsole, "\n\r"); }/* 5 Read characters from remote end and pass them to ZmRcv() until ZmRcv() returns ZmDone or an error. */ if(done == 0) { done = ZmDoIO(& zmInfo); } txerr = done; // save tx error done = (done != ZmDone); break ; case ZmErrCantOpen://////////////////////////////////////////////////////////////////////////////// ZM_PUTS(ZmUartConsole, "\n\rcannot open file"); ZM_PUTS(ZmUartConsole, "mem"); ZM_PUTS(ZmUartConsole, "\n\r"); done = 0; break; case ZmFileTooLong://////////////////////////////////////////////////////////////////////////////// ZM_PUTS(ZmUartConsole, "\n\rfilename ["); ZM_PUTS(ZmUartConsole, "mem"); ZM_PUTS(ZmUartConsole, "] too long, skipping...\n\r"); done = 0; break; case ZmDone: done = 0; break; default: txerr = done; // save tx error done = 1; break; } if(done) {// transmit error //////////////////////////////////////////////////////////////// TimerStop(0); // stop timer 0 #ifdef ZM_IO_INT UARTTxIntOff(0); // UART0 Tx Int off UARTRxIntOff(0); // UART0 Rx Int off UARTTxIntOff(1); // UART1 Tx Int off UARTRxIntOff(1); // UART1 Rx Int off#endif Disable_Int(nGLOBAL_INT); // disable global int Print("\n\rconnect failed [%d]\r\n", txerr);// UARTSetSpeed(ZmUartData, speed); // restore data port speed return(0); }/* 7 Call ZmTFinish() to indicate that all files have been transfered. */ done = ZmTFinish(& zmInfo);/* 8 Read characters from remote end and pass them to ZmRcv() until ZmRcv() returns ZmDone or an error.*/ if(done == 0) { done = ZmDoIO(& zmInfo); }// ZM_FLUSH(ZmUartConsole);// TimerStop(0); // stop timer 0 #ifdef ZM_IO_INT UARTTxIntOff(0); // UART0 Tx Int Off UARTRxIntOff(0); // UART0 Rx Int Off UARTTxIntOff(1); // UART1 Tx Int Off UARTRxIntOff(1); // UART1 Rx Int Off#endif Disable_Int(nGLOBAL_INT); // disable global int// UARTSetSpeed(ZmUartData, speed); // restore data port speed return(1);}void ZmStartProgramm(void){ U8 it; // Print("\n\nStart Programm at address 0x%08x [Y/N]", ZmMemoryBase); it = get_upper(); if(it == 'Y') { StartDownPgm(ZmMemoryBase); } }//////////////////////////////////////////////////////////////////////////////// // receive memory content ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int ZmMemRx(void) { U32 addr; // memory address int speed; // data port saved speed int done; // done flag zmInfo.ifd = -1; zmInfo.ofd = -1; zmInfo.zrinitflags = 0; zmInfo.zsinitflags = 0; zmInfo.attn = NULL; zmInfo.packetsize = ZmPacketSize; zmInfo.windowsize = ZmWin; zmInfo.bufsize = 0;// while(1) { U8 ch;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -