📄 usbtors232_sk_eng.asm
字号:
out outputport,temp0 ;vysli von na USB ;ENG;transmit to USB
brne SendUSBAnswerLoop ;opakuj pre cely buffer (pokial temp3=0) ;ENG;repeat for all buffer (till temp3=0)
mov bitcount,OutBitStuffNumber ;pocitadlo bitov pre bitstuff ;ENG;bits counter for bitstuff
cpi bitcount,0 ;ak nie je potrebny bitstuff ;ENG;if not be needed bitstuff
breq ZeroBitStuf
SendUSBAnswerBitstuffLoop:
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 NoXORBitstuffSend ;ak je jedna - nemen stav na USB ;ENG;if is one - don't change state on USB
eor temp0,temp2 ;inak sa bude stav menit ;ENG;otherwise state will be changed
NoXORBitstuffSend:
out outputport,temp0 ;vysli von na USB ;ENG;transmit to USB
nop ;oneskorenie kvoli casovaniu ;ENG;delay because of timing
dec bitcount ;zmensi pocitadlo bitov - podla carry flagu ;ENG;decrement bits counter - according to carry flag
brne SendUSBAnswerBitstuffLoop ;ak pocitadlo bitov nie je nulove - opakuj vysielanie s dalsim bitom ;ENG;if bits counter isn't zero - repeat transmiting with next bit
ld inputbuf,Y ;oneskorenie 2 cykly ;ENG;delay 2 cycle
ZeroBitStuf:
nop ;oneskorenie 1 cyklus ;ENG;delay 1 cycle
cbr temp0,3
out outputport,temp0 ;vysli EOP na USB ;ENG;transmit EOP on USB
ldi bitcount,5 ;pocitadlo oneskorenia: EOP ma trvat 2 bity (16 cyklov pri 12MHz) ;ENG;delay counter: EOP shouls exists 2 bits (16 cycle at 12MHz)
SendUSBWaitEOP:
dec bitcount
brne SendUSBWaitEOP
sbi outputport,DATAminus ;nahodenie DATAminus : kludovy stav na port USB ;ENG;set DATAMINUS : idle state on USB port
sbi outputport,DATAminus ;oneskorenie 2 cykly: Idle ma trvat 1 bit (8 cyklov pri 12MHz) ;ENG;delay 2 cycle: Idle should exists 1 bit (8 cycle at 12MHz)
cbi USBdirection,DATAplus ;DATAplus ako vstupny ;ENG;DATAPLUS as input
cbi USBdirection,DATAminus ;DATAminus ako vstupny ;ENG;DATAMINUS as input
cbi outputport,DATAminus ;zhodenie DATAminus : treti stav na port USB ;ENG;reset DATAMINUS : the third state on USB port
ret
;------------------------------------------------------------------------------------------
ToggleDATAPID:
lds temp0,OutputBufferBegin+1 ;nahraj posledne PID ;ENG;load last PID
cpi temp0,DATA1PID ;ak bolo posledne DATA1PID byte ;ENG;if last was DATA1PID byte
ldi temp0,DATA0PID
breq SendData0PID ;tak posli nulovu odpoved s DATA0PID ;ENG;then send zero answer with DATA0PID
ldi temp0,DATA1PID ;inak posli nulovu odpoved s DATA1PID ;ENG;otherwise send zero answer with DATA1PID
SendData0PID:
sts OutputBufferBegin+1,temp0 ;DATA0PID byte ;ENG;DATA0PID byte
ret
;------------------------------------------------------------------------------------------
ComposeZeroDATA1PIDAnswer:
ldi temp0,DATA0PID ;DATA0 PID - v skutocnosti sa stoggluje na DATA1PID v nahrati deskriptora ;ENG;DATA0 PID - in the next will be toggled to DATA1PID in load descriptor
sts OutputBufferBegin+1,temp0 ;nahraj do vyst buffera ;ENG;load to output buffer
ComposeZeroAnswer:
ldi temp0,SOPbyte
sts OutputBufferBegin+0,temp0 ;SOP byte ;ENG;SOP byte
rcall ToggleDATAPID ;zmen DATAPID ;ENG;change DATAPID
ldi temp0,0x00
sts OutputBufferBegin+2,temp0 ;CRC byte ;ENG;CRC byte
sts OutputBufferBegin+3,temp0 ;CRC byte ;ENG;CRC byte
ldi ByteCount,2+2 ;dlzka vystupneho buffera (SOP a PID + CRC16) ;ENG;length of output buffer (SOP and PID + CRC16)
ret
;------------------------------------------------------------------------------------------
InitACKBufffer:
ldi temp0,SOPbyte
sts ACKBufferBegin+0,temp0 ;SOP byte ;ENG;SOP byte
ldi temp0,ACKPID
sts ACKBufferBegin+1,temp0 ;ACKPID byte ;ENG;ACKPID byte
ret
;------------------------------------------------------------------------------------------
SendACK:
push USBBufptrY
push bitcount
push OutBitStuffNumber
ldi USBBufptrY,ACKBufferBegin ;pointer na zaciatok ACK buffera ;ENG;pointer to begin of ACK buffer
ldi ByteCount,2 ;pocet vyslanych bytov (iba SOP a ACKPID) ;ENG;number of transmit bytes (only SOP and ACKPID)
clr OutBitStuffNumber
rcall SendUSBBuffer
pop OutBitStuffNumber
pop bitcount
pop USBBufptrY
ret
;------------------------------------------------------------------------------------------
InitNAKBufffer:
ldi temp0,SOPbyte
sts NAKBufferBegin+0,temp0 ;SOP byte ;ENG;SOP byte
ldi temp0,NAKPID
sts NAKBufferBegin+1,temp0 ;NAKPID byte ;ENG;NAKPID byte
ret
;------------------------------------------------------------------------------------------
SendNAK:
push OutBitStuffNumber
ldi USBBufptrY,NAKBufferBegin ;pointer na zaciatok ACK buffera ;ENG;pointer to begin of ACK buffer
ldi ByteCount,2 ;pocet vyslanych bytov (iba SOP a NAKPID) ;ENG;number of transmited bytes (only SOP and NAKPID)
clr OutBitStuffNumber
rcall SendUSBBuffer
pop OutBitStuffNumber
ret
;------------------------------------------------------------------------------------------
ComposeSTALL:
ldi temp0,SOPbyte
sts OutputBufferBegin+0,temp0 ;SOP byte ;ENG;SOP byte
ldi temp0,STALLPID
sts OutputBufferBegin+1,temp0 ;STALLPID byte ;ENG;STALLPID byte
ldi ByteCount,2 ;dlzka vystupneho buffera (SOP a PID) ;ENG;length of output buffer (SOP and PID)
ret
;------------------------------------------------------------------------------------------
DecodeNRZI: ;enkodovanie buffera z NRZI kodu do binarneho ;ENG;encoding of buffer from NRZI code to binary
push USBBufptrY ;zalohuj pointer do buffera ;ENG;back up pointer to buffer
push ByteCount ;zalohuj dlzku buffera ;ENG;back up length of buffer
add ByteCount,USBBufptrY ;koniec buffera do ByteCount ;ENG;end of buffer to ByteCount
ser temp0 ;na zabezpecenie jednotkoveho carry (v nasledujucej rotacii) ;ENG;to ensure unit carry (in the next rotation)
NRZIloop:
ror temp0 ;naplnenie carry z predchadzajuceho byte ;ENG;filling carry from previous byte
ld temp0,Y ;nahraj prijaty byte z buffera ;ENG;load received byte from buffer
mov temp2,temp0 ;posunuty register o jeden bit vpravo a XOR na funkciu NRZI dekodovania ;ENG;shifted register to one bit to the right and XOR for function of NRZI decoding
ror temp2 ;carry do najvyssieho bitu a sucasne posuv ;ENG;carry to most significant digit bit and shift
eor temp2,temp0 ;samotne dekodovanie NRZI ;ENG;NRZI decoding
com temp2 ;negovanie ;ENG;negate
st Y+,temp2 ;ulozenie spat ako dekodovany byte a zvys pointer do buffera ;ENG;save back as decoded byte and increment pointer to buffer
cp USBBufptrY,ByteCount ;ak este neboli vsetky ;ENG;if not all bytes
brne NRZIloop ;tak opakuj ;ENG;then repeat
pop ByteCount ;obnov dlzku buffera ;ENG;restore buffer length
pop USBBufptrY ;obnov pointer do buffera ;ENG;restore pointer to buffer
ret ;inak skonci ;ENG;otherwise finish
;------------------------------------------------------------------------------------------
BitStuff: ;odstranenie bit-stuffingu v buffri ;ENG;removal of bitstuffing in buffer
clr temp3 ;pocitadlo vynechanych bitov ;ENG;counter of omitted bits
clr lastBitstufNumber ;0xFF do lastBitstufNumber ;ENG;0xFF to lastBitstufNumber
dec lastBitstufNumber
BitStuffRepeat:
push USBBufptrY ;zalohuj pointer do buffera ;ENG;back up pointer to buffer
push ByteCount ;zalohuj dlzku buffera ;ENG;back up buffer length
mov temp1,temp3 ;pocitadlo vsetkych bitov ;ENG;counter of all bits
ldi temp0,8 ;spocitat vsetky bity v bufferi ;ENG;sum all bits in buffer
SumAllBits:
add temp1,temp0
dec ByteCount
brne SumAllBits
ldi temp2,6 ;inicializuj pocitadlo jednotiek ;ENG;initialize counter of ones
pop ByteCount ;obnov dlzku buffera ;ENG;restore buffer length
push ByteCount ;zalohuj dlzku buffera ;ENG;back up buffer length
add ByteCount,USBBufptrY ;koniec buffera do ByteCount ;ENG;end of buffer to ByteCount
inc ByteCount ;a pre istotu ho zvys o 2 (kvoli posuvaniu) ;ENG;and for safety increment it with 2 (because of shifting)
inc ByteCount
BitStuffLoop:
ld temp0,Y ;nahraj prijaty byte z buffera ;ENG;load received byte from buffer
ldi bitcount,8 ;pocitadlo bitov v byte ;ENG;bits counter in byte
BitStuffByteLoop:
ror temp0 ;naplnenie carry z LSB ;ENG;filling carry from LSB
brcs IncrementBitstuff ;ak LSB=0 ;ENG;if that LSB=0
ldi temp2,7 ;inicializuj pocitadlo jednotiek +1 (ak bola nula) ;ENG;initialize counter of ones +1 (if was zero)
IncrementBitstuff:
dec temp2 ;zniz pocitadlo jednotiek (predpoklad jednotkoveho bitu) ;ENG;decrement counter of ones (assumption of one bit)
brne DontShiftBuffer ;ak este nebolo 6 jednotiek za sebou - neposun buffer ;ENG;if there was not 6 ones together - don't shift buffer
cp temp1,lastBitstufNumber ;
ldi temp2,6 ;inicializuj pocitadlo jednotiek (ak by sa nerobil bitstuffing tak sa musi zacat odznova) ;ENG;initialize counter of ones (if no bitstuffing will be made then must be started again)
brcc DontShiftBuffer ;ak sa tu uz robil bitstuffing - neposun buffer ;ENG;if already was made bitstuffing - don't shift buffer
dec temp1 ;
mov lastBitstufNumber,temp1 ;zapamataj si poslednu poziciu bitstuffingu ;ENG;remember last position of bitstuffing
cpi bitcount,1 ;aby sa ukazovalo na 7 bit (ktory sa ma vymazat alebo kde sa ma vlozit nula) ;ENG;for pointing to 7-th bit (which must be deleted or where to insert zero)
brne NoBitcountCorrect
ldi bitcount,9 ;
inc USBBufptrY ;zvys pointer do buffera ENG;increment pointer to buffer
NoBitcountCorrect:
dec bitcount
bst BitStuffInOut,0
brts CorrectOutBuffer ;ak je Out buffer - zvys dlzku buffera ;ENG;if this is Out buffer - increment buffer length
rcall ShiftDeleteBuffer ;posun In buffer ;ENG;shift In buffer
dec temp3 ;zniz pocitadlo vynechani ;ENG;decrement counter of omission
rjmp CorrectBufferEnd
CorrectOutBuffer:
rcall ShiftInsertBuffer ;posun Out buffer ;ENG;shift Out buffer
inc temp3 ;zvys pocitadlo vynechani ;ENG;increment counter of omission
CorrectBufferEnd:
pop ByteCount ;obnov dlzku buffera ;ENG;restore buffer length
pop USBBufptrY ;obnov pointer do buffera ;ENG;restore pointer to buffer
rjmp BitStuffRepeat ;a restartni od zaciatku ;ENG;and restart from begin
DontShiftBuffer:
dec temp1 ;ak uz boli vsetky bity ;ENG;if already were all bits
breq EndBitStuff ;ukonci cyklus ;ENG;finish cycle
dec bitcount ;zniz pocitadlo bitov v byte ;ENG;decrement bits counter in byte
brne BitStuffByteLoop ;ak este neboli vsetky bity v byte - chod na dalsi bit ;ENG;if not yet been all bits in byte - go to next bit
;inak nahraj dalsi byte ;ENG;otherwise load next byte
inc USBBufptrY ;zvys pointer do buffera ;ENG;increment pointer to buffer
rjmp BitStuffLoop ;a opakuj ;ENG;and repeat
EndBitStuff:
pop ByteCount ;obnov dlzku buffera ;ENG;restore buffer length
pop USBBufptrY ;obnov pointer do buffera ;ENG;restore pointer to buffer
bst BitStuffInOut,0
brts IncrementLength ;ak je Out buffer - zvys dlzku Out buffera ;ENG;if this is Out buffer - increment length of Out buffer
DecrementLength: ;ak je In buffer - zniz dlzku In buffera ;ENG;if this is In buffer - decrement length of In buffer
cpi temp3,0 ;bolo aspon jedno znizenie ;ENG;was at least one decrement
breq NoChangeByteCount ;ak nie - nemen dlzku buffera ;ENG;if no - don't change buffer length
dec ByteCount ;ak je In buffer - zniz dlzku buffera ;ENG;if this is In buffer - decrement buffer length
subi temp3,256-8 ;ak nebolo viac ako 8 bitov naviac ;ENG;if there wasn't above 8 bits over
brcc NoChangeByteCount ;tak skonci ;ENG;then finish
dec ByteCount ;inak este zniz dlzku buffera ;ENG;otherwise next decrement buffer length
ret ;a skonci ;ENG;and finish
IncrementLength:
mov OutBitStuffNumber,temp3 ;zapamataj si pocet bitov naviac ;ENG;remember number of bits over
subi temp3,8 ;ak nebolo viac ako 8 bitov naviac ;ENG;if there wasn't above 8 bits over
brcs NoChangeByteCount ;tak skonci ;ENG;then finish
inc ByteCount ;inak zvys dlzku buffera ;ENG;otherwise increment buffer length
mov OutBitStuffNumber,temp3 ;a zapamataj si pocet bitov naviac (znizene o 8) ;ENG;and remember number of bits over (decremented by 8)
NoChangeByteCount:
ret ;skonci ;ENG;finish
;------------------------------------------------------------------------------------------
ShiftInsertBuffer: ;posuv buffer o jeden bit vpravo od konca az po poziciu: byte-USBBufptrY a bit-bitcount ;ENG;shift buffer by one bit to right from end till to position: byte-USBBufptrY and bit-bitcount
mov temp0,bitcount ;vypocet: bitcount= 9-bitcount ;ENG;calculation: bitcount= 9-bitcount
ldi bitcount,9
sub bitcount,temp0 ;do bitcount poloha bitu, ktory treba nulovat ;ENG;to bitcount bit position, which is necessary to clear
ld temp1,Y ;nahraj byte ktory este treba posunut od pozicie bitcount ;ENG;load byte which still must be shifted from position bitcount
rol temp1 ;a posun vlavo cez Carry (prenos z vyssieho byte a LSB do Carry) ;ENG;and shift to the left through Carry (transmission from higher byte and LSB to Carry)
ser temp2 ;FF do masky - temp2 ;ENG;FF to mask - temp2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -