📄 usbtors232_sk_eng.asm
字号:
.equ DataBits7 =2
.equ DataBits8 =3
;parity ;ENG;parity
.equ ParityNone =0
.equ ParityOdd =1
.equ ParityEven =2
.equ ParityMark =3
.equ ParitySpace =4
;stopbits ;ENG;stopbits
.equ StopBit1 =0
.equ StopBit2 =1
;user function start number
.equ USER_FNC_NUMBER =100
;------------------------------------------------------------------------------------------
;********************************************************************
;* Interrupt table ;ENG;* Interrupt table
;********************************************************************
.cseg
;------------------------------------------------------------------------------------------
.org 0 ;po resete ;ENG;after reset
rjmp reset
;------------------------------------------------------------------------------------------
.org INT0addr ;externe prerusenie INT0 ;ENG;external interrupt INT0
rjmp INT0handler
;------------------------------------------------------------------------------------------
.org URXCaddr ;prijem zo seriovej linky ;ENG;receiving from serial line
push temp0
cbi UCR,RXCIE ;zakazat interrupt od prijimania UART ;ENG;disable interrupt from UART receiving
sei ;povol interrupty na obsluhu USB ;ENG;enable interrupts to service USB
in temp0,UDR ;nacitaj do temp0 prijate data z UART-u ;ENG;put to temp0 received data from UART
in backupSREGTimer,SREG ;zaloha SREG ;ENG;backup SREG
push temp2
push temp3
lds temp2,RS232LengthPosPtr
lds temp3,RS232LengthPosPtr+1 ;zisti dlzku buffera RS232 kodu ;ENG;determine length of RS232 code buffer
cpi temp3,HIGH(RS232BufferEnd-RS232FIFOBegin-1) ;ci by mal pretiect buffer ;ENG;if the buffer would overflow
brlo FIFOBufferNoOverflow ;ak nepretecie, tak zapis do FIFO ;ENG;if not overflow then write to FIFO
brne NoIncRS232BufferFull ;ak by mal pretiect, tak zabran prepisaniu ;ENG;if buffer would overflow, then prevent of overwriting
;inak (pri rovnosti) este porovnaj Lo byty ;ENG;otherwise (if equall) still compare Lo bytes
cpi temp2,LOW(RS232BufferEnd-RS232FIFOBegin-1) ;ak by mal pretiect buffer (Lo byte) ;ENG;if buffer would overflow (Lo byte)
brcc NoIncRS232BufferFull ;tak zabran prepisaniu ;ENG;then prevent of overwriting
FIFOBufferNoOverflow:
push RS232BufptrX
push RS232BufptrXH
lds RS232BufptrX,RS232WritePosPtr ;nastavenie sa na zaciatok buffera zapisu RS232 kodu ;ENG;set position to begin of buffer write RS232 code
lds RS232BufptrXH,RS232WritePosPtr+1 ;nastavenie sa na zaciatok buffera zapisu RS232 kodu ;ENG;set position to begin of buffer write RS232 code
st X+,temp0 ;a uloz ho do buffera ;ENG;and save it to buffer
cpi RS232BufptrXH,HIGH(RS232BufferEnd+1) ;ak sa nedosiahol maximum RS232 buffera ;ENG;if not reached maximum of RS232 buffer
brlo NoUARTBufferOverflow ;tak pokracuj ;ENG;then continue
brne UARTBufferOverflow ;skontroluj aj LSB ;ENG;check althen LSB
cpi RS232BufptrX,LOW(RS232BufferEnd+1) ;ak sa nedosiahol maximum RS232 buffera ;ENG;if not reached maximum of RS232 buffer
brlo NoUARTBufferOverflow ;tak pokracuj ;ENG;then continue
UARTBufferOverflow:
ldi RS232BufptrX,LOW(RS232FIFOBegin) ;inak sa nastav na zaciatok buffera ;ENG;otherwise set position to buffer begin
ldi RS232BufptrXH,HIGH(RS232FIFOBegin) ;inak sa nastav na zaciatok buffera ;ENG;otherwise set position to buffer begin
NoUARTBufferOverflow:
sts RS232WritePosPtr,RS232BufptrX ;ulozenie noveho offsetu buffera zapisu RS232 kodu ;ENG;save new offset of buffer write RS232 code
sts RS232WritePosPtr+1,RS232BufptrXH ;ulozenie noveho offsetu buffera zapisu RS232 kodu ;ENG;save new offset of buffer write RS232 code
ldi temp0,1 ;zvys dlzku RS232 buffera ;ENG;increment length of RS232 buffer
add temp2,temp0
ldi temp0,0
adc temp3,temp0
sts RS232LengthPosPtr,temp2 ;uloz dlzku buffera RS232 kodu ;ENG;save length of buffer RS232 code
sts RS232LengthPosPtr+1,temp3 ;uloz dlzku buffera RS232 kodu ;ENG;save length of buffer RS232 code
pop RS232BufptrXH
pop RS232BufptrX
NoIncRS232BufferFull:
pop temp3
pop temp2
pop temp0
out SREG,backupSREGTimer ;obnova SREG ;ENG;restore SREG
cli ;zakazat interrupt kvoli zacykleniu ;ENG;disable interrupt because to prevent reentrant interrupt call
sbi UCR,RXCIE ;povolit interrupt od prijimania UART ;ENG;enable interrupt from receiving of UART
reti
;------------------------------------------------------------------------------------------
;********************************************************************
;* Init program ;ENG;* Init program
;********************************************************************
;------------------------------------------------------------------------------------------
reset: ;inicializacia procesora a premennych na spravne hodnoty ;ENG;initialization of processor and variables to right values
ldi temp0,StackBegin ;inicializacia stacku ;ENG;initialization of stack
out SPL,temp0
clr XH ;RS232 pointer ;ENG;RS232 pointer
clr YH ;USB pointer ;ENG;USB pointer
clr ZH ;ROM pointer ;ENG;ROM pointer
ldi temp0,LOW(RS232FIFOBegin) ;nastav na zaciatok buffera Low ;ENG;set Low to begin of buffer
sts RS232ReadPosPtr,temp0 ;znuluj ukazovatel citania ;ENG;zero index of reading
sts RS232WritePosPtr,temp0 ;znuluj ukazovatel zapisu ;ENG;zero index of writing
ldi temp0,HIGH(RS232FIFOBegin) ;nastav na zaciatok buffera High ;ENG;set High to begin of buffer
sts RS232ReadPosPtr+1,temp0 ;znuluj ukazovatel citania ;ENG;zero index of reading
sts RS232WritePosPtr+1,temp0 ;znuluj ukazovatel zapisu ;ENG;zero index of writing
sts RS232LengthPosPtr,YH ;znuluj ukazovatel dlzky ;ENG;zero index of length
sts RS232LengthPosPtr+1,YH ;znuluj ukazovatel dlzky ;ENG;zero index of length
clr RS232BufferFull
rcall InitACKBufffer ;inicializacia ACK buffera ;ENG;initialization of ACK buffer
rcall InitNAKBufffer ;inicializacia NAK buffera ;ENG;initialization of NAK buffer
rcall USBReset ;inicializacia USB adresy ;ENG;initialization of USB addresses
ldi temp0,0b00111100 ;nahodit pull-up na PORTB ;ENG;set pull-up on PORTB
out PORTB,temp0
ldi temp0,0b11111111 ;nahodit pull-up na PORTC ;ENG;set pull-up on PORTC
out PORTC,temp0
ldi temp0,0b11111011 ;nahodit pull-up na PORTD ;ENG;set pull-up on PORTD
out PORTD,temp0
clr temp0 ;
out UBRRH,temp0 ;nastavit vysielaciu rychlost UART-u High ;ENG;set UART speed High
out EEARH,temp0 ;znulovat EEPROM ukazovatel ;ENG;zero EEPROM index
ldi temp0,1<<U2X ;nastavit mod X2 na UART-e ;ENG;set mode X2 on UART
out USR,temp0
ldi temp0,InitBaudRate ;nastavit vysielaciu rychlost UART-u ;ENG;set UART speed
out UBRR,temp0
sbi UCR,TXEN ;povolit vysielanie UART-u ;ENG;enable transmiting of UART
sbi UCR,RXEN ;povolit prijimanie UART-u ;ENG;enable receiving of UART
sbi UCR,RXCIE ;povolit interrupt od prijimania UART ;ENG;enable interrupt from receiving of UART
ldi temp0,0x0F ;INT0 - reagovanie na nabeznu hranu ;ENG;INT0 - respond to leading edge
out MCUCR,temp0 ;
ldi temp0,1<<INT0 ;povolit externy interrupt INT0 ;ENG;enable external interrupt INT0
out GIMSK,temp0
;------------------------------------------------------------------------------------------
;********************************************************************
;* Main program ;ENG;* Main program
;********************************************************************
sei ;povolit interrupty globalne ;ENG;enable interrupts globally
Main:
sbis inputport,DATAminus ;cakanie az sa zmeni D- na 0 ;ENG;waiting till change D- to 0
rjmp CheckUSBReset ;a skontroluj, ci to nie je USB reset ;ENG;and check, if isn't USB reset
cpi ActionFlag,DoReceiveSetupData
breq ProcReceiveSetupData
cpi ActionFlag,DoPrepareOutContinuousBuffer
breq ProcPrepareOutContinuousBuffer
rjmp Main
CheckUSBReset:
ldi temp0,255 ;pocitadlo trvania reset-u (podla normy je to cca 10ms - tu je to cca 100us) ;ENG;counter duration of reset (according to specification is that cca 10ms - here is cca 100us)
WaitForUSBReset:
sbic inputport,DATAminus ;cakanie az sa zmeni D+ na 0 ;ENG;waiting till change D+ to 0
rjmp Main
dec temp0
brne WaitForUSBReset
rcall USBReset
rjmp Main
ProcPrepareOutContinuousBuffer:
rcall PrepareOutContinuousBuffer ;priprav pokracovanie odpovede do buffera ;ENG;prepare next sequence of answer to buffer
ldi ActionFlag,DoReadySendAnswer
rjmp Main
ProcReceiveSetupData:
ldi USBBufptrY,InputBufferBegin ;pointer na zaciatok prijimacieho buffera ;ENG;pointer to begin of receiving buffer
mov ByteCount,InputBufferLength ;dlzka vstupneho buffera ;ENG;length of input buffer
rcall DecodeNRZI ;prevod kodovania NRZI na bity ;ENG;transfer NRZI coding to bits
rcall MirrorInBufferBytes ;prehodit poradie bitov v bajtoch ;ENG;invert bits order in bytes
rcall BitStuff ;odstranenie bit stuffing ;ENG;removal of bitstuffing
;rcall CheckCRCIn ;kontrola CRC ;ENG;rcall CheckCRCIn ;check CRC
rcall PrepareUSBOutAnswer ;pripravenie odpovede do vysielacieho buffera ;ENG;prepare answers to transmitting buffer
ldi ActionFlag,DoReadySendAnswer
rjmp Main
;********************************************************************
;* Main program END ;ENG;* Main program END
;********************************************************************
;------------------------------------------------------------------------------------------
;********************************************************************
;* Interrupt0 interrupt handler ;ENG;* Interrupt0 interrupt handler
;********************************************************************
INT0Handler: ;prerusenie INT0 ;ENG;interrupt INT0
in backupSREG,SREG
push temp0
push temp1
ldi temp0,3 ;pocitadlo trvania log0 ;ENG;counter of duration log0
ldi temp1,2 ;pocitadlo trvania log1 ;ENG;counter of duration log1
;cakanie na zaciatok paketu ;ENG;waiting for begin packet
CheckchangeMinus:
sbis inputport,DATAminus ;cakanie az sa zmeni D- na 1 ;ENG;waiting till change D- to 1
rjmp CheckchangeMinus
CheckchangePlus:
sbis inputport,DATAplus ;cakanie az sa zmeni D+ na 1 ;ENG;waiting till change D+ to 1
rjmp CheckchangePlus
DetectSOPEnd:
sbis inputport,DATAplus
rjmp Increment0 ;D+ =0 ;ENG;D+ =0
Increment1:
ldi temp0,3 ;pocitadlo trvania log0 ;ENG;counter of duration log0
dec temp1 ;kolko cyklov trvala log1 ;ENG;how many cycles takes log1
nop
breq USBBeginPacket ;ak je to koniec SOP - prijimaj paket ;ENG;if this is end of SOP - receive packet
rjmp DetectSOPEnd
Increment0:
ldi temp1,2 ;pocitadlo trvania log1 ;ENG;counter of duration log1
dec temp0 ;kolko cyklov trvala log0 ;ENG;how many cycles take log0
nop
brne DetectSOPEnd ;ak nenastal SOF - pokracuj ;ENG;if there isn't SOF - continue
rjmp EndInt0HandlerPOP2
EndInt0Handler:
pop ACC
pop RS232BufptrX
pop temp3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -