3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Header file for ISPTarget.c.
41 #include <avr/pgmspace.h>
42 #include <util/delay.h>
44 #include <LUFA/Drivers/USB/USB.h>
45 #include <LUFA/Drivers/Peripheral/SPI.h>
47 #include "../V2ProtocolParams.h"
49 /* Preprocessor Checks: */
50 #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
51 #undef ENABLE_ISP_PROTOCOL
53 #if !defined(ENABLE_XPROG_PROTOCOL)
54 #define ENABLE_XPROG_PROTOCOL
59 /** Low level device command to issue an extended FLASH address, for devices with over 128KB of FLASH. */
60 #define LOAD_EXTENDED_ADDRESS_CMD 0x4D
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) / 2 / freq) - 1)
65 /** ISP rescue clock speed in Hz, for clocking targets with incorrectly set fuses. */
66 #define ISP_RESCUE_CLOCK_SPEED 4000000
68 /* External Variables: */
69 extern bool HardwareSPIMode
;
71 /* Function Prototypes: */
72 void ISPTarget_EnableTargetISP(void);
73 void ISPTarget_DisableTargetISP(void);
74 void ISPTarget_ConfigureRescueClock(void);
75 void ISPTarget_ConfigureSoftwareSPI(const uint8_t SCKDuration
);
76 uint8_t ISPTarget_TransferSoftSPIByte(const uint8_t Byte
);
77 void ISPTarget_ChangeTargetResetLine(const bool ResetTarget
);
78 uint8_t ISPTarget_WaitWhileTargetBusy(void);
79 void ISPTarget_LoadExtendedAddress(void);
80 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode
,
81 const uint16_t PollAddress
,
82 const uint8_t PollValue
,
83 const uint8_t DelayMS
,
84 const uint8_t ReadMemCommand
);
86 /* Inline Functions: */
87 /** Sends a byte of ISP data to the attached target, using the appropriate SPI hardware or
88 * software routines depending on the selected ISP speed.
90 * \param[in] Byte Byte of data to send to the attached target
92 static inline void ISPTarget_SendByte(const uint8_t Byte
)
97 ISPTarget_TransferSoftSPIByte(Byte
);
100 /** Receives a byte of ISP data from the attached target, using the appropriate
101 * SPI hardware or software routines depending on the selected ISP speed.
103 * \return Received byte of data from the attached target
105 static inline uint8_t ISPTarget_ReceiveByte(void)
107 #if !defined(INVERTED_ISP_MISO)
109 return SPI_ReceiveByte();
111 return ISPTarget_TransferSoftSPIByte(0x00);
114 return ~SPI_ReceiveByte();
116 return ~ISPTarget_TransferSoftSPIByte(0x00);
120 /** Sends and receives a byte of ISP data to and from the attached target, using the
121 * appropriate SPI hardware or software routines depending on the selected ISP speed.
123 * \param[in] Byte Byte of data to send to the attached target
125 * \return Received byte of data from the attached target
127 static inline uint8_t ISPTarget_TransferByte(const uint8_t Byte
)
129 #if !defined(INVERTED_ISP_MISO)
131 return SPI_TransferByte(Byte
);
133 return ISPTarget_TransferSoftSPIByte(Byte
);
136 return ~SPI_TransferByte(Byte
);
138 return ~ISPTarget_TransferSoftSPIByte(Byte
);