From da6cc353dae6703cc05b7ae39f841f858822cd36 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 30 Jul 2011 03:26:59 +0200 Subject: [PATCH] Better serial input/output --- firmware/applications/serial/serial.c | 217 ++++++++++++++++---------- firmware/usbcdc/util.c | 15 +- firmware/usbcdc/util.h | 1 + 3 files changed, 152 insertions(+), 81 deletions(-) diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index b7d547e..75c0790 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -60,6 +60,7 @@ int process(char * input); void dwim(void){ char input[INPUTLEN+1]; int inputptr=0; + char dirty=0; usbCDCInit(); delayms(500); @@ -67,16 +68,23 @@ void dwim(void){ puts("D start\r\n"); while(!getInputRaw()){ - delayms(10); // Input int l=INPUTLEN-inputptr; CDC_OutBufAvailChar (&l); if(l>0){ + dirty=10; + if(l>1) + dirty=50; CDC_RdOutBuf (input+inputptr, &l); + if(input[inputptr]==8){ + inputptr=0; + puts("\r\n"); + continue; + }; input[inputptr+l]=0; - puts(&input[inputptr]); + puts_plus(&input[inputptr]); for(int i=0;i0) + dirty--; + if(dirty==1) + puts(""); }; inputptr+=l; }; @@ -94,13 +108,35 @@ void dwim(void){ 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'9')?input[p]-'a'+10:input[p]-'0')*16+ - ((input[p+1]>'9')?input[p+1]-'a'+10:input[p+1]-'0'); -// puts(IntToStrX( c,2 )); -// puts("."); - buf[bp]=c; - bp++;p+=2; + uint8_t *hex=hextobyte(&input[2],&len); + if(len<10) len=10; + + len+=2; + puts_plus("S Len:"); + puts_plus(IntToStrX( len,2 )); + puts_plus("\r\n"); + + status=nrf_snd_pkt_crc_encr(len,hex,thekey); + + puts_plus("P "); + puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); + for(int i=0;i0){ + puts_plus("\r\nD Abort.\r\n"); + break; + }; }; - if(len==-3) - puts(" [!crc]"); puts("\r\n"); - delayms(1); if(pctr--==0) break; }while(t>getTimer()); nrf_rcv_pkt_end(); }else{ - puts("D no action\r\n"); + puts_plus("D no action\r\n"); }; puts("D done.\r\n"); return 0; diff --git a/firmware/usbcdc/util.c b/firmware/usbcdc/util.c index 66024f6..fd3b041 100644 --- a/firmware/usbcdc/util.c +++ b/firmware/usbcdc/util.c @@ -28,17 +28,30 @@ int puts(const char * str){ if (currentTick != lastTick){ uint8_t frame[64]; uint32_t bytesRead = 0; + char repeat=0; while (cdcBufferDataPending()){ // Read up to 64 bytes as long as possible bytesRead = cdcBufferReadLen(frame, 64); USB_WriteEP (CDC_DEP_IN, frame, bytesRead); - systickDelay(1); + if(repeat) + systickDelay(1); + else + repeat=1; } lastTick = currentTick; } return 0; } +int puts_plus(const char * str){ + if(!USB_Configuration) + return -1; + + while(*str) + cdcBufferWrite(*str++); + return 0; +} + void usbCDCInit(){ lastTick = systickGetTicks(); // Used to control output/printf timing CDC_Init(); // Initialise VCOM diff --git a/firmware/usbcdc/util.h b/firmware/usbcdc/util.h index d743b2f..021ab7b 100644 --- a/firmware/usbcdc/util.h +++ b/firmware/usbcdc/util.h @@ -1,3 +1,4 @@ int puts(const char * str); +int puts_plus(const char * str); void usbCDCInit(void); void usbCDCOff(void);