From 60b30c036397cb5627fa374bb930794b225daa29 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Fri, 7 Jul 2017 11:55:23 -0400 Subject: Squashed 'lib/lufa/' content from commit 385d40300 git-subtree-dir: lib/lufa git-subtree-split: 385d4030035dbaf41591309dbde47653bd03841b --- Projects/AVRISP-MKII/AVRISP-MKII.c | 164 ++ Projects/AVRISP-MKII/AVRISP-MKII.h | 90 + Projects/AVRISP-MKII/AVRISP-MKII.txt | 346 +++ Projects/AVRISP-MKII/AVRISPDescriptors.c | 203 ++ Projects/AVRISP-MKII/AVRISPDescriptors.h | 100 + Projects/AVRISP-MKII/Config/AppConfig.h | 68 + Projects/AVRISP-MKII/Config/LUFAConfig.h | 93 + Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c | 531 +++++ Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.h | 81 + Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c | 370 +++ Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h | 147 ++ Projects/AVRISP-MKII/Lib/V2Protocol.c | 267 +++ Projects/AVRISP-MKII/Lib/V2Protocol.h | 105 + Projects/AVRISP-MKII/Lib/V2ProtocolConstants.h | 90 + Projects/AVRISP-MKII/Lib/V2ProtocolParams.c | 207 ++ Projects/AVRISP-MKII/Lib/V2ProtocolParams.h | 91 + Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c | 274 +++ Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.h | 86 + Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c | 468 ++++ Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.h | 140 ++ Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c | 480 ++++ Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.h | 136 ++ Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.c | 209 ++ Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h | 136 ++ Projects/AVRISP-MKII/WindowsDriver/AVRISP_mkII.inf | Bin 0 -> 8070 bytes .../AVRISP-MKII/WindowsDriver/amd64/libusb0.dll | Bin 0 -> 76384 bytes .../AVRISP-MKII/WindowsDriver/amd64/libusb0.sys | Bin 0 -> 52832 bytes Projects/AVRISP-MKII/WindowsDriver/avrisp_mkii.cat | Bin 0 -> 9610 bytes .../AVRISP-MKII/WindowsDriver/ia64/libusb0.dll | Bin 0 -> 157792 bytes .../AVRISP-MKII/WindowsDriver/ia64/libusb0.sys | Bin 0 -> 110176 bytes .../AVRISP-MKII/WindowsDriver/installer_x64.exe | Bin 0 -> 25088 bytes .../AVRISP-MKII/WindowsDriver/installer_x86.exe | Bin 0 -> 23552 bytes .../license/libusb0/installer_license.txt | 851 +++++++ Projects/AVRISP-MKII/WindowsDriver/x86/libusb0.sys | Bin 0 -> 42592 bytes .../AVRISP-MKII/WindowsDriver/x86/libusb0_x86.dll | Bin 0 -> 67680 bytes Projects/AVRISP-MKII/asf.xml | 91 + Projects/AVRISP-MKII/doxyfile | 2396 ++++++++++++++++++++ Projects/AVRISP-MKII/makefile | 44 + 38 files changed, 8264 insertions(+) create mode 100644 Projects/AVRISP-MKII/AVRISP-MKII.c create mode 100644 Projects/AVRISP-MKII/AVRISP-MKII.h create mode 100644 Projects/AVRISP-MKII/AVRISP-MKII.txt create mode 100644 Projects/AVRISP-MKII/AVRISPDescriptors.c create mode 100644 Projects/AVRISP-MKII/AVRISPDescriptors.h create mode 100644 Projects/AVRISP-MKII/Config/AppConfig.h create mode 100644 Projects/AVRISP-MKII/Config/LUFAConfig.h create mode 100644 Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c create mode 100644 Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.h create mode 100644 Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c create mode 100644 Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h create mode 100644 Projects/AVRISP-MKII/Lib/V2Protocol.c create mode 100644 Projects/AVRISP-MKII/Lib/V2Protocol.h create mode 100644 Projects/AVRISP-MKII/Lib/V2ProtocolConstants.h create mode 100644 Projects/AVRISP-MKII/Lib/V2ProtocolParams.c create mode 100644 Projects/AVRISP-MKII/Lib/V2ProtocolParams.h create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.h create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.h create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.c create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XPROGProtocol.h create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.c create mode 100644 Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h create mode 100644 Projects/AVRISP-MKII/WindowsDriver/AVRISP_mkII.inf create mode 100644 Projects/AVRISP-MKII/WindowsDriver/amd64/libusb0.dll create mode 100644 Projects/AVRISP-MKII/WindowsDriver/amd64/libusb0.sys create mode 100644 Projects/AVRISP-MKII/WindowsDriver/avrisp_mkii.cat create mode 100644 Projects/AVRISP-MKII/WindowsDriver/ia64/libusb0.dll create mode 100644 Projects/AVRISP-MKII/WindowsDriver/ia64/libusb0.sys create mode 100644 Projects/AVRISP-MKII/WindowsDriver/installer_x64.exe create mode 100644 Projects/AVRISP-MKII/WindowsDriver/installer_x86.exe create mode 100644 Projects/AVRISP-MKII/WindowsDriver/license/libusb0/installer_license.txt create mode 100644 Projects/AVRISP-MKII/WindowsDriver/x86/libusb0.sys create mode 100644 Projects/AVRISP-MKII/WindowsDriver/x86/libusb0_x86.dll create mode 100644 Projects/AVRISP-MKII/asf.xml create mode 100644 Projects/AVRISP-MKII/doxyfile create mode 100644 Projects/AVRISP-MKII/makefile (limited to 'Projects/AVRISP-MKII') diff --git a/Projects/AVRISP-MKII/AVRISP-MKII.c b/Projects/AVRISP-MKII/AVRISP-MKII.c new file mode 100644 index 0000000000..a734d960ba --- /dev/null +++ b/Projects/AVRISP-MKII/AVRISP-MKII.c @@ -0,0 +1,164 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Main source file for the AVRISP project. This file contains the main tasks of + * the project and is responsible for the initial application hardware configuration. + */ + +#include "AVRISP-MKII.h" + +#if (BOARD != BOARD_NONE) + /* Some board hardware definitions (e.g. the Arduino Micro) have their LEDs defined on the same pins + as the ISP, PDI or TPI interfaces (see the accompanying project documentation). If a board other + than NONE is selected (to enable the LED driver with the programmer) you should double-check that + no conflicts will occur. If there is a conflict, turn off the LEDs (set BOARD to NONE in the makefile) + or define a custom board driver (see the LUFA manual) with alternative LED mappings. + */ + #warning Board specific drivers have been selected; make sure the board LED driver does not conflict with the programmer ISP/PDI/TPI interfaces. +#endif + +/** Main program entry point. This routine contains the overall program flow, including initial + * setup of all components and the main program loop. + */ +int main(void) +{ + SetupHardware(); + V2Protocol_Init(); + + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + GlobalInterruptEnable(); + + for (;;) + { + #if (BOARD == BOARD_USBTINYMKII) + /* On the USBTINY-MKII target, there is a secondary LED which indicates the current selected power + mode - either VBUS, or sourced from the VTARGET pin of the programming connectors */ + LEDs_ChangeLEDs(LEDMASK_VBUSPOWER, (PIND & (1 << 0)) ? 0 : LEDMASK_VBUSPOWER); + #endif + + AVRISP_Task(); + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ +#if (ARCH == ARCH_AVR8) + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); +#endif + + /* Hardware Initialization */ + LEDs_Init(); + #if defined(RESET_TOGGLES_LIBUSB_COMPAT) + UpdateCurrentCompatibilityMode(); + #endif + + /* USB Stack Initialization */ + USB_Init(); +} + +/** Event handler for the library USB Connection event. */ +void EVENT_USB_Device_Connect(void) +{ + LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); +} + +/** Event handler for the library USB Disconnection event. */ +void EVENT_USB_Device_Disconnect(void) +{ + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); +} + +/** Event handler for the library USB Configuration Changed event. */ +void EVENT_USB_Device_ConfigurationChanged(void) +{ + bool ConfigSuccess = true; + + /* Setup AVRISP Data OUT endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1); + + /* Setup AVRISP Data IN endpoint if it is using a physically different endpoint */ + if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK)) + ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1); + + /* Indicate endpoint configuration success or failure */ + LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); +} + +/** Processes incoming V2 Protocol commands from the host, returning a response when required. */ +void AVRISP_Task(void) +{ + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + + V2Params_UpdateParamValues(); + + Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR); + + /* Check to see if a V2 Protocol command has been received */ + if (Endpoint_IsOUTReceived()) + { + LEDs_SetAllLEDs(LEDMASK_BUSY); + + /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */ + V2Protocol_ProcessCommand(); + + LEDs_SetAllLEDs(LEDMASK_USB_READY); + } +} + +/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" + * documentation) by the application code so that the address and size of a requested descriptor can be given + * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function + * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the + * USB host. + * + * \param[in] wValue Descriptor type and index to retrieve + * \param[in] wIndex Sub-index to retrieve (such as a localized string language) + * \param[out] DescriptorAddress Address of the retrieved descriptor + * + * \return Length of the retrieved descriptor in bytes, or NO_DESCRIPTOR if the descriptor was not found + */ +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, + const uint16_t wIndex, + const void** const DescriptorAddress) +{ + return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress); +} + diff --git a/Projects/AVRISP-MKII/AVRISP-MKII.h b/Projects/AVRISP-MKII/AVRISP-MKII.h new file mode 100644 index 0000000000..ae2d041484 --- /dev/null +++ b/Projects/AVRISP-MKII/AVRISP-MKII.h @@ -0,0 +1,90 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for AVRISP.c. + */ + +#ifndef _AVRISP_H_ +#define _AVRISP_H_ + + /* Includes: */ + #include + #include + #include + #include + + #include + #include + #include + + #if defined(ADC) + #include + #endif + + #include "AVRISPDescriptors.h" + #include "Lib/V2Protocol.h" + #include "Config/AppConfig.h" + + /* Macros: */ + /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */ + #define LEDMASK_USB_NOTREADY LEDS_LED1 + + /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */ + #define LEDMASK_USB_ENUMERATING (LEDS_LED1 | LEDS_LED2) + + /** LED mask for the library LED driver, to indicate that the USB interface is ready. */ + #define LEDMASK_USB_READY LEDS_LED2 + + /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ + #define LEDMASK_USB_ERROR LEDS_LED1 + + /** LED mask for the library LED driver, to indicate that the USB interface is busy. */ + #define LEDMASK_BUSY (LEDS_LED1 | LEDS_LED2) + + /** LED mask for the library LED driver, to indicate that the target is being powered by VBUS. */ + #define LEDMASK_VBUSPOWER LEDS_LED3 + + /* Function Prototypes: */ + void SetupHardware(void); + void AVRISP_Task(void); + + void EVENT_USB_Device_Connect(void); + void EVENT_USB_Device_Disconnect(void); + void EVENT_USB_Device_ConfigurationChanged(void); + + uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, + const uint16_t wIndex, + const void** const DescriptorAddress) + ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3); + +#endif + diff --git a/Projects/AVRISP-MKII/AVRISP-MKII.txt b/Projects/AVRISP-MKII/AVRISP-MKII.txt new file mode 100644 index 0000000000..76d612ec05 --- /dev/null +++ b/Projects/AVRISP-MKII/AVRISP-MKII.txt @@ -0,0 +1,346 @@ +/** \file + * + * This file contains special DoxyGen information for the generation of the main page and other special + * documentation pages. It is not a project source file. + */ + +/** \mainpage AVRISP MKII Programmer Project + * + * \section Sec_Compat Project Compatibility + * + * The following list indicates what microcontrollers are compatible with this project. + * + * \li Series 7 USB AVRs (AT90USBxxx7) + * \li Series 6 USB AVRs (AT90USBxxx6) + * \li Series 4 USB AVRs (ATMEGAxxU4) + * \li Series 2 USB AVRs (AT90USBxx2, ATMEGAxxU2) - 8KB versions with reduced features only + * + * \section Sec_Info USB Information + * + * The following table gives a rundown of the USB utilization of this project. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
USB Mode:Device
USB Class:Vendor Specific Class
USB Subclass:N/A
Relevant Standards:Atmel AVRISP MKII Protocol Specification
Supported USB Speeds:Full Speed Mode
+ * + * \section Sec_Description Project Description + * + * Firmware for an Atmel Studio compatible AVRISP-MKII clone programmer. This project will enable the USB + * AVR series of microcontrollers to act as a clone of the official Atmel AVRISP-MKII programmer, usable within + * Atmel Studio or with any software capable of driving a real Atmel AVRISP-MKII programmer. In its most + * basic form, it allows for the programming of AVR TINY, MEGA and XMEGA devices at the programmer's VCC voltage from + * within Atmel Studio with no special hardware other than the USB AVR and the parts needed for the USB + * interface. If the user desires, more advanced circuits incorporating level conversion can be made to allow for the + * programming of target AVRs running at a different voltage to the programmer. + * + * This device spoofs Atmel's official AVRISP-MKII device PID so that it remains compatible with Atmel's AVRISP-MKII + * drivers. It is currently tested working under the following configurations: + * + * - Windows: Atmel Studio 7, with alternative driver + * - Windows: AVRDUDE 6.2, with alternative driver + * - Linux: AVRDUDE 6.1 + * + * Note that this clone requires a libUSB based driver under Windows, due to an incompatible change in the official + * Jungo based driver. The alternative driver given here will function with both real and clone AVRISP devices in Atmel + * Studio 7 onwards under Windows - and as a bonus, also provides AVRDude access to the programmer. + * + * As of Atmel Studio version 7.0.1417 the legacy Jungo driver has been deprecated in favor of an official libUSB based + * driver, making the driver packaged here superfluous. Use the driver packaged here only if you are still using an earlier + * Atmel Studio 7 build, otherwise the official Atmel libUSB driver should be used. + * + * Note that this design currently has the following limitations: + * - No reversed/shorted target connector detection and notification + * - A separate header is required for each of the ISP, PDI and TPI programming protocols that the user wishes to use + * + * On AVR models with an ADC converter, the USB AVR's AVCC pin should be tied to 5V (e.g. VBUS) and the + * \c VTARGET_ADC_CHANNEL token should be set to an appropriate ADC channel number in the project makefile for VTARGET + * detection to operate correctly. On models without an ADC converter, VTARGET will report a fixed 3.3V level at all times + * which should allow the programmer to remain compatible at the protocol level with all AVR devices. + * + * While this application can be compiled for USB AVRs with as little as 8KB of FLASH, for full functionality 16KB or more + * of FLASH is required. On 8KB devices, ISP or PDI/TPI protocol programming support can be disabled to reduce program size. + * + * \section Sec_KnownIssues Known Issues: + * + * \par XMEGA EEPROM programming fails in some cases. + * Several users have reported that XMEGA EEPROM programming fails unless the chip is erased first. If a non-blank EEPROM + * is present, writing further EEPROM data causes corruption. + * LUFA issue tracker entry. + * + * \section Sec_Installation Installation + * The programmer supports multiple platforms, both Windows and Linux. + * + * \subsection SSec_LinuxInstallation Linux Installation + * On Linux systems, the programmer should be usable out of the box with no special setup other than (on some systems) + * editing of the system permissions to allow the programmer to be used from a non-elevated (root) context. The programmer + * is compatible with the free open source AVRDude programming software project. + * + * \subsection SSec_WindowsInstallation Windows Installation + * On Windows systems, due to an unfortunate limitation of the USB AVR devices and the Atmel Studio platform, the programmer + * requires an alternative libUSB based driver. Uninstall the existing Jungo driver for the device (if installed) and replace + * it with the driver that ships with this project, to enable access to the programmer in Atmel Studio and AVRDUDE. + * + * \section Sec_ISP ISP Connections + * Connections to the device for SPI programming (when enabled): + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Programmer Pin:Target Device Pin:ISP 6 Pin Layout:
MISOPDO1
ADCx 1VTARGET2
SCLKSCLK3
MOSIPDI4
PORTx.y 2/RESET5
GNDGND6
+ * + * In addition, the AVR's OCR1A pin will generate a 4MHz clock, to act as an external rescue device clock if the + * fuses have been mis-set. To use the recovery clock, connect the OCR1A pin of the USB AVR to the target AVR's + * XTAL1 pin, and set the ISP programming speed to 125KHz (note: other ISP speeds will not work correctly). + * + * 1 Optional, see \ref Sec_Options section - for USB AVRs with ADC modules only \n + * 2 See AUX line related tokens in the \ref Sec_Options section + * + * \section Sec_PDI PDI Connections + * Connections to the device for PDI programming (when enabled): + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Programmer Pin:Target Device Pin:PDI 6 Pin Layout:
Tx/Rx 2DATA1
ADCx 1VTARGET2
N/AN/A3
N/AN/A4
XCKCLOCK5
GNDGND6
+ * + * 1 Optional, see \ref Sec_Options section - for USB AVRs with ADC modules only \n + * 2 The AVR's Tx and Rx become the DATA line when connected together via a pair of 220 ohm resistors \n + * + * \section Sec_TPI TPI Connections + * Connections to the device for TPI programming (when enabled): + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Programmer Pin:Target Device Pin:TPI 6 Pin Layout:
Tx/Rx 2DATA1
ADCx 1VTARGET2
XCK 2CLOCK3
N/AN/A4
PORTx.y 3/RESET5
GNDGND6
+ * + * 1 Optional, see \ref Sec_Options section - for USB AVRs with ADC modules only \n + * 2 The AVR's Tx and Rx become the DATA line when connected together via a pair of 220 ohm resistors \n + * 3 See AUX line related tokens in the \ref Sec_Options section + * + * \section Sec_Options Project Options + * + * The following defines can be found in this project, which can control the project behaviour when defined, or changed in value. + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Define Name:Location:Description:
AUX_LINE_PORTAppConfig.hPORT register for the programmer's AUX target line. The use of this line varies between the programming protocols, + * but is generally used for the target's /RESET line. + * \n \n Ignored when compiled for the XPLAIN board.
AUX_LINE_PINAppConfig.hPIN register for the programmer's AUX target line. The use of this line varies between the programming protocols, + * but is generally used for the target's /RESET line. + * \n \n Ignored when compiled for the XPLAIN board.
AUX_LINE_DDRAppConfig.hDDR register for the programmer's AUX target line. The use of this line varies between the programming protocols, + * but is generally used for the target's /RESET line. + * \n \n Ignored when compiled for the XPLAIN board.
AUX_LINE_MASKAppConfig.hMask for the programmer's AUX target line. The use of this line varies between the programming protocols, + * but is generally used for the target's /RESET line. Must not be the AVR's /SS pin. + * \n \n Ignored when compiled for the XPLAIN board.
VTARGET_ADC_CHANNELAppConfig.hADC channel number (on supported AVRs) to use for VTARGET level detection, if NO_VTARGET_DETECT is not defined. + * \n \n Ignored when compiled for targets lacking an ADC.
ENABLE_ISP_PROTOCOLAppConfig.hDefine to enable SPI programming protocol support. + * \n \n Ignored when compiled for the XPLAIN board.
ENABLE_XPROG_PROTOCOLAppConfig.hDefine to enable PDI and TPI programming protocol support. + * \n \n Ignored when compiled for the XPLAIN board.
NO_VTARGET_DETECTAppConfig.hDefine to disable VTARGET sampling and reporting on AVR models with an ADC converter. This will cause the programmer + * to report a fixed 3.3V target voltage to the host regardless of the real target voltage. + * \n \n Ignored when compiled for targets lacking an ADC.
VTARGET_REF_VOLTSAppConfig.hIndicates the programmer AVR's AVCC reference voltage when measuring the target's supply voltage. Note that the supply + * voltage should never exceed the reference voltage on the programmer AVR without some form of protection to prevent damage + * to the ADC. + * \n \n Ignored when compiled for targets lacking an ADC, or when NO_VTARGET_DETECT is defined.
VTARGET_USE_INTERNAL_REFAppConfig.hSelects the internal 2.56V ADC reference voltage, instead of using the AVR's VREF pin. When enabled, this option will + * override the VTARGET_REF_VOLTS configuration option. + * \n \n Ignored when compiled for targets lacking an ADC, or when NO_VTARGET_DETECT is defined.
VTARGET_SCALE_FACTORAppConfig.hIndicates the target's supply voltage scale factor when applied to the ADC. A simple resistive divider can be used on the + * ADC pin for measuring the target's supply voltage, so that voltages above the programmer AVR's AVCC reference voltage can be + * measured. This should be the reciprocal of the division performed - e.g. if the VTARGET voltage is halved, this should be set + * to 2. + * \n \n Ignored when compiled for targets lacking an ADC, or when NO_VTARGET_DETECT is defined.
XCK_RESCUE_CLOCK_ENABLEAppConfig.hDefine to move the ISP rescue clock to the AVR's XCK pin instead of the OCR1A output pin. This is useful for existing programming + * hardware that does not expose the OCR1A pin of the AVR, but may cause some issues with PDI programming mode.
INVERTED_ISP_MISOAppConfig.hDefine to invert the received data on the ISP MISO line. This is sometimes needed depending on the level translation hardware used, + * if the translator hardware inverts the received logic level.
FIRMWARE_VERSION_MINORAppConfig.hDefine to set the minor firmware revision nunber reported to the host on request. By default this will use a firmware version compatible + * with the latest Atmel IDE version, however if desired the reported minor value can be adjusted here.
+ */ + diff --git a/Projects/AVRISP-MKII/AVRISPDescriptors.c b/Projects/AVRISP-MKII/AVRISPDescriptors.c new file mode 100644 index 0000000000..f4210fbbbf --- /dev/null +++ b/Projects/AVRISP-MKII/AVRISPDescriptors.c @@ -0,0 +1,203 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * USB Device Descriptors, for library use when in USB device mode. Descriptors are special + * computer-readable structures which the host requests upon device enumeration, to determine + * the device's capabilities and functions. + */ + +#include "AVRISPDescriptors.h" + +/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall + * device characteristics, including the supported USB version, control endpoint size and the + * number of device configurations. The descriptor is read out by the USB host when the enumeration + * process begins. + */ +const USB_Descriptor_Device_t PROGMEM AVRISP_DeviceDescriptor = +{ + .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, + + .USBSpecification = VERSION_BCD(1,1,0), + .Class = USB_CSCP_VendorSpecificClass, + .SubClass = USB_CSCP_NoDeviceSubclass, + .Protocol = USB_CSCP_NoDeviceProtocol, + + .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, + + .VendorID = 0x03EB, + .ProductID = 0x2104, + .ReleaseNumber = VERSION_BCD(2,0,0), + + .ManufacturerStrIndex = AVRISP_STRING_ID_Manufacturer, + .ProductStrIndex = AVRISP_STRING_ID_Product, + .SerialNumStrIndex = AVRISP_STRING_ID_Serial, + + .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS +}; + +/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage + * of the device in one of its supported configurations, including information about any device interfaces + * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting + * a configuration so that the host may correctly communicate with the USB device. + */ +const AVRISP_USB_Descriptor_Configuration_t PROGMEM AVRISP_ConfigurationDescriptor = +{ + .Config = + { + .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, + + .TotalConfigurationSize = sizeof(AVRISP_USB_Descriptor_Configuration_t), + .TotalInterfaces = 1, + + .ConfigurationNumber = 1, + .ConfigurationStrIndex = NO_DESCRIPTOR, + + .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), + + .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) + }, + + .AVRISP_Interface = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = INTERFACE_ID_AVRISP, + .AlternateSetting = 0, + + .TotalEndpoints = 2, + + .Class = USB_CSCP_VendorSpecificClass, + .SubClass = USB_CSCP_NoDeviceSubclass, + .Protocol = USB_CSCP_NoDeviceProtocol, + + .InterfaceStrIndex = NO_DESCRIPTOR + }, + + .AVRISP_DataInEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = AVRISP_DATA_IN_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = AVRISP_DATA_EPSIZE, + .PollingIntervalMS = 0x0A + }, + + .AVRISP_DataOutEndpoint = + { + .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = AVRISP_DATA_OUT_EPADDR, + .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = AVRISP_DATA_EPSIZE, + .PollingIntervalMS = 0x0A + }, +}; + +/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests + * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate + * via the language ID table available at USB.org what languages the device supports for its string descriptors. + */ +const USB_Descriptor_String_t PROGMEM AVRISP_LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG); + +/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable + * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device + * Descriptor. + */ +const USB_Descriptor_String_t PROGMEM AVRISP_ManufacturerString = USB_STRING_DESCRIPTOR(L"ATMEL"); + +/** Product descriptor string. This is a Unicode string containing the product's details in human readable form, + * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device + * Descriptor. + */ +const USB_Descriptor_String_t PROGMEM AVRISP_ProductString = USB_STRING_DESCRIPTOR(L"AVRISP mkII"); + +/** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a + * series of uppercase hexadecimal digits. + */ +const USB_Descriptor_String_t PROGMEM AVRISP_SerialString = USB_STRING_DESCRIPTOR(L"000200012345\0" + // Note: Real AVRISP-MKII has the embedded NUL byte, bug in firmware? +); + +/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" + * documentation) by the application code so that the address and size of a requested descriptor can be given + * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function + * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the + * USB host. + */ +uint16_t AVRISP_GetDescriptor(const uint16_t wValue, + const uint16_t wIndex, + const void** const DescriptorAddress) +{ + const uint8_t DescriptorType = (wValue >> 8); + const uint8_t DescriptorNumber = (wValue & 0xFF); + + const void* Address = NULL; + uint16_t Size = NO_DESCRIPTOR; + + switch (DescriptorType) + { + case DTYPE_Device: + Address = &AVRISP_DeviceDescriptor; + Size = sizeof(USB_Descriptor_Device_t); + break; + case DTYPE_Configuration: + Address = &AVRISP_ConfigurationDescriptor; + Size = sizeof(AVRISP_USB_Descriptor_Configuration_t); + break; + case DTYPE_String: + switch (DescriptorNumber) + { + case AVRISP_STRING_ID_Language: + Address = &AVRISP_LanguageString; + Size = pgm_read_byte(&AVRISP_LanguageString.Header.Size); + break; + case AVRISP_STRING_ID_Manufacturer: + Address = &AVRISP_ManufacturerString; + Size = pgm_read_byte(&AVRISP_ManufacturerString.Header.Size); + break; + case AVRISP_STRING_ID_Product: + Address = &AVRISP_ProductString; + Size = pgm_read_byte(&AVRISP_ProductString.Header.Size); + break; + case AVRISP_STRING_ID_Serial: + Address = &AVRISP_SerialString; + Size = AVRISP_SerialString.Header.Size; + break; + } + + break; + } + + *DescriptorAddress = Address; + return Size; +} diff --git a/Projects/AVRISP-MKII/AVRISPDescriptors.h b/Projects/AVRISP-MKII/AVRISPDescriptors.h new file mode 100644 index 0000000000..56dcf25a9b --- /dev/null +++ b/Projects/AVRISP-MKII/AVRISPDescriptors.h @@ -0,0 +1,100 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for Descriptors.c. + */ + +#ifndef _DESCRIPTORS_H_ +#define _DESCRIPTORS_H_ + + /* Includes: */ + #include + + #include + #include + + #include "Config/AppConfig.h" + + /* Macros: */ + /** Endpoint address of the AVRISP data OUT endpoint. */ + #define AVRISP_DATA_OUT_EPADDR (ENDPOINT_DIR_OUT | 2) + + /** Endpoint address of the AVRISP data IN endpoint. */ + #define AVRISP_DATA_IN_EPADDR (ENDPOINT_DIR_IN | 2) + + /** Size in bytes of the AVRISP data endpoint. */ + #define AVRISP_DATA_EPSIZE 64 + + /* Type Defines: */ + /** Type define for the device configuration descriptor structure. This must be defined in the + * application code, as the configuration descriptor contains several sub-descriptors which + * vary between devices, and which describe the device's usage to the host. + */ + typedef struct + { + USB_Descriptor_Configuration_Header_t Config; + + // Atmel AVRISP-MKII Interface + USB_Descriptor_Interface_t AVRISP_Interface; + USB_Descriptor_Endpoint_t AVRISP_DataInEndpoint; + USB_Descriptor_Endpoint_t AVRISP_DataOutEndpoint; + } AVRISP_USB_Descriptor_Configuration_t; + + /** Enum for the device interface descriptor IDs within the device. Each interface descriptor + * should have a unique ID index associated with it, which can be used to refer to the + * interface from other descriptors. + */ + enum InterfaceDescriptors_t + { + INTERFACE_ID_AVRISP = 0, /**< AVRISP interface descriptor ID */ + }; + + /** Enum for the device string descriptor IDs within the device. Each string descriptor should + * have a unique ID index associated with it, which can be used to refer to the string from + * other descriptors. + */ + enum AVRISP_StringDescriptors_t + { + AVRISP_STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */ + AVRISP_STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */ + AVRISP_STRING_ID_Product = 2, /**< Product string ID */ + AVRISP_STRING_ID_Serial = 3, /**< Serial number string ID */ + }; + + /* Function Prototypes: */ + uint16_t AVRISP_GetDescriptor(const uint16_t wValue, + const uint16_t wIndex, + const void** const DescriptorAddress) + ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3); + +#endif + diff --git a/Projects/AVRISP-MKII/Config/AppConfig.h b/Projects/AVRISP-MKII/Config/AppConfig.h new file mode 100644 index 0000000000..a2d15abdc9 --- /dev/null +++ b/Projects/AVRISP-MKII/Config/AppConfig.h @@ -0,0 +1,68 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief Application Configuration Header File + * + * This is a header file which is be used to configure some of + * the application's compile time options, as an alternative to + * specifying the compile time constants supplied through a + * makefile or build system. + * + * For information on what each token does, refer to the + * \ref Sec_Options section of the application documentation. + */ + +#ifndef _APP_CONFIG_H_ +#define _APP_CONFIG_H_ + + #define AUX_LINE_PORT PORTB + #define AUX_LINE_PIN PINB + #define AUX_LINE_DDR DDRB + #if (BOARD == BOARD_U2S) + #define AUX_LINE_MASK (1 << 0) + #else + #define AUX_LINE_MASK (1 << 4) + #endif + + #define ENABLE_ISP_PROTOCOL + #define ENABLE_XPROG_PROTOCOL + + #define VTARGET_ADC_CHANNEL 2 + #define VTARGET_REF_VOLTS 5 + #define VTARGET_SCALE_FACTOR 1 +// #define VTARGET_USE_INTERNAL_REF + #define NO_VTARGET_DETECT +// #define XCK_RESCUE_CLOCK_ENABLE +// #define INVERTED_ISP_MISO + +// #define FIRMWARE_VERSION_MINOR 0x11 + +#endif diff --git a/Projects/AVRISP-MKII/Config/LUFAConfig.h b/Projects/AVRISP-MKII/Config/LUFAConfig.h new file mode 100644 index 0000000000..ed160230ca --- /dev/null +++ b/Projects/AVRISP-MKII/Config/LUFAConfig.h @@ -0,0 +1,93 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief LUFA Library Configuration Header File + * + * This header file is used to configure LUFA's compile time options, + * as an alternative to the compile time constants supplied through + * a makefile. + * + * For information on what each token does, refer to the LUFA + * manual section "Summary of Compile Tokens". + */ + +#ifndef _LUFA_CONFIG_H_ +#define _LUFA_CONFIG_H_ + + #if (ARCH == ARCH_AVR8) + + /* Non-USB Related Configuration Tokens: */ +// #define DISABLE_TERMINAL_CODES + + /* USB Class Driver Related Tokens: */ +// #define HID_HOST_BOOT_PROTOCOL_ONLY +// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} +// #define HID_USAGE_STACK_DEPTH {Insert Value Here} +// #define HID_MAX_COLLECTIONS {Insert Value Here} +// #define HID_MAX_REPORTITEMS {Insert Value Here} +// #define HID_MAX_REPORT_IDS {Insert Value Here} +// #define NO_CLASS_DRIVER_AUTOFLUSH + + /* General USB Driver Related Tokens: */ + #define ORDERED_EP_CONFIG + #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) + #define USB_DEVICE_ONLY +// #define USB_HOST_ONLY +// #define USB_STREAM_TIMEOUT_MS {Insert Value Here} +// #define NO_LIMITED_CONTROLLER_CONNECT + #define NO_SOF_EVENTS + + /* USB Device Mode Driver Related Tokens: */ +// #define USE_RAM_DESCRIPTORS + #define USE_FLASH_DESCRIPTORS +// #define USE_EEPROM_DESCRIPTORS + #define NO_INTERNAL_SERIAL + #define FIXED_CONTROL_ENDPOINT_SIZE 16 + #define DEVICE_STATE_AS_GPIOR 0 + #define FIXED_NUM_CONFIGURATIONS 1 +// #define CONTROL_ONLY_DEVICE +// #define INTERRUPT_CONTROL_ENDPOINT + #define NO_DEVICE_REMOTE_WAKEUP + #define NO_DEVICE_SELF_POWER + + /* USB Host Mode Driver Related Tokens: */ +// #define HOST_STATE_AS_GPIOR {Insert Value Here} +// #define USB_HOST_TIMEOUT_MS {Insert Value Here} +// #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} +// #define NO_AUTO_VBUS_MANAGEMENT +// #define INVERTED_VBUS_ENABLE_LINE + + #else + + #error Unsupported architecture for this LUFA configuration file. + + #endif +#endif diff --git a/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c new file mode 100644 index 0000000000..6553504d5e --- /dev/null +++ b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c @@ -0,0 +1,531 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * ISP Protocol handler, to process V2 Protocol wrapped ISP commands used in Atmel programmer devices. + */ + +#include "ISPProtocol.h" + +#if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__) + +/** Handler for the CMD_ENTER_PROGMODE_ISP command, which attempts to enter programming mode on + * the attached device, returning success or failure back to the host. + */ +void ISPProtocol_EnterISPMode(void) +{ + struct + { + uint8_t TimeoutMS; + uint8_t PinStabDelayMS; + uint8_t ExecutionDelayMS; + uint8_t SynchLoops; + uint8_t ByteDelay; + uint8_t PollValue; + uint8_t PollIndex; + uint8_t EnterProgBytes[4]; + } Enter_ISP_Params; + + Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NULL); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + uint8_t ResponseStatus = STATUS_CMD_FAILED; + + CurrentAddress = 0; + + /* Perform execution delay, initialize SPI bus */ + ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS); + ISPTarget_EnableTargetISP(); + + ISPTarget_ChangeTargetResetLine(true); + ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS); + + /* Continuously attempt to synchronize with the target until either the number of attempts specified + * by the host has exceeded, or the the device sends back the expected response values */ + while (Enter_ISP_Params.SynchLoops-- && TimeoutTicksRemaining) + { + uint8_t ResponseBytes[4]; + + for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++) + { + ISPProtocol_DelayMS(Enter_ISP_Params.ByteDelay); + ResponseBytes[RByte] = ISPTarget_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]); + } + + /* Check if polling disabled, or if the polled value matches the expected value */ + if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue)) + { + ResponseStatus = STATUS_CMD_OK; + break; + } + else + { + ISPTarget_ChangeTargetResetLine(false); + ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS); + ISPTarget_ChangeTargetResetLine(true); + ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS); + } + } + + Endpoint_Write_8(CMD_ENTER_PROGMODE_ISP); + Endpoint_Write_8(ResponseStatus); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */ +void ISPProtocol_LeaveISPMode(void) +{ + struct + { + uint8_t PreDelayMS; + uint8_t PostDelayMS; + } Leave_ISP_Params; + + Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NULL); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */ + ISPProtocol_DelayMS(Leave_ISP_Params.PreDelayMS); + ISPTarget_ChangeTargetResetLine(false); + ISPTarget_DisableTargetISP(); + ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS); + + Endpoint_Write_8(CMD_LEAVE_PROGMODE_ISP); + Endpoint_Write_8(STATUS_CMD_OK); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes, + * words or pages of data to the attached device. + * + * \param[in] V2Command Issued V2 Protocol command byte from the host + */ +void ISPProtocol_ProgramMemory(uint8_t V2Command) +{ + struct + { + uint16_t BytesToWrite; + uint8_t ProgrammingMode; + uint8_t DelayMS; + uint8_t ProgrammingCommands[3]; + uint8_t PollValue1; + uint8_t PollValue2; + uint8_t ProgData[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the + } Write_Memory_Params; // whole page and ACK the packet as fast as possible to prevent it from aborting + + Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) - + sizeof(Write_Memory_Params.ProgData)), NULL); + Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite); + + if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData)) + { + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + Endpoint_Write_8(V2Command); + Endpoint_Write_8(STATUS_CMD_FAILED); + Endpoint_ClearIN(); + return; + } + + Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NULL); + + // The driver will terminate transfers that are a round multiple of the endpoint bank in size with a ZLP, need + // to catch this and discard it before continuing on with packet processing to prevent communication issues + if (((sizeof(uint8_t) + sizeof(Write_Memory_Params) - sizeof(Write_Memory_Params.ProgData)) + + Write_Memory_Params.BytesToWrite) % AVRISP_DATA_EPSIZE == 0) + { + Endpoint_ClearOUT(); + Endpoint_WaitUntilReady(); + } + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + uint8_t ProgrammingStatus = STATUS_CMD_OK; + uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 : + Write_Memory_Params.PollValue2; + uint16_t PollAddress = 0; + uint8_t* NextWriteByte = Write_Memory_Params.ProgData; + uint16_t PageStartAddress = (CurrentAddress & 0xFFFF); + + for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++) + { + uint8_t ByteToWrite = *(NextWriteByte++); + uint8_t ProgrammingMode = Write_Memory_Params.ProgrammingMode; + + /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */ + if (MustLoadExtendedAddress) + { + ISPTarget_LoadExtendedAddress(); + MustLoadExtendedAddress = false; + } + + ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]); + ISPTarget_SendByte(CurrentAddress >> 8); + ISPTarget_SendByte(CurrentAddress & 0xFF); + ISPTarget_SendByte(ByteToWrite); + + /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high + * or low byte at the current word address */ + if (V2Command == CMD_PROGRAM_FLASH_ISP) + Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK; + + /* Check to see if we have a valid polling address */ + if (!(PollAddress) && (ByteToWrite != PollValue)) + { + if ((CurrentByte & 0x01) && (V2Command == CMD_PROGRAM_FLASH_ISP)) + Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK; + else + Write_Memory_Params.ProgrammingCommands[2] &= ~READ_WRITE_HIGH_BYTE_MASK; + + PollAddress = (CurrentAddress & 0xFFFF); + } + + /* If in word programming mode, commit the byte to the target's memory */ + if (!(ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)) + { + /* If the current polling address is invalid, switch to timed delay write completion mode */ + if (!(PollAddress) && !(ProgrammingMode & PROG_MODE_WORD_READYBUSY_MASK)) + ProgrammingMode = (ProgrammingMode & ~PROG_MODE_WORD_VALUE_MASK) | PROG_MODE_WORD_TIMEDELAY_MASK; + + ProgrammingStatus = ISPTarget_WaitForProgComplete(ProgrammingMode, PollAddress, PollValue, + Write_Memory_Params.DelayMS, + Write_Memory_Params.ProgrammingCommands[2]); + + /* Abort the programming loop early if the byte/word programming failed */ + if (ProgrammingStatus != STATUS_CMD_OK) + break; + + /* Must reset the polling address afterwards, so it is not erroneously used for the next byte */ + PollAddress = 0; + } + + /* EEPROM just increments the address each byte, flash needs to increment on each word and + * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended + * address boundary has been crossed during FLASH memory programming */ + if ((CurrentByte & 0x01) || (V2Command == CMD_PROGRAM_EEPROM_ISP)) + { + CurrentAddress++; + + if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF)) + MustLoadExtendedAddress = true; + } + } + + /* If the current page must be committed, send the PROGRAM PAGE command to the target */ + if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK) + { + ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[1]); + ISPTarget_SendByte(PageStartAddress >> 8); + ISPTarget_SendByte(PageStartAddress & 0xFF); + ISPTarget_SendByte(0x00); + + /* Check if polling is enabled and possible, if not switch to timed delay mode */ + if ((Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_VALUE_MASK) && !(PollAddress)) + { + Write_Memory_Params.ProgrammingMode = (Write_Memory_Params.ProgrammingMode & ~PROG_MODE_PAGED_VALUE_MASK) | + PROG_MODE_PAGED_TIMEDELAY_MASK; + } + + ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, + Write_Memory_Params.DelayMS, + Write_Memory_Params.ProgrammingCommands[2]); + + /* Check to see if the FLASH address has crossed the extended address boundary */ + if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF)) + MustLoadExtendedAddress = true; + } + + Endpoint_Write_8(V2Command); + Endpoint_Write_8(ProgrammingStatus); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes, + * words or pages of data from the attached device. + * + * \param[in] V2Command Issued V2 Protocol command byte from the host + */ +void ISPProtocol_ReadMemory(uint8_t V2Command) +{ + struct + { + uint16_t BytesToRead; + uint8_t ReadMemoryCommand; + } Read_Memory_Params; + + Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NULL); + Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + Endpoint_Write_8(V2Command); + Endpoint_Write_8(STATUS_CMD_OK); + + /* Read each byte from the device and write them to the packet for the host */ + for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++) + { + /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */ + if (MustLoadExtendedAddress) + { + ISPTarget_LoadExtendedAddress(); + MustLoadExtendedAddress = false; + } + + /* Read the next byte from the desired memory space in the device */ + ISPTarget_SendByte(Read_Memory_Params.ReadMemoryCommand); + ISPTarget_SendByte(CurrentAddress >> 8); + ISPTarget_SendByte(CurrentAddress & 0xFF); + Endpoint_Write_8(ISPTarget_ReceiveByte()); + + /* Check if the endpoint bank is currently full, if so send the packet */ + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearIN(); + Endpoint_WaitUntilReady(); + } + + /* AVR FLASH addressing requires us to modify the read command based on if we are reading a high + * or low byte at the current word address */ + if (V2Command == CMD_READ_FLASH_ISP) + Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK; + + /* EEPROM just increments the address each byte, flash needs to increment on each word and + * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended + * address boundary has been crossed */ + if ((CurrentByte & 0x01) || (V2Command == CMD_READ_EEPROM_ISP)) + { + CurrentAddress++; + + if ((V2Command != CMD_READ_EEPROM_ISP) && !(CurrentAddress & 0xFFFF)) + MustLoadExtendedAddress = true; + } + } + + Endpoint_Write_8(STATUS_CMD_OK); + + bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed()); + Endpoint_ClearIN(); + + /* Ensure last packet is a short packet to terminate the transfer */ + if (IsEndpointFull) + { + Endpoint_WaitUntilReady(); + Endpoint_ClearIN(); + Endpoint_WaitUntilReady(); + } +} + +/** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */ +void ISPProtocol_ChipErase(void) +{ + struct + { + uint8_t EraseDelayMS; + uint8_t PollMethod; + uint8_t EraseCommandBytes[4]; + } Erase_Chip_Params; + + Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NULL); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + uint8_t ResponseStatus = STATUS_CMD_OK; + + /* Send the chip erase commands as given by the host to the device */ + for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++) + ISPTarget_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]); + + /* Use appropriate command completion check as given by the host (delay or busy polling) */ + if (!(Erase_Chip_Params.PollMethod)) + ISPProtocol_DelayMS(Erase_Chip_Params.EraseDelayMS); + else + ResponseStatus = ISPTarget_WaitWhileTargetBusy(); + + Endpoint_Write_8(CMD_CHIP_ERASE_ISP); + Endpoint_Write_8(ResponseStatus); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands, + * reading the requested configuration byte from the device. + * + * \param[in] V2Command Issued V2 Protocol command byte from the host + */ +void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command) +{ + struct + { + uint8_t RetByte; + uint8_t ReadCommandBytes[4]; + } Read_FuseLockSigOSCCAL_Params; + + Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NULL); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + uint8_t ResponseBytes[4]; + + /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */ + for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++) + ResponseBytes[RByte] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]); + + Endpoint_Write_8(V2Command); + Endpoint_Write_8(STATUS_CMD_OK); + Endpoint_Write_8(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]); + Endpoint_Write_8(STATUS_CMD_OK); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration + * byte to the device. + * + * \param[in] V2Command Issued V2 Protocol command byte from the host + */ +void ISPProtocol_WriteFuseLock(uint8_t V2Command) +{ + struct + { + uint8_t WriteCommandBytes[4]; + } Write_FuseLockSig_Params; + + Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NULL); + + Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + /* Send the Fuse or Lock byte program commands as given by the host to the device */ + for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++) + ISPTarget_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]); + + Endpoint_Write_8(V2Command); + Endpoint_Write_8(STATUS_CMD_OK); + Endpoint_Write_8(STATUS_CMD_OK); + Endpoint_ClearIN(); +} + +/** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */ +void