]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_AT91SAM3U256_IAR/AT91Lib/peripherals/dbgu/dbgu.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_AT91SAM3U256_IAR / 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 //         Global functions\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
52     unsigned int mode,\r
53     unsigned int baudrate,\r
54     unsigned int mck)\r
55 {   \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
59     \r
60     // Configure baud rate\r
61     AT91C_BASE_DBGU->DBGU_BRGR = mck / (baudrate * 16);\r
62     \r
63     // Configure mode register\r
64     AT91C_BASE_DBGU->DBGU_MR = mode;\r
65     \r
66     // Disable DMA channel\r
67     AT91C_BASE_DBGU->DBGU_PTCR = AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS;\r
68 \r
69     // Enable receiver and transmitter\r
70     AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;\r
71 }\r
72 \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
79 {\r
80     // Wait for the transmitter to be ready\r
81     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);\r
82     \r
83     // Send character\r
84     AT91C_BASE_DBGU->DBGU_THR = c;\r
85     \r
86     // Wait for the transfer to complete\r
87     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXEMPTY) == 0);\r
88 }\r
89 \r
90 //------------------------------------------------------------------------------\r
91 /// Return 1 if a character can be read in DBGU\r
92 //------------------------------------------------------------------------------\r
93 unsigned int DBGU_IsRxReady()\r
94 {\r
95     return (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY);\r
96 }\r
97 \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
104 {\r
105     while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0);\r
106     return AT91C_BASE_DBGU->DBGU_RHR;\r
107 }\r
108 \r
109 #ifndef NOFPUT\r
110 #include <stdio.h>\r
111 \r
112 //------------------------------------------------------------------------------\r
113 /// \exclude\r
114 /// Implementation of fputc using the DBGU as the standard output. Required\r
115 /// for printf().\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
122 {\r
123     if ((pStream == stdout) || (pStream == stderr)) {\r
124     \r
125         DBGU_PutChar(c);\r
126         return c;\r
127     }\r
128     else {\r
129 \r
130         return EOF;\r
131     }\r
132 }\r
133 \r
134 //------------------------------------------------------------------------------\r
135 /// \exclude\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
144 {\r
145     signed int num = 0;\r
146 \r
147     while (*pStr != 0) {\r
148 \r
149         if (fputc(*pStr, pStream) == -1) {\r
150 \r
151             return -1;\r
152         }\r
153         num++;\r
154         pStr++;\r
155     }\r
156 \r
157     return num;\r
158 }\r
159 \r
160 #undef putchar\r
161 \r
162 //------------------------------------------------------------------------------\r
163 /// \exclude\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
169 {\r
170     return fputc(c, stdout);\r
171 }\r
172 \r
173 #endif //#ifndef NOFPUT\r
174 \r