📄 xmodem.c
字号:
#include "at91rm9200.h"#include "xmodem.h"static int ticks,timeout,state,seconds,times;int getc_non_block(unsigned char* c){ dbgu_reg_p dbgu = (dbgu_reg_s *)DBGU_BASE; ticks = 0; timeout = 0; while(1){ if( dbgu->dbgu_sr & (USART_RXRDY) ){ *c = (unsigned char)(dbgu->dbgu_rhr & 0xff); if( state==XM_WAIT ){ seconds=0; times=0; } ticks=0; return 1; } if( timeout ){ timeout = 0; if( state==XM_WAIT ){ seconds++; if( seconds==3 ){ seconds = 0; times++; if( times==2 ){ times = 0; return -1; } } } else return 0; } }} void delay_s(int second){ while(second--){ ticks=0; timeout=0; while(timeout==0); }}int x_receive(unsigned* mem_start){ x_packet packet; unsigned *p = (unsigned *)&packet + 1; unsigned char nextChar,blk_num=1; int got_pkt=0,i,j,k=30; ticks=timeout=seconds=times=0; state=XM_START; while( 1 ){ if( got_pkt ){ blk_num++; for( i=0;i<32;i++ ) *mem_start++ = p[i]; got_pkt = 0; put_char( XM_ACK ); } else put_char( XM_NAK );recv_pkt://soh i = getc_non_block(&nextChar); if( i==-1 ){ put_char( XM_CAN ); return 0; } else if( i==0 ){ if(k-->0) continue; else{ put_char(XM_CAN); return 0; } } if( nextChar==XM_EOT ){ put_char( XM_ACK ); return 1; } else if( nextChar==XM_SOH ){ packet.soh = nextChar; state = XM_RECV; } else { state = XM_WAIT; delay_s(1); continue; }//blk i = getc_non_block(&nextChar); if( i==0 ){ state = XM_WAIT; continue; } if( nextChar==blk_num-1 ){ state = XM_WAIT; delay_s(1); got_pkt=0; put_char(XM_ACK); goto recv_pkt; continue; } else if( nextChar==blk_num ) packet.blk = blk_num; else{ put_char( XM_CAN ); return 0; }//blk_comp i = getc_non_block(&nextChar); if( i==0 ){ state = XM_WAIT; continue; } if( nextChar!=0xff-packet.blk ){ state = XM_WAIT; delay_s(1); continue; } else packet.blk_comp = nextChar;//data[128] for(j=0;j<128;j++){ i = getc_non_block(&nextChar); if( i==0 ){ state = XM_WAIT; break; } packet.data[j] = nextChar; } if( j<128 ) continue;//checksum i = getc_non_block(&nextChar); if( i==0 ){ state=XM_WAIT; continue; } for(i=j=0;j<128;j++) i+=packet.data[j]; i = i&0xff; if(i==nextChar) got_pkt = 1; state = XM_WAIT; }}void int_tc(){ tc_reg_p tc0 = (tc_reg_s *)TC0_BASE; int value = tc0->tcc[0].tc_sr; ticks++; if( ticks==6 ){ ticks=0; timeout=1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -