usbdrv/usbdrvasm20.inc

Fri, 17 Feb 2017 13:26:39 +0100

author
mbayer
date
Fri, 17 Feb 2017 13:26:39 +0100
changeset 6
b84252fe69c6
parent 0
9e9b2c78bd31
permissions
-rw-r--r--

changes to run client on windows platform too via libusb dev

0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
1 /* Name: usbdrvasm20.inc
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
2 * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
3 * Author: Jeroen Benschop
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
4 * Based on usbdrvasm16.inc from Christian Starkjohann
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
5 * Creation Date: 2008-03-05
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
6 * Tabsize: 4
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
7 * Copyright: (c) 2008 by Jeroen Benschop and OBJECTIVE DEVELOPMENT Software GmbH
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
8 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
9 */
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
10
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
11 /* Do not link this file! Link usbdrvasm.S instead, which includes the
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
12 * appropriate implementation!
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
13 */
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
14
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
15 /*
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
16 General Description:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
17 This file is the 20 MHz version of the asssembler part of the USB driver. It
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
18 requires a 20 MHz crystal (not a ceramic resonator and not a calibrated RC
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
19 oscillator).
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
20
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
21 See usbdrv.h for a description of the entire driver.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
22
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
23 Since almost all of this code is timing critical, don't change unless you
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
24 really know what you are doing! Many parts require not only a maximum number
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
25 of CPU cycles, but even an exact number of cycles!
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
26 */
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
27
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
28 #define leap2 x3
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
29 #ifdef __IAR_SYSTEMS_ASM__
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
30 #define nextInst $+2
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
31 #else
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
32 #define nextInst .+0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
33 #endif
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
34
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
35 ;max stack usage: [ret(2), YL, SREG, YH, bitcnt, shift, x1, x2, x3, x4, cnt] = 12 bytes
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
36 ;nominal frequency: 20 MHz -> 13.333333 cycles per bit, 106.666667 cycles per byte
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
37 ; Numbers in brackets are clocks counted from center of last sync bit
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
38 ; when instruction starts
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
39 ;register use in receive loop:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
40 ; shift assembles the byte currently being received
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
41 ; x1 holds the D+ and D- line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
42 ; x2 holds the previous line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
43 ; x4 (leap) is used to add a leap cycle once every three bytes received
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
44 ; X3 (leap2) is used to add a leap cycle once every three stuff bits received
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
45 ; bitcnt is used to determine when a stuff bit is due
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
46 ; cnt holds the number of bytes left in the receive buffer
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
47
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
48 USB_INTR_VECTOR:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
49 ;order of registers pushed: YL, SREG YH, [sofError], bitcnt, shift, x1, x2, x3, x4, cnt
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
50 push YL ;[-28] push only what is necessary to sync with edge ASAP
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
51 in YL, SREG ;[-26]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
52 push YL ;[-25]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
53 push YH ;[-23]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
54 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
55 ; Synchronize with sync pattern:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
56 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
57 ;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
58 ;sync up with J to K edge during sync pattern -- use fastest possible loops
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
59 ;The first part waits at most 1 bit long since we must be in sync pattern.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
60 ;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
61 ;waitForJ, ensure that this prerequisite is met.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
62 waitForJ:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
63 inc YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
64 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
65 brne waitForJ ; just make sure we have ANY timeout
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
66 waitForK:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
67 ;The following code results in a sampling window of < 1/4 bit which meets the spec.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
68 sbis USBIN, USBMINUS ;[-19]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
69 rjmp foundK ;[-18]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
70 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
71 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
72 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
73 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
74 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
75 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
76 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
77 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
78 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
79 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
80 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
81 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
82 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
83 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
84 sbis USBIN, USBMINUS
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
85 rjmp foundK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
86 #if USB_COUNT_SOF
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
87 lds YL, usbSofCount
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
88 inc YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
89 sts usbSofCount, YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
90 #endif /* USB_COUNT_SOF */
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
91 #ifdef USB_SOF_HOOK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
92 USB_SOF_HOOK
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
93 #endif
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
94 rjmp sofError
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
95 foundK: ;[-16]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
96 ;{3, 5} after falling D- edge, average delay: 4 cycles
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
97 ;bit0 should be at 34 for center sampling. Currently at 4 so 30 cylces till bit 0 sample
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
98 ;use 1 bit time for setup purposes, then sample again. Numbers in brackets
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
99 ;are cycles from center of first sync (double K) bit after the instruction
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
100 push bitcnt ;[-16]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
101 ; [---] ;[-15]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
102 lds YL, usbInputBufOffset;[-14]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
103 ; [---] ;[-13]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
104 clr YH ;[-12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
105 subi YL, lo8(-(usbRxBuf));[-11] [rx loop init]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
106 sbci YH, hi8(-(usbRxBuf));[-10] [rx loop init]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
107 push shift ;[-9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
108 ; [---] ;[-8]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
109 ldi shift,0x40 ;[-7] set msb to "1" so processing bit7 can be detected
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
110 nop2 ;[-6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
111 ; [---] ;[-5]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
112 ldi bitcnt, 5 ;[-4] [rx loop init]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
113 sbis USBIN, USBMINUS ;[-3] we want two bits K (sample 3 cycles too early)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
114 rjmp haveTwoBitsK ;[-2]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
115 pop shift ;[-1] undo the push from before
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
116 pop bitcnt ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
117 rjmp waitForK ;[3] this was not the end of sync, retry
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
118 ; The entire loop from waitForK until rjmp waitForK above must not exceed two
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
119 ; bit times (= 27 cycles).
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
120
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
121 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
122 ; push more registers and initialize values while we sample the first bits:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
123 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
124 haveTwoBitsK:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
125 push x1 ;[0]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
126 push x2 ;[2]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
127 push x3 ;[4] (leap2)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
128 ldi leap2, 0x55 ;[6] add leap cycle on 2nd,5th,8th,... stuff bit
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
129 push x4 ;[7] == leap
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
130 ldi leap, 0x55 ;[9] skip leap cycle on 2nd,5th,8th,... byte received
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
131 push cnt ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
132 ldi cnt, USB_BUFSIZE ;[12] [rx loop init]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
133 ldi x2, 1<<USBPLUS ;[13] current line state is K state. D+=="1", D-=="0"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
134 bit0:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
135 in x1, USBIN ;[0] sample line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
136 andi x1, USBMASK ;[1] filter only D+ and D- bits
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
137 rjmp handleBit ;[2] make bit0 14 cycles long
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
138
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
139 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
140 ; Process bit7. However, bit 6 still may need unstuffing.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
141 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
142
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
143 b6checkUnstuff:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
144 dec bitcnt ;[9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
145 breq unstuff6 ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
146 bit7:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
147 subi cnt, 1 ;[11] cannot use dec becaus it does not affect the carry flag
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
148 brcs overflow ;[12] Too many bytes received. Ignore packet
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
149 in x1, USBIN ;[0] sample line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
150 andi x1, USBMASK ;[1] filter only D+ and D- bits
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
151 cpse x1, x2 ;[2] when previous line state equals current line state, handle "1"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
152 rjmp b7handle0 ;[3] when line state differs, handle "0"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
153 sec ;[4]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
154 ror shift ;[5] shift "1" into the data
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
155 st y+, shift ;[6] store the data into the buffer
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
156 ldi shift, 0x40 ;[7] reset data for receiving the next byte
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
157 subi leap, 0x55 ;[9] trick to introduce a leap cycle every 3 bytes
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
158 brcc nextInst ;[10 or 11] it will fail after 85 bytes. However low speed can only receive 11
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
159 dec bitcnt ;[11 or 12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
160 brne bit0 ;[12 or 13]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
161 ldi x1, 1 ;[13 or 14] unstuffing bit 7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
162 in bitcnt, USBIN ;[0] sample stuff bit
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
163 rjmp unstuff ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
164
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
165 b7handle0:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
166 mov x2,x1 ;[5] Set x2 to current line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
167 ldi bitcnt, 6 ;[6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
168 lsr shift ;[7] shift "0" into the data
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
169 st y+, shift ;[8] store data into the buffer
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
170 ldi shift, 0x40 ;[10] reset data for receiving the next byte
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
171 subi leap, 0x55 ;[11] trick to introduce a leap cycle every 3 bytes
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
172 brcs bit0 ;[12] it will fail after 85 bytes. However low speed can only receive 11
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
173 rjmp bit0 ;[13]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
174
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
175
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
176 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
177 ; Handle unstuff
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
178 ; x1==0xFF indicate unstuffing bit6
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
179 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
180
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
181 unstuff6:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
182 ldi x1,0xFF ;[12] indicate unstuffing bit 6
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
183 in bitcnt, USBIN ;[0] sample stuff bit
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
184 nop ;[1] fix timing
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
185 unstuff: ;b0-5 b6 b7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
186 mov x2,bitcnt ;[3] [2] [3] Set x2 to match line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
187 subi leap2, 0x55 ;[4] [3] [4] delay loop
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
188 brcs nextInst ;[5] [4] [5] add one cycle every three stuff bits
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
189 sbci leap2,0 ;[6] [5] [6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
190 ldi bitcnt,6 ;[7] [6] [7] reset bit stuff counter
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
191 andi x2, USBMASK ;[8] [7] [8] only keep D+ and D-
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
192 cpi x1,0 ;[9] [8] [9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
193 brmi bit7 ;[10] [9] [10] finished unstuffing bit6 When x1<0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
194 breq bitloop ;[11] --- [11] finished unstuffing bit0-5 when x1=0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
195 nop ;--- --- [12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
196 in x1, USBIN ;--- --- [0] sample line state for bit0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
197 andi x1, USBMASK ;--- --- [1] filter only D+ and D- bits
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
198 rjmp handleBit ;--- --- [2] make bit0 14 cycles long
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
199
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
200 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
201 ; Receiver loop (numbers in brackets are cycles within byte after instr)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
202 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
203 bitloop:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
204 in x1, USBIN ;[0] sample line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
205 andi x1, USBMASK ;[1] filter only D+ and D- bits
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
206 breq se0 ;[2] both lines are low so handle se0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
207 handleBit:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
208 cpse x1, x2 ;[3] when previous line state equals current line state, handle "1"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
209 rjmp handle0 ;[4] when line state differs, handle "0"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
210 sec ;[5]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
211 ror shift ;[6] shift "1" into the data
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
212 brcs b6checkUnstuff ;[7] When after shift C is set, next bit is bit7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
213 nop2 ;[8]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
214 dec bitcnt ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
215 brne bitloop ;[11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
216 ldi x1,0 ;[12] indicate unstuff for bit other than bit6 or bit7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
217 in bitcnt, USBIN ;[0] sample stuff bit
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
218 rjmp unstuff ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
219
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
220 handle0:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
221 mov x2, x1 ;[6] Set x2 to current line state
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
222 ldi bitcnt, 6 ;[7] reset unstuff counter.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
223 lsr shift ;[8] shift "0" into the data
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
224 brcs bit7 ;[9] When after shift C is set, next bit is bit7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
225 nop ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
226 rjmp bitloop ;[11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
227
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
228 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
229 ; End of receive loop. Now start handling EOP
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
230 ;----------------------------------------------------------------------------
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
231
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
232 macro POP_STANDARD ; 14 cycles
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
233 pop cnt
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
234 pop x4
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
235 pop x3
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
236 pop x2
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
237 pop x1
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
238 pop shift
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
239 pop bitcnt
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
240 endm
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
241 macro POP_RETI ; 7 cycles
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
242 pop YH
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
243 pop YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
244 out SREG, YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
245 pop YL
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
246 endm
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
247
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
248
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
249
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
250 #include "asmcommon.inc"
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
251
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
252 ; USB spec says:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
253 ; idle = J
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
254 ; J = (D+ = 0), (D- = 1)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
255 ; K = (D+ = 1), (D- = 0)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
256 ; Spec allows 7.5 bit times from EOP to SOP for replies
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
257 ; 7.5 bit times is 100 cycles. This implementation arrives a bit later at se0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
258 ; then specified in the include file but there is plenty of time
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
259
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
260 bitstuffN:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
261 eor x1, x4 ;[8]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
262 ldi x2, 0 ;[9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
263 nop2 ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
264 out USBOUT, x1 ;[12] <-- out
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
265 rjmp didStuffN ;[0]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
266
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
267 bitstuff7:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
268 eor x1, x4 ;[6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
269 ldi x2, 0 ;[7] Carry is zero due to brcc
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
270 rol shift ;[8] compensate for ror shift at branch destination
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
271 nop2 ;[9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
272 rjmp didStuff7 ;[11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
273
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
274 sendNakAndReti:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
275 ldi x3, USBPID_NAK ;[-18]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
276 rjmp sendX3AndReti ;[-17]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
277 sendAckAndReti:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
278 ldi cnt, USBPID_ACK ;[-17]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
279 sendCntAndReti:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
280 mov x3, cnt ;[-16]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
281 sendX3AndReti:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
282 ldi YL, 20 ;[-15] x3==r20 address is 20
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
283 ldi YH, 0 ;[-14]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
284 ldi cnt, 2 ;[-13]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
285 ; rjmp usbSendAndReti fallthrough
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
286
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
287 ;usbSend:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
288 ;pointer to data in 'Y'
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
289 ;number of bytes in 'cnt' -- including sync byte [range 2 ... 12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
290 ;uses: x1...x4, btcnt, shift, cnt, Y
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
291 ;Numbers in brackets are time since first bit of sync pattern is sent
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
292 ;We don't match the transfer rate exactly (don't insert leap cycles every third
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
293 ;byte) because the spec demands only 1.5% precision anyway.
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
294 usbSendAndReti: ; 12 cycles until SOP
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
295 in x2, USBDDR ;[-12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
296 ori x2, USBMASK ;[-11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
297 sbi USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
298 in x1, USBOUT ;[-8] port mirror for tx loop
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
299 out USBDDR, x2 ;[-7] <- acquire bus
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
300 ; need not init x2 (bitstuff history) because sync starts with 0
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
301 ldi x4, USBMASK ;[-6] exor mask
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
302 ldi shift, 0x80 ;[-5] sync byte is first byte sent
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
303 txByteLoop:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
304 ldi bitcnt, 0x49 ;[-4] [10] binary 01001001
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
305 txBitLoop:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
306 sbrs shift, 0 ;[-3] [10] [11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
307 eor x1, x4 ;[-2] [11] [12]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
308 out USBOUT, x1 ;[-1] [12] [13] <-- out N
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
309 ror shift ;[0] [13] [14]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
310 ror x2 ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
311 didStuffN:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
312 nop2 ;[2]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
313 nop ;[4]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
314 cpi x2, 0xfc ;[5]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
315 brcc bitstuffN ;[6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
316 lsr bitcnt ;[7]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
317 brcc txBitLoop ;[8]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
318 brne txBitLoop ;[9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
319
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
320 sbrs shift, 0 ;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
321 eor x1, x4 ;[11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
322 didStuff7:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
323 out USBOUT, x1 ;[-1] [13] <-- out 7
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
324 ror shift ;[0] [14]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
325 ror x2 ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
326 nop ;[2]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
327 cpi x2, 0xfc ;[3]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
328 brcc bitstuff7 ;[4]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
329 ld shift, y+ ;[5]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
330 dec cnt ;[7]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
331 brne txByteLoop ;[8]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
332 ;make SE0:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
333 cbr x1, USBMASK ;[9] prepare SE0 [spec says EOP may be 25 to 30 cycles]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
334 lds x2, usbNewDeviceAddr;[10]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
335 lsl x2 ;[12] we compare with left shifted address
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
336 out USBOUT, x1 ;[13] <-- out SE0 -- from now 2 bits = 22 cycles until bus idle
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
337 subi YL, 20 + 2 ;[0] Only assign address on data packets, not ACK/NAK in x3
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
338 sbci YH, 0 ;[1]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
339 ;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
340 ;set address only after data packet was sent, not after handshake
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
341 breq skipAddrAssign ;[2]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
342 sts usbDeviceAddr, x2; if not skipped: SE0 is one cycle longer
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
343 skipAddrAssign:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
344 ;end of usbDeviceAddress transfer
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
345 ldi x2, 1<<USB_INTR_PENDING_BIT;[4] int0 occurred during TX -- clear pending flag
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
346 USB_STORE_PENDING(x2) ;[5]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
347 ori x1, USBIDLE ;[6]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
348 in x2, USBDDR ;[7]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
349 cbr x2, USBMASK ;[8] set both pins to input
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
350 mov x3, x1 ;[9]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
351 cbr x3, USBMASK ;[10] configure no pullup on both pins
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
352 ldi x4, 5 ;[11]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
353 se0Delay:
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
354 dec x4 ;[12] [15] [18] [21] [24]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
355 brne se0Delay ;[13] [16] [19] [22] [25]
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
356 out USBOUT, x1 ;[26] <-- out J (idle) -- end of SE0 (EOP signal)
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
357 out USBDDR, x2 ;[27] <-- release bus now
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
358 out USBOUT, x3 ;[28] <-- ensure no pull-up resistors are active
9e9b2c78bd31 added V-USB stack
Malte Di Donato <mdd@neo-soft.org>
parents:
diff changeset
359 rjmp doReturn

mercurial