/* Name: asmcommon.inc * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers * Author: Christian Starkjohann * Creation Date: 2007-11-05 * Tabsize: 4 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) */ /* Do not link this file! Link usbdrvasm.S instead, which includes the * appropriate implementation! */ /* General Description: This file contains assembler code which is shared among the USB driver implementations for different CPU cocks. Since the code must be inserted in the middle of the module, it's split out into this file and #included. Jump destinations called from outside: sofError: Called when no start sequence was found. se0: Called when a package has been successfully received. overflow: Called when receive buffer overflows. doReturn: Called after sending data. Outside jump destinations used by this module: waitForJ: Called to receive an already arriving packet. sendAckAndReti: sendNakAndReti: sendCntAndReti: usbSendAndReti: The following macros must be defined before this file is included: .macro POP_STANDARD .endm .macro POP_RETI .endm */ #define token x1 overflow: ldi x2, 1< going to 21 cycles to be safe. handleSetupOrOut: ;[32] ; Delay, see above #if (F_CPU >= 16000000) ldi YL, 7 ; USBdelay: subi YL, 1 brne USBdelay #endif #if USB_CFG_IMPLEMENT_FN_WRITEOUT /* if we have data for endpoint != 0, set usbCurrentTok to address */ andi x3, 0xf ;[32] breq storeTokenAndReturn ;[33] mov token, x3 ;[34] indicate that this is endpoint x OUT #endif storeTokenAndReturn: sts usbCurrentTok, token;[35] doReturn: POP_STANDARD ;[37] 12...16 cycles USB_LOAD_PENDING(YL) ;[49] sbrc YL, USB_INTR_PENDING_BIT;[50] check whether data is already arriving rjmp waitForJ ;[51] save the pops and pushes -- a new interrupt is already pending sofError: POP_RETI ;macro call CBI PORTB,0 ; reti ret handleData: #if USB_CFG_CHECK_CRC CRC_CLEANUP_AND_CHECK ; jumps to ignorePacket if CRC error #endif lds shift, usbCurrentTok;[18] tst shift ;[20] breq doReturn ;[21] lds x2, usbRxLen ;[22] tst x2 ;[24] brne sendNakAndReti ;[25] ; 2006-03-11: The following two lines fix a problem where the device was not ; recognized if usbPoll() was called less frequently than once every 4 ms. cpi cnt, 4 ;[26] zero sized data packets are status phase only -- ignore and ack brmi sendAckAndReti ;[27] keep rx buffer clean -- we must not NAK next SETUP #if USB_CFG_CHECK_DATA_TOGGLING sts usbCurrentDataToken, token ; store for checking by C code #endif sts usbRxLen, cnt ;[28] store received data, swap buffers sts usbRxToken, shift ;[30] ; Micronculeus v2 needs no double buffer due to in-order processing ; TB 2014-01-04 ; lds x2, usbInputBufOffset;[32] swap buffers ; ldi cnt, USB_BUFSIZE ;[34] ; sub cnt, x2 ;[35] ; sts usbInputBufOffset, cnt;[36] buffers now swapped rjmp sendAckAndReti ;[38] 40 + 17 = 57 until SOP handleIn: ;We don't send any data as long as the C code has not processed the current ;input data and potentially updated the output data. That's more efficient ;in terms of code size than clearing the tx buffers when a packet is received. lds x1, usbRxLen ;[30] cpi x1, 1 ;[32] negative values are flow control, 0 means "buffer free" brge sendNakAndReti ;[33] unprocessed input packet? ldi x1, USBPID_NAK ;[34] prepare value for usbTxLen #if USB_CFG_HAVE_INTRIN_ENDPOINT andi x3, 0xf ;[35] x3 contains endpoint #if USB_CFG_SUPPRESS_INTR_CODE brne sendNakAndReti ;[36] #else brne handleIn1 ;[36] #endif #endif lds cnt, usbTxLen ;[37] sbrc cnt, 4 ;[39] all handshake tokens have bit 4 set rjmp sendCntAndReti ;[40] 42 + 16 = 58 until SOP sts usbTxLen, x1 ;[41] x1 == USBPID_NAK from above ldi YL, lo8(usbTxBuf) ;[43] ldi YH, hi8(usbTxBuf) ;[44] rjmp usbSendAndReti ;[45] 57 + 12 = 59 until SOP ; Comment about when to set usbTxLen to USBPID_NAK: ; We should set it back when we receive the ACK from the host. This would ; be simple to implement: One static variable which stores whether the last ; tx was for endpoint 0 or 1 and a compare in the receiver to distinguish the ; ACK. However, we set it back immediately when we send the package, ; assuming that no error occurs and the host sends an ACK. We save one byte ; RAM this way and avoid potential problems with endless retries. The rest of ; the driver assumes error-free transfers anyway. #if !USB_CFG_SUPPRESS_INTR_CODE && USB_CFG_HAVE_INTRIN_ENDPOINT /* placed here due to relative jump range */ handleIn1: ;[38] #if USB_CFG_HAVE_INTRIN_ENDPOINT3 ; 2006-06-10 as suggested by O.Tamura: support second INTR IN / BULK IN endpoint cpi x3, USB_CFG_EP3_NUMBER;[38] breq handleIn3 ;[39] #endif lds cnt, usbTxLen1 ;[40] sbrc cnt, 4 ;[42] all handshake tokens have bit 4 set rjmp sendCntAndReti ;[43] 47 + 16 = 63 until SOP sts usbTxLen1, x1 ;[44] x1 == USBPID_NAK from above ldi YL, lo8(usbTxBuf1) ;[46] ldi YH, hi8(usbTxBuf1) ;[47] rjmp usbSendAndReti ;[48] 50 + 12 = 62 until SOP #if USB_CFG_HAVE_INTRIN_ENDPOINT3 handleIn3: lds cnt, usbTxLen3 ;[41] sbrc cnt, 4 ;[43] rjmp sendCntAndReti ;[44] 49 + 16 = 65 until SOP sts usbTxLen3, x1 ;[45] x1 == USBPID_NAK from above ldi YL, lo8(usbTxBuf3) ;[47] ldi YH, hi8(usbTxBuf3) ;[48] rjmp usbSendAndReti ;[49] 51 + 12 = 63 until SOP #endif #endif