-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
434 lines (392 loc) · 11.6 KB
/
main.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* Name: main.c
created by chris chung, 2010 April
based on the works found in
v-usb framework http://www.obdev.at/vusb/
Project: Thermostat based on AVR USB driver
Author: Christian Starkjohann
usbtiny isp http://www.xs4all.nl/~dicks/avr/usbtiny/
Dick Streefland
please observe licensing term from the above two projects
Copyright (C) 2010 chris chung
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**** fuse setting,
**** this will blow reset fuse, u will need to use HV programmer to recover if u mess up
avrdude -c usbtiny -p t45 -V -U lfuse:w:0xe1:m -U hfuse:w:0x5d:m -U efuse:w:0xff:m
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include "usbdrv.h"
#include "oddebug.h"
enum
{
// Generic requests
USBTINY_ECHO, // echo test
USBTINY_READ, // read byte (wIndex:address)
USBTINY_WRITE, // write byte (wIndex:address, wValue:value)
USBTINY_CLR, // clear bit (wIndex:address, wValue:bitno)
USBTINY_SET, // set bit (wIndex:address, wValue:bitno)
// Programming requests
USBTINY_POWERUP, // apply power (wValue:SCK-period, wIndex:RESET)
USBTINY_POWERDOWN, // remove power from chip
USBTINY_SPI, // issue SPI command (wValue:c1c0, wIndex:c3c2)
USBTINY_POLL_BYTES, // set poll bytes for write (wValue:p1p2)
USBTINY_FLASH_READ, // read flash (wIndex:address)
USBTINY_FLASH_WRITE, // write flash (wIndex:address, wValue:timeout)
USBTINY_EEPROM_READ, // read eeprom (wIndex:address)
USBTINY_EEPROM_WRITE, // write eeprom (wIndex:address, wValue:timeout)
};
#define PORT PORTB
#define DDR DDRB
#define PIN PINB
//
// to reduce pin count so that this can fit in a 8 pin tiny
// . no power nor ground pins to target, they are to be connected always
// . no reset control pin to target, target reset always grounded
// * this had caused problem and there are two solutions
// 1. provide a toggle switch to off-on-off target reset to ground
// 2. introduce reset control and use reset pin as io
//
#define POWER_MASK 0x00
#define GND_MASK 0x00
#define RESET_MASK (1 << 5)
#define SCK_MASK (1 << 2)
#define MISO_MASK (1 << 1)
#define MOSI_MASK (1 << 0)
// ----------------------------------------------------------------------
// Programmer input pins:
// MISO PD3 (ACK)
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Local data
// ----------------------------------------------------------------------
static uchar sck_period=50; // SCK period in microseconds (1..250)
static uchar poll1; // first poll byte for write
static uchar poll2; // second poll byte for write
static unsigned address; // read/write address
static unsigned timeout; // write timeout in usec
static uchar cmd0; // current read/write command byte
static uchar cmd[4]; // SPI command buffer
static uchar res[4]; // SPI result buffer
// ----------------------------------------------------------------------
// Delay exactly <sck_period> times 0.5 microseconds (6 cycles).
// ----------------------------------------------------------------------
__attribute__((always_inline))
static inline void delay ( void )
{
asm volatile(
" mov __tmp_reg__,%0 \n"
"0: rjmp 1f \n"
"1: nop \n"
"2: nop \n"
"3: nop \n"
" dec __tmp_reg__ \n"
" brne 0b \n"
: : "r" (sck_period) );
}
// ----------------------------------------------------------------------
// Issue one SPI command.
// ----------------------------------------------------------------------
static void spi ( uchar* cmd, uchar* res )
{
uchar i;
uchar c;
uchar r;
uchar mask;
for ( i = 0; i < 4; i++ )
{
c = *cmd++;
r = 0;
for ( mask = 0x80; mask; mask >>= 1 )
{
if ( c & mask )
{
PORT |= MOSI_MASK;
}
delay();
PORT |= SCK_MASK;
delay();
r <<= 1;
if ( PIN & MISO_MASK )
{
r++;
}
PORT &= ~MOSI_MASK;
PORT &= ~SCK_MASK;
}
*res++ = r;
}
}
// ----------------------------------------------------------------------
// Create and issue a read or write SPI command.
// ----------------------------------------------------------------------
static void spi_rw ( void )
{
unsigned a;
a = address++;
if ( cmd0 & 0x80 )
{ // eeprom
a <<= 1;
}
cmd[0] = cmd0;
if ( a & 1 )
{
cmd[0] |= 0x08;
}
cmd[1] = a >> 9;
cmd[2] = a >> 1;
spi( cmd, res );
}
// ----------------------------------------------------------------------
// Handle an IN packet.
// ----------------------------------------------------------------------
uchar usbFunctionRead(uchar *data, uchar len)
{
uchar i;
for ( i = 0; i < len; i++ )
{
spi_rw();
data[i] = res[3];
}
return len;
}
// ----------------------------------------------------------------------
// Handle an OUT packet.
// ----------------------------------------------------------------------
uchar usbFunctionWrite(uchar *data, uchar len)
{
uchar i;
unsigned usec;
uchar r;
//uchar last = (len != 8);
for ( i = 0; i < len; i++ )
{
cmd[3] = data[i];
spi_rw();
cmd[0] ^= 0x60; // turn write into read
//
for ( usec = 0; usec < timeout; usec += 32 * sck_period )
{ // when timeout > 0, poll until byte is written
spi( cmd, res );
r = res[3];
if ( r == cmd[3] && r != poll1 && r != poll2 )
{
break;
}
}
//
}
//return last;
return 1;
}
/* ------------------------------------------------------------------------- */
/* ------------------------ interface to USB driver ------------------------ */
/* ------------------------------------------------------------------------- */
uchar usbFunctionSetup(uchar data[8])
{
// ----------------------------------------------------------------------
// Handle a non-standard SETUP packet.
// ----------------------------------------------------------------------
uchar bit;
uchar mask;
uchar* addr;
uchar req;
// Generic requests
req = data[1];
if ( req == USBTINY_ECHO )
{
usbMsgPtr = data;
return 8;
}
addr = (uchar*) (int) data[4];
if ( req == USBTINY_READ )
{
data[0] = *addr;
usbMsgPtr = data;
return 1;
}
if ( req == USBTINY_WRITE )
{
*addr = data[2];
return 0;
}
bit = data[2] & 7;
mask = 1 << bit;
if ( req == USBTINY_CLR )
{
*addr &= ~ mask;
return 0;
}
if ( req == USBTINY_SET )
{
*addr |= mask;
return 0;
}
// Programming requests
if ( req == USBTINY_POWERUP )
{
sck_period = data[2];
mask = POWER_MASK;
if ( data[4] )
{
mask |= RESET_MASK;
}
DDR &= ~MISO_MASK;
DDR |= (RESET_MASK|SCK_MASK|MOSI_MASK);
PORT &= ~(RESET_MASK|SCK_MASK|MOSI_MASK|MISO_MASK);
return 0;
}
if ( req == USBTINY_POWERDOWN )
{
//PORT |= RESET_MASK;
//DDR &= ~(SCK_MASK|MOSI_MASK);
DDRB = RESET_MASK;
PORTB = RESET_MASK;
return 0;
}
/* have to remove the following check as we strip a lot of io
if ( ! PORT )
{
return 0;
}
*/
if ( req == USBTINY_SPI )
{
spi( data + 2, data + 0 );
usbMsgPtr = data;
return 4;
}
if ( req == USBTINY_POLL_BYTES )
{
poll1 = data[2];
poll2 = data[3];
return 0;
}
address = * (unsigned*) & data[4];
if ( req == USBTINY_FLASH_READ )
{
cmd0 = 0x20;
return 0xff; // usb_in() will be called to get the data
}
if ( req == USBTINY_EEPROM_READ )
{
cmd0 = 0xa0;
return 0xff; // usb_in() will be called to get the data
}
timeout = * (unsigned*) & data[2];
if ( req == USBTINY_FLASH_WRITE )
{
cmd0 = 0x40;
return 0xff; // data will be received by usb_out()
}
if ( req == USBTINY_EEPROM_WRITE )
{
cmd0 = 0xc0;
return 0xff; // data will be received by usb_out()
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* ------------------------ Oscillator Calibration ------------------------- */
/* ------------------------------------------------------------------------- */
/* Calibrate the RC oscillator to 8.25 MHz. The core clock of 16.5 MHz is
* derived from the 66 MHz peripheral clock by dividing. Our timing reference
* is the Start Of Frame signal (a single SE0 bit) available immediately after
* a USB RESET. We first do a binary search for the OSCCAL value and then
* optimize this value with a neighboorhod search.
* This algorithm may also be used to calibrate the RC oscillator directly to
* 12 MHz (no PLL involved, can therefore be used on almost ALL AVRs), but this
* is wide outside the spec for the OSCCAL value and the required precision for
* the 12 MHz clock! Use the RC oscillator calibrated to 12 MHz for
* experimental purposes only!
*/
static void calibrateOscillator(void)
{
uchar step = 128;
uchar trialValue = 0, optimumValue;
int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
/* do a binary search: */
do{
OSCCAL = trialValue + step;
x = usbMeasureFrameLength(); /* proportional to current real frequency */
if(x < targetValue) /* frequency still too low */
trialValue += step;
step >>= 1;
}while(step > 0);
/* We have a precision of +/- 1 for optimum OSCCAL here */
/* now do a neighborhood search for optimum value */
optimumValue = trialValue;
optimumDev = x; /* this is certainly far away from optimum */
for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
x = usbMeasureFrameLength() - targetValue;
if(x < 0)
x = -x;
if(x < optimumDev){
optimumDev = x;
optimumValue = OSCCAL;
}
}
OSCCAL = optimumValue;
}
/*
Note: This calibration algorithm may try OSCCAL values of up to 192 even if
the optimum value is far below 192. It may therefore exceed the allowed clock
frequency of the CPU in low voltage designs!
You may replace this search algorithm with any other algorithm you like if
you have additional constraints such as a maximum CPU clock.
For version 5.x RC oscillators (those with a split range of 2x128 steps, e.g.
ATTiny25, ATTiny45, ATTiny85), it may be useful to search for the optimum in
both regions.
*/
void usbEventResetReady(void)
{
calibrateOscillator();
eeprom_write_byte(0, OSCCAL); /* store the calibrated value in EEPROM */
}
/* ------------------------------------------------------------------------- */
/* --------------------------------- main ---------------------------------- */
/* ------------------------------------------------------------------------- */
int main(void) {
uchar i;
uchar calibrationValue;
//DDRB = (RESET_MASK|SCK_MASK|MOSI_MASK);
DDRB = RESET_MASK;
PORTB = RESET_MASK;
/*
_delay_ms(25);
uchar pgm[] = { 0xac, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
spi(pgm, pgm+4);
*/
calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
if(calibrationValue != 0xff){
OSCCAL = calibrationValue;
}
odDebugInit();
usbDeviceDisconnect();
for(i=0;i<20;i++){ /* 300 ms disconnect */
_delay_ms(15);
}
usbDeviceConnect();
wdt_enable(WDTO_1S);
usbInit();
sei();
for(;;){ /* main event loop */
wdt_reset();
usbPoll();
}
return 0;
}