/* Name: usbdrvasm16.inc * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers * Author: Christian Starkjohann * Creation Date: 2007-06-15 * Tabsize: 4 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) * TinyAvr1 TinyAvr0 code port by: 12oClocker */ /* Do not link this file! Link usbdrvasm.S instead, which includes the * appropriate implementation! */ /* General Description: This file is the 16 MHz version of the asssembler part of the USB driver. It requires a 16 MHz crystal (not a ceramic resonator and not a calibrated RC oscillator). See usbdrv.h for a description of the entire driver. Since almost all of this code is timing critical, don't change unless you really know what you are doing! Many parts require not only a maximum number of CPU cycles, but even an exact number of cycles! ,----Likely to cause the most issues with TinyAvr0 TinyAvr1 series... | --AVR---|--TinyAvr-- CPU cycle differences for opcodes X push 2 push 1 X cbi 2 cbi 1 X sbi 2 sbi 1 X st 2 st 1,2 1 cycles for SRAM, 2 cycles for FLASH or EEPROM X std 2 std 1,2 1 cycles for SRAM, 2 cycles for FLASH or EEPROM sts 2 sts 2,3 only different if NOT reading from SRAM X lds 2 lds 3,4 3 cycles for SRAM, 4 cycles for FLASH or EEPROM ld 2 ld 2,3 only different if NOT reading from SRAM ldd 2 ldd 2,3 only different if NOT reading from SRAM ret 4 ret 4,5 only different if NOT reading from SRAM reti 4 reti 4,5 only different if NOT reading from SRAM icall 3 icall 2,3 rcall 3 rcall 2,3 */ ;max stack usage: [ret(2), YL, SREG, YH, bitcnt, shift, x1, x2, x3, x4, cnt] = 12 bytes ;nominal frequency: 16 MHz -> 10.6666666 cycles per bit, 85.333333333 cycles per byte ; Numbers in brackets are clocks counted from center of last sync bit ; when instruction starts USB_INTR_VECTOR: ;order of registers pushed: YL, SREG YH, [sofError], bitcnt, shift, x1, x2, x3, x4, cnt #if USB_CFG_TINYAVR_SERIES == 1 push YL ;[-25] push only what is necessary to sync with edge ASAP ldi YL, (1< subtract 1/3 rjmp didUnstuff6 ;[08] unstuff7: ori x3, 1<<7 ;[09] will not be shifted any more in x2, USBIN ;[00] [10] re-sample bit 7 andi x2, USBMASK ;[01] andi shift, ~0x80;[02] subi leap, 2 ;[03] total duration = 10 bits -> add 1/3 rjmp didUnstuff7 ;[04] unstuffEven: ori x3, 1<<6 ;[09] will be shifted right 6 times for bit 0 in x1, USBIN ;[00] [10] andi shift, ~0x80;[01] andi x1, USBMASK ;[02] breq se0 ;[03] subi leap, -1 ;[04] total duration = 11 bits -> subtract 1/3 nop2 ;[05] rjmp didUnstuffE ;[06] unstuffOdd: ori x3, 1<<5 ;[09] will be shifted right 4 times for bit 1 in x2, USBIN ;[00] [10] andi shift, ~0x80;[01] andi x2, USBMASK ;[02] breq se0 ;[03] subi leap, -1 ;[04] total duration = 11 bits -> subtract 1/3 nop2 ;[05] rjmp didUnstuffO ;[06] rxByteLoop: andi x1, USBMASK ;[03] eor x2, x1 ;[04] subi leap, 1 ;[05] brpl skipLeap ;[06] subi leap, -3 ;1 one leap cycle every 3rd byte -> 85 + 1/3 cycles per byte nop ;1 skipLeap: subi x2, 1 ;[08] ror shift ;[09] didUnstuff6: cpi shift, 0xfc ;[10] in x2, USBIN ;[00] [11] <-- sample bit 7 brcc unstuff6 ;[01] andi x2, USBMASK ;[02] eor x1, x2 ;[03] subi x1, 1 ;[04] ror shift ;[05] didUnstuff7: cpi shift, 0xfc ;[06] brcc unstuff7 ;[07] eor x3, shift ;[08] reconstruct: x3 is 1 at bit locations we changed, 0 at others st y+, x3 ;[09] store data nop_tas ; TinyAvr1, st is -1 cycle, compenstate rxBitLoop: in x1, USBIN ;[00] [11] <-- sample bit 0/2/4 andi x1, USBMASK ;[01] eor x2, x1 ;[02] andi x3, 0x3f ;[03] topmost two bits reserved for 6 and 7 subi x2, 1 ;[04] ror shift ;[05] cpi shift, 0xfc ;[06] brcc unstuffEven ;[07] didUnstuffE: lsr x3 ;[08] lsr x3 ;[09] rxbit1: in x2, USBIN ;[00] [10] <-- sample bit 1/3/5 andi x2, USBMASK ;[01] breq se0 ;[02] eor x1, x2 ;[03] subi x1, 1 ;[04] ror shift ;[05] cpi shift, 0xfc ;[06] brcc unstuffOdd ;[07] didUnstuffO: subi bitcnt, 0xab;[08] == addi 0x55, 0x55 = 0x100/3 brcs rxBitLoop ;[09] subi cnt, 1 ;[10] in x1, USBIN ;[00] [11] <-- sample bit 6 brcc rxByteLoop ;[01] rjmp overflow macro POP_STANDARD ; 14 cycles pop cnt pop x4 pop x3 pop x2 pop x1 pop shift pop bitcnt endm macro POP_RETI ; 7 cycles pop YH pop YL out SREG, YL pop YL endm #include "asmcommon.inc" ; USB spec says: ; idle = J ; J = (D+ = 0), (D- = 1) ; K = (D+ = 1), (D- = 0) ; Spec allows 7.5 bit times from EOP to SOP for replies bitstuffN: eor x1, x4 ;[5] ldi x2, 0 ;[6] nop2 ;[7] nop ;[9] out USBOUT, x1 ;[10] <-- out rjmp didStuffN ;[0] bitstuff6: eor x1, x4 ;[5] ldi x2, 0 ;[6] Carry is zero due to brcc rol shift ;[7] compensate for ror shift at branch destination rjmp didStuff6 ;[8] bitstuff7: ldi x2, 0 ;[2] Carry is zero due to brcc rjmp didStuff7 ;[3] sendNakAndReti: ldi x3, USBPID_NAK ;[-18] rjmp sendX3AndReti ;[-17] sendAckAndReti: ldi cnt, USBPID_ACK ;[-17] sendCntAndReti: mov x3, cnt ;[-16] sendX3AndReti: #if USB_CFG_TINYAVR_SERIES == 1 out USB_GPIOR1_REG, x3 ; TinyAvr1 GPIO register, We are 1 extra cycle because of this, but because we are begining an XMT this is OK ldi YL, USB_GPIOR1_MEM ;[-15] TinyAvr1 GPIO register, we use this because x3 does NOT memory map to 20 for TinyAvr1 series #else ldi YL, 20 ;[-15] x3==r20 address is 20 (does NOT work for TinyAvr0 TinyAvr1 series, memory mapping is different) #endif ldi YH, 0 ;[-14] ldi cnt, 2 ;[-13] ; rjmp usbSendAndReti fallthrough ;usbSend: ;pointer to data in 'Y' ;number of bytes in 'cnt' -- including sync byte [range 2 ... 12] ;uses: x1...x4, btcnt, shift, cnt, Y ;Numbers in brackets are time since first bit of sync pattern is sent ;We don't match the transfer rate exactly (don't insert leap cycles every third ;byte) because the spec demands only 1.5% precision anyway. usbSendAndReti: ; 12 cycles until SOP in x2, USBDDR ;[-12] ori x2, USBMASK ;[-11] sbi USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) nop_tas ; TinyAvr1, sbi is -1 cycle, compenstate in x1, USBOUT ;[-8] port mirror for tx loop out USBDDR, x2 ;[-7] <- acquire bus ; need not init x2 (bitstuff history) because sync starts with 0 ldi x4, USBMASK ;[-6] exor mask ldi shift, 0x80 ;[-5] sync byte is first byte sent txByteLoop: ldi bitcnt, 0x35 ;[-4] [6] binary 0011 0101 txBitLoop: sbrs shift, 0 ;[-3] [7] eor x1, x4 ;[-2] [8] out USBOUT, x1 ;[-1] [9] <-- out N ror shift ;[0] [10] ror x2 ;[1] didStuffN: cpi x2, 0xfc ;[2] brcc bitstuffN ;[3] lsr bitcnt ;[4] brcc txBitLoop ;[5] brne txBitLoop ;[6] sbrs shift, 0 ;[7] eor x1, x4 ;[8] didStuff6: out USBOUT, x1 ;[-1] [9] <-- out 6 ror shift ;[0] [10] ror x2 ;[1] cpi x2, 0xfc ;[2] brcc bitstuff6 ;[3] ror shift ;[4] didStuff7: ror x2 ;[5] sbrs x2, 7 ;[6] eor x1, x4 ;[7] nop ;[8] cpi x2, 0xfc ;[9] out USBOUT, x1 ;[-1][10] <-- out 7 brcc bitstuff7 ;[0] [11] ld shift, y+ ;[1] dec cnt ;[3] brne txByteLoop ;[4] ;make SE0: ;-------------------------------------------------------------- ;--------makeSE0------- for TinyAvr0 TinyAvr1 ----------------- ;-------------------------------------------------------------- #if USB_CFG_TINYAVR_SERIES == 1 cbr x1, USBMASK ;[5] prepare SE0 [spec says EOP may be 21 to 25 cycles] lds x2, usbNewDeviceAddr ;[6,7,8] TinyAvr1 uses 3 cycles instead of 2 and also usbNewDeviceAddr is defined as USB_GPIOR0_REG in usbdrv.c and in usbdrvasm.S lsl x2 ;[9] we compare with left shifted address nop ;[10] out USBOUT, x1 ;[11] <-- out SE0 -- from now 2 bits = 22 cycles until bus idle subi YL, 20 + 2 ;[0] Only assign address on data packets, not ACK/NAK in x3 sbci YH, 0 ;[1] ;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: ;set address only after data packet was sent, not after handshake breq skipAddrAssign ;[2,3] sts usbDeviceAddr, x2; if not skipped: SE0 is one cycle longer skipAddrAssign: ;end of usbDeviceAddress transfer ldi x2, 1<