Started implementing the low level PDI protocol in the AVRISP project, for XMEGA...
[pub/USBasp.git] / Projects / AVRISP / Lib / PDITarget.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 #if defined(ENABLE_XPROG_PROTOCOL)
32
33 /** \file
34 *
35 * Target-related functions for the PDI Protocol decoder.
36 */
37
38 #define INCLUDE_FROM_PDITARGET_C
39 #include "PDITarget.h"
40
41 void PDITarget_SendByte(uint8_t Byte)
42 {
43 PDIDATA_LINE_PORT &= ~PDIDATA_LINE_MASK;
44
45 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
46 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
47
48 for (uint8_t i = 0; i < 8; i++)
49 {
50 if (Byte & 0x01)
51 PDIDATA_LINE_PORT |= PDIDATA_LINE_MASK;
52 else
53 PDIDATA_LINE_PORT &= ~PDIDATA_LINE_MASK;
54
55 Byte >>= 1;
56
57 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
58 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
59 }
60
61 PDIDATA_LINE_PORT |= PDIDATA_LINE_MASK;
62
63 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
64 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
65 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
66 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
67 }
68
69 uint8_t PDITarget_ReceiveByte(void)
70 {
71 uint8_t ReceivedByte = 0;
72
73 PDIDATA_LINE_DDR &= ~PDIDATA_LINE_MASK;
74
75 bool FoundStartBit;
76
77 do
78 {
79 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
80 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
81 FoundStartBit = !(PDIDATA_LINE_PIN & PDIDATA_LINE_MASK);
82 } while (!FoundStartBit);
83
84 for (uint8_t i = 0; i < 8; i++)
85 {
86 if (PDIDATA_LINE_PIN & PDIDATA_LINE_MASK)
87 ReceivedByte |= 0x01;
88
89 ReceivedByte <<= 1;
90
91 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
92 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
93 }
94
95 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
96 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
97 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
98 PDICLOCK_LINE_PORT ^= PDICLOCK_LINE_MASK;
99
100 PDIDATA_LINE_DDR |= PDIDATA_LINE_MASK;
101
102 return ReceivedByte;
103 }
104
105 #endif