From c63871ac816955469c7178eed3467de52d4c05e7 Mon Sep 17 00:00:00 2001 From: henryk Date: Sun, 18 Nov 2007 00:04:42 +0000 Subject: Add (ring-)buffered print functions to enable debug output from IRQ handlers or similar situations git-svn-id: https://svn.openpcd.org:2342/trunk@330 6dc7ffe9-61d6-0310-9af1-9938baff3ed1 --- openpicc/Makefile | 1 + openpicc/application/cmd.c | 13 ++--- openpicc/application/cmd.h | 1 + openpicc/application/main.c | 3 ++ openpicc/application/usb_print.c | 105 +++++++++++++++++++++++++++++++++++++++ openpicc/application/usb_print.h | 15 ++++++ 6 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 openpicc/application/usb_print.c create mode 100644 openpicc/application/usb_print.h diff --git a/openpicc/Makefile b/openpicc/Makefile index a1b7a55..d3faac0 100644 --- a/openpicc/Makefile +++ b/openpicc/Makefile @@ -82,6 +82,7 @@ ARM_SRC= \ application/tc_cdiv_sync.c \ application/tc_fdt.c \ application/tc_cdiv.c \ + application/usb_print.c \ os/boot/Cstartup_SAM7.c \ os/core/list.c \ os/core/queue.c \ diff --git a/openpicc/application/cmd.c b/openpicc/application/cmd.c index 0b5b8da..7390775 100644 --- a/openpicc/application/cmd.c +++ b/openpicc/application/cmd.c @@ -15,6 +15,7 @@ #include "tc_cdiv.h" #include "tc_cdiv_sync.h" #include "pio_irq.h" +#include "usb_print.h" xQueueHandle xCmdQueue; xTaskHandle xCmdTask; @@ -47,17 +48,13 @@ void DumpUIntToUSB(unsigned int data) } while(data); while(i--) - vUSBSendByte(*p++); + usb_print_char(*p++); } /**********************************************************************/ void DumpStringToUSB(char* text) { - unsigned char data; - - if(text) - while((data=*text++)!=0) - vUSBSendByte(data); + usb_print_string(text); } /**********************************************************************/ @@ -71,8 +68,8 @@ void DumpBufferToUSB(char* buffer, int len) int i; for(i=0; i> 4)); - vUSBSendByte(HexChar( *buffer++ & 0xf)); + usb_print_char(HexChar( *buffer >> 4)); + usb_print_char(HexChar( *buffer++ & 0xf)); } } /**********************************************************************/ diff --git a/openpicc/application/cmd.h b/openpicc/application/cmd.h index 8c048f2..0033b58 100644 --- a/openpicc/application/cmd.h +++ b/openpicc/application/cmd.h @@ -11,6 +11,7 @@ typedef struct { } cmd_type; portBASE_TYPE vCmdInit(void); +extern void DumpUIntToUSB(unsigned int data); extern void DumpStringToUSB(char *string); extern void DumpBufferToUSB(char* buffer, int len); extern xQueueHandle xCmdQueue; diff --git a/openpicc/application/main.c b/openpicc/application/main.c index eb51aeb..fd776ec 100644 --- a/openpicc/application/main.c +++ b/openpicc/application/main.c @@ -46,6 +46,7 @@ #include "tc_cdiv.h" #include "tc_cdiv_sync.h" #include "tc_fdt.h" +#include "usb_print.h" /**********************************************************************/ static inline void prvSetupHardware (void) @@ -79,12 +80,14 @@ void vApplicationIdleHook(void) //vLedSetGreen(0); disabled_green = 1; } + usb_print_flush(); } /**********************************************************************/ int main (void) { prvSetupHardware (); + usb_print_init(); pio_irq_init(); diff --git a/openpicc/application/usb_print.c b/openpicc/application/usb_print.c new file mode 100644 index 0000000..74ffc3c --- /dev/null +++ b/openpicc/application/usb_print.c @@ -0,0 +1,105 @@ +/* Buffered USB debug output + * (C) 2007 Henryk Plötz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include + +#include "usb_print.h" + +#define BUFLEN 1024 + +static char ringbuffer[BUFLEN]; +static int ringstart, ringstop; +static int default_flush = 1; + +void usb_print_buffer(const char* buffer, int start, int stop) { + usb_print_buffer_f(buffer,start,stop,default_flush); +} +int usb_print_buffer_f(const char* buffer, int start, int stop, int flush) +{ + int pos=start, endpos=stop; + while(posavailable) endpos=pos+available; + + while(pos < endpos) { + ringbuffer[ringstop] = buffer[pos]; + pos++; + available--; + ringstop = (ringstop+1) % BUFLEN; + } + + if(flush) usb_print_flush(); + else if(available==0) + return 0; + endpos=stop; + if(pos>=stop) + return (available > 0); + } + return 0; +} + +void usb_print_string(const char *string) { + usb_print_string_f(string, default_flush); +} +int usb_print_string_f(const char* string, int flush) +{ + int start = 0, stop = strlen(string); + return usb_print_buffer_f(string, start, stop, flush); +} + +void usb_print_char(const char c) { + usb_print_char_f(c, default_flush); +} +int usb_print_char_f(const char c, int flush) +{ + return usb_print_buffer_f(&c, 0, 1, flush); +} + +int usb_print_get_default_flush(void) +{ + return default_flush; +} + +int usb_print_set_default_flush(int flush) +{ + int old_flush = default_flush; + default_flush = flush; + return old_flush; +} + + +void usb_print_flush(void) +{ + while(ringstart != ringstop) { + vUSBSendByte(ringbuffer[ringstart]); + ringstart = (ringstart+1) % BUFLEN; + } +} + +void usb_print_init(void) +{ + memset(ringbuffer, 0, BUFLEN); + ringstart = ringstop = 0; +} diff --git a/openpicc/application/usb_print.h b/openpicc/application/usb_print.h new file mode 100644 index 0000000..9186dc9 --- /dev/null +++ b/openpicc/application/usb_print.h @@ -0,0 +1,15 @@ +#ifndef USB_PRINT_H_ +#define USB_PRINT_H_ + +extern void usb_print_buffer(const char* buffer, int start, int stop); +extern int usb_print_buffer_f(const char* buffer, int start, int stop, int flush); +extern void usb_print_string(const char *string); +extern int usb_print_string_f(const char* string, int flush); +extern void usb_print_char(const char c); +extern int usb_print_char_f(const char c, int flush); +extern void usb_print_flush(void); +extern int usb_print_get_default_flush(void); +extern int usb_print_set_default_flush(int flush); +extern void usb_print_init(void); + +#endif /*USB_PRINT_H_*/ -- cgit v1.2.3