Added new incomplete AudioInputHost Host LowLevel demo.
[pub/lufa.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGTarget.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Target-related functions for the PDI Protocol decoder.
34 */
35
36 #define INCLUDE_FROM_XPROGTARGET_C
37 #include "XPROGTarget.h"
38
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
40
41 /** Flag to indicate if the USART is currently in Tx or Rx mode. */
42 bool IsSending;
43
44 /** Enables the target's PDI interface, holding the target in reset until PDI mode is exited. */
45 void XPROGTarget_EnableTargetPDI(void)
46 {
47 IsSending = false;
48
49 #if (ARCH == ARCH_AVR8)
50 /* Set Tx and XCK as outputs, Rx as input */
51 DDRD |= (1 << 5) | (1 << 3);
52 DDRD &= ~(1 << 2);
53
54 /* Set DATA line high for at least 90ns to disable /RESET functionality */
55 PORTD |= (1 << 3);
56 Delay_MS(1);
57
58 /* Set up the synchronous USART for XMEGA communications - 8 data bits, even parity, 2 stop bits */
59 UBRR1 = ((F_CPU / 2 / XPROG_HARDWARE_SPEED) - 1);
60 UCSR1B = (1 << TXEN1);
61 UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
62 #elif (ARCH == ARCH_UC3)
63 // TODO: FIXME
64 #endif
65
66 /* Send two IDLEs of 12 bits each to enable PDI interface (need at least 16 idle bits) */
67 XPROGTarget_SendIdle();
68 XPROGTarget_SendIdle();
69 }
70
71 /** Enables the target's TPI interface, holding the target in reset until TPI mode is exited. */
72 void XPROGTarget_EnableTargetTPI(void)
73 {
74 IsSending = false;
75
76 #if (ARCH == ARCH_AVR8)
77 /* Set /RESET line low for at least 400ns to enable TPI functionality */
78 AUX_LINE_DDR |= AUX_LINE_MASK;
79 AUX_LINE_PORT &= ~AUX_LINE_MASK;
80 Delay_MS(1);
81
82 /* Set Tx and XCK as outputs, Rx as input */
83 DDRD |= (1 << 5) | (1 << 3);
84 DDRD &= ~(1 << 2);
85
86 /* Set up the synchronous USART for TINY communications - 8 data bits, even parity, 2 stop bits */
87 UBRR1 = ((F_CPU / 2 / XPROG_HARDWARE_SPEED) - 1);
88 UCSR1B = (1 << TXEN1);
89 UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
90 #elif (ARCH == ARCH_UC3)
91 // TODO: FIXME
92 #endif
93
94 /* Send two IDLEs of 12 bits each to enable TPI interface (need at least 16 idle bits) */
95 XPROGTarget_SendIdle();
96 XPROGTarget_SendIdle();
97 }
98
99 /** Disables the target's PDI interface, exits programming mode and starts the target's application. */
100 void XPROGTarget_DisableTargetPDI(void)
101 {
102 /* Switch to Rx mode to ensure that all pending transmissions are complete */
103 XPROGTarget_SetRxMode();
104
105 #if (ARCH == ARCH_AVR8)
106 /* Turn off receiver and transmitter of the USART, clear settings */
107 UCSR1A = ((1 << TXC1) | (1 << RXC1));
108 UCSR1B = 0;
109 UCSR1C = 0;
110
111 /* Tristate all pins */
112 DDRD &= ~((1 << 5) | (1 << 3));
113 PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
114 #elif (ARCH == ARCH_UC3)
115 // TODO: FIXME
116 #endif
117 }
118
119 /** Disables the target's TPI interface, exits programming mode and starts the target's application. */
120 void XPROGTarget_DisableTargetTPI(void)
121 {
122 /* Switch to Rx mode to ensure that all pending transmissions are complete */
123 XPROGTarget_SetRxMode();
124
125 #if (ARCH == ARCH_AVR8)
126 /* Turn off receiver and transmitter of the USART, clear settings */
127 UCSR1A |= (1 << TXC1) | (1 << RXC1);
128 UCSR1B = 0;
129 UCSR1C = 0;
130
131 /* Set all USART lines as inputs, tristate */
132 DDRD &= ~((1 << 5) | (1 << 3));
133 PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
134
135 /* Tristate target /RESET line */
136 AUX_LINE_DDR &= ~AUX_LINE_MASK;
137 AUX_LINE_PORT &= ~AUX_LINE_MASK;
138 #elif (ARCH == ARCH_UC3)
139 // TODO: FIXME
140 #endif
141 }
142
143 /** Sends a byte via the USART.
144 *
145 * \param[in] Byte Byte to send through the USART
146 */
147 void XPROGTarget_SendByte(const uint8_t Byte)
148 {
149 /* Switch to Tx mode if currently in Rx mode */
150 if (!(IsSending))
151 XPROGTarget_SetTxMode();
152
153 #if (ARCH == ARCH_AVR8)
154 /* Wait until there is space in the hardware Tx buffer before writing */
155 while (!(UCSR1A & (1 << UDRE1)));
156 UCSR1A |= (1 << TXC1);
157 UDR1 = Byte;
158 #elif (ARCH == ARCH_UC3)
159 // TODO: FIXME
160 #endif
161 }
162
163 /** Receives a byte via the software USART, blocking until data is received.
164 *
165 * \return Received byte from the USART
166 */
167 uint8_t XPROGTarget_ReceiveByte(void)
168 {
169 /* Switch to Rx mode if currently in Tx mode */
170 if (IsSending)
171 XPROGTarget_SetRxMode();
172
173 #if (ARCH == ARCH_AVR8)
174 /* Wait until a byte has been received before reading */
175 while (!(UCSR1A & (1 << RXC1)) && !(TimeoutExpired));
176
177 return UDR1;
178 #elif (ARCH == ARCH_UC3)
179 // TODO: FIXME
180 return 0;
181 #endif
182 }
183
184 /** Sends an IDLE via the USART to the attached target, consisting of a full frame of idle bits. */
185 void XPROGTarget_SendIdle(void)
186 {
187 /* Switch to Tx mode if currently in Rx mode */
188 if (!(IsSending))
189 XPROGTarget_SetTxMode();
190
191 #if (ARCH == ARCH_AVR8)
192 /* Need to do nothing for a full frame to send an IDLE */
193 for (uint8_t i = 0; i < BITS_IN_USART_FRAME; i++)
194 {
195 /* Wait for a full cycle of the clock */
196 while (PIND & (1 << 5));
197 while (!(PIND & (1 << 5)));
198 }
199 #elif (ARCH == ARCH_UC3)
200 // TODO: FIXME
201 #endif
202 }
203
204 static void XPROGTarget_SetTxMode(void)
205 {
206 #if (ARCH == ARCH_AVR8)
207 /* Wait for a full cycle of the clock */
208 while (PIND & (1 << 5));
209 while (!(PIND & (1 << 5)));
210
211 PORTD |= (1 << 3);
212 DDRD |= (1 << 3);
213
214 UCSR1B &= ~(1 << RXEN1);
215 UCSR1B |= (1 << TXEN1);
216 #elif (ARCH == ARCH_UC3)
217 // TODO: FIXME
218 #endif
219
220 IsSending = true;
221 }
222
223 static void XPROGTarget_SetRxMode(void)
224 {
225 #if (ARCH == ARCH_AVR8)
226 while (!(UCSR1A & (1 << TXC1)));
227 UCSR1A |= (1 << TXC1);
228
229 UCSR1B &= ~(1 << TXEN1);
230 UCSR1B |= (1 << RXEN1);
231
232 DDRD &= ~(1 << 3);
233 PORTD &= ~(1 << 3);
234 #elif (ARCH == ARCH_UC3)
235 // TODO: FIXME
236 #endif
237
238 IsSending = false;
239 }
240
241 #endif
242