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
40 //------------------------------------------------------------------------------
\r
41 //------------------------------------------------------------------------------
\r
42 /// Initializes the DBGU with the given parameters, and enables both the
\r
43 /// transmitter and the receiver. The mode parameter contains the value of the
\r
44 /// DBGU_MR register.
\r
45 /// Value DBGU_STANDARD can be used for mode to get the most common configuration
\r
46 /// (i.e. aysnchronous, 8bits, no parity, 1 stop bit, no flow control).
\r
47 /// \param mode Operating mode to configure.
\r
48 /// \param baudrate Desired baudrate (e.g. 115200).
\r
49 /// \param mck Frequency of the system master clock in Hz.
\r
50 //------------------------------------------------------------------------------
\r
51 void DBGU_Configure(
\r
53 unsigned int baudrate,
\r
56 // Reset & disable receiver and transmitter, disable interrupts
\r
57 AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RSTRX | AT91C_US_RSTTX;
\r
58 AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
\r
60 // Configure baud rate
\r
61 AT91C_BASE_DBGU->DBGU_BRGR = mck / (baudrate * 16);
\r
63 // Configure mode register
\r
64 AT91C_BASE_DBGU->DBGU_MR = mode;
\r
66 // Disable DMA channel
\r
67 AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;
\r
69 // Enable receiver and transmitter
\r
70 AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;
\r
73 //------------------------------------------------------------------------------
\r
74 /// Outputs a character on the DBGU line.
\r
75 /// \note This function is synchronous (i.e. uses polling).
\r
76 /// \param c Character to send.
\r
77 //------------------------------------------------------------------------------
\r
78 void DBGU_PutChar(unsigned char c)
\r
80 // Wait for the transmitter to be ready
\r
81 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
\r
84 AT91C_BASE_DBGU->DBGU_THR = c;
\r
86 // Wait for the transfer to complete
\r
87 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);
\r
90 //------------------------------------------------------------------------------
\r
91 /// Return 1 if a character can be read in DBGU
\r
92 //------------------------------------------------------------------------------
\r
93 unsigned int DBGU_IsRxReady()
\r
95 return (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY);
\r
98 //------------------------------------------------------------------------------
\r
99 /// Reads and returns a character from the DBGU.
\r
100 /// \note This function is synchronous (i.e. uses polling).
\r
101 /// \return Character received.
\r
102 //------------------------------------------------------------------------------
\r
103 unsigned char DBGU_GetChar(void)
\r
105 while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0);
\r
106 return AT91C_BASE_DBGU->DBGU_RHR;
\r
112 //------------------------------------------------------------------------------
\r
114 /// Implementation of fputc using the DBGU as the standard output. Required
\r
116 /// \param c Character to write.
\r
117 /// \param pStream Output stream.
\r
118 /// \param The character written if successful, or -1 if the output stream is
\r
119 /// not stdout or stderr.
\r
120 //------------------------------------------------------------------------------
\r
121 signed int fputc(signed int c, FILE *pStream)
\r
123 if ((pStream == stdout) || (pStream == stderr)) {
\r
134 //------------------------------------------------------------------------------
\r
136 /// Implementation of fputs using the DBGU as the standard output. Required
\r
137 /// for printf(). Does NOT currently use the PDC.
\r
138 /// \param pStr String to write.
\r
139 /// \param pStream Output stream.
\r
140 /// \return Number of characters written if successful, or -1 if the output
\r
141 /// stream is not stdout or stderr.
\r
142 //------------------------------------------------------------------------------
\r
143 signed int fputs(const char *pStr, FILE *pStream)
\r
145 signed int num = 0;
\r
147 while (*pStr != 0) {
\r
149 if (fputc(*pStr, pStream) == -1) {
\r
162 //------------------------------------------------------------------------------
\r
164 /// Outputs a character on the DBGU.
\r
165 /// \param c Character to output.
\r
166 /// \return The character that was output.
\r
167 //------------------------------------------------------------------------------
\r
168 signed int putchar(signed int c)
\r
170 return fputc(c, stdout);
\r
173 #endif //#ifndef NOFPUT
\r