📄 keyspan_pda.s
字号:
addc a, dph mov dph, a ; dph = desc_strings[a]. big endian! (handy) ;; it looks like my adapter uses a revision of the EZUSB that ;; contains "rev D errata number 8", as hinted in the EzUSB example ;; code. I cannot find an actual errata description on the Cypress ;; web site, but from the example code it looks like this bug causes ;; the length of string descriptors to be read incorrectly, possibly ;; sending back more characters than the descriptor has. The workaround ;; is to manually send out all of the data. The consequence of not ;; using the workaround is that the strings gathered by the kernel ;; driver are too long and are filled with trailing garbage (including ;; leftover strings). Writing this out by hand is a nuisance, so for ;; now I will just live with the bug. movx a, @dptr mov r1, a inc dptr movx a, @dptr mov r2, a mov dptr, SUDPTRH mov a, r1 movx @dptr, a mov dptr, SUDPTRL mov a, r2 movx @dptr, a ;; done ljmp setup_done_ack setup_Get_Descriptor_not_string: ljmp setup_stall setup_breq_not_06: cjne r2, #0x08, setup_breq_not_08 ;; Get_Configuration. always 1. return one byte. ;; this is reusable mov a, #1setup_return_one_byte: mov dptr, IN0BUF movx @dptr, a mov a, #1 mov dptr, IN0BC movx @dptr, a ljmp setup_done_acksetup_breq_not_08: cjne r2, #0x09, setup_breq_not_09 ;; 09: Set_Configuration. ignored. ljmp setup_done_acksetup_breq_not_09: cjne r2, #0x0a, setup_breq_not_0a ;; 0a: Get_Interface. get the current altsetting for int[wIndexL] ;; since we only have one interface, ignore wIndexL, return a 0 mov a, #0 ljmp setup_return_one_bytesetup_breq_not_0a: cjne r2, #0x0b, setup_breq_not_0b ;; 0b: Set_Interface. set altsetting for interface[wIndexL]. ignored ljmp setup_done_acksetup_breq_not_0b: ljmp setup_stall setup_done_ack: ;; now clear HSNAK mov dptr, EP0CS mov a, #0x02 movx @dptr, a sjmp setup_donesetup_stall: ;; unhandled. STALL ;EP0CS |= bmEPSTALL mov dptr, EP0CS movx a, @dptr orl a, EP0STALLbit movx @dptr, a sjmp setup_done setup_done: pop acc pop dph1 pop dpl1 pop dph pop dpl pop dps reti;;; ============================================================== set_baud: ; baud index in r3 ;; verify a < 10 mov a, r3 jb ACC.7, set_baud__badbaud clr c subb a, #10 jnc set_baud__badbaud mov a, r3 rl a ; a = index*2 add a, #LOW(baud_table) mov dpl, a mov a, #HIGH(baud_table) addc a, #0 mov dph, a ;; TODO: shut down xmit/receive ;; TODO: wait for current xmit char to leave ;; TODO: shut down timer to avoid partial-char glitch movx a,@dptr ; BAUD_HIGH mov RCAP2H, a mov TH2, a inc dptr movx a,@dptr ; BAUD_LOW mov RCAP2L, a mov TL2, a ;; TODO: restart xmit/receive ;; TODO: reenable interrupts, resume tx if pending clr c ; c=0: success retset_baud__badbaud: setb c ; c=1: failure ret ;;; ==================================================control_pins: cjne r1, #0x41, control_pins_incontrol_pins_out: mov a, r3 ; wValue[0] holds new bits: b7 is new DTR, b2 is new RTS xrl a, #0xff ; 1 means active, 0V, +12V ? anl a, #0x84 mov r3, a mov dptr, OUTC movx a, @dptr ; only change bits 7 and 2 anl a, #0x7b ; ~0x84 orl a, r3 movx @dptr, a ; other pins are inputs, bits ignored ljmp setup_done_ackcontrol_pins_in: mov dptr, PINSC movx a, @dptr xrl a, #0xff ljmp setup_return_one_byte;;; ======================================== ISR_Ep2in: push dps push dpl push dph push dpl1 push dph1 push acc mov a,EXIF clr acc.4 mov EXIF,a ; clear INT2 first mov dptr, IN07IRQ ; clear USB int mov a,#04h movx @dptr,a ;; do stuff lcall start_in pop acc pop dph1 pop dpl1 pop dph pop dpl pop dps retiISR_Ep2out: push dps push dpl push dph push dpl1 push dph1 push acc mov a,EXIF clr acc.4 mov EXIF,a ; clear INT2 first mov dptr, OUT07IRQ ; clear USB int mov a,#04h movx @dptr,a ;; do stuff ;; copy data into buffer. for now, assume we will have enough space mov dptr, OUT2BC ; get byte count movx a,@dptr mov r1, a clr a mov dps, a mov dptr, OUT2BUF ; load DPTR0 with source mov dph1, #HIGH(tx_ring) ; load DPTR1 with target mov dpl1, tx_ring_inOUT_loop: movx a,@dptr ; read inc dps ; switch to DPTR1: target inc dpl1 ; target = tx_ring_in+1 movx @dptr,a ; store mov a,dpl1 cjne a, tx_ring_out, OUT_no_overflow sjmp OUT_overflowOUT_no_overflow: inc tx_ring_in ; tx_ring_in++ inc dps ; switch to DPTR0: source inc dptr djnz r1, OUT_loop sjmp OUT_doneOUT_overflow: ;; signal overflow ;; fall throughOUT_done: ;; ack mov dptr,OUT2BC movx @dptr,a ;; start tx acall maybe_start_tx ;acall dump_stat pop acc pop dph1 pop dpl1 pop dph pop dpl pop dps retidump_stat: ;; fill in EP4in with a debugging message: ;; tx_ring_in, tx_ring_out, rx_ring_in, rx_ring_out ;; tx_active ;; tx_ring[0..15] ;; 0xfc ;; rx_ring[0..15] clr a mov dps, a mov dptr, IN4CS movx a, @dptr jb acc.1, dump_stat__done; busy: cannot dump, old one still pending mov dptr, IN4BUF mov a, tx_ring_in movx @dptr, a inc dptr mov a, tx_ring_out movx @dptr, a inc dptr mov a, rx_ring_in movx @dptr, a inc dptr mov a, rx_ring_out movx @dptr, a inc dptr clr a jnb TX_RUNNING, dump_stat__no_tx_running inc adump_stat__no_tx_running: movx @dptr, a inc dptr ;; tx_ring[0..15] inc dps mov dptr, #tx_ring ; DPTR1: source mov r1, #16dump_stat__tx_ring_loop: movx a, @dptr inc dptr inc dps movx @dptr, a inc dptr inc dps djnz r1, dump_stat__tx_ring_loop inc dps mov a, #0xfc movx @dptr, a inc dptr ;; rx_ring[0..15] inc dps mov dptr, #rx_ring ; DPTR1: source mov r1, #16dump_stat__rx_ring_loop: movx a, @dptr inc dptr inc dps movx @dptr, a inc dptr inc dps djnz r1, dump_stat__rx_ring_loop ;; now send it clr a mov dps, a mov dptr, IN4BC mov a, #38 movx @dptr, adump_stat__done: ret ;;; ============================================================ maybe_start_tx: ;; make sure the tx process is running. jb TX_RUNNING, start_tx_donestart_tx: ;; is there work to be done? mov a, tx_ring_in cjne a,tx_ring_out, start_tx__work ret ; no workstart_tx__work: ;; tx was not running. send the first character, setup the TI int inc tx_ring_out ; [++tx_ring_out] mov dph, #HIGH(tx_ring) mov dpl, tx_ring_out movx a, @dptr mov sbuf, a setb TX_RUNNINGstart_tx_done: ;; can we unthrottle the host tx process? ;; step 1: do we care? mov a, #0 cjne a, tx_unthrottle_threshold, start_tx__maybe_unthrottle_tx ;; nopestart_tx_really_done: retstart_tx__maybe_unthrottle_tx: ;; step 2: is there now room? mov a, tx_ring_out setb c subb a, tx_ring_in ;; a is now write_room. If thresh >= a, we can unthrottle clr c subb a, tx_unthrottle_threshold jc start_tx_really_done ; nope ;; yes, we can unthrottle. remove the threshold and mark a request mov tx_unthrottle_threshold, #0 setb DO_TX_UNTHROTTLE ;; prod rx, which will actually send the message when in2 becomes free ljmp start_in serial_int: push dps push dpl push dph push dpl1 push dph1 push acc jnb TI, serial_int__not_tx ;; tx finished. send another character if we have one clr TI ; clear int clr TX_RUNNING lcall start_txserial_int__not_tx: jnb RI, serial_int__not_rx lcall get_rx_char clr RI ; clear intserial_int__not_rx: ;; return pop acc pop dph1 pop dpl1 pop dph pop dpl pop dps retiget_rx_char: mov dph, #HIGH(rx_ring) mov dpl, rx_ring_in inc dpl ; target = rx_ring_in+1 mov a, sbuf movx @dptr, a ;; check for overflow before incrementing rx_ring_in mov a, dpl cjne a, rx_ring_out, get_rx_char__no_overflow ;; signal overflow retget_rx_char__no_overflow: inc rx_ring_in ;; kick off USB INpipe acall start_in retstart_in: ;; check if the inpipe is already running. mov dptr, IN2CS movx a, @dptr jb acc.1, start_in__done; int will handle it jb DO_TX_UNTHROTTLE, start_in__do_tx_unthrottle ;; see if there is any work to do. a serial interrupt might occur ;; during this sequence? mov a, rx_ring_in cjne a, rx_ring_out, start_in__have_work ret ; nopestart_in__have_work: ;; now copy as much data as possible into the pipe. 63 bytes max. clr a mov dps, a mov dph, #HIGH(rx_ring) ; load DPTR0 with source inc dps mov dptr, IN2BUF ; load DPTR1 with target movx @dptr, a ; in[0] signals that rest of IN is rx data inc dptr inc dps ;; loop until we run out of data, or we have copied 64 bytes mov r1, #1 ; INbuf size counterstart_in__loop: mov a, rx_ring_in cjne a, rx_ring_out, start_in__still_copying sjmp start_in__kickstart_in__still_copying: inc rx_ring_out mov dpl, rx_ring_out movx a, @dptr inc dps movx @dptr, a ; write into IN buffer inc dptr inc dps inc r1 cjne r1, #64, start_in__loop; loopstart_in__kick: ;; either we ran out of data, or we copied 64 bytes. r1 has byte count ;; kick off IN mov dptr, IN2BC mov a, r1 jz start_in__done movx @dptr, a ;; donestart_in__done: ;acall dump_stat retstart_in__do_tx_unthrottle: ;; special sequence: send a tx unthrottle message clr DO_TX_UNTHROTTLE clr a mov dps, a mov dptr, IN2BUF mov a, #1 movx @dptr, a inc dptr mov a, #2 movx @dptr, a mov dptr, IN2BC movx @dptr, a ret putchar: clr TI mov SBUF, aputchar_wait: jnb TI, putchar_wait clr TI ret baud_table: ; baud_high, then baud_low ;; baud[0]: 110 .byte BAUD_HIGH(110) .byte BAUD_LOW(110) ;; baud[1]: 300 .byte BAUD_HIGH(300) .byte BAUD_LOW(300) ;; baud[2]: 1200 .byte BAUD_HIGH(1200) .byte BAUD_LOW(1200) ;; baud[3]: 2400 .byte BAUD_HIGH(2400) .byte BAUD_LOW(2400) ;; baud[4]: 4800 .byte BAUD_HIGH(4800) .byte BAUD_LOW(4800) ;; baud[5]: 9600 .byte BAUD_HIGH(9600) .byte BAUD_LOW(9600) ;; baud[6]: 19200 .byte BAUD_HIGH(19200) .byte BAUD_LOW(19200) ;; baud[7]: 38400 .byte BAUD_HIGH(38400) .byte BAUD_LOW(38400) ;; baud[8]: 57600 .byte BAUD_HIGH(57600) .byte BAUD_LOW(57600) ;; baud[9]: 115200 .byte BAUD_HIGH(115200) .byte BAUD_LOW(115200)desc_device: .byte 0x12, 0x01, 0x00, 0x01, 0xff, 0xff, 0xff, 0x40 .byte 0xcd, 0x06, 0x04, 0x01, 0x89, 0xab, 1, 2, 3, 0x01;;; The "real" device id, which must match the host driver, is that;;; "0xcd 0x06 0x04 0x01" sequence, which is 0x06cd, 0x0104 desc_config1: .byte 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32 .byte 0x09, 0x04, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0x00 .byte 0x07, 0x05, 0x82, 0x03, 0x40, 0x00, 0x01 .byte 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00desc_strings: .word string_langids, string_mfg, string_product, string_serialdesc_strings_end:string_langids: .byte string_langids_end-string_langids .byte 3 .word 0string_langids_end: ;; sigh. These strings are Unicode, meaning UTF16? 2 bytes each. Now ;; *that* is a pain in the ass to encode. And they are little-endian ;; too. Use this perl snippet to get the bytecodes: /* while (<>) { @c = split(//); foreach $c (@c) { printf("0x%02x, 0x00, ", ord($c)); } } */string_mfg: .byte string_mfg_end-string_mfg .byte 3; .byte "ACME usb widgets" .byte 0x41, 0x00, 0x43, 0x00, 0x4d, 0x00, 0x45, 0x00, 0x20, 0x00, 0x75, 0x00, 0x73, 0x00, 0x62, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00, 0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00, 0x73, 0x00string_mfg_end: string_product: .byte string_product_end-string_product .byte 3; .byte "ACME USB serial widget" .byte 0x41, 0x00, 0x43, 0x00, 0x4d, 0x00, 0x45, 0x00, 0x20, 0x00, 0x55, 0x00, 0x53, 0x00, 0x42, 0x00, 0x20, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00, 0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00string_product_end: string_serial: .byte string_serial_end-string_serial .byte 3; .byte "47" .byte 0x34, 0x00, 0x37, 0x00string_serial_end: ;;; ring buffer memory ;; tx_ring_in+1 is where the next input byte will go ;; [tx_ring_out] has been sent ;; if tx_ring_in == tx_ring_out, theres no work to do ;; there are (tx_ring_in - tx_ring_out) chars to be written ;; dont let _in lap _out ;; cannot inc if tx_ring_in+1 == tx_ring_out ;; write [tx_ring_in+1] then tx_ring_in++ ;; if (tx_ring_in+1 == tx_ring_out), overflow ;; else tx_ring_in++ ;; read/send [tx_ring_out+1], then tx_ring_out++ ;; rx_ring_in works the same way .org 0x1000tx_ring: .skip 0x100 ; 256 bytesrx_ring: .skip 0x100 ; 256 bytes .END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -