3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Target-related functions for the PDI Protocol decoder.
36 #define INCLUDE_FROM_XPROGTARGET_C
37 #include "XPROGTarget.h"
39 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
41 /** Flag to indicate if the USART is currently in Tx or Rx mode. */
44 /** Enables the target's PDI interface, holding the target in reset until PDI mode is exited. */
45 void XPROGTarget_EnableTargetPDI(void)
49 /* Set Tx and XCK as outputs, Rx as input */
50 DDRD
|= (1 << 5) | (1 << 3);
53 /* Set DATA line high for at least 90ns to disable /RESET functionality */
57 /* Set up the synchronous USART for XMEGA communications - 8 data bits, even parity, 2 stop bits */
58 UBRR1
= ((F_CPU
/ 2 / XPROG_HARDWARE_SPEED
) - 1);
59 UCSR1B
= (1 << TXEN1
);
60 UCSR1C
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
);
62 /* Send two IDLEs of 12 bits each to enable PDI interface (need at least 16 idle bits) */
63 XPROGTarget_SendIdle();
64 XPROGTarget_SendIdle();
67 /** Enables the target's TPI interface, holding the target in reset until TPI mode is exited. */
68 void XPROGTarget_EnableTargetTPI(void)
72 /* Set /RESET line low for at least 400ns to enable TPI functionality */
73 AUX_LINE_DDR
|= AUX_LINE_MASK
;
74 AUX_LINE_PORT
&= ~AUX_LINE_MASK
;
77 /* Set Tx and XCK as outputs, Rx as input */
78 DDRD
|= (1 << 5) | (1 << 3);
81 /* Set up the synchronous USART for TINY communications - 8 data bits, even parity, 2 stop bits */
82 UBRR1
= ((F_CPU
/ 2 / XPROG_HARDWARE_SPEED
) - 1);
83 UCSR1B
= (1 << TXEN1
);
84 UCSR1C
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
);
86 /* Send two IDLEs of 12 bits each to enable TPI interface (need at least 16 idle bits) */
87 XPROGTarget_SendIdle();
88 XPROGTarget_SendIdle();
91 /** Disables the target's PDI interface, exits programming mode and starts the target's application. */
92 void XPROGTarget_DisableTargetPDI(void)
94 /* Switch to Rx mode to ensure that all pending transmissions are complete */
95 XPROGTarget_SetRxMode();
97 /* Turn off receiver and transmitter of the USART, clear settings */
98 UCSR1A
= ((1 << TXC1
) | (1 << RXC1
));
102 /* Tristate all pins */
103 DDRD
&= ~((1 << 5) | (1 << 3));
104 PORTD
&= ~((1 << 5) | (1 << 3) | (1 << 2));
107 /** Disables the target's TPI interface, exits programming mode and starts the target's application. */
108 void XPROGTarget_DisableTargetTPI(void)
110 /* Switch to Rx mode to ensure that all pending transmissions are complete */
111 XPROGTarget_SetRxMode();
113 /* Turn off receiver and transmitter of the USART, clear settings */
114 UCSR1A
|= (1 << TXC1
) | (1 << RXC1
);
118 /* Set all USART lines as inputs, tristate */
119 DDRD
&= ~((1 << 5) | (1 << 3));
120 PORTD
&= ~((1 << 5) | (1 << 3) | (1 << 2));
122 /* Tristate target /RESET line */
123 AUX_LINE_DDR
&= ~AUX_LINE_MASK
;
124 AUX_LINE_PORT
&= ~AUX_LINE_MASK
;
127 /** Sends a byte via the USART.
129 * \param[in] Byte Byte to send through the USART
131 void XPROGTarget_SendByte(const uint8_t Byte
)
133 /* Switch to Tx mode if currently in Rx mode */
135 XPROGTarget_SetTxMode();
137 /* Wait until there is space in the hardware Tx buffer before writing */
138 while (!(UCSR1A
& (1 << UDRE1
)));
139 UCSR1A
|= (1 << TXC1
);
143 /** Receives a byte via the software USART, blocking until data is received.
145 * \return Received byte from the USART
147 uint8_t XPROGTarget_ReceiveByte(void)
149 /* Switch to Rx mode if currently in Tx mode */
151 XPROGTarget_SetRxMode();
153 /* Wait until a byte has been received before reading */
154 while (!(UCSR1A
& (1 << RXC1
)) && TimeoutTicksRemaining
);
159 /** Sends an IDLE via the USART to the attached target, consisting of a full frame of idle bits. */
160 void XPROGTarget_SendIdle(void)
162 /* Switch to Tx mode if currently in Rx mode */
164 XPROGTarget_SetTxMode();
166 /* Need to do nothing for a full frame to send an IDLE */
167 for (uint8_t i
= 0; i
< BITS_IN_USART_FRAME
; i
++)
169 /* Wait for a full cycle of the clock */
170 while (PIND
& (1 << 5));
171 while (!(PIND
& (1 << 5)));
175 static void XPROGTarget_SetTxMode(void)
177 /* Wait for a full cycle of the clock */
178 while (PIND
& (1 << 5));
179 while (!(PIND
& (1 << 5)));
184 UCSR1B
&= ~(1 << RXEN1
);
185 UCSR1B
|= (1 << TXEN1
);
190 static void XPROGTarget_SetRxMode(void)
192 while (!(UCSR1A
& (1 << TXC1
)));
193 UCSR1A
|= (1 << TXC1
);
195 UCSR1B
&= ~(1 << TXEN1
);
196 UCSR1B
|= (1 << RXEN1
);