Rescue clock of the AVRISP-MKII moved to the AVR's OCR1A pin, so that the clock can...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 <avr/io.h>
41 #include <avr/pgmspace.h>
42 #include <util/delay.h>
43
44 #include <LUFA/Drivers/USB/USB.h>
45 #include <LUFA/Drivers/Peripheral/SPI.h>
46
47 #include "../V2ProtocolParams.h"
48
49 /* Preprocessor Checks: */
50 #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
51 #undef ENABLE_ISP_PROTOCOL
52
53 #if !defined(ENABLE_XPROG_PROTOCOL)
54 #define ENABLE_XPROG_PROTOCOL
55 #endif
56 #endif
57
58 /* Macros: */
59 /** Low level device command to issue an extended FLASH address, for devices with other 128KB of FLASH. */
60 #define LOAD_EXTENDED_ADDRESS_CMD 0x4D
61
62 /** Macro to convert an ISP frequency to a number of timer clock cycles for the software SPI driver */
63 #define TIMER_COMP(freq) ((((F_CPU / 8) / freq) / 2) - 1)
64
65 /* External Variables: */
66 extern bool HardwareSPIMode;
67
68 /* Function Prototypes: */
69 void ISPTarget_Init(void);
70 void ISPTarget_ShutDown(void);
71 void ISPTarget_ConfigureRescueClock(void);
72 void ISPTarget_ConfigureSoftwareISP(const uint8_t SCKDuration);
73 uint8_t ISPTarget_TransferSoftSPIByte(const uint8_t Byte);
74 void ISPTarget_ChangeTargetResetLine(const bool ResetTarget);
75 uint8_t ISPTarget_WaitWhileTargetBusy(void);
76 void ISPTarget_LoadExtendedAddress(void);
77 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode,
78 const uint16_t PollAddress,
79 const uint8_t PollValue,
80 const uint8_t DelayMS,
81 const uint8_t ReadMemCommand);
82
83 /* Inline Functions: */
84 /** Sends a byte of ISP data to the attached target, using the appropriate SPI hardware or
85 * software routines depending on the selected ISP speed.
86 *
87 * \param[in] Byte Byte of data to send to the attached target
88 */
89 static inline void ISPTarget_SendByte(const uint8_t Byte)
90 {
91 if (HardwareSPIMode)
92 SPI_SendByte(Byte);
93 else
94 ISPTarget_TransferSoftSPIByte(Byte);
95 }
96
97 /** Receives a byte of ISP data from the attached target, using the appropriate
98 * SPI hardware or software routines depending on the selected ISP speed.
99 *
100 * \return Received byte of data from the attached target
101 */
102 static inline uint8_t ISPTarget_ReceiveByte(void)
103 {
104 if (HardwareSPIMode)
105 return SPI_ReceiveByte();
106 else
107 return ISPTarget_TransferSoftSPIByte(0x00);
108 }
109
110 /** Sends and receives a byte of ISP data to and from the attached target, using the
111 * appropriate SPI hardware or software routines depending on the selected ISP speed.
112 *
113 * \param[in] Byte Byte of data to send to the attached target
114 *
115 * \return Received byte of data from the attached target
116 */
117 static inline uint8_t ISPTarget_TransferByte(const uint8_t Byte)
118 {
119 if (HardwareSPIMode)
120 return SPI_TransferByte(Byte);
121 else
122 return ISPTarget_TransferSoftSPIByte(Byte);
123 }
124
125 #endif
126