📄 usbdrvasm12.inc
字号:
didUnstuff1: subi cnt, 1 ;1 [20] brcs overflow ;1 [21] loop control eor x2, x1 ;1 [22] bst x2, USBMINUS;1 [23] bld shift, 2 ;1 [24] in x2, USBIN ;1 [25] <-- sample bit 3 (or possibly bit 2 stuffed) andi shift, 0xe7 ;1 [26] breq unstuff2 ;1 [27]didUnstuff2: eor x1, x2 ;1 [28] bst x1, USBMINUS;1 [29] bld shift, 3 ;1 [30]didUnstuff3: andi shift, 0xcf ;1 [31] breq unstuff3 ;1 [32] in x1, USBIN ;1 [33] <-- sample bit 4 eor x2, x1 ;1 [34] bst x2, USBMINUS;1 [35] bld shift, 4 ;1 [36]didUnstuff4: andi shift, 0x9f ;1 [37] breq unstuff4 ;1 [38] nop2 ;2 [40] in x2, USBIN ;1 [41] <-- sample bit 5 eor x1, x2 ;1 [42] bst x1, USBMINUS;1 [43] bld shift, 5 ;1 [44]didUnstuff5: andi shift, 0x3f ;1 [45] breq unstuff5 ;1 [46] nop2 ;2 [48] in x1, USBIN ;1 [49] <-- sample bit 6 eor x2, x1 ;1 [50] bst x2, USBMINUS;1 [51] bld shift, 6 ;1 [52]didUnstuff6: cpi shift, 0x02 ;1 [53] brlo unstuff6 ;1 [54] nop2 ;2 [56] in x2, USBIN ;1 [57] <-- sample bit 7 eor x1, x2 ;1 [58] bst x1, USBMINUS;1 [59] bld shift, 7 ;1 [60]didUnstuff7: cpi shift, 0x04 ;1 [61] brsh rxLoop ;2 [63] loop controlunstuff7: andi x3, ~0x80 ;1 [63] ori shift, 0x80 ;1 [64] in x2, USBIN ;1 [65] <-- sample stuffed bit 7 nop ;1 [66] rjmp didUnstuff7 ;2 [68]macro POP_STANDARD ; 12 cycles pop cnt pop x3 pop x2 pop x1 pop shift pop YH endmmacro POP_RETI ; 5 cycles pop YL out SREG, YL pop YL endm#include "asmcommon.inc";----------------------------------------------------------------------------; Transmitting data;----------------------------------------------------------------------------txByteLoop:txBitloop:stuffN1Delay: ; [03] ror shift ;[-5] [11] [59] brcc doExorN1 ;[-4] [60] subi x4, 1 ;[-3] brne commonN1 ;[-2] lsl shift ;[-1] compensate ror after rjmp stuffDelay nop ;[00] stuffing consists of just waiting 8 cycles rjmp stuffN1Delay ;[01] after ror, C bit is reliably clearsendNakAndReti: ;0 [-19] 19 cycles until SOP ldi x3, USBPID_NAK ;1 [-18] rjmp usbSendX3 ;2 [-16]sendAckAndReti: ;0 [-19] 19 cycles until SOP ldi x3, USBPID_ACK ;1 [-18] rjmp usbSendX3 ;2 [-16]sendCntAndReti: ;0 [-17] 17 cycles until SOP mov x3, cnt ;1 [-16]usbSendX3: ;0 [-16] ldi YL, 20 ;1 [-15] 'x3' is R20 ldi YH, 0 ;1 [-14] ldi cnt, 2 ;1 [-13]; rjmp usbSendAndReti fallthrough; USB spec says:; idle = J; J = (D+ = 0), (D- = 1) or USBOUT = 0x01; K = (D+ = 1), (D- = 0) or USBOUT = 0x02; Spec allows 7.5 bit times from EOP to SOP for replies (= 60 cycles);usbSend:;pointer to data in 'Y';number of bytes in 'cnt' -- including sync byte;uses: x1...x2, x4, shift, cnt, Y [x1 = mirror USBOUT, x2 = USBMASK, x4 = bitstuff cnt];Numbers in brackets are time since first bit of sync pattern is sent (start of instruction)usbSendAndReti: in x2, USBDDR ;[-12] 12 cycles until SOP ori x2, USBMASK ;[-11] sbi USBOUT, USBMINUS ;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) out USBDDR, x2 ;[-8] <--- acquire bus in x1, USBOUT ;[-7] port mirror for tx loop ldi shift, 0x40 ;[-6] sync byte is first byte sent (we enter loop after ror) ldi x2, USBMASK ;[-5] push x4 ;[-4]doExorN1: eor x1, x2 ;[-2] [06] [62] ldi x4, 6 ;[-1] [07] [63]commonN1:stuffN2Delay: out USBOUT, x1 ;[00] [08] [64] <--- set bit ror shift ;[01] brcc doExorN2 ;[02] subi x4, 1 ;[03] brne commonN2 ;[04] lsl shift ;[05] compensate ror after rjmp stuffDelay rjmp stuffN2Delay ;[06] after ror, C bit is reliably cleardoExorN2: eor x1, x2 ;[04] [12] ldi x4, 6 ;[05] [13]commonN2: nop ;[06] [14] subi cnt, 171 ;[07] [15] trick: (3 * 171) & 0xff = 1 out USBOUT, x1 ;[08] [16] <--- set bit brcs txBitloop ;[09] [25] [41]stuff6Delay: ror shift ;[42] [50] brcc doExor6 ;[43] subi x4, 1 ;[44] brne common6 ;[45] lsl shift ;[46] compensate ror after rjmp stuffDelay nop ;[47] stuffing consists of just waiting 8 cycles rjmp stuff6Delay ;[48] after ror, C bit is reliably cleardoExor6: eor x1, x2 ;[45] [53] ldi x4, 6 ;[46]common6:stuff7Delay: ror shift ;[47] [55] out USBOUT, x1 ;[48] <--- set bit brcc doExor7 ;[49] subi x4, 1 ;[50] brne common7 ;[51] lsl shift ;[52] compensate ror after rjmp stuffDelay rjmp stuff7Delay ;[53] after ror, C bit is reliably cleardoExor7: eor x1, x2 ;[51] [59] ldi x4, 6 ;[52]common7: ld shift, y+ ;[53] tst cnt ;[55] out USBOUT, x1 ;[56] <--- set bit brne txByteLoop ;[57];make SE0: cbr x1, USBMASK ;[58] prepare SE0 [spec says EOP may be 15 to 18 cycles] lds x2, usbNewDeviceAddr;[59] lsl x2 ;[61] we compare with left shifted address subi YL, 2 + 20 ;[62] Only assign address on data packets, not ACK/NAK in x3 sbci YH, 0 ;[63] out USBOUT, x1 ;[00] <-- out SE0 -- from now 2 bits = 16 cycles until bus idle;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 ;[01] sts usbDeviceAddr, x2 ; if not skipped: SE0 is one cycle longerskipAddrAssign:;end of usbDeviceAddress transfer ldi x2, 1<<USB_INTR_PENDING_BIT;[03] int0 occurred during TX -- clear pending flag USB_STORE_PENDING(x2) ;[04] ori x1, USBIDLE ;[05] in x2, USBDDR ;[06] cbr x2, USBMASK ;[07] set both pins to input mov x3, x1 ;[08] cbr x3, USBMASK ;[09] configure no pullup on both pins pop x4 ;[10] nop2 ;[12] nop2 ;[14] out USBOUT, x1 ;[16] <-- out J (idle) -- end of SE0 (EOP signal) out USBDDR, x2 ;[17] <-- release bus now out USBOUT, x3 ;[18] <-- ensure no pull-up resistors are active rjmp doReturn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -