XPROGTarget.c 6.41 KB
Newer Older
1
2
/*
             LUFA Library
Dean Camera's avatar
Dean Camera committed
3
     Copyright (C) Dean Camera, 2010.
4
5
6
7
8
9
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
Dean Camera's avatar
Dean Camera committed
10
  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11

12
13
14
15
16
17
18
  Permission to use, copy, modify, distribute, and sell this 
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in 
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting 
  documentation, and that the name of the author not be used in 
  advertising or publicity pertaining to distribution of the 
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Target-related functions for the PDI Protocol decoder.
 */

36
37
#define  INCLUDE_FROM_XPROGTARGET_C
#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
volatile bool IsSending;
43

44
45
/** Enables the target's PDI interface, holding the target in reset until PDI mode is exited. */
void XPROGTarget_EnableTargetPDI(void)
46
{
47
48
	IsSending = false;

49
50
51
	/* Set Tx and XCK as outputs, Rx as input */
	DDRD |=  (1 << 5) | (1 << 3);
	DDRD &= ~(1 << 2);
52
	
53
	/* Set DATA line high for at least 90ns to disable /RESET functionality */
54
	PORTD |= (1 << 3);
55
	_delay_us(1);
56
	
57
	/* Set up the synchronous USART for XMEGA communications - 8 data bits, even parity, 2 stop bits */
58
	UBRR1  = (F_CPU / XPROG_HARDWARE_SPEED);
59
60
	UCSR1B = (1 << TXEN1);
	UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
61
62

	/* Send two BREAKs of 12 bits each to enable PDI interface (need at least 16 idle bits) */
63
64
65
66
	XPROGTarget_SendBreak();
	XPROGTarget_SendBreak();
}

67
68
/** Enables the target's TPI interface, holding the target in reset until TPI mode is exited. */
void XPROGTarget_EnableTargetTPI(void)
69
{
70
71
	IsSending = false;

72
	/* Set /RESET line low for at least 400ns to enable TPI functionality */
73
74
	AUX_LINE_DDR  |=  AUX_LINE_MASK;
	AUX_LINE_PORT &= ~AUX_LINE_MASK;
75
	_delay_us(1);
76

77
78
79
	/* Set Tx and XCK as outputs, Rx as input */
	DDRD |=  (1 << 5) | (1 << 3);
	DDRD &= ~(1 << 2);
80
		
81
	/* Set up the synchronous USART for TINY communications - 8 data bits, even parity, 2 stop bits */
82
	UBRR1  = (F_CPU / XPROG_HARDWARE_SPEED);
83
	UCSR1B = (1 << TXEN1);
84
	UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
85

86
	/* Send two BREAKs of 12 bits each to enable TPI interface (need at least 16 idle bits) */
87
88
	XPROGTarget_SendBreak();
	XPROGTarget_SendBreak();
89
90
}

91
92
/** Disables the target's PDI interface, exits programming mode and starts the target's application. */
void XPROGTarget_DisableTargetPDI(void)
93
{
94
95
96
	/* Switch to Rx mode to ensure that all pending transmissions are complete */
	XPROGTarget_SetRxMode();

97
	/* Turn off receiver and transmitter of the USART, clear settings */
98
	UCSR1A  = ((1 << TXC1) | (1 << RXC1));
99
100
101
	UCSR1B  = 0;
	UCSR1C  = 0;

102
	/* Tristate all pins */
103
	DDRD  &= ~((1 << 5) | (1 << 3));
104
	PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
105
106
}

107
108
/** Disables the target's TPI interface, exits programming mode and starts the target's application. */
void XPROGTarget_DisableTargetTPI(void)
109
{
110
111
112
	/* Switch to Rx mode to ensure that all pending transmissions are complete */
	XPROGTarget_SetRxMode();

113
114
115
116
	/* Turn off receiver and transmitter of the USART, clear settings */
	UCSR1A |= (1 << TXC1) | (1 << RXC1);
	UCSR1B  = 0;
	UCSR1C  = 0;
117
118

	/* Set all USART lines as input, tristate */
119
	DDRD  &= ~((1 << 5) | (1 << 3));
120
	PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
121
122

	/* Tristate target /RESET line */
123
124
	AUX_LINE_DDR  &= ~AUX_LINE_MASK;
	AUX_LINE_PORT &= ~AUX_LINE_MASK;
125
126
}

127
128
129
130
/** Sends a byte via the USART.
 *
 *  \param[in] Byte  Byte to send through the USART
 */
131
void XPROGTarget_SendByte(const uint8_t Byte)
132
{
133
134
	/* Switch to Tx mode if currently in Rx mode */
	if (!(IsSending))
135
136
	  XPROGTarget_SetTxMode();
	  
137
138
	/* Wait until there is space in the hardware Tx buffer before writing */
	while (!(UCSR1A & (1 << UDRE1)));
139
140
	UCSR1A |= (1 << TXC1);
	UDR1    = Byte;
141
142
143

    if (TimeoutMSRemaining)
	  TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
144
145
}

146
147
148
149
/** Receives a byte via the software USART, blocking until data is received.
 *
 *  \return Received byte from the USART
 */
150
uint8_t XPROGTarget_ReceiveByte(void)
151
{
152
153
	/* Switch to Rx mode if currently in Tx mode */
	if (IsSending)
154
	  XPROGTarget_SetRxMode();
155
156

	/* Wait until a byte has been received before reading */
157
	while (!(UCSR1A & (1 << RXC1)) && TimeoutMSRemaining);
158
	
159
160
161
	if (TimeoutMSRemaining)
	  TimeoutMSRemaining = COMMAND_TIMEOUT_MS;

162
163
164
	return UDR1;
}

165
/** Sends a BREAK via the USART to the attached target, consisting of a full frame of idle bits. */
166
void XPROGTarget_SendBreak(void)
167
{
168
169
	/* Switch to Tx mode if currently in Rx mode */
	if (!(IsSending))
170
	  XPROGTarget_SetTxMode();
171

172
	/* Need to do nothing for a full frame to send a BREAK */
173
	for (uint8_t i = 0; i < BITS_IN_USART_FRAME; i++)
174
	{
175
		/* Wait for a full cycle of the clock */
176
177
178
		while (PIND & (1 << 5));
		while (!(PIND & (1 << 5)));
	}
179
180
181

    if (TimeoutMSRemaining)
	  TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}

static void XPROGTarget_SetTxMode(void)
{
	/* Wait for a full cycle of the clock */
	while (PIND & (1 << 5));
	while (!(PIND & (1 << 5)));

	PORTD  |=  (1 << 3);
	DDRD   |=  (1 << 3);

	UCSR1B &= ~(1 << RXEN1);
	UCSR1B |=  (1 << TXEN1);
		
	IsSending = true;

198
199
200
    if (TimeoutMSRemaining)
	  TimeoutMSRemaining = COMMAND_TIMEOUT_MS;

201
202
203
204
205
206
207
208
209
210
211
212
213
214
	IsSending = true;
}

static void XPROGTarget_SetRxMode(void)
{
	while (!(UCSR1A & (1 << TXC1)));
	UCSR1A |=  (1 << TXC1);

	UCSR1B &= ~(1 << TXEN1);
	UCSR1B |=  (1 << RXEN1);

	DDRD   &= ~(1 << 3);
	PORTD  &= ~(1 << 3);

215
216
217
    if (TimeoutMSRemaining)
	  TimeoutMSRemaining = COMMAND_TIMEOUT_MS;

218
	IsSending = false;
219
220
}

221
#endif