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

📄 usbtors232_sk_eng.asm

📁 USB技术大全-54.8M.zip
💻 ASM
📖 第 1 页 / 共 5 页
字号:
		pop	temp2
EndInt0HandlerPOP:
		pop	USBBufptrY
		pop	ByteCount
		mov	bitcount,backupbitcount	;obnova bitcount registra	;ENG;restore bitcount register
EndInt0HandlerPOP2:
		pop	temp1
		pop	temp0
		out	SREG,backupSREG
		ldi	shiftbuf,1<<INTF0	;znulovat flag interruptu INTF0	;ENG;zero interruptu flag INTF0
		out	GIFR,shiftbuf
		reti				;inak skonci (bol iba SOF - kazdu milisekundu)	;ENG;otherwise finish (was only SOF - every millisecond)

USBBeginPacket:
		mov	backupbitcount,bitcount	;zaloha bitcount registra	;ENG;backup bitcount register
		in	shiftbuf,inputport	;ak ano nacitaj ho ako nulty bit priamo do shift registra	;ENG;if yes load it as zero bit directly to shift register
USBloopBegin:
		push	ByteCount		;dalsia zaloha registrov (setrenie casu)	;ENG;additional backup of registers (save of time)
		push	USBBufptrY
		ldi	bitcount,6		;inicializacia pocitadla bitov v bajte	;ENG;initialization of bits counter in byte
		ldi	ByteCount,MAXUSBBYTES	;inicializacia max poctu prijatych bajtov v pakete	;ENG;initialization of max number of received bytes in packet
		ldi	USBBufptrY,InputShiftBufferBegin	;nastav vstupny buffera	;ENG;set the input buffer
USBloop1_6:
		in	inputbuf,inputport
		cbr	inputbuf,USBpinmask	;odmaskovat spodne 2 bity	;ENG;unmask low 2 bits
		breq	USBloopEnd		;ak su nulove - koniec USB packetu	;ENG;if they are zeros - end of USB packet
		ror	inputbuf		;presun Data+ do shift registra	;ENG;transfer Data+ to shift register
		rol	shiftbuf
		dec	bitcount		;zmensi pocitadlo bitov	;ENG;decrement bits counter
		brne	USBloop1_6		;ak nie je nulove - opakuj naplnanie shift registra	;ENG;if it isn't zero - repeat filling of shift register
		nop				;inak bude nutne skopirovat shift register bo buffera	;ENG;otherwise is necessary copy shift register to buffer
USBloop7:
		in	inputbuf,inputport
		cbr	inputbuf,USBpinmask	;odmaskovat spodne 2 bity	;ENG;unmask low 2 bits
		breq	USBloopEnd		;ak su nulove - koniec USB packetu	;ENG;if they are zeros - end of USB packet
		ror	inputbuf		;presun Data+ do shift registra	;ENG;transfer Data+ to shift register
		rol	shiftbuf
		ldi	bitcount,7		;inicializacia pocitadla bitov v bajte	;ENG;initialization of bits counter in byte
		st	Y+,shiftbuf		;skopiruj shift register bo buffera a zvys pointer do buffera	;ENG;copy shift register into buffer and increment pointer to buffer
USBloop0:					;a zacni prijimat dalsi bajt	;ENG;and start receiving next byte
		in	shiftbuf,inputport	;nulty bit priamo do shift registra	;ENG;zero bit directly to shift register
		cbr	shiftbuf,USBpinmask	;odmaskovat spodne 2 bity	;ENG;unmask low 2 bits
		breq	USBloopEnd		;ak su nulove - koniec USB packetu	;ENG;if they are zeros - end of USB packet
		dec	bitcount		;zmensi pocitadlo bitov	;ENG;decrement bits counter
		nop				;
		dec	ByteCount		;ak sa nedosiahol maximum buffera	;ENG;if not reached maximum buffer
		brne	USBloop1_6		;tak prijimaj dalej	;ENG;then receive next

		rjmp	EndInt0HandlerPOP	;inak opakuj od zaciatku	;ENG;otherwise repeat back from begin

USBloopEnd:
		cpi	USBBufptrY,InputShiftBufferBegin+3	;ak sa neprijali aspon 3 byte	;ENG;if at least 3 byte not received
		brcs	EndInt0HandlerPOP	;tak skonci	;ENG;then finish
		lds	temp0,InputShiftBufferBegin+0	;identifikator paketu do temp0	;ENG;identifier of packet to temp0
		lds	temp1,InputShiftBufferBegin+1	;adresa do temp1	;ENG;address to temp1
		brne	TestDataPacket		;ak je dlzka ina ako 3 - tak to moze byt iba DataPaket	;ENG;if is length different from 3 - then this can be only DataPaket
TestIOPacket:
;		cp	temp1,MyAddress		;ak to nie je urcene (adresa) pre mna	;ENG;if this isn't assigned (address) for me
;		brne	TestDataPacket		;tak to moze byt este Data Packet	;ENG;then this can be still DataPacket
TestSetupPacket:;test na SETUP paket	;ENG;test to SETUP packet
		cpi	temp0,nNRZISETUPPID
		brne	TestOutPacket		;ak nie je Setup PID - dekoduj iny paket	;ENG;if this isn't Setup PID - decode other packet
		cp	temp1,MyInAddress	;ak to nie je urcene (adresa) pre mna	;ENG;if this isn't assigned (address) for me
		brne	TestDataPacket		;tak to moze byt este Data Packet	;ENG;then this can be still DataPacket
		ldi	State,SetupState
		rjmp	EndInt0HandlerPOP	;ak je Setup PID - prijimaj nasledny Data paket	;ENG;if this is Setup PID - receive consecutive Data packet
TestOutPacket:	;test na OUT paket	;ENG;test for OUT packet
		cpi	temp0,nNRZIOUTPID
		brne	TestInPacket		;ak nie je Out PID - dekoduj iny paket	;ENG;if this isn't Out PID - decode other packet
		cp	temp1,MyOutAddress	;ak to nie je urcene (adresa) pre mna	;ENG;if this isn't assigned (address) for me
		brne	TestDataPacket		;tak to moze byt este Data Packet	;ENG;then this can be still DataPacket
		ldi	State,OutState
		rjmp	EndInt0HandlerPOP	;ak je Out PID - prijimaj nasledny Data paket	;ENG;if this is Out PID - receive consecutive Data packet
TestInPacket:	;test na IN paket	;ENG;test on IN packet
		cpi	temp0,nNRZIINPID
		brne	TestDataPacket		;ak nie je In PID - dekoduj iny paket	;ENG;if this isn't In PID - decode other packet
		cp	temp1,MyInAddress	;ak to nie je urcene (adresa) pre mna	;ENG;if this isn't assigned (address) for me
		breq	AnswerToInRequest
TestDataPacket:	;test na DATA0 a DATA1 paket	;ENG;test for DATA0 and DATA1 packet
		cpi	temp0,nNRZIDATA0PID
		breq	Data0Packet		;ak nie je Data0 PID - dekoduj iny paket	;ENG;if this isn't Data0 PID - decode other packet
		cpi	temp0,nNRZIDATA1PID
		brne	NoMyPacked		;ak nie je Data1 PID - dekoduj iny paket	;ENG;if this isn't Data1 PID - decode other packet
Data0Packet:
		cpi	State,SetupState	;ak bol stav Setup	;ENG;if was state Setup
		breq	ReceiveSetupData	;prijmi ho	;ENG;receive it
		cpi	State,OutState		;ak bol stav Out	;ENG;if was state Out
		breq	ReceiveOutData		;prijmi ho	;ENG;receive it
NoMyPacked:
		ldi	State,BaseState		;znuluj stav	;ENG;zero state
		rjmp	EndInt0HandlerPOP	;a prijimaj nasledny Data paket	;ENG;and receive consecutive Data packet

AnswerToInRequest:
		push	temp2			;zazalohuj dalsie registre a pokracuj	;ENG;backup next registers and continue
		push	temp3
		push	RS232BufptrX
		push	ACC
		cpi	ActionFlag,DoReadySendAnswer	;ak nie je pripravena odpoved	;ENG;if isn't prepared answer
		brne	NoReadySend		;tak posli NAK	;ENG;then send NAK
		rcall	SendPreparedUSBAnswer	;poslanie odpovede naspat	;ENG;transmitting answer back
		cpi	State,AddressChangeState ;ak je stav AddressChange	;ENG;if state is AddressChange
		breq	SetMyNewUSBAddress	;tak treba zmenit USB adresu	;ENG;then is necessary to change USB address
		ldi	State,InState
		ldi	ActionFlag,DoPrepareOutContinuousBuffer
		rjmp	EndInt0Handler		;a opakuj - cakaj na dalsiu odozvu z USB	;ENG;and repeat - wait for next response from USB
ReceiveSetupData:
		push	temp2			;zazalohuj dalsie registre a pokracuj	;ENG;backup next registers and continue
		push	temp3
		push	RS232BufptrX
		push	ACC
		rcall	SendACK			;akceptovanie Setup Data paketu	;ENG;accept Setup Data packet
		rcall	FinishReceiving		;ukonci prijem	;ENG;finish receiving
		ldi	ActionFlag,DoReceiveSetupData
		rjmp	EndInt0Handler
ReceiveOutData:
		push	temp2			;zazalohuj dalsie registre a pokracuj	;ENG;backup next registers and continue
		push	temp3
		push	RS232BufptrX
		push	ACC
		cpi	ActionFlag,DoReceiveSetupData	;ak sa prave spracovava prikaz Setup	;ENG;if is currently in process command Setup
		breq	NoReadySend		;tak posli NAK	;ENG;then send NAK
		rcall	SendACK			;akceptovanie Out paketu	;ENG;accept Out packet
		clr	ActionFlag
		rjmp	EndInt0Handler
NoReadySend:
		rcall	SendNAK			;este nie som pripraveny s odpovedou	;ENG;still I am not ready to answer
		rjmp	EndInt0Handler		;a opakuj - cakaj na dalsiu odozvu z USB	;ENG;and repeat - wait for next response from USB
;------------------------------------------------------------------------------------------
SetMyNewUSBAddress:		;nastavi novu USB adresu v NRZI kodovani	;ENG;set new USB address in NRZI coded
		lds	MyInAddress,MyInAddressSRAM
		lds	MyOutAddress,MyOutAddressSRAM
		rjmp	EndInt0Handler
;------------------------------------------------------------------------------------------
FinishReceiving:		;korekcne akcie na ukoncenie prijmu	;ENG;corrective actions for receive termination
		cpi	bitcount,7		;prenes do buffera aj posledny necely byte	;ENG;transfer to buffer also last not completed byte
		breq	NoRemainingBits		;ak boli vsetky byty prenesene, tak neprenasaj nic	;ENG;if were all bytes transfered, then nothing transfer
		inc	bitcount
ShiftRemainingBits:
		rol	shiftbuf		;posun ostavajuce necele bity na spravnu poziciu	;ENG;shift remaining not completed bits on right position
		dec	bitcount
		brne	ShiftRemainingBits
		st	Y+,shiftbuf		;a skopiruj shift register bo buffera - necely byte	;ENG;and copy shift register bo buffer - not completed byte
NoRemainingBits:
		mov	ByteCount,USBBufptrY
		subi	ByteCount,InputShiftBufferBegin-1	;v ByteCount je pocet prijatych byte (vratane necelych byte)	;ENG;in ByteCount is number of received bytes (including not completed bytes)

		mov	InputBufferLength,ByteCount		;a uchovat pre pouzitie v hlavnom programe	;ENG;and save for use in main program
		ldi	USBBufptrY,InputShiftBufferBegin	;pointer na zaciatok prijimacieho shift buffera	;ENG;pointer to begin of receiving shift buffer
		ldi	RS232BufptrX,InputBufferBegin+1		;data buffer (vynechat SOP)	;ENG;data buffer (leave out SOP)
		push	XH					;uschova RS232BufptrX Hi ukazovatela	;ENG;save RS232BufptrX Hi index
		clr	XH
MoveDataBuffer:
		ld	temp0,Y+
		st	X+,temp0
		dec	ByteCount
		brne	MoveDataBuffer

		pop	XH					;obnova RS232BufptrX Hi ukazovatela	;ENG;restore RS232BufptrX Hi index
		ldi	ByteCount,nNRZISOPbyte
		sts	InputBufferBegin,ByteCount		;ako keby sa prijal SOP - nekopiruje sa zo shift buffera	;ENG;like received SOP - it is not copied from shift buffer
		ret
;------------------------------------------------------------------------------------------
;********************************************************************
;* Other procedures	;ENG;* Other procedures
;********************************************************************
;------------------------------------------------------------------------------------------
USBReset:		;inicializacia USB stavoveho stroja	;ENG;initialization of USB state engine
		ldi	temp0,nNRZIADDR0	;inicializacia USB adresy	;ENG;initialization of USB address
		mov	MyOutAddress,temp0
		mov	MyInAddress,temp0
		clr	State			;inicializacia stavoveho stroja	;ENG;initialization of state engine
		clr	BitStuffInOut
		clr	OutBitStuffNumber
		clr	ActionFlag
		clr	RAMread			;bude sa vycitavat z ROM-ky	;ENG;will be reading from ROM
		sts	ConfigByte,RAMread	;nenakonfiguravany stav	;ENG;unconfigured state
		ret
;------------------------------------------------------------------------------------------
SendPreparedUSBAnswer:	;poslanie kodovanim NRZI OUT buffera s dlzkou OutputBufferLength do USB	;ENG;transmitting by NRZI coding OUT buffer with length OutputBufferLength to USB
		mov	ByteCount,OutputBufferLength		;dlzka odpovede	;ENG;length of answer
SendUSBAnswer:	;poslanie kodovanim NRZI OUT buffera do USB	;ENG;transmitting by NRZI coding OUT buffer to USB
		ldi	USBBufptrY,OutputBufferBegin		;pointer na zaciatok vysielacieho buffera	;ENG;pointer to begin of transmitting buffer
SendUSBBuffer:	;poslanie kodovanim NRZI dany buffer do USB	;ENG;transmitting by NRZI coding given buffer to USB
		ldi	temp1,0			;zvysovanie pointera (pomocna premenna)	;ENG;incrementing pointer (temporary variable)
		mov	temp3,ByteCount		;pocitadlo bytov: temp3 = ByteCount	;ENG;byte counter: temp3 = ByteCount
		ldi	temp2,0b00000011	;maska na xorovanie	;ENG;mask for xoring
		ld	inputbuf,Y+		;nacitanie prveho bytu do inputbuf a zvys pointer do buffera	;ENG;load first byte to inputbuf and increment pointer to buffer
						;USB ako vystup:	;ENG;USB as output:
		cbi	outputport,DATAplus	;zhodenie DATAplus : kludovy stav portu USB	;ENG;down DATAPLUS : idle state of USB port
		sbi	outputport,DATAminus	;nahodenie DATAminus : kludovy stav portu USB	;ENG;set DATAMINUS : idle state of USB port
		sbi	USBdirection,DATAplus	;DATAplus ako vystupny	;ENG;DATAPLUS as output
		sbi	USBdirection,DATAminus	;DATAminus ako vystupny	;ENG;DATAMINUS as output

		in	temp0,outputport	;kludovy stav portu USB do temp0	;ENG;idle state of USB port to temp0
SendUSBAnswerLoop:
		ldi	bitcount,7		;pocitadlo bitov	;ENG;bits counter
SendUSBAnswerByteLoop:
		nop				;oneskorenie kvoli casovaniu	;ENG;delay because timing
		ror	inputbuf		;do carry vysielany bit (v smere naskor LSB a potom MSB)	;ENG;to carry transmiting bit (in direction first LSB then MSB)
		brcs	NoXORSend		;ak je jedna - nemen stav na USB	;ENG;if that it is one - don't change USB state
		eor	temp0,temp2		;inak sa bude stav menit	;ENG;otherwise state will be changed
NoXORSend:
		out	outputport,temp0	;vysli von na USB	;ENG;send out to USB
		dec	bitcount		;zmensi pocitadlo bitov - podla carry flagu	;ENG;decrement bits counter - according to carry flag
		brne	SendUSBAnswerByteLoop	;ak pocitadlo bitov nie je nulove - opakuj vysielanie s dalsim bitom	;ENG;if bits counter isn't zero - repeat transmiting with next bit
		sbrs	inputbuf,0		;ak je vysielany bit jedna - nemen stav na USB	;ENG;if is transmiting bit one - don't change USB state
		eor	temp0,temp2		;inak sa bude stav menit	;ENG;otherwise state will be changed
NoXORSendLSB:
		dec	temp3			;zniz pocitadlo bytov	;ENG;decrement bytes counter
		ld	inputbuf,Y+		;nacitanie dalsieho bytu a zvys pointer do buffera	;ENG;load next byte and increment pointer to buffer

⌨️ 快捷键说明

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