eb16e0a28c62cbfbb62840e824f7a9c1a476e7c6
3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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
31 #if defined(ENABLE_PDI_PROTOCOL) || defined(__DOXYGEN__)
35 * Target-related functions for the PDI Protocol decoder.
38 #define INCLUDE_FROM_PDITARGET_C
39 #include "PDITarget.h"
41 volatile bool IsSending
;
43 #if !defined(PDI_VIA_HARDWARE_USART)
44 volatile uint16_t DataBits
;
45 volatile uint8_t BitCount
;
47 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
49 BITBANG_PDICLOCK_PORT
^= BITBANG_PDICLOCK_MASK
;
51 /* If not sending or receiving, just exit */
55 /* Check to see if the current clock state is on the rising or falling edge */
56 bool IsRisingEdge
= (BITBANG_PDICLOCK_PORT
& BITBANG_PDICLOCK_MASK
);
58 if (IsSending
&& !IsRisingEdge
)
61 BITBANG_PDIDATA_PORT
&= ~BITBANG_PDIDATA_MASK
;
63 BITBANG_PDIDATA_PORT
|= BITBANG_PDIDATA_MASK
;
68 else if (!IsSending
&& IsRisingEdge
)
70 /* Wait for the start bit when receiving */
71 if ((BitCount
== BITS_IN_FRAME
) && (BITBANG_PDIDATA_PORT
& BITBANG_PDIDATA_MASK
))
74 if (BITBANG_PDIDATA_PORT
& BITBANG_PDIDATA_MASK
)
75 DataBits
|= (1 << (BITS_IN_FRAME
- 1));
82 void PDITarget_EnableTargetPDI(void)
84 /* Set DATA and CLOCK lines to outputs */
85 BITBANG_PDIDATA_DDR
|= BITBANG_PDIDATA_MASK
;
86 BITBANG_PDICLOCK_DDR
|= BITBANG_PDICLOCK_MASK
;
88 /* Set DATA line high for 90ns to disable /RESET functionality */
89 BITBANG_PDIDATA_PORT
|= BITBANG_PDIDATA_MASK
;
90 asm volatile ("NOP"::);
91 asm volatile ("NOP"::);
93 /* Fire timer compare ISR every 160 cycles */
95 TCCR0A
= (1 << WGM01
);
97 TIMSK0
= (1 << OCIE0A
);
100 void PDITarget_DisableTargetPDI(void)
102 /* Set DATA and CLOCK lines to inputs */
103 BITBANG_PDIDATA_DDR
&= ~BITBANG_PDIDATA_MASK
;
104 BITBANG_PDICLOCK_DDR
&= ~BITBANG_PDICLOCK_MASK
;
106 /* Tristate DATA and CLOCK lines */
107 BITBANG_PDIDATA_PORT
&= ~BITBANG_PDIDATA_MASK
;
108 BITBANG_PDICLOCK_PORT
&= ~BITBANG_PDICLOCK_MASK
;
113 void PDITarget_SendByte(uint8_t Byte
)
115 bool IsOddBitsSet
= false;
117 /* Compute Even parity bit */
118 for (uint8_t i
= 0; i
< 8; i
++)
121 IsOddBitsSet
= !(IsOddBitsSet
);
124 /* Data shifted out LSB first, START DATA PARITY STOP STOP */
125 DataBits
= ((uint16_t)IsOddBitsSet
<< 10) | ((uint16_t)Byte
<< 1) | (1 << 0);
127 BITBANG_PDIDATA_PORT
|= BITBANG_PDIDATA_MASK
;
128 BITBANG_PDIDATA_DDR
|= BITBANG_PDIDATA_MASK
;
131 BitCount
= BITS_IN_FRAME
;
134 BITBANG_PDIDATA_PORT
&= ~BITBANG_PDIDATA_MASK
;
135 BITBANG_PDIDATA_DDR
&= ~BITBANG_PDIDATA_MASK
;
138 uint8_t PDITarget_ReceiveByte(void)
141 BitCount
= BITS_IN_FRAME
;
144 return (DataBits
>> 1);
147 void PDITarget_SendBreak(void)
151 BITBANG_PDIDATA_PORT
|= BITBANG_PDIDATA_MASK
;
152 BITBANG_PDIDATA_DDR
|= BITBANG_PDIDATA_MASK
;
155 BitCount
= BITS_IN_FRAME
;
158 BITBANG_PDIDATA_PORT
&= ~BITBANG_PDIDATA_MASK
;
159 BITBANG_PDIDATA_DDR
&= ~BITBANG_PDIDATA_MASK
;
162 void PDITarget_EnableTargetPDI(void)
164 /* Set Tx and XCK as outputs, Rx as input */
165 DDRD
|= (1 << 5) | (1 << 3);
168 /* Set DATA line high for 90ns to disable /RESET functionality */
170 asm volatile ("NOP"::);
171 asm volatile ("NOP"::);
173 /* Set up the synchronous USART for XMEGA communications -
174 8 data bits, even parity, 2 stop bits */
176 UCSR1B
= (1 << TXEN1
);
177 UCSR1C
= (1 << UMSEL10
) | (1 << UPM11
) | (1 << USBS1
) | (1 << UCSZ11
) | (1 << UCSZ10
) | (1 << UCPOL1
);
179 /* Send two BREAKs of 12 bits each to enable PDI interface (need at least 16 idle bits) */
180 PDITarget_SendBreak();
181 PDITarget_SendBreak();
184 void PDITarget_DisableTargetPDI(void)
186 /* Turn off receiver and transmitter of the USART, clear settings */
187 UCSR1A
|= (1 << TXC1
) | (1 << RXC1
);
191 /* Set all USART lines as input, tristate */
192 DDRD
&= ~((1 << 5) | (1 << 3));
193 PORTD
&= ~((1 << 5) | (1 << 3) | (1 << 2));
196 void PDITarget_SendByte(uint8_t Byte
)
198 /* Switch to Tx mode if currently in Rx mode */
204 UCSR1B
&= ~(1 << RXEN1
);
205 UCSR1B
|= (1 << TXEN1
);
210 /* Wait until there is space in the hardware Tx buffer before writing */
211 while (!(UCSR1A
& (1 << UDRE1
)));
215 uint8_t PDITarget_ReceiveByte(void)
217 /* Switch to Rx mode if currently in Tx mode */
220 while (!(UCSR1A
& (1 << TXC1
)));
221 UCSR1A
|= (1 << TXC1
);
223 UCSR1B
&= ~(1 << TXEN1
);
224 UCSR1B
|= (1 << RXEN1
);
232 /* Wait until a byte has been received before reading */
233 while (!(UCSR1A
& (1 << RXC1
)));
237 void PDITarget_SendBreak(void)
239 /* Switch to Tx mode if currently in Rx mode */
245 UCSR1B
&= ~(1 << RXEN1
);
246 UCSR1B
|= (1 << TXEN1
);
251 /* Need to do nothing for a full frame to send a BREAK */
252 for (uint8_t i
= 0; i
<= BITS_IN_FRAME
; i
++)
254 /* Wait for rising edge of clock */
255 while (PIND
& (1 << 5));
257 /* Wait for falling edge of clock */
258 while (!(PIND
& (1 << 5)));