Added new incomplete AudioInputHost Host LowLevel demo.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.h
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 * Header file for ISPTarget.c.
34 */
35
36 #ifndef _ISP_TARGET_
37 #define _ISP_TARGET_
38
39 /* Includes: */
40 #include <LUFA/Common/Common.h>
41 #include <LUFA/Drivers/USB/USB.h>
42
43 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
44 #include <LUFA/Drivers/Peripheral/SPI.h> // TODO: FIXME
45 #endif
46
47 #if (ARCH == ARCH_AVR8)
48 #include <avr/io.h>
49 #include <avr/pgmspace.h>
50 #elif (ARCH == ARCH_UC3)
51 #include <avr32/io.h>
52 #endif
53
54 #include "../V2ProtocolParams.h"
55
56 /* Preprocessor Checks: */
57 #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
58 #undef ENABLE_ISP_PROTOCOL
59
60 #if !defined(ENABLE_XPROG_PROTOCOL)
61 #define ENABLE_XPROG_PROTOCOL
62 #endif
63 #endif
64
65 /* Macros: */
66 /** Low level device command to issue an extended FLASH address, for devices with other 128KB of FLASH. */
67 #define LOAD_EXTENDED_ADDRESS_CMD 0x4D
68
69 /** Macro to convert an ISP frequency to a number of timer clock cycles for the software SPI driver. */
70 #define TIMER_COMP(freq) (((F_CPU / 8) / 2 / freq) - 1)
71
72 /** ISP rescue clock speed in Hz, for clocking targets with incorrectly set fuses. */
73 #define ISP_RESCUE_CLOCK_SPEED 4000000
74
75 /* External Variables: */
76 extern bool HardwareSPIMode;
77
78 /* Function Prototypes: */
79 void ISPTarget_EnableTargetISP(void);
80 void ISPTarget_DisableTargetISP(void);
81 void ISPTarget_ConfigureRescueClock(void);
82 void ISPTarget_ConfigureSoftwareISP(const uint8_t SCKDuration);
83 uint8_t ISPTarget_TransferSoftSPIByte(const uint8_t Byte);
84 void ISPTarget_ChangeTargetResetLine(const bool ResetTarget);
85 uint8_t ISPTarget_WaitWhileTargetBusy(void);
86 void ISPTarget_LoadExtendedAddress(void);
87 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode,
88 const uint16_t PollAddress,
89 const uint8_t PollValue,
90 const uint8_t DelayMS,
91 const uint8_t ReadMemCommand);
92
93 /* Inline Functions: */
94 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
95 /** Sends a byte of ISP data to the attached target, using the appropriate SPI hardware or
96 * software routines depending on the selected ISP speed.
97 *
98 * \param[in] Byte Byte of data to send to the attached target
99 */
100 static inline void ISPTarget_SendByte(const uint8_t Byte)
101 {
102 if (HardwareSPIMode)
103 SPI_SendByte(Byte);
104 else
105 ISPTarget_TransferSoftSPIByte(Byte);
106 }
107
108 /** Receives a byte of ISP data from the attached target, using the appropriate
109 * SPI hardware or software routines depending on the selected ISP speed.
110 *
111 * \return Received byte of data from the attached target
112 */
113 static inline uint8_t ISPTarget_ReceiveByte(void)
114 {
115 if (HardwareSPIMode)
116 return SPI_ReceiveByte();
117 else
118 return ISPTarget_TransferSoftSPIByte(0x00);
119 }
120
121 /** Sends and receives a byte of ISP data to and from the attached target, using the
122 * appropriate SPI hardware or software routines depending on the selected ISP speed.
123 *
124 * \param[in] Byte Byte of data to send to the attached target
125 *
126 * \return Received byte of data from the attached target
127 */
128 static inline uint8_t ISPTarget_TransferByte(const uint8_t Byte)
129 {
130 if (HardwareSPIMode)
131 return SPI_TransferByte(Byte);
132 else
133 return ISPTarget_TransferSoftSPIByte(Byte);
134 }
135 #endif
136
137 #endif
138