summaryrefslogtreecommitdiff
path: root/avr/main_jtag.c
blob: b5142d4c67a0aa748845cb7e2434ed8c34e7b31c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/************************************************************************************************
 * Project: USB AVR-ISP
 * Author: Christian Ulrich
 * Contact: christian at ullihome dot de
 *
 * Creation Date: 2008-12-10
 * Copyright: (c) 2008 by Christian Ulrich
 * License: GPLv2 for private use
 *	        commercial use prohibited
 *
 * Changes:
 ***********************************************************************************************/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include "usbdrv.h"
#include "usbconfig.h"
#include "led.h"
#include "timer.h"
#include "main.h"
#include "jtag.h"

#define FUNC_READ_CHAIN 			1
#define FUNC_WRITE_CHAIN 			2
#define FUNC_TAP_STATE  			3
#define FUNC_GET_TAP_STATE 			4
#define FUNC_OPEN 					5
#define FUNC_CLOSE 					6
#define FUNC_SHIFTBITS 				7
#define FUNC_CHAINSIZE 				8
#define FUNC_EXECCHAIN				9

#define FUNC_START_BOOTLOADER		30
#define FUNC_GET_TYPE				0xFE

#define STATE_IDLE					0
#define STATE_READ					1
#define STATE_WRITE					2

uint8_t usb_state = STATE_IDLE;

#ifndef USBASP_COMPATIBLE
led_t leds[] =  {{4,LED_OFF,LED_OFF},
	{3,LED_OFF,LED_OFF},
	{5,LED_OFF,LED_OFF}
};
#else
led_t leds[] =  {{0,LED_OFF,LED_OFF},
	{1,LED_OFF,LED_OFF},
	{3,LED_OFF,LED_OFF}
};
#endif
const uint8_t led_count = sizeof(leds)/sizeof(led_t);


#define MAX_CHAINSIZE 	900
unsigned char JTAG_CHAIN[MAX_CHAINSIZE];
unsigned int chainpos = 0;

int main(void)
{
	extern uchar usbNewDeviceAddr;
	uint8_t i;
	PORTC |= (1<<PC2);
//Reconnect USB
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
	i = 0;
	while(--i)
		_delay_ms(2);
	usbDeviceConnect();
	usbInit();
	sei();


	leds[LED_RED].frequency = LED_ON;
	LED_init();
	for (i=0; i<3; i++)
		TIMER_delay(250);
	leds[LED_RED].frequency = LED_OFF;

	while(1)
	{
		usbPoll();
		LED_poll();
		if(usbNewDeviceAddr)
			leds[LED_BLUE].frequency = LED_ON;
	}
}

static uchar replyBuffer[8];

uint8_t usbFunctionSetup(uint8_t data[8])
{
	uchar len = 0;

	usb_state = STATE_IDLE;
	if(data[1] == FUNC_GET_TYPE)
	{
		replyBuffer[0] = 10;
		len = 1;
	}
	else if(data[1] == FUNC_START_BOOTLOADER)
	{
		cli();
		wdt_enable(WDTO_15MS);
		while(1);
		len = 0;
	}
	else if(data[1] == FUNC_READ_CHAIN)
	{
		chainpos = 0;
		usb_state = STATE_READ;
		len = 0xff;
	}
	else if(data[1] == FUNC_WRITE_CHAIN)
	{
		chainpos = 0;
		usb_state = STATE_WRITE;
		len = 0xff;
	}
	else if(data[1] == FUNC_TAP_STATE)
	{
		JTAG_goto_tap_state(data[2]);
		len = 0;
	}
	else if(data[1] == FUNC_GET_TAP_STATE)
	{
		replyBuffer[0] = JTAG_TAP_STATE;
		len = 1;
	}
	else if(data[1] == FUNC_OPEN)
	{
		leds[LED_BLUE].frequency = LED_ON;
		JTAG_attatch();
		len = 0;
	}
	else if(data[1] == FUNC_CLOSE)
	{
		leds[LED_BLUE].frequency = LED_OFF;
		JTAG_detatch();
		len = 0;
	}
	else if(data[1] == FUNC_SHIFTBITS)
	{
		replyBuffer[0] = JTAG_shift_bits(data[2],data[3]);
		len = 1;
	}
	else if(data[1] == FUNC_CHAINSIZE)
	{
		replyBuffer[0] = chainpos;
		replyBuffer[1] = chainpos>>8;
		len = 2;
	}
	else if (data[1] == FUNC_EXECCHAIN)
	{
		/*	  unsigned char i;
		      for (i = 0; i < len / 2; i++)
		        {
		          unsigned char z = JTAG_CHAIN[i];
		          JTAG_CHAIN[i] = JTAG_CHAIN[len - i - 1];
		          JTAG_CHAIN[len - i - 1] = z;
		        }*/
		JTAG_shift_bytes(JTAG_CHAIN,chainpos);
		leds[LED_GREEN].counter = 10;
		leds[LED_GREEN].frequency = LED_FLASH_NEG;
	}
	leds[LED_BLUE].counter = 10;
	leds[LED_BLUE].frequency = LED_FLASH_NEG;
	usbMsgPtr = replyBuffer;
	return len;
}

uint8_t usbFunctionRead( uint8_t *data, uint8_t len )
{
	if (usb_state != STATE_READ)
		return 0xff;
	uint8_t asize = 0;
	memcpy(data,&JTAG_CHAIN[chainpos],len);
	chainpos += len;
	asize+=len;
	return asize;
}

uint8_t usbFunctionWrite( uint8_t *data, uint8_t len )
{
	if (usb_state != STATE_WRITE)
		return 0xff;
	memcpy(&JTAG_CHAIN[chainpos],data,len);
	chainpos += len;
	return len;
}