├── jtag_commands.h ├── .github └── FUNDING.yml ├── defines.h ├── jtag.h ├── README.md ├── clujtag.h ├── makefile ├── clujtag.inf ├── USBtoSerial.txt ├── USBtoSerial.h ├── Config └── LUFAConfig.h ├── clujtag.c ├── Descriptors.h ├── jtag.c └── Descriptors.c /jtag_commands.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ClusterM/clujtag-avr/HEAD/jtag_commands.h -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [ClusterM] 2 | custom: ["https://www.buymeacoffee.com/cluster", "https://boosty.to/cluster"] 3 | -------------------------------------------------------------------------------- /defines.h: -------------------------------------------------------------------------------- 1 | #define JTAG_PORT B 2 | #define TMS_PIN 0 3 | #define TCK_PIN 1 4 | #define TDO_PIN 2 5 | #define TDI_PIN 3 6 | 7 | #define LED_PORT D 8 | #define LED_PIN 6 9 | 10 | #define ACK_STEP 1024 11 | -------------------------------------------------------------------------------- /jtag.h: -------------------------------------------------------------------------------- 1 | #include "defines.h" 2 | 3 | #define CLUJTAG_CONCAT(a, b) a ## b 4 | #define CLUJTAG_OUTPORT(name) CLUJTAG_CONCAT(PORT, name) 5 | #define CLUJTAG_INPORT(name) CLUJTAG_CONCAT(PIN, name) 6 | #define CLUJTAG_DDRPORT(name) CLUJTAG_CONCAT(DDR, name) 7 | 8 | #define PORT CLUJTAG_OUTPORT(JTAG_PORT) 9 | #define PORT_DDR CLUJTAG_DDRPORT(JTAG_PORT) 10 | #define PORT_PIN CLUJTAG_INPORT(JTAG_PORT) 11 | #define PORT_LED CLUJTAG_OUTPORT(LED_PORT) 12 | #define PORT_LED_DDR CLUJTAG_DDRPORT(LED_PORT) 13 | 14 | void jtag_setup(void); 15 | void jtag_shutdown(void); 16 | int jtag_execute(uint8_t data); 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clujtag 2 | =========== 3 | 4 | Very simple JTAG programmer based on AVR microcontroller with hardware USB. 5 | 6 | You can use it to flash SVF or XSVF files via JTAG interface. 7 | 8 | How to build: 9 | * You need LUFA library, download it here: http://www.fourwalledcubicle.com/LUFA.php 10 | * Edit Makefile, set path to LUFA library and microcontroller type 11 | * Edit *defines.h* to set JTAG pins and optionally led pin 12 | * Compile and flash it 13 | 14 | Device should be detected as virtual serial port. Under Windows use *clujtag.inf* as driver if necessary. Now you can use client to play SVF or XSVF files: https://github.com/ClusterM/clujtag-client 15 | -------------------------------------------------------------------------------- /clujtag.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define CLUJTAG_CONCAT(a, b) a ## b 4 | #define CLUJTAG_OUTPORT(name) CLUJTAG_CONCAT(PORT, name) 5 | #define CLUJTAG_INPORT(name) CLUJTAG_CONCAT(PIN, name) 6 | #define CLUJTAG_DDRPORT(name) CLUJTAG_CONCAT(DDR, name) 7 | 8 | #define PORT CLUJTAG_OUTPORT(JTAG_PORT) 9 | #define PORT_DDR CLUJTAG_DDRPORT(JTAG_PORT) 10 | #define PORT_PIN CLUJTAG_INPORT(JTAG_PORT) 11 | #define PORT_LED CLUJTAG_OUTPORT(LED_PORT) 12 | #define PORT_LED_DDR CLUJTAG_DDRPORT(LED_PORT) 13 | 14 | #ifdef LED_PIN 15 | #define LED_ON PORT_LED |= (1< 23 | * 24 | * USB Mode: 25 | * Device 26 | * 27 | * 28 | * USB Class: 29 | * Communications Device Class (CDC) 30 | * 31 | * 32 | * USB Subclass: 33 | * Abstract Control Model (ACM) 34 | * 35 | * 36 | * Relevant Standards: 37 | * USBIF CDC Class Standard 38 | * 39 | * 40 | * Supported USB Speeds: 41 | * Full Speed Mode 42 | * 43 | * 44 | * 45 | * \section Sec_Description Project Description: 46 | * 47 | * USB to Serial bridge project. This project allows a USB AVR to serve 48 | * as a USB to USART bridge between a USB host and a device lacking a 49 | * USB port. When programmed into a USB AVR, the AVR will enumerate as a 50 | * virtual COM port. 51 | * 52 | * The AVR's hardware USART's settings will change to mirror as closely as 53 | * possible the serial settings set on the host. However, due to hardware 54 | * limitations, some options may not be supported (baud rates with unacceptable 55 | * error rates at the AVR's clock speed, data lengths other than 6, 7 or 8 bits, 56 | * 1.5 stop bits, parity other than none, even or odd). 57 | * 58 | * After running this project for the first time on a new computer, 59 | * you will need to supply the .INF file located in this project 60 | * project's directory as the device's driver when running under 61 | * Windows. This will enable Windows to use its inbuilt CDC drivers, 62 | * negating the need for custom drivers for the device. Other 63 | * Operating Systems should automatically use their own inbuilt 64 | * CDC-ACM drivers. 65 | * 66 | * \section Sec_Options Project Options 67 | * 68 | * The following defines can be found in this project, which can control the project behaviour when defined, or changed in value. 69 | * 70 | * 71 | * 72 | * 75 | * 76 | *
73 | * None 74 | *
77 | */ 78 | 79 | -------------------------------------------------------------------------------- /USBtoSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | LUFA Library 3 | Copyright (C) Dean Camera, 2014. 4 | 5 | dean [at] fourwalledcubicle [dot] com 6 | www.lufa-lib.org 7 | */ 8 | 9 | /* 10 | Copyright 2014 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 disclaims 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 USBtoSerial.c. 34 | */ 35 | 36 | #ifndef _USB_SERIAL_H_ 37 | #define _USB_SERIAL_H_ 38 | 39 | /* Includes: */ 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | #include "Descriptors.h" 46 | 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | /* Macros: */ 54 | /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */ 55 | #define LEDMASK_USB_NOTREADY LEDS_LED1 56 | 57 | /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */ 58 | #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3) 59 | 60 | /** LED mask for the library LED driver, to indicate that the USB interface is ready. */ 61 | #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4) 62 | 63 | /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ 64 | #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3) 65 | 66 | /* Function Prototypes: */ 67 | void SetupHardware(void); 68 | 69 | void EVENT_USB_Device_Connect(void); 70 | void EVENT_USB_Device_Disconnect(void); 71 | void EVENT_USB_Device_ConfigurationChanged(void); 72 | void EVENT_USB_Device_ControlRequest(void); 73 | 74 | void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo); 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /Config/LUFAConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | LUFA Library 3 | Copyright (C) Dean Camera, 2014. 4 | 5 | dean [at] fourwalledcubicle [dot] com 6 | www.lufa-lib.org 7 | */ 8 | 9 | /* 10 | Copyright 2014 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 disclaims 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 | * \brief LUFA Library Configuration Header File 33 | * 34 | * This header file is used to configure LUFA's compile time options, 35 | * as an alternative to the compile time constants supplied through 36 | * a makefile. 37 | * 38 | * For information on what each token does, refer to the LUFA 39 | * manual section "Summary of Compile Tokens". 40 | */ 41 | 42 | #ifndef _LUFA_CONFIG_H_ 43 | #define _LUFA_CONFIG_H_ 44 | 45 | #if (ARCH == ARCH_AVR8) 46 | 47 | /* Non-USB Related Configuration Tokens: */ 48 | // #define DISABLE_TERMINAL_CODES 49 | 50 | /* USB Class Driver Related Tokens: */ 51 | // #define HID_HOST_BOOT_PROTOCOL_ONLY 52 | // #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} 53 | // #define HID_USAGE_STACK_DEPTH {Insert Value Here} 54 | // #define HID_MAX_COLLECTIONS {Insert Value Here} 55 | // #define HID_MAX_REPORTITEMS {Insert Value Here} 56 | // #define HID_MAX_REPORT_IDS {Insert Value Here} 57 | // #define NO_CLASS_DRIVER_AUTOFLUSH 58 | 59 | /* General USB Driver Related Tokens: */ 60 | // #define ORDERED_EP_CONFIG 61 | #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) 62 | #define USB_DEVICE_ONLY 63 | // #define USB_HOST_ONLY 64 | // #define USB_STREAM_TIMEOUT_MS {Insert Value Here} 65 | // #define NO_LIMITED_CONTROLLER_CONNECT 66 | // #define NO_SOF_EVENTS 67 | 68 | /* USB Device Mode Driver Related Tokens: */ 69 | // #define USE_RAM_DESCRIPTORS 70 | #define USE_FLASH_DESCRIPTORS 71 | // #define USE_EEPROM_DESCRIPTORS 72 | // #define NO_INTERNAL_SERIAL 73 | #define FIXED_CONTROL_ENDPOINT_SIZE 8 74 | #define DEVICE_STATE_AS_GPIOR 0 75 | #define FIXED_NUM_CONFIGURATIONS 1 76 | // #define CONTROL_ONLY_DEVICE 77 | #define INTERRUPT_CONTROL_ENDPOINT 78 | // #define NO_DEVICE_REMOTE_WAKEUP 79 | // #define NO_DEVICE_SELF_POWER 80 | 81 | /* USB Host Mode Driver Related Tokens: */ 82 | // #define HOST_STATE_AS_GPIOR 0 83 | // #define USB_HOST_TIMEOUT_MS {Insert Value Here} 84 | // #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} 85 | // #define NO_AUTO_VBUS_MANAGEMENT 86 | // #define INVERTED_VBUS_ENABLE_LINE 87 | 88 | #else 89 | 90 | #error Unsupported architecture for this LUFA configuration file. 91 | 92 | #endif 93 | #endif 94 | -------------------------------------------------------------------------------- /clujtag.c: -------------------------------------------------------------------------------- 1 | #include "USBtoSerial.h" 2 | #include "defines.h" 3 | #include "jtag.h" 4 | #include "jtag_commands.h" 5 | 6 | /** LUFA CDC Class driver interface configuration and state information. This structure is 7 | * passed to all CDC Class driver functions, so that multiple instances of the same class 8 | * within a device can be differentiated from one another. 9 | */ 10 | USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = 11 | { 12 | .Config = 13 | { 14 | .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI, 15 | .DataINEndpoint = 16 | { 17 | .Address = CDC_TX_EPADDR, 18 | .Size = CDC_TXRX_EPSIZE, 19 | .Banks = 1, 20 | }, 21 | .DataOUTEndpoint = 22 | { 23 | .Address = CDC_RX_EPADDR, 24 | .Size = CDC_TXRX_EPSIZE, 25 | .Banks = 1, 26 | }, 27 | .NotificationEndpoint = 28 | { 29 | .Address = CDC_NOTIFICATION_EPADDR, 30 | .Size = CDC_NOTIFICATION_EPSIZE, 31 | .Banks = 1, 32 | }, 33 | }, 34 | }; 35 | 36 | // blocking byte receiving 37 | uint8_t get_usb_byte(void) 38 | { 39 | int16_t ReceivedByte; 40 | do 41 | { 42 | ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); 43 | CDC_Device_USBTask(&VirtualSerial_CDC_Interface); 44 | USB_USBTask(); 45 | } 46 | while (ReceivedByte < 0); 47 | return (uint8_t) ReceivedByte; 48 | } 49 | 50 | /** Main program entry point. This routine contains the overall program flow, including initial 51 | * setup of all components and the main program loop. 52 | */ 53 | 54 | 55 | int main(void) 56 | { 57 | jtag_shutdown(); 58 | #ifdef LED_PIN 59 | #ifdef PORT_LED_DDR 60 | PORT_LED_DDR |= 1<= 0) 74 | CDC_Device_SendByte(&VirtualSerial_CDC_Interface, (uint8_t)res); 75 | CDC_Device_USBTask(&VirtualSerial_CDC_Interface); 76 | USB_USBTask(); 77 | } 78 | } 79 | 80 | /** Configures the board hardware and chip peripherals for the demo's functionality. */ 81 | void SetupHardware(void) 82 | { 83 | #if (ARCH == ARCH_AVR8) 84 | /* Disable watchdog if enabled by bootloader/fuses */ 85 | MCUSR &= ~(1 << WDRF); 86 | wdt_disable(); 87 | 88 | /* Disable clock division */ 89 | clock_prescale_set(clock_div_1); 90 | #endif 91 | 92 | /* Hardware Initialization */ 93 | LEDs_Init(); 94 | USB_Init(); 95 | } 96 | 97 | /** Event handler for the library USB Connection event. */ 98 | void EVENT_USB_Device_Connect(void) 99 | { 100 | LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); 101 | } 102 | 103 | /** Event handler for the library USB Disconnection event. */ 104 | void EVENT_USB_Device_Disconnect(void) 105 | { 106 | LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); 107 | } 108 | 109 | /** Event handler for the library USB Configuration Changed event. */ 110 | void EVENT_USB_Device_ConfigurationChanged(void) 111 | { 112 | bool ConfigSuccess = true; 113 | 114 | ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); 115 | 116 | LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); 117 | } 118 | 119 | /** Event handler for the library USB Control Request reception event. */ 120 | void EVENT_USB_Device_ControlRequest(void) 121 | { 122 | CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); 123 | } 124 | 125 | /** Event handler for the CDC Class driver Line Encoding Changed event. 126 | * 127 | * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced 128 | */ 129 | void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) 130 | { 131 | } 132 | 133 | -------------------------------------------------------------------------------- /Descriptors.h: -------------------------------------------------------------------------------- 1 | /* 2 | LUFA Library 3 | Copyright (C) Dean Camera, 2014. 4 | 5 | dean [at] fourwalledcubicle [dot] com 6 | www.lufa-lib.org 7 | */ 8 | 9 | /* 10 | Copyright 2014 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 disclaims 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 Descriptors.c. 34 | */ 35 | 36 | 37 | #ifndef _DESCRIPTORS_H_ 38 | #define _DESCRIPTORS_H_ 39 | 40 | 41 | 42 | 43 | /* Includes: */ 44 | #include 45 | 46 | #include 47 | 48 | /* Macros: */ 49 | /** Endpoint address of the CDC device-to-host notification IN endpoint. */ 50 | #define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 2) 51 | 52 | /** Endpoint address of the CDC device-to-host data IN endpoint. */ 53 | #define CDC_TX_EPADDR (ENDPOINT_DIR_IN | 3) 54 | 55 | /** Endpoint address of the CDC host-to-device data OUT endpoint. */ 56 | #define CDC_RX_EPADDR (ENDPOINT_DIR_OUT | 4) 57 | 58 | /** Size in bytes of the CDC device-to-host notification IN endpoint. */ 59 | #define CDC_NOTIFICATION_EPSIZE 8 60 | 61 | /** Size in bytes of the CDC data IN and OUT endpoints. */ 62 | #define CDC_TXRX_EPSIZE 16 63 | 64 | /* Type Defines: */ 65 | /** Type define for the device configuration descriptor structure. This must be defined in the 66 | * application code, as the configuration descriptor contains several sub-descriptors which 67 | * vary between devices, and which describe the device's usage to the host. 68 | */ 69 | typedef struct 70 | { 71 | USB_Descriptor_Configuration_Header_t Config; 72 | 73 | // CDC Command Interface 74 | USB_Descriptor_Interface_t CDC_CCI_Interface; 75 | USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header; 76 | USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM; 77 | USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union; 78 | USB_Descriptor_Endpoint_t CDC_NotificationEndpoint; 79 | 80 | // CDC Data Interface 81 | USB_Descriptor_Interface_t CDC_DCI_Interface; 82 | USB_Descriptor_Endpoint_t CDC_DataOutEndpoint; 83 | USB_Descriptor_Endpoint_t CDC_DataInEndpoint; 84 | } USB_Descriptor_Configuration_t; 85 | 86 | /** Enum for the device interface descriptor IDs within the device. Each interface descriptor 87 | * should have a unique ID index associated with it, which can be used to refer to the 88 | * interface from other descriptors. 89 | */ 90 | enum InterfaceDescriptors_t 91 | { 92 | INTERFACE_ID_CDC_CCI = 0, /**< CDC CCI interface descriptor ID */ 93 | INTERFACE_ID_CDC_DCI = 1, /**< CDC DCI interface descriptor ID */ 94 | }; 95 | 96 | /** Enum for the device string descriptor IDs within the device. Each string descriptor should 97 | * have a unique ID index associated with it, which can be used to refer to the string from 98 | * other descriptors. 99 | */ 100 | enum StringDescriptors_t 101 | { 102 | STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */ 103 | STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */ 104 | STRING_ID_Product = 2, /**< Product string ID */ 105 | }; 106 | 107 | /* Function Prototypes: */ 108 | uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, 109 | const uint8_t wIndex, 110 | const void** const DescriptorAddress) 111 | ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3); 112 | 113 | 114 | #endif 115 | 116 | -------------------------------------------------------------------------------- /jtag.c: -------------------------------------------------------------------------------- 1 | #include "defines.h" 2 | #include 3 | #include 4 | #include 5 | #include "clujtag.h" 6 | #include "jtag.h" 7 | #include "jtag_commands.h" 8 | 9 | static uint8_t jtag_current_command = 0; 10 | static uint16_t jtag_pos = 0; 11 | static uint32_t jtag_tck_delay_value = 0; 12 | static uint32_t jtag_num_tck = 0; 13 | static uint32_t jtag_usecs = 0; 14 | static uint16_t jtag_multi_count = 0; 15 | static uint16_t jtag_multi_pos = 0; 16 | static uint16_t jtag_multi_multi = 0; 17 | 18 | static int byte_counter = 0; 19 | static uint8_t ok = 0; 20 | static uint8_t setup_counter = 0; 21 | 22 | static void tck_delay(uint8_t tms, uint32_t num_tck, uint32_t usecs) 23 | { 24 | if (tms) 25 | PORT |= (1<= 0) 46 | { 47 | if (tdi) 48 | PORT |= (1<> TDO_PIN) & 1) == tdo; 56 | } 57 | 58 | int jtag_parse_byte(uint8_t data) 59 | { 60 | int i, tms, tdi, tdo; 61 | switch (jtag_current_command) 62 | { 63 | case 0: 64 | jtag_current_command = data; 65 | jtag_pos = 0; 66 | break; 67 | 68 | case JTAG_PULSE_TCK_DELAY: 69 | if (jtag_pos == 0) 70 | { 71 | jtag_num_tck = 0; 72 | jtag_usecs = 0; 73 | jtag_tck_delay_value = data; 74 | } else { 75 | if ((jtag_pos < 4) && (jtag_tck_delay_value & 0b10)) // num_tck 76 | { 77 | jtag_num_tck |= (uint32_t)data << (8*(jtag_pos-1)); 78 | } else if ((jtag_pos < 4) && !(jtag_tck_delay_value & 0b10)) // usecs 79 | { 80 | jtag_usecs |= (uint32_t)data << (8*(jtag_pos-1)); 81 | } else { 82 | jtag_usecs |= (uint32_t)data << (8*(jtag_pos-5)); 83 | } 84 | } 85 | if ( ((jtag_tck_delay_value & 0b110) == 0) 86 | || (((((jtag_tck_delay_value & 0b110) == 0b100) || (jtag_tck_delay_value & 0b110) == 0b010)) && jtag_pos == 4) 87 | || (((jtag_tck_delay_value & 0b110) == 0b110) && jtag_pos == 8) 88 | ) 89 | { 90 | tck_delay(jtag_tck_delay_value&1, jtag_num_tck, jtag_usecs); 91 | jtag_current_command = 0; 92 | } 93 | jtag_pos++; 94 | break; 95 | 96 | case JTAG_PULSE_TCK_MULTI: 97 | if (jtag_pos == 0) 98 | { 99 | jtag_multi_count = data; 100 | jtag_multi_pos = 0; 101 | jtag_multi_multi = 0; 102 | } else if (jtag_pos == 1) 103 | { 104 | jtag_multi_count |= (uint16_t)data << 8; 105 | } else { 106 | if (!jtag_multi_multi && !(data & 0x80)) 107 | { 108 | jtag_multi_multi = data; 109 | } else { 110 | if (!jtag_multi_multi) jtag_multi_multi = 1; 111 | tms = data&1; 112 | tdi = (data>>1)&1; 113 | tdo = -1; 114 | if (data & (1<<2)) 115 | { 116 | tdo = (data>>3)&1; 117 | } 118 | for (i = 0; i < jtag_multi_multi; i++) 119 | { 120 | if (!pulse_tck(tms, tdi, tdo)) return 0; 121 | jtag_multi_pos++; 122 | } 123 | jtag_multi_multi = 0; 124 | } 125 | if (jtag_multi_pos >= jtag_multi_count) 126 | jtag_current_command = 0; 127 | } 128 | jtag_pos++; 129 | break; 130 | 131 | default: 132 | return 0; 133 | } 134 | return 1; 135 | } 136 | 137 | void jtag_setup(void) 138 | { 139 | PORT &= ~((1<= 16) 159 | { 160 | jtag_setup(); 161 | ok = 1; 162 | byte_counter = 0; 163 | return -1; 164 | } 165 | } else setup_counter = 0; 166 | 167 | if (jtag_current_command == 0 && data == JTAG_SHUTDOWN) 168 | { 169 | jtag_shutdown(); 170 | byte_counter = ACK_STEP; 171 | LED_OFF; 172 | } else 173 | if (jtag_current_command == 0 && data == JTAG_TDO_REQUEST) 174 | { 175 | byte_counter = 0; 176 | return (PORT_PIN >> TDO_PIN) & 1; 177 | } else 178 | if (ok && !jtag_parse_byte(data)) 179 | { 180 | ok = 0; 181 | jtag_shutdown(); 182 | } 183 | 184 | byte_counter++; 185 | if (byte_counter >= ACK_STEP) 186 | { 187 | byte_counter = 0; 188 | return ok; 189 | } 190 | 191 | LED_OFF; 192 | return -1; 193 | } 194 | -------------------------------------------------------------------------------- /Descriptors.c: -------------------------------------------------------------------------------- 1 | /* 2 | LUFA Library 3 | Copyright (C) Dean Camera, 2014. 4 | 5 | dean [at] fourwalledcubicle [dot] com 6 | www.lufa-lib.org 7 | */ 8 | 9 | /* 10 | Copyright 2014 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 disclaims 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 | * USB Device Descriptors, for library use when in USB device mode. Descriptors are special 34 | * computer-readable structures which the host requests upon device enumeration, to determine 35 | * the device's capabilities and functions. 36 | */ 37 | 38 | #include "Descriptors.h" 39 | 40 | 41 | /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall 42 | * device characteristics, including the supported USB version, control endpoint size and the 43 | * number of device configurations. The descriptor is read out by the USB host when the enumeration 44 | * process begins. 45 | */ 46 | const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = 47 | { 48 | .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, 49 | 50 | .USBSpecification = VERSION_BCD(1,1,0), 51 | .Class = CDC_CSCP_CDCClass, 52 | .SubClass = CDC_CSCP_NoSpecificSubclass, 53 | .Protocol = CDC_CSCP_NoSpecificProtocol, 54 | 55 | .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, 56 | 57 | .VendorID = 0xF7C0, 58 | .ProductID = 0xDF08, 59 | .ReleaseNumber = VERSION_BCD(0,0,1), 60 | 61 | .ManufacturerStrIndex = STRING_ID_Manufacturer, 62 | .ProductStrIndex = STRING_ID_Product, 63 | .SerialNumStrIndex = USE_INTERNAL_SERIAL, 64 | 65 | .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS 66 | }; 67 | 68 | /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage 69 | * of the device in one of its supported configurations, including information about any device interfaces 70 | * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting 71 | * a configuration so that the host may correctly communicate with the USB device. 72 | */ 73 | const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = 74 | { 75 | .Config = 76 | { 77 | .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, 78 | 79 | .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), 80 | .TotalInterfaces = 2, 81 | 82 | .ConfigurationNumber = 1, 83 | .ConfigurationStrIndex = NO_DESCRIPTOR, 84 | 85 | .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), 86 | 87 | .MaxPowerConsumption = USB_CONFIG_POWER_MA(100) 88 | }, 89 | 90 | .CDC_CCI_Interface = 91 | { 92 | .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, 93 | 94 | .InterfaceNumber = INTERFACE_ID_CDC_CCI, 95 | .AlternateSetting = 0, 96 | 97 | .TotalEndpoints = 1, 98 | 99 | .Class = CDC_CSCP_CDCClass, 100 | .SubClass = CDC_CSCP_ACMSubclass, 101 | .Protocol = CDC_CSCP_ATCommandProtocol, 102 | 103 | .InterfaceStrIndex = NO_DESCRIPTOR 104 | }, 105 | 106 | .CDC_Functional_Header = 107 | { 108 | .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface}, 109 | .Subtype = CDC_DSUBTYPE_CSInterface_Header, 110 | 111 | .CDCSpecification = VERSION_BCD(1,1,0), 112 | }, 113 | 114 | .CDC_Functional_ACM = 115 | { 116 | .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface}, 117 | .Subtype = CDC_DSUBTYPE_CSInterface_ACM, 118 | 119 | .Capabilities = 0x06, 120 | }, 121 | 122 | .CDC_Functional_Union = 123 | { 124 | .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface}, 125 | .Subtype = CDC_DSUBTYPE_CSInterface_Union, 126 | 127 | .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI, 128 | .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI, 129 | }, 130 | 131 | .CDC_NotificationEndpoint = 132 | { 133 | .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, 134 | 135 | .EndpointAddress = CDC_NOTIFICATION_EPADDR, 136 | .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), 137 | .EndpointSize = CDC_NOTIFICATION_EPSIZE, 138 | .PollingIntervalMS = 0xFF 139 | }, 140 | 141 | .CDC_DCI_Interface = 142 | { 143 | .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, 144 | 145 | .InterfaceNumber = INTERFACE_ID_CDC_DCI, 146 | .AlternateSetting = 0, 147 | 148 | .TotalEndpoints = 2, 149 | 150 | .Class = CDC_CSCP_CDCDataClass, 151 | .SubClass = CDC_CSCP_NoDataSubclass, 152 | .Protocol = CDC_CSCP_NoDataProtocol, 153 | 154 | .InterfaceStrIndex = NO_DESCRIPTOR 155 | }, 156 | 157 | .CDC_DataOutEndpoint = 158 | { 159 | .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, 160 | 161 | .EndpointAddress = CDC_RX_EPADDR, 162 | .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), 163 | .EndpointSize = CDC_TXRX_EPSIZE, 164 | .PollingIntervalMS = 0x05 165 | }, 166 | 167 | .CDC_DataInEndpoint = 168 | { 169 | .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, 170 | 171 | .EndpointAddress = CDC_TX_EPADDR, 172 | .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), 173 | .EndpointSize = CDC_TXRX_EPSIZE, 174 | .PollingIntervalMS = 0x05 175 | } 176 | }; 177 | 178 | /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests 179 | * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate 180 | * via the language ID table available at USB.org what languages the device supports for its string descriptors. 181 | */ 182 | const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG); 183 | 184 | /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable 185 | * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 186 | * Descriptor. 187 | */ 188 | const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Cluster"); 189 | 190 | /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, 191 | * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device 192 | * Descriptor. 193 | */ 194 | const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Cluster's JTAG Programmer"); 195 | 196 | /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" 197 | * documentation) by the application code so that the address and size of a requested descriptor can be given 198 | * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function 199 | * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the 200 | * USB host. 201 | */ 202 | uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, 203 | const uint8_t wIndex, 204 | const void** const DescriptorAddress) 205 | { 206 | const uint8_t DescriptorType = (wValue >> 8); 207 | const uint8_t DescriptorNumber = (wValue & 0xFF); 208 | 209 | const void* Address = NULL; 210 | uint16_t Size = NO_DESCRIPTOR; 211 | 212 | switch (DescriptorType) 213 | { 214 | case DTYPE_Device: 215 | Address = &DeviceDescriptor; 216 | Size = sizeof(USB_Descriptor_Device_t); 217 | break; 218 | case DTYPE_Configuration: 219 | Address = &ConfigurationDescriptor; 220 | Size = sizeof(USB_Descriptor_Configuration_t); 221 | break; 222 | case DTYPE_String: 223 | switch (DescriptorNumber) 224 | { 225 | case STRING_ID_Language: 226 | Address = &LanguageString; 227 | Size = pgm_read_byte(&LanguageString.Header.Size); 228 | break; 229 | case STRING_ID_Manufacturer: 230 | Address = &ManufacturerString; 231 | Size = pgm_read_byte(&ManufacturerString.Header.Size); 232 | break; 233 | case STRING_ID_Product: 234 | Address = &ProductString; 235 | Size = pgm_read_byte(&ProductString.Header.Size); 236 | break; 237 | } 238 | 239 | break; 240 | } 241 | 242 | *DescriptorAddress = Address; 243 | return Size; 244 | } 245 | 246 | --------------------------------------------------------------------------------