1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
38 //------------------------------------------------------------------------------
\r
39 // Exported functions
\r
40 //------------------------------------------------------------------------------
\r
41 //------------------------------------------------------------------------------
\r
42 /// Initializes the DBGU with the given parameters, and enables both the
\r
43 /// transmitter and the receiver.
\r
44 /// \param mode Operating mode to configure (see <Modes>).
\r
45 /// \param baudrate Desired baudrate.
\r
46 /// \param mck Frequency of the system master clock.
\r
47 //------------------------------------------------------------------------------
\r
48 void DBGU_Configure(unsigned int mode,
\r
49 unsigned int baudrate,
\r
52 // Reset & disable receiver and transmitter, disable interrupts
\r
53 AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RSTRX | AT91C_US_RSTTX;
\r
54 AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
\r
56 // Configure baud rate
\r
57 AT91C_BASE_DBGU->DBGU_BRGR = mck / (baudrate * 16);
\r
59 // Configure mode register
\r
60 AT91C_BASE_DBGU->DBGU_MR = mode;
\r
62 // Disable DMA channel
\r
63 AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;
\r
65 // Enable receiver and transmitter
\r
66 AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;
\r
69 //------------------------------------------------------------------------------
\r
70 /// Outputs a character on the DBGU line.
\r
71 /// \param c Character to send.
\r
72 //------------------------------------------------------------------------------
\r
73 static void DBGU_PutChar(unsigned char c)
\r
75 // Wait for the transmitter to be ready
\r
76 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
\r
79 AT91C_BASE_DBGU->DBGU_THR = c;
\r
81 // Wait for the transfer to complete
\r
82 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
\r
85 //------------------------------------------------------------------------------
\r
86 /// Reads and returns a character from the DBGU.
\r
87 //------------------------------------------------------------------------------
\r
88 unsigned char DBGU_GetChar()
\r
90 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0);
\r
91 return AT91C_BASE_DBGU->DBGU_RHR;
\r
98 //------------------------------------------------------------------------------
\r
99 /// Implementation of fputc using the DBGU as the standard output. Required
\r
101 /// Returns the character written if successful, or -1 if the output stream is
\r
102 /// not stdout or stderr.
\r
103 /// \param c Character to write.
\r
104 /// \param pStream Output stream.
\r
105 //------------------------------------------------------------------------------
\r
106 signed int fputc(signed int c, FILE *pStream)
\r
108 if ((pStream == stdout) || (pStream == stderr)) {
\r
119 //------------------------------------------------------------------------------
\r
120 /// Implementation of fputs using the DBGU as the standard output. Required
\r
121 /// for printf(). Does NOT currently use the PDC.
\r
122 /// Returns the number of characters written if successful, or -1 if the output
\r
123 /// stream is not stdout or stderr.
\r
124 /// \param pStr String to write.
\r
125 /// \param pStream Output stream.
\r
126 //------------------------------------------------------------------------------
\r
127 signed int fputs(const char *pStr, FILE *pStream)
\r
129 signed int num = 0;
\r
131 while (*pStr != 0) {
\r
133 if (fputc(*pStr, pStream) == -1) {
\r
146 //------------------------------------------------------------------------------
\r
147 /// Outputs a character on the DBGU. Returns the character itself.
\r
148 /// \param c Character to output.
\r
149 //------------------------------------------------------------------------------
\r
150 signed int putchar(signed int c)
\r
152 return fputc(c, stdout);
\r
155 #endif //#ifndef NOFPUT
\r