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