Initial commit
[kk_librfid.git] / firmware / src / dfu / dbgu.c
blobf77600d8fd6c5f63c261514b26970ec86f7c7099
1 /* AT91SAM7 debug function implementation for OpenPCD
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <lib_AT91SAM7.h>
21 #include <board.h>
22 #include <dfu/dbgu.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #define USART_SYS_LEVEL 4
28 void AT91F_DBGU_Ready(void)
30 while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY)) ;
33 static void DBGU_irq_handler(void)
35 static char value;
37 AT91F_DBGU_Get(&value);
38 switch (value) {
39 case '9':
40 AT91F_DBGU_Printk("Resetting SAM7\n\r");
41 AT91F_RSTSoftReset(AT91C_BASE_RSTC, AT91C_RSTC_PROCRST|
42 AT91C_RSTC_PERRST|AT91C_RSTC_EXTRST);
43 break;
44 default:
45 AT91F_DBGU_Printk("\n\r");
49 void AT91F_DBGU_Init(void)
51 /* Open PIO for DBGU */
52 AT91F_DBGU_CfgPIO();
53 /* Enable Transmitter & receivier */
54 ((AT91PS_USART) AT91C_BASE_DBGU)->US_CR =
55 AT91C_US_RSTTX | AT91C_US_RSTRX;
57 /* Configure DBGU */
58 AT91F_US_Configure(AT91C_BASE_DBGU,
59 MCK, AT91C_US_ASYNC_MODE,
60 AT91C_DBGU_BAUD, 0);
62 /* Enable Transmitter & receivier */
63 ((AT91PS_USART) AT91C_BASE_DBGU)->US_CR =
64 AT91C_US_RXEN | AT91C_US_TXEN;
66 /* Enable USART IT error and AT91C_US_ENDRX */
67 AT91F_US_EnableIt((AT91PS_USART) AT91C_BASE_DBGU, AT91C_US_RXRDY);
69 /* open interrupt */
71 AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_SYS, USART_SYS_LEVEL,
72 AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
73 DBGU_irq_handler);
74 AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);
78 void AT91F_DBGU_Printk(char *buffer)
80 while (*buffer != '\0') {
81 while (!AT91F_US_TxReady((AT91PS_USART) AT91C_BASE_DBGU)) ;
82 AT91F_US_PutChar((AT91PS_USART) AT91C_BASE_DBGU, *buffer++);
86 int AT91F_DBGU_Get(char *val)
88 if ((AT91F_US_RxReady((AT91PS_USART) AT91C_BASE_DBGU)) == 0)
89 return (0);
90 else {
91 *val = AT91F_US_GetChar((AT91PS_USART) AT91C_BASE_DBGU);
92 return (-1);
96 #ifdef DEBUG
98 void AT91F_DBGU_Frame(char *buffer)
100 unsigned char len;
102 for (len = 0; buffer[len] != '\0'; len++) { }
104 AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU,
105 (unsigned char *)buffer, len, 0, 0);
109 const char *
110 hexdump(const void *data, unsigned int len)
112 static char string[256];
113 unsigned char *d = (unsigned char *) data;
114 unsigned int i, left;
116 string[0] = '\0';
117 left = sizeof(string);
118 for (i = 0; len--; i += 3) {
119 if (i >= sizeof(string) -4)
120 break;
121 snprintf(string+i, 4, " %02x", *d++);
123 return string;
126 static char dbg_buf[2048];
127 void debugp(const char *format, ...)
129 va_list ap;
131 va_start(ap, format);
132 vsnprintf(dbg_buf, sizeof(dbg_buf)-1, format, ap);
133 va_end(ap);
135 dbg_buf[sizeof(dbg_buf)-1] = '\0';
136 //AT91F_DBGU_Frame(dbg_buf);
137 AT91F_DBGU_Printk(dbg_buf);
140 #endif