Better serial input/output

This commit is contained in:
Stefan `Sec` Zehl 2011-07-30 03:26:59 +02:00
parent d621d9b117
commit da6cc353da
3 changed files with 152 additions and 81 deletions

View File

@ -60,6 +60,7 @@ int process(char * input);
void dwim(void){ void dwim(void){
char input[INPUTLEN+1]; char input[INPUTLEN+1];
int inputptr=0; int inputptr=0;
char dirty=0;
usbCDCInit(); usbCDCInit();
delayms(500); delayms(500);
@ -67,16 +68,23 @@ void dwim(void){
puts("D start\r\n"); puts("D start\r\n");
while(!getInputRaw()){ while(!getInputRaw()){
delayms(10);
// Input // Input
int l=INPUTLEN-inputptr; int l=INPUTLEN-inputptr;
CDC_OutBufAvailChar (&l); CDC_OutBufAvailChar (&l);
if(l>0){ if(l>0){
dirty=10;
if(l>1)
dirty=50;
CDC_RdOutBuf (input+inputptr, &l); CDC_RdOutBuf (input+inputptr, &l);
if(input[inputptr]==8){
inputptr=0;
puts("<oops>\r\n");
continue;
};
input[inputptr+l]=0; input[inputptr+l]=0;
puts(&input[inputptr]); puts_plus(&input[inputptr]);
for(int i=0;i<l;i++){ for(int i=0;i<l;i++){
if(input[inputptr+i] =='\r'){ if(input[inputptr+i] =='\r'){
input[inputptr+i]=0; input[inputptr+i]=0;
@ -87,6 +95,12 @@ void dwim(void){
break; break;
}; };
}; };
}else{
delayms(1);
if(dirty>0)
dirty--;
if(dirty==1)
puts("");
}; };
inputptr+=l; inputptr+=l;
}; };
@ -94,13 +108,35 @@ void dwim(void){
puts("D exit\r\n"); puts("D exit\r\n");
} }
#define BUFLEN 32
#define NYB(x) ((x>'9')?(x|0x20)-'a'+10:x-'0')
uint8_t * hextobyte(char * input, int *len){
static uint8_t buf[BUFLEN];
int p=0;
int bp=0;
char c;
while(bp<BUFLEN){
if(input[p]==0 || input[p+1]==0)
break;
if(input[p]==' '){
p++;
continue;
};
buf[bp]= c=NYB(input[p])*16+NYB(input[p+1]);
bp++;p+=2;
};
*len=bp;
return buf;
};
int process(char * input){ int process(char * input){
if(input == NULL || input[0]==0) if(input == NULL || input[0]==0)
return; return -1;
puts("\r\n"); puts_plus("\r\n");
puts("D ["); puts_plus("D [");
puts(input); puts_plus(input);
puts("]\r\n"); puts("]\r\n");
if(input[0]=='i'){ if(input[0]=='i'){
@ -133,85 +169,88 @@ int process(char * input){
memcpy(thekey,beaconkey,sizeof(thekey)); memcpy(thekey,beaconkey,sizeof(thekey));
}else if(input[1]=='?'){ }else if(input[1]=='?'){
nrf_config_get(&config); nrf_config_get(&config);
puts("Ch: ");puts(IntToStrX( config.channel,2 )); puts("\r\n"); puts_plus("Ch: ");puts_plus(IntToStrX( config.channel,2 )); puts_plus("\r\n");
puts("TxMac: "); puts_plus("TxMac: ");
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
puts(IntToStrX( config.txmac[i],2 )); puts_plus(IntToStrX( config.txmac[i],2 ));
puts("\r\n"); puts_plus("\r\n");
puts("Len : "); puts_plus("Len : ");
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
puts(IntToStrX( config.maclen[i],2 )); puts_plus(IntToStrX( config.maclen[i],2 ));
puts("\r\n"); puts_plus("\r\n");
puts("0mac : "); puts_plus("0mac : ");
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
puts(IntToStrX( config.mac0[i],2 )); puts_plus(IntToStrX( config.mac0[i],2 ));
puts("\r\n"); puts_plus("\r\n");
puts("1mac : "); puts_plus("1mac : ");
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
puts(IntToStrX( config.mac1[i],2 )); puts_plus(IntToStrX( config.mac1[i],2 ));
puts("\r\n"); puts_plus("\r\n");
puts("2345 : "); puts_plus("2345 : ");
for(int i=0;i<4;i++) for(int i=0;i<4;i++)
puts(IntToStrX( config.mac2345[i],2 )); puts_plus(IntToStrX( config.mac2345[i],2 ));
puts("\r\n"); puts_plus("\r\n");
puts("key : "); puts_plus("key : ");
for(int i=0;i<4;i++){ for(int i=0;i<4;i++){
puts(IntToStrX( thekey[i],8 )); puts_plus(IntToStrX( thekey[i],8 ));
puts(" "); puts_plus(" ");
}; };
puts("\r\n"); puts_plus("\r\n");
}else if(input[1]=='k'){ };
thekey[0]=0; }else if(input[0]=='C'){
thekey[1]=0; int len;
thekey[2]=0; uint8_t *hex=hextobyte(&input[2],&len);
thekey[3]=0; if(input[1]=='k'){
thekey[0]=uint8ptouint32(hex);
thekey[1]=uint8ptouint32(hex+4);
thekey[2]=uint8ptouint32(hex+8);
thekey[3]=uint8ptouint32(hex+12);
}else if(input[1]=='m'){
config.mac0[0]=uint8ptouint32(hex);
config.mac0[1]=uint8ptouint32(hex+4);
config.mac0[2]=uint8ptouint32(hex+8);
config.mac0[3]=uint8ptouint32(hex+12);
config.mac0[4]=uint8ptouint32(hex+16);
nrf_config_set(&config);
}else if(input[1]=='t'){
config.txmac[0]=uint8ptouint32(hex);
config.txmac[1]=uint8ptouint32(hex+4);
config.txmac[2]=uint8ptouint32(hex+8);
config.txmac[3]=uint8ptouint32(hex+12);
config.txmac[4]=uint8ptouint32(hex+16);
nrf_config_set(&config);
}else if(input[1]=='c'){
config.channel=*hex;
nrf_config_set(&config);
}; };
}else if (input[0]=='s'){ }else if (input[0]=='s'){
__attribute__ ((aligned (4))) uint8_t buf[32]; __attribute__ ((aligned (4))) uint8_t buf[32];
int status=0; int status=0;
int len;
if (input[1]==' '){ if (input[1]==' '){
int p=2; uint8_t *hex=hextobyte(&input[2],&len);
int bp=0; if(len<10) len=10;
char c;
// puts("\r\n"); len+=2;
while(bp<32){ puts_plus("S Len:");
if(input[p]==0 || input[p+1]==0) puts_plus(IntToStrX( len,2 ));
break; puts_plus("\r\n");
if(input[p]==' '){
p++; status=nrf_snd_pkt_crc_encr(len,hex,thekey);
continue;
}; puts_plus("P ");
c=((input[p]>'9')?input[p]-'a'+10:input[p]-'0')*16+ puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] ");
((input[p+1]>'9')?input[p+1]-'a'+10:input[p+1]-'0'); for(int i=0;i<len;i++){
// puts(IntToStrX( c,2 )); puts_plus(IntToStrX( hex[i],2 ));
// puts("."); puts_plus(" ");
buf[bp]=c;
bp++;p+=2;
}; };
// puts("\r\n"); puts_plus("\r\n");
if(bp<10) bp=10;
bp+=2;
puts("S Len:");
puts(IntToStrX( bp,2 ));
puts("\r\n");
status=nrf_snd_pkt_crc_encr(bp,buf,thekey);
puts("P ");
puts("[");puts(IntToStrX(bp,2));puts("] ");
for(int i=0;i<bp;i++){
puts(IntToStrX( buf[i],2 ));
puts(" ");
};
puts("\r\n");
}else if (input[1]=='t'){ }else if (input[1]=='t'){
static int ctr=1; static int ctr=1;
int status; int status;
@ -230,19 +269,28 @@ int process(char * input){
status=nrf_snd_pkt_crc_encr(16,buf,thekey); status=nrf_snd_pkt_crc_encr(16,buf,thekey);
}else{ }else{
}; };
puts("S state="); puts_plus("S state=");
puts(IntToStrX( status,2 )); puts_plus(IntToStrX( status,2 ));
puts("\r\n"); puts_plus("\r\n");
}else if (input[0]=='r'){ }else if (input[0]=='r'){
__attribute__ ((aligned (4))) uint8_t buf[32]; __attribute__ ((aligned (4))) uint8_t buf[32];
int len; int len;
int pctr=5; int pctr=5;
int t=getTimer()+5000/SYSTICKSPEED; int t=getTimer()+5000/SYSTICKSPEED;
if(input[1]=='0') if(input[1]=='+'){
if(input[2]!=0){
uint8_t *hex=hextobyte(&input[2],&len);
t=getTimer()+hex[0]*1000/SYSTICKSPEED;
};
pctr=-1; pctr=-1;
}
if(input[1]=='-'){
uint8_t *hex=hextobyte(&input[2],&len);
pctr=hex[0];
}
puts("D receive ...\r\n"); puts_plus("D receive ...\r\n");
nrf_rcv_pkt_start(); nrf_rcv_pkt_start();
do{ do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,thekey); len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,thekey);
@ -251,23 +299,32 @@ int process(char * input){
delayms(10); delayms(10);
continue; continue;
}; };
puts("R "); puts_plus("R ");
puts("[");puts(IntToStrX(len,2));puts("] "); puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] ");
for(int i=0;i<len;i++){ if(len==-3){
puts(IntToStrX( buf[i],2 )); puts_plus("[!crc] ");
puts(" "); len=16;
};
for(int i=0;i<len;i++){
puts_plus(IntToStrX( buf[i],2 ));
puts_plus(" ");
};
if(pctr<0){
int l=0;
CDC_OutBufAvailChar (&l);
if(l>0){
puts_plus("\r\nD Abort.\r\n");
break;
};
}; };
if(len==-3)
puts(" [!crc]");
puts("\r\n"); puts("\r\n");
delayms(1);
if(pctr--==0) if(pctr--==0)
break; break;
}while(t>getTimer()); }while(t>getTimer());
nrf_rcv_pkt_end(); nrf_rcv_pkt_end();
}else{ }else{
puts("D no action\r\n"); puts_plus("D no action\r\n");
}; };
puts("D done.\r\n"); puts("D done.\r\n");
return 0; return 0;

View File

@ -28,17 +28,30 @@ int puts(const char * str){
if (currentTick != lastTick){ if (currentTick != lastTick){
uint8_t frame[64]; uint8_t frame[64];
uint32_t bytesRead = 0; uint32_t bytesRead = 0;
char repeat=0;
while (cdcBufferDataPending()){ while (cdcBufferDataPending()){
// Read up to 64 bytes as long as possible // Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64); bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead); USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
systickDelay(1); if(repeat)
systickDelay(1);
else
repeat=1;
} }
lastTick = currentTick; lastTick = currentTick;
} }
return 0; return 0;
} }
int puts_plus(const char * str){
if(!USB_Configuration)
return -1;
while(*str)
cdcBufferWrite(*str++);
return 0;
}
void usbCDCInit(){ void usbCDCInit(){
lastTick = systickGetTicks(); // Used to control output/printf timing lastTick = systickGetTicks(); // Used to control output/printf timing
CDC_Init(); // Initialise VCOM CDC_Init(); // Initialise VCOM

View File

@ -1,3 +1,4 @@
int puts(const char * str); int puts(const char * str);
int puts_plus(const char * str);
void usbCDCInit(void); void usbCDCInit(void);
void usbCDCOff(void); void usbCDCOff(void);