]> git.sur5r.net Git - freertos/blob - Demo/Common/drivers/Atmel/at91lib/peripherals/dbgu/dbgu.c
Atmel provided hardware specifics.
[freertos] / Demo / Common / drivers / Atmel / at91lib / peripherals / dbgu / dbgu.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 //------------------------------------------------------------------------------\r
31 //      Headers\r
32 //------------------------------------------------------------------------------\r
33 \r
34 #include "dbgu.h"\r
35 #include <stdarg.h>\r
36 #include <board.h>\r
37             \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
50                            unsigned int mck)\r
51 {   \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
55     \r
56     // Configure baud rate\r
57     AT91C_BASE_DBGU->DBGU_BRGR = mck / (baudrate * 16);\r
58     \r
59     // Configure mode register\r
60     AT91C_BASE_DBGU->DBGU_MR = mode;\r
61     \r
62     // Disable DMA channel\r
63     AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;\r
64 \r
65     // Enable receiver and transmitter\r
66     AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;\r
67 }\r
68 \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
74 {\r
75     // Wait for the transmitter to be ready\r
76     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);\r
77     \r
78     // Send character\r
79     AT91C_BASE_DBGU->DBGU_THR = c;\r
80     \r
81     // Wait for the transfer to complete\r
82     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);\r
83 }\r
84 \r
85 //------------------------------------------------------------------------------\r
86 /// Reads and returns a character from the DBGU.\r
87 //------------------------------------------------------------------------------\r
88 unsigned char DBGU_GetChar()\r
89 {\r
90     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0);\r
91     return AT91C_BASE_DBGU->DBGU_RHR;\r
92 }\r
93 \r
94 #ifndef NOFPUT\r
95 \r
96 #include <stdio.h>\r
97 \r
98 //------------------------------------------------------------------------------\r
99 /// Implementation of fputc using the DBGU as the standard output. Required\r
100 /// for printf().\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
107 {\r
108     if ((pStream == stdout) || (pStream == stderr)) {\r
109     \r
110         DBGU_PutChar(c);\r
111         return c;\r
112     }\r
113     else {\r
114 \r
115         return EOF;\r
116     }\r
117 }\r
118 \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
128 {\r
129     signed int num = 0;\r
130 \r
131     while (*pStr != 0) {\r
132 \r
133         if (fputc(*pStr, pStream) == -1) {\r
134 \r
135             return -1;\r
136         }\r
137         num++;\r
138         pStr++;\r
139     }\r
140 \r
141     return num;\r
142 }\r
143 \r
144 #undef putchar\r
145 \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
151 {\r
152     return fputc(c, stdout);\r
153 }\r
154 \r
155 #endif //#ifndef NOFPUT\r
156 \r