]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-at91sam7/libs/at91_dbgu/at91_dbgu.c
Build framework for the AT91SAM7 architecture
[sysless.git] / arch / arm / mach-at91sam7 / libs / at91_dbgu / at91_dbgu.c
1 /*
2  * at91_dbgu.c
3  *
4  * A driver for the DBGU serial unit of the Atmel AT91 series.
5  *
6  * Copyright (c) 2009 Tran Duy Khanh. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18
19 #include <stdint.h>
20 #include <system_def.h>
21 #include <lib_AT91SAM7XC256.h>
22 #include <hwinit.h>
23 #include <system_stub.h>
24
25 #include "at91_dbgu.h"
26
27 #define configCPU_CLOCK_HZ              ((unsigned long) 48000000)
28 #define DBGU_BAUD_RATE                  115200
29
30 /* Initialization of the serial console. */
31 void at91_dbgu_init(void)
32 {
33         AT91F_DBGU_CfgPIO();
34         ((AT91PS_USART)AT91C_BASE_DBGU)->US_CR = AT91C_US_RSTTX|AT91C_US_RSTRX;
35         AT91F_US_Configure(
36                 (AT91PS_USART)AT91C_BASE_DBGU,
37                 configCPU_CLOCK_HZ,
38                 AT91C_US_ASYNC_MODE,
39                 DBGU_BAUD_RATE,
40                 0);
41         /* Enable Transmitter & receiver */
42         ((AT91PS_USART)AT91C_BASE_DBGU)->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
43 }
44
45 /* Put a char to the serial console. */
46 int at91_dbgu_putchar(int ch)
47 {
48         /* wait for TX buffer to empty */
49         while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY))
50                 continue;
51
52         /* put char to Transmit Holding Register */
53         AT91C_BASE_DBGU->DBGU_THR = (uint8_t)ch;
54
55         /* return a char - stdio.h compatible? */
56         return (uint8_t)ch;
57 }
58
59 /* Write to the serial console. */
60 int at91_dbgu_write(int file, const char *ptr, int len)
61 {
62         int cnt;
63         unsigned char ch;
64
65         for (cnt=0; cnt<len; cnt++,ptr++) {
66                 ch = *ptr;
67                 if(ch == 0xa)
68                         at91_dbgu_putchar(0xd);
69                 at91_dbgu_putchar(ch);
70         }
71
72         return cnt;
73 }
74
75 void at91_dbgu_init_printf(void)
76 {
77         /* Debug Unit initialization */
78         at91_dbgu_init();
79
80         system_stub_ops.write = at91_dbgu_write;
81 }