⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbtiny2313.asm

📁 AVR的USB开发源代码 USB-RS232转换
💻 ASM
📖 第 1 页 / 共 5 页
字号:
;***************************************************************************
;* U S B   S T A C K   F O R   T H E   A V R   F A M I L Y
;*
;* File Name            :"USBtiny2313.asm"
;* Title                :AVR309:USB to UART protocol converter (simple - small FIFO)
;* Date                 :09.01.2006
;* Version              :1.1
;* Target MCU           :ATtiny2313
;* AUTHOR		:Ing. Igor Cesko
;* 			 Slovakia
;* 			 cesko@internet.sk
;* 			 http://www.cesko.host.sk
;*
;* DESCRIPTION:
;*  USB protocol implementation into MCU with noUSB interface:
;*  Device:
;*  Universal USB interface (8-bit I/O port + RS232 serial line + EEPROM)
;*  + added RS232 FIFO buffer
;*
;* The timing is adapted for 12 MHz crystal (overclocked MCU !!!)
;*
;*
;* to add your own functions - see section: TEMPLATE OF YOUR FUNCTION
;*
;* to customize device to your company you must change VendorUSB ID (VID)
;* to VID assigned to your company (for more information see www.usb.org)
;*
;***************************************************************************
.include "tn2313def.inc"

.equ	E2END			=127

.equ	inputport		=PINB
.equ	outputport		=PORTB
.equ	USBdirection		=DDRB
.equ	DATAplus		=1		;signal D+ on PB1
.equ	DATAminus		=0		;signal D- on PB0 - mount 1.5kOhm pull-up on this pin
.equ	USBpinmask		=0b11111100	;mask low 2 bits (D+,D-) on PB
.equ	USBpinmaskDplus		=~(1<<DATAplus)	;mask D+ bit on PB
.equ	USBpinmaskDminus	=~(1<<DATAminus);mask D- bit on PB

.equ	TSOPPort		=PINB
.equ	TSOPpullupPort		=PORTB
.equ	TSOPPin			=2		;signal OUT from IR sensor TSOP1738 on PB2

.equ	LEDPortLSB		=PORTD		;connecting LED diode LSB
.equ	LEDPinLSB		=PIND		;connecting LED diode LSB (input)
.equ	LEDdirectionLSB		=DDRD		;input/output LED LSB
.equ	LEDPortMSB		=PORTB		;LED diodes MSB
.equ	LEDPinMSB		=PINB		;LED diodes MSB  (input)
.equ	LEDdirectionMSB		=DDRB		;input/output LED MSB
.equ	LEDlsb0			=3		;LED0 to pin PD3
.equ	LEDlsb1			=5		;LED1 to pin PD5
.equ	LEDlsb2			=6		;LED2 to pin PD6
.equ	LEDmsb3			=3		;LED3 to pin PB3
.equ	LEDmsb4			=4		;LED4 to pin PB4
.equ	LEDmsb5			=5		;LED5 to pin PB5
.equ	LEDmsb6			=6		;LED6 to pin PB6
.equ	LEDmsb7			=7		;LED7 to pin PB7

.equ	SOPbyte			=0b10000000	;Start of Packet byte
.equ	DATA0PID		=0b11000011	;PID for DATA0 part
.equ	DATA1PID		=0b01001011	;PID for DATA1 part
.equ	OUTPID			=0b11100001	;PID for OUT part
.equ	INPID			=0b01101001	;PID for IN part
.equ	SOFPID			=0b10100101	;PID for SOF part
.equ	SETUPPID		=0b00101101	;PID for SETUP part
.equ	ACKPID			=0b11010010	;PID for ACK part
.equ	NAKPID			=0b01011010	;PID for NAK part
.equ	STALLPID		=0b00011110	;PID for STALL part
.equ	PREPID			=0b00111100	;PID for PRE part

.equ	nSOPbyte		=0b00000001	;Start of Packet byte - reverse order
.equ	nDATA0PID		=0b11000011	;PID for DATA0 part - reverse order
.equ	nDATA1PID		=0b11010010	;PID for DATA1 part - reverse order
.equ	nOUTPID			=0b10000111	;PID for OUT part - reverse order
.equ	nINPID			=0b10010110	;PID for IN part - reverse order
.equ	nSOFPID			=0b10100101	;PID for SOF part - reverse order
.equ	nSETUPPID		=0b10110100	;PID for SETUP part - reverse order
.equ	nACKPID			=0b01001011	;PID for ACK part - reverse order
.equ	nNAKPID			=0b01011010	;PID for NAK part - reverse order
.equ	nSTALLPID		=0b01111000	;PID for STALL part - reverse order
.equ	nPREPID			=0b00111100	;PID for PRE part - reverse order

.equ	nNRZITokenPID		=~0b10000000	;PID mask for Token packet (IN,OUT,SOF,SETUP) - reverse order NRZI
.equ	nNRZISOPbyte		=~0b10101011	;Start of Packet byte - reverse order NRZI
.equ	nNRZIDATA0PID		=~0b11010111	;PID for DATA0 part - reverse order NRZI
.equ	nNRZIDATA1PID		=~0b11001001	;PID for DATA1 part - reverse order NRZI
.equ	nNRZIOUTPID		=~0b10101111	;PID for OUT part - reverse order NRZI
.equ	nNRZIINPID		=~0b10110001	;PID for IN part - reverse order NRZI
.equ	nNRZISOFPID		=~0b10010011	;PID for SOF part - reverse order NRZI
.equ	nNRZISETUPPID		=~0b10001101	;PID for SETUP part - reverse order NRZI
.equ	nNRZIACKPID		=~0b00100111	;PID for ACK part - reverse order NRZI
.equ	nNRZINAKPID		=~0b00111001	;PID for NAK part - reverse order NRZI
.equ	nNRZISTALLPID		=~0b00000111	;PID for STALL part - reverse order NRZI
.equ	nNRZIPREPID		=~0b01111101	;PID for PRE part - reverse order NRZI
.equ	nNRZIADDR0		=~0b01010101	;Address = 0 - reverse order NRZI

						;status bytes - State
.equ	BaseState		=0		;
.equ	SetupState	=1		;
.equ	InState			=2		;
.equ	OutState		=3		;
.equ	SOFState		=4		;
.equ	DataState		=5		;
.equ	AddressChangeState	=6		;

						;Flags of required task
.equ	DoNone					=0
.equ	DoReceiveOutData			=1
.equ	DoReceiveSetupData			=2
.equ	DoPrepareOutContinuousBuffer		=3
.equ	DoReadySendAnswer			=4


.equ	CRC5poly		=0b00101		;CRC5 polynomial
.equ	CRC5zvysok		=0b01100		;CRC5 remainder after correct CRC5
.equ	CRC16poly		=0b1000000000000101	;CRC16 polynomial
.equ	CRC16zvysok		=0b1000000000001101	;CRC16 remainder after correct CRC16

.equ	MAXUSBBYTES		=14			;maximum bytes in USB input message
.equ	MAXRS232LENGTH		=36			;maximum length of RS232 code (count of ones and zeros together) (attention: MAXRS232LENGTH must be even number !!!)
.equ	NumberOfFirstBits	=10			;how many first bits allowed be longer
.equ	NoFirstBitsTimerOffset	=256-12800*12/1024	;Timeout 12.8ms (12800us) to terminate after firsts bits(12Mhz:clock, 1024:timer predivider, 256:timer overflow value)
.equ	InitBaudRate		=12000000/16/57600-1	;UART on 57600 (for 12MHz=12000000Hz)

.equ	InputBufferBegin	=RAMEND-127				;start of receiving buffer
.equ	InputShiftBufferBegin	=InputBufferBegin+MAXUSBBYTES		;start of receiving shift buffer
.equ	RS232BufferBegin	=InputShiftBufferBegin+MAXUSBBYTES	;start of buffer for RS232 receiving

.equ	MyInAddressSRAM		=RS232BufferBegin+MAXRS232LENGTH+1
.equ	MyOutAddressSRAM	=MyInAddressSRAM+1

.equ	OutputBufferBegin	=RAMEND-MAXUSBBYTES-2	;begin of transmitting buffer
.equ	AckBufferBegin		=OutputBufferBegin-3	;begin of transmitting buffer Ack
.equ	NakBufferBegin		=AckBufferBegin-3	;begin of transmitting buffer Nak

.equ	StackBegin		=NakBufferBegin-1	;bottom of stack

.def	ConfigByte		=R1		;0=unconfigured state
.def	backupbitcount		=R2		;backup bitcount register in INT0 disconnected
.def	RAMread			=R3		;if reading from SRAM
.def	backupSREGTimer		=R4		;backup Flag register in Timer interrupt
.def	backupSREG		=R5		;backup Flag register in INT0 interrupt
.def	ACC			=R6		;accumulator
.def	lastBitstufNumber	=R7		;position in bitstuffing
.def	OutBitStuffNumber	=R8		;how many bits to send last byte - bitstuffing
.def	BitStuffInOut		=R9		;if insertion or deleting of bitstuffing
.def	TotalBytesToSend	=R10		;how many bytes to send
.def	TransmitPart		=R11		;order number of transmitting part
.def	InputBufferLength	=R12		;length prepared in input USB buffer
.def	OutputBufferLength	=R13		;length answers prepared in USB buffer
.def	MyOutAddress		=R14		;my USB address (Out Packet) for update
.def	MyInAddress		=R15		;my USB address (In/SetupPacket)


.def	ActionFlag		=R16		;what to do in main program loop
.def	temp3			=R17		;temporary register
.def	temp2			=R18		;temporary register
.def	temp1			=R19		;temporary register
.def	temp0			=R20		;temporary register
.def	bitcount		=R21		;counter of bits in byte
.def	ByteCount		=R22		;counter of maximum number of received bytes
.def	inputbuf		=R23		;receiver register
.def	shiftbuf		=R24		;shift receiving register
.def	State			=R25		;state byte of status of state machine
.def	RS232BufptrX		=R26		;XL register - pointer to buffer of received IR codes
.def	RS232BufferFull		=R27		;XH register - flag of full RS232 Buffer
.def	USBBufptrY		=R28		;YL register - pointer to USB buffer input/output
.def	ROMBufptrZ		=R30		;ZL register - pointer to buffer of ROM data

;requirements on descriptors
.equ	GET_STATUS		=0
.equ	CLEAR_FEATURE		=1
.equ	SET_FEATURE		=3
.equ	SET_ADDRESS		=5
.equ	GET_DESCRIPTOR		=6
.equ	SET_DESCRIPTOR		=7
.equ	GET_CONFIGURATION	=8
.equ	SET_CONFIGURATION	=9
.equ	GET_INTERFACE		=10
.equ	SET_INTERFACE		=11
.equ	SYNCH_FRAME		=12

;descriptor types
.equ	DEVICE			=1
.equ	CONFIGURATION		=2
.equ	STRING			=3
.equ	INTERFACE		=4
.equ	ENDPOINT		=5

.equ	USER_FNC_NUMBER		=100


;------------------------------------------------------------------------------------------
;********************************************************************
;* Interrupt table
;********************************************************************
.cseg
;------------------------------------------------------------------------------------------
.org 0						;after reset
		rjmp	reset
;------------------------------------------------------------------------------------------
.org INT0addr					;external interrupt INT0
		rjmp	INT0handler
;------------------------------------------------------------------------------------------
.org URXCaddr					;receiving from serial line
		push	temp0
		in	temp0,UDR			;put to temp0 received data from UART
		sei					;enable interrupts to service USB
		in	backupSREGTimer,SREG		;backup SREG
		cbi	UCSRB,RXCIE			;disable interrupt from UART receiving
		cpi	RS232BufferFull,MAXRS232LENGTH-4
		brcc	NoIncRS232BufferFull
		push	RS232BufptrX
		lds	RS232BufptrX,RS232BufferBegin+2	;set position to begin of buffer write RS232 code : 3-th.byte of header (code length + reading + writing + reserve)
		st	X+,temp0			;and save it to buffer
		cpi	RS232BufptrX,RS232BufferBegin+MAXRS232LENGTH+1	;if not reached maximum of RS232 buffer
		brne	NoUARTBufferOverflow		;then continue
		ldi	RS232BufptrX,RS232BufferBegin+4	;otherwise set position to buffer begin
 NoUARTBufferOverflow:
		sts	RS232BufferBegin+2,RS232BufptrX	;save new offset of buffer write RS232 code : 3-th.byte of header (code length + reading + writing + reserve)
		inc	RS232BufferFull			;increment length of RS232 buffer
		pop	RS232BufptrX
 NoIncRS232BufferFull:
		pop	temp0
		out	SREG,backupSREGTimer		;restore SREG
		cli					;disable interrupt because to prevent reentrant interrupt call
		sbi	UCSRB,RXCIE			;enable interrupt from receiving of UART
		reti
;------------------------------------------------------------------------------------------
;********************************************************************
;* Init program
;********************************************************************
;------------------------------------------------------------------------------------------
reset:			;initialization of processor and variables to right values
		ldi	temp0,StackBegin	;initialization of stack
		out	SPL,temp0

		clr	XH			;RS232 pointer
		clr	YH			;USB pointer
		clr	ZH			;ROM pointer
		sts	RS232BufferBegin+0,YH	;clear lengths of RS232 code in buffer
		ldi	temp0,RS232BufferBegin+4
		sts	RS232BufferBegin+1,temp0;znuluj ukazovatel citania
		sts	RS232BufferBegin+2,temp0;znuluj ukazovatel zapisu
		clr	RS232BufferFull

		rcall	InitACKBufffer		;initialization of ACK buffer
		rcall	InitNAKBufffer		;initialization of NAK buffer

		rcall	USBReset		;initialization of USB addresses

		sbi	TSOPpullupPort,TSOPpin	;set pull-up on TSOP input

		ldi	temp0,(1<<LEDlsb0)+(1<<LEDlsb1)+(1<<LEDlsb2)
		out	LEDPortLSB,temp0	;set pull-up on all LED LSB
		ldi	temp0,(1<<LEDmsb3)+(1<<LEDmsb4)+(1<<LEDmsb5)+(1<<LEDmsb6)+(1<<LEDmsb7)
		out	LEDPortMSB,temp0	;set pull-up on all LED MSB

		sbi	PORTD,0			;set pull-up on RxD input
		ldi	temp0,InitBaudRate	;set UART speed
		out	UBRRL,temp0
		sbi	UCSRB,TXEN		;enable transmiting of UART
		sbi	UCSRB,RXEN		;enable receiving of UART
		sbi	UCSRB,RXCIE		;enable interrupt from receiving of UART

		ldi	temp0,0x0F		;INT0 - respond to leading edge
		out	MCUCR,temp0		;
		ldi	temp0,1<<INT0		;enable external interrupt INT0
		out	GIMSK,temp0
;------------------------------------------------------------------------------------------
;********************************************************************
;* Main program
;********************************************************************
		sei					;enable interrupts globally
Main:
		sbis	inputport,DATAminus	;waiting till change D- to 0
		rjmp	CheckUSBReset		;and check, if isn't USB reset

		cpi	ActionFlag,DoReceiveSetupData
		breq	ProcReceiveSetupData
		cpi	ActionFlag,DoPrepareOutContinuousBuffer
		breq	ProcPrepareOutContinuousBuffer
		rjmp	Main

CheckUSBReset:
		ldi	temp0,255		;counter duration of reset (according to specification is that cca 10ms - here is cca 100us)
WaitForUSBReset:
		sbic	inputport,DATAminus	;waiting till change D+ to 0
		rjmp	Main
		dec	temp0
		brne	WaitForUSBReset
		rcall	USBReset
		rjmp	Main

ProcPrepareOutContinuousBuffer:
		rcall	PrepareOutContinuousBuffer	;prepare next sequence of answer to buffer
		ldi	ActionFlag,DoReadySendAnswer
		rjmp	Main
ProcReceiveSetupData:
		ldi	USBBufptrY,InputBufferBegin	;pointer to begin of receiving buffer

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -