URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [usb/] [serial/] [xircom_pgs.S] - Rev 79
Go to most recent revision | Compare with Previous | Blame | View Log
/* $Id: loop.s,v 1.23 2000/03/20 09:49:06 warner Exp $** Firmware for the Keyspan PDA Serial Adapter, a USB serial port based on* the EzUSB microcontroller.** (C) Copyright 2000 Brian Warner <warner@lothar.com>** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** "Keyspan PDA Serial Adapter" is probably a copyright of Keyspan, the* company.** This serial adapter is basically an EzUSB chip and an RS-232 line driver* in a little widget that has a DB-9 on one end and a USB plug on the other.* It uses the EzUSB's internal UART0 (using the pins from Port C) and timer2* as a baud-rate generator. The wiring is:* PC0/RxD0 <- rxd (DB9 pin 2) PC4 <- dsr pin 6* PC1/TxD0 -> txd pin 3 PC5 <- ri pin 9* PC2 -> rts pin 7 PC6 <- dcd pin 1* PC3 <- cts pin 8 PC7 -> dtr pin 4* PB1 -> line driver standby** The EzUSB register constants below come from their excellent documentation* and sample code (which used to be available at www.anchorchips.com, but* that has now been absorbed into Cypress' site and the CD-ROM contents* don't appear to be available online anymore). If we get multiple* EzUSB-based drivers into the kernel, it might be useful to pull them out* into a separate .h file.** THEORY OF OPERATION:** There are two 256-byte ring buffers, one for tx, one for rx.** EP2out is pure tx data. When it appears, the data is copied into the tx* ring and serial transmission is started if it wasn't already running. The* "tx buffer empty" interrupt may kick off another character if the ring* still has data. If the host is tx-blocked because the ring filled up,* it will request a "tx unthrottle" interrupt. If sending a serial character* empties the ring below the desired threshold, we set a bit that will send* up the tx unthrottle message as soon as the rx buffer becomes free.** EP2in (interrupt) is used to send both rx chars and rx status messages* (only "tx unthrottle" at this time) back up to the host. The first byte* of the rx message indicates data (0) or status msg (1). Status messages* are sent before any data.** Incoming serial characters are put into the rx ring by the serial* interrupt, and the EP2in buffer sent if it wasn't already in transit.* When the EP2in buffer returns, the interrupt prompts us to send more* rx chars (or status messages) if they are pending.** Device control happens through "vendor specific" control messages on EP0.* All messages are destined for the "Interface" (with the index always 0,* so that if their two-port device might someday use similar firmware, we* can use index=1 to refer to the second port). The messages defined are:** bRequest = 0 : set baud/bits/parity* 1 : unused* 2 : reserved for setting HW flow control (CTSRTS)* 3 : get/set "modem info" (pin states: DTR, RTS, DCD, RI, etc)* 4 : set break (on/off)* 5 : reserved for requesting interrupts on pin state change* 6 : query buffer room or chars in tx buffer* 7 : request tx unthrottle interrupt** The host-side driver is set to recognize the device ID values stashed in* serial EEPROM (0x06cd, 0x0103), program this firmware into place, then* start it running. This firmware will use EzUSB's "renumeration" trick by* simulating a bus disconnect, then reconnect with a different device ID* (encoded in the desc_device descriptor below). The host driver then* recognizes the new device ID and glues it to the real serial driver code.** USEFUL DOCS:* EzUSB Technical Reference Manual: <http://www.anchorchips.com>* 8051 manuals: everywhere, but try www.dalsemi.com because the EzUSB is* basically the Dallas enhanced 8051 code. Remember that the EzUSB IO ports* use totally different registers!* USB 1.1 spec: www.usb.org** HOW TO BUILD:* gcc -x assembler-with-cpp -P -E -o keyspan_pda.asm keyspan_pda.s* as31 -l keyspan_pda.asm* mv keyspan_pda.obj keyspan_pda.hex* perl ezusb_convert.pl keyspan_pda < keyspan_pda.hex > keyspan_pda_fw.h* Get as31 from <http://www.pjrc.com/tech/8051/index.html>, and hack on it* a bit to make it build.** THANKS:* Greg Kroah-Hartman, for coordinating the whole usb-serial thing.* AnchorChips, for making such an incredibly useful little microcontroller.* KeySpan, for making a handy, cheap ($40) widget that was so easy to take* apart and trace with an ohmmeter.** TODO:* lots. grep for TODO. Interrupt safety needs stress-testing. Better flow* control. Interrupting host upon change in DCD, etc, counting transitions.* Need to find a safe device id to use (the one used by the Keyspan firmware* under Windows would be ideal.. can anyone figure out what it is?). Parity.* More baud rates. Oh, and the string-descriptor-length silicon bug* workaround should be implemented, but I'm lazy, and the consequence is* that the device name strings that show up in your kernel log will have* lots of trailing binary garbage in them (appears as ????). Device strings* should be made more accurate.** Questions, bugs, patches to Brian.** -Brian Warner <warner@lothar.com>**/#define HIGH(x) (((x) & 0xff00) / 256)#define LOW(x) ((x) & 0xff)#define dpl1 0x84#define dph1 0x85#define dps 0x86;;; our bit assignments#define TX_RUNNING 0#define DO_TX_UNTHROTTLE 1;; stack from 0x60 to 0x7f: should really set SP to 0x60-1, not 0x60#define STACK #0x60-1#define EXIF 0x91#define EIE 0xe8.flag EUSB, EIE.0.flag ES0, IE.4#define EP0CS #0x7fb4#define EP0STALLbit #0x01#define IN0BUF #0x7f00#define IN0BC #0x7fb5#define OUT0BUF #0x7ec0#define OUT0BC #0x7fc5#define IN2BUF #0x7e00#define IN2BC #0x7fb9#define IN2CS #0x7fb8#define OUT2BC #0x7fc9#define OUT2CS #0x7fc8#define OUT2BUF #0x7dc0#define IN4BUF #0x7d00#define IN4BC #0x7fbd#define IN4CS #0x7fbc#define OEB #0x7f9d#define OUTB #0x7f97#define OEC #0x7f9e#define OUTC #0x7f98#define PINSC #0x7f9b#define PORTBCFG #0x7f94#define PORTCCFG #0x7f95#define OEA #0x7f9c#define IN07IRQ #0x7fa9#define OUT07IRQ #0x7faa#define IN07IEN #0x7fac#define OUT07IEN #0x7fad#define USBIRQ #0x7fab#define USBIEN #0x7fae#define USBBAV #0x7faf#define USBCS #0x7fd6#define SUDPTRH #0x7fd4#define SUDPTRL #0x7fd5#define SETUPDAT #0x7fe8;; usb interrupt : enable is EIE.0 (0xe8), flag is EXIF.4 (0x91).org 0ljmp start;; interrupt vectors.org 23Hljmp serial_int.byte 0.org 43Hljmp USB_Jump_Table.byte 0 ; filled in by the USB core;;; local variables. These are not initialized properly: do it by hand..org 30Hrx_ring_in: .byte 0rx_ring_out: .byte 0tx_ring_in: .byte 0tx_ring_out: .byte 0tx_unthrottle_threshold: .byte 0.org 0x100H ; wants to be on a page boundaryUSB_Jump_Table:ljmp ISR_Sudav ; Setup Data Available.byte 0ljmp 0 ; Start of Frame.byte 0ljmp 0 ; Setup Data Loading.byte 0ljmp 0 ; Global Suspend.byte 0ljmp 0 ; USB Reset.byte 0ljmp 0 ; Reserved.byte 0ljmp 0 ; End Point 0 In.byte 0ljmp 0 ; End Point 0 Out.byte 0ljmp 0 ; End Point 1 In.byte 0ljmp 0 ; End Point 1 Out.byte 0ljmp ISR_Ep2in.byte 0ljmp ISR_Ep2out.byte 0.org 0x200start: mov SP,STACK-1 ; set stack;; clear local variablesclr amov tx_ring_in, amov tx_ring_out, amov rx_ring_in, amov rx_ring_out, amov tx_unthrottle_threshold, aclr TX_RUNNINGclr DO_TX_UNTHROTTLE;; clear fifo with "fe"mov r1, 0mov a, #0xfemov dptr, #tx_ringclear_tx_ring_loop:movx @dptr, ainc dptrdjnz r1, clear_tx_ring_loopmov a, #0xfdmov dptr, #rx_ringclear_rx_ring_loop:movx @dptr, ainc dptrdjnz r1, clear_rx_ring_loop;;; turn on the RS-232 driver chip (bring the STANDBY pin low);;; on Xircom the STANDBY is wired to PB6 and PC4mov dptr, PORTBCFGmov a, #0xBfmovx @dptr, amov dptr, PORTCCFGmov a, #0xefmovx @dptr, a;; set OEC.4mov a, #0x10mov dptr,OECmovx @dptr,a;; clear PC4mov a, #0x00mov dptr,OUTCmovx @dptr,a;; set OEB.6mov a, #0x40mov dptr,OEBmovx @dptr,a;; clear PB6mov a, #0x00mov dptr,OUTBmovx @dptr,a;; set OEC.[17]mov a, #0x82mov dptr,OECmovx @dptr,a;; set PORTCCFG.[01] to route TxD0,RxD0 to serial portmov dptr, PORTCCFGmov a, #0x03movx @dptr, a;; set up interrupts, autovectoring;; set BKPTmov dptr, USBBAVmovx a,@dptrsetb acc.0 ; AVEN bit to 0movx @dptr, amov a,#0x01 ; enable SUDAV: setup data available (for ep0)mov dptr, USBIRQmovx @dptr, a ; clear SUDAVImov dptr, USBIENmovx @dptr, amov dptr, IN07IENmov a,#0x04 ; enable IN2 intmovx @dptr, amov dptr, OUT07IENmov a,#0x04 ; enable OUT2 intmovx @dptr, amov dptr, OUT2BCmovx @dptr, a ; arm OUT2;; mov a, #0x84 ; turn on RTS, DTR;; mov dptr,OUTC;; movx @dptr, amov a, #0x7 ; turn on DTRmov dptr,USBBAVmovx @dptr, amov a, #0x20 ; turn on the RED ledmov dptr,OEAmovx @dptr, amov a, #0x80 ; turn on RTSmov dptr,OUTCmovx @dptr, a;; setup the serial port. 9600 8N1.mov a,#0x53 ; mode 1, enable rx, clear intmov SCON, a;; using timer2, in 16-bit baud-rate-generator mode;; (xtal 12MHz, internal fosc 24MHz);; RCAP2H,RCAP2L = 65536 - fosc/(32*baud);; 57600: 0xFFF2.F, say 0xFFF3;; 9600: 0xFFB1.E, say 0xFFB2;; 300: 0xF63C#define BAUD 9600#define BAUD_TIMEOUT(rate) (65536 - (24 * 1000 * 1000) / (32 * rate))#define BAUD_HIGH(rate) HIGH(BAUD_TIMEOUT(rate))#define BAUD_LOW(rate) LOW(BAUD_TIMEOUT(rate))mov T2CON, #030h ; rclk=1,tclk=1,cp=0,tr2=0(enable later)mov r3, #5acall set_baudsetb TR2mov SCON, #050h#if 0mov r1, #0x40mov a, #0x41send:mov SBUF, ainc aanl a, #0x3Forl a, #0x40; xrl a, #0x02wait1:jnb TI, wait1clr TIdjnz r1, send;done: sjmp done#endifsetb EUSBsetb EAsetb ES0;acall dump_stat;; hey, what say we RENUMERATE! (TRM p.62)mov a, #0mov dps, amov dptr, USBCSmov a, #0x02 ; DISCON=0, DISCOE=0, RENUM=1movx @dptr, a;; now presence pin is floating, simulating disconnect. wait 0.5smov r1, #46renum_wait1:mov r2, #0renum_wait2:mov r3, #0renum_wait3:djnz r3, renum_wait3djnz r2, renum_wait2djnz r1, renum_wait1 ; wait about n*(256^2) 6MHz clocksmov a, #0x06 ; DISCON=0, DISCOE=1, RENUM=1movx @dptr, a;; we are back online. the host device will now re-query usmain: sjmp mainISR_Sudav:push dpspush dplpush dphpush dpl1push dph1push accmov a,EXIFclr acc.4mov EXIF,a ; clear INT2 firstmov dptr, USBIRQ ; clear USB intmov a,#01hmovx @dptr,a;; get request typemov dptr, SETUPDATmovx a, @dptrmov r1, a ; r1 = bmRequestTypeinc dptrmovx a, @dptrmov r2, a ; r2 = bRequestinc dptrmovx a, @dptrmov r3, a ; r3 = wValueLinc dptrmovx a, @dptrmov r4, a ; r4 = wValueH;; main switch on bmRequest.type: standard or vendormov a, r1anl a, #0x60cjne a, #0x00, setup_bmreq_type_not_standard;; standard request: now main switch is on bRequestljmp setup_bmreq_is_standardsetup_bmreq_type_not_standard:;; a still has bmreq&0x60cjne a, #0x40, setup_bmreq_type_not_vendor;; Anchor reserves bRequest 0xa0-0xaf, we use small ones;; switch on bRequest. bmRequest will always be 0x41 or 0xc1cjne r2, #0x00, setup_ctrl_not_00;; 00 is set baud, wValue[0] has baud rate indexlcall set_baud ; index in r3, carry set if errorjc setup_bmreq_type_not_standard__do_stallljmp setup_done_acksetup_bmreq_type_not_standard__do_stall:ljmp setup_stallsetup_ctrl_not_00:cjne r2, #0x01, setup_ctrl_not_01;; 01 is reserved for set bits (parity). TODOljmp setup_stallsetup_ctrl_not_01:cjne r2, #0x02, setup_ctrl_not_02;; 02 is set HW flow control. TODOljmp setup_stallsetup_ctrl_not_02:cjne r2, #0x03, setup_ctrl_not_03;; 03 is control pins (RTS, DTR).ljmp control_pins ; will jump to setup_done_ack,; or setup_return_one_bytesetup_ctrl_not_03:cjne r2, #0x04, setup_ctrl_not_04;; 04 is send break (really "turn break on/off"). TODOcjne r3, #0x00, setup_ctrl_do_break_on;; do break off: restore PORTCCFG.1 to reconnect TxD0 to serial portmov dptr, PORTCCFGmovx a, @dptrorl a, #0x02movx @dptr, aljmp setup_done_acksetup_ctrl_do_break_on:;; do break on: clear PORTCCFG.0, set TxD high(?) (b1 low)mov dptr, OUTCmovx a, @dptranl a, #0xfd ; ~0x02movx @dptr, amov dptr, PORTCCFGmovx a, @dptranl a, #0xfd ; ~0x02movx @dptr, aljmp setup_done_acksetup_ctrl_not_04:cjne r2, #0x05, setup_ctrl_not_05;; 05 is set desired interrupt bitmap. TODOljmp setup_stallsetup_ctrl_not_05:cjne r2, #0x06, setup_ctrl_not_06;; 06 is query roomcjne r3, #0x00, setup_ctrl_06_not_00;; 06, wValue[0]=0 is query write_roommov a, tx_ring_outsetb csubb a, tx_ring_in ; out-1-in = 255 - (in-out)ljmp setup_return_one_bytesetup_ctrl_06_not_00:cjne r3, #0x01, setup_ctrl_06_not_01;; 06, wValue[0]=1 is query chars_in_buffermov a, tx_ring_inclr csubb a, tx_ring_out ; in-outljmp setup_return_one_bytesetup_ctrl_06_not_01:ljmp setup_stallsetup_ctrl_not_06:cjne r2, #0x07, setup_ctrl_not_07;; 07 is request tx unthrottle interruptmov tx_unthrottle_threshold, r3; wValue[0] is threshold valueljmp setup_done_acksetup_ctrl_not_07:ljmp setup_stallsetup_bmreq_type_not_vendor:ljmp setup_stallsetup_bmreq_is_standard:cjne r2, #0x00, setup_breq_not_00;; 00: Get_Status (sub-switch on bmRequestType: device, ep, int)cjne r1, #0x80, setup_Get_Status_not_device;; Get_Status(device);; are we self-powered? no. can we do remote wakeup? no;; so return two zero bytes. This is reusablesetup_return_two_zero_bytes:mov dptr, IN0BUFclr amovx @dptr, ainc dptrmovx @dptr, amov dptr, IN0BCmov a, #2movx @dptr, aljmp setup_done_acksetup_Get_Status_not_device:cjne r1, #0x82, setup_Get_Status_not_endpoint;; Get_Status(endpoint);; must get stall bit for ep[wIndexL], return two bytes, bit in lsb 0;; for now: cheat. TODOsjmp setup_return_two_zero_bytessetup_Get_Status_not_endpoint:cjne r1, #0x81, setup_Get_Status_not_interface;; Get_Status(interface): return two zerossjmp setup_return_two_zero_bytessetup_Get_Status_not_interface:ljmp setup_stallsetup_breq_not_00:cjne r2, #0x01, setup_breq_not_01;; 01: Clear_Feature (sub-switch on wValueL: stall, remote wakeup)cjne r3, #0x00, setup_Clear_Feature_not_stall;; Clear_Feature(stall). should clear a stall bit. TODOljmp setup_stallsetup_Clear_Feature_not_stall:cjne r3, #0x01, setup_Clear_Feature_not_rwake;; Clear_Feature(remote wakeup). ignored.ljmp setup_done_acksetup_Clear_Feature_not_rwake:ljmp setup_stallsetup_breq_not_01:cjne r2, #0x03, setup_breq_not_03;; 03: Set_Feature (sub-switch on wValueL: stall, remote wakeup)cjne r3, #0x00, setup_Set_Feature_not_stall;; Set_Feature(stall). Should set a stall bit. TODOljmp setup_stallsetup_Set_Feature_not_stall:cjne r3, #0x01, setup_Set_Feature_not_rwake;; Set_Feature(remote wakeup). ignored.ljmp setup_done_acksetup_Set_Feature_not_rwake:ljmp setup_stallsetup_breq_not_03:cjne r2, #0x06, setup_breq_not_06;; 06: Get_Descriptor (s-switch on wValueH: dev, config[n], string[n])cjne r4, #0x01, setup_Get_Descriptor_not_device;; Get_Descriptor(device)mov dptr, SUDPTRHmov a, #HIGH(desc_device)movx @dptr, amov dptr, SUDPTRLmov a, #LOW(desc_device)movx @dptr, aljmp setup_done_acksetup_Get_Descriptor_not_device:cjne r4, #0x02, setup_Get_Descriptor_not_config;; Get_Descriptor(config[n])cjne r3, #0x00, setup_stall; only handle n==0;; Get_Descriptor(config[0])mov dptr, SUDPTRHmov a, #HIGH(desc_config1)movx @dptr, amov dptr, SUDPTRLmov a, #LOW(desc_config1)movx @dptr, aljmp setup_done_acksetup_Get_Descriptor_not_config:cjne r4, #0x03, setup_Get_Descriptor_not_string;; Get_Descriptor(string[wValueL]);; if (wValueL >= maxstrings) stallmov a, #((desc_strings_end-desc_strings)/2)clr csubb a,r3 ; a=4, r3 = 0..3 . if a<=0 then stalljc setup_stalljz setup_stallmov a, r3add a, r3 ; a = 2*wValueLmov dptr, #desc_stringsadd a, dplmov dpl, amov a, #0addc a, dphmov 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, @dptrmov r1, ainc dptrmovx a, @dptrmov r2, amov dptr, SUDPTRHmov a, r1movx @dptr, amov dptr, SUDPTRLmov a, r2movx @dptr, a;; doneljmp setup_done_acksetup_Get_Descriptor_not_string:ljmp setup_stallsetup_breq_not_06:cjne r2, #0x08, setup_breq_not_08;; Get_Configuration. always 1. return one byte.;; this is reusablemov a, #1setup_return_one_byte:mov dptr, IN0BUFmovx @dptr, amov a, #1mov dptr, IN0BCmovx @dptr, aljmp 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 0mov a, #0ljmp setup_return_one_bytesetup_breq_not_0a:cjne r2, #0x0b, setup_breq_not_0b;; 0b: Set_Interface. set altsetting for interface[wIndexL]. ignoredljmp setup_done_acksetup_breq_not_0b:ljmp setup_stallsetup_done_ack:;; now clear HSNAKmov dptr, EP0CSmov a, #0x02movx @dptr, asjmp setup_donesetup_stall:;; unhandled. STALL;EP0CS |= bmEPSTALLmov dptr, EP0CSmovx a, @dptrorl a, EP0STALLbitmovx @dptr, asjmp setup_donesetup_done:pop accpop dph1pop dpl1pop dphpop dplpop dpsreti;;; ==============================================================set_baud: ; baud index in r3;; verify a < 10mov a, r3jb ACC.7, set_baud__badbaudclr csubb a, #10jnc set_baud__badbaudmov a, r3rl a ; a = index*2add a, #LOW(baud_table)mov dpl, amov a, #HIGH(baud_table)addc a, #0mov dph, a;; TODO: shut down xmit/receive;; TODO: wait for current xmit char to leave;; TODO: shut down timer to avoid partial-char glitchmovx a,@dptr ; BAUD_HIGHmov RCAP2H, amov TH2, ainc dptrmovx a,@dptr ; BAUD_LOWmov RCAP2L, amov TL2, a;; TODO: restart xmit/receive;; TODO: reenable interrupts, resume tx if pendingclr c ; c=0: successretset_baud__badbaud:setb c ; c=1: failureret;;; ==================================================control_pins:cjne r1, #0x41, control_pins_incontrol_pins_out:;TODO BKPT is DTRmov a, r3 ; wValue[0] holds new bits: b7 is new RTSxrl a, #0xff ; 1 means active, 0V, +12V ?anl a, #0x80mov r3, amov dptr, OUTCmovx a, @dptr ; only change bit 7anl a, #0x7F ; ~0x84orl a, r3movx @dptr, a ; other pins are inputs, bits ignoredljmp setup_done_ackcontrol_pins_in:mov dptr, PINSCmovx a, @dptrxrl a, #0xffljmp setup_return_one_byte;;; ========================================ISR_Ep2in:push dpspush dplpush dphpush dpl1push dph1push accmov a,EXIFclr acc.4mov EXIF,a ; clear INT2 firstmov dptr, IN07IRQ ; clear USB intmov a,#04hmovx @dptr,amov a, #0x20 ; Turn off the green LEDmov dptr,OEAmovx @dptr, a;; do stufflcall start_inmov a, #0x20 ; Turn off the green LEDmov dptr,OEAmovx @dptr, apop accpop dph1pop dpl1pop dphpop dplpop dpsretiISR_Ep2out:push dpspush dplpush dphpush dpl1push dph1push accmov a, #0x10 ; Turn the green LEDmov dptr,OEAmovx @dptr, amov a,EXIFclr acc.4mov EXIF,a ; clear INT2 firstmov dptr, OUT07IRQ ; clear USB intmov a,#04hmovx @dptr,a;; do stuff;; copy data into buffer. for now, assume we will have enough spacemov dptr, OUT2BC ; get byte countmovx a,@dptrmov r1, aclr amov dps, amov dptr, OUT2BUF ; load DPTR0 with sourcemov dph1, #HIGH(tx_ring) ; load DPTR1 with targetmov dpl1, tx_ring_inOUT_loop:movx a,@dptr ; readinc dps ; switch to DPTR1: targetinc dpl1 ; target = tx_ring_in+1movx @dptr,a ; storemov a,dpl1cjne a, tx_ring_out, OUT_no_overflowsjmp OUT_overflowOUT_no_overflow:inc tx_ring_in ; tx_ring_in++inc dps ; switch to DPTR0: sourceinc dptrdjnz r1, OUT_loopsjmp OUT_doneOUT_overflow:;; signal overflow;; fall throughOUT_done:;; ackmov dptr,OUT2BCmovx @dptr,a;; start txacall maybe_start_tx;acall dump_statmov a, #0x20 ; Turn off the green LEDmov dptr,OEAmovx @dptr, apop accpop dph1pop dpl1pop dphpop dplpop dpsretidump_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 amov dps, amov dptr, IN4CSmovx a, @dptrjb acc.1, dump_stat__done; busy: cannot dump, old one still pendingmov dptr, IN4BUFmov a, tx_ring_inmovx @dptr, ainc dptrmov a, tx_ring_outmovx @dptr, ainc dptrmov a, rx_ring_inmovx @dptr, ainc dptrmov a, rx_ring_outmovx @dptr, ainc dptrclr ajnb TX_RUNNING, dump_stat__no_tx_runninginc adump_stat__no_tx_running:movx @dptr, ainc dptr;; tx_ring[0..15]inc dpsmov dptr, #tx_ring ; DPTR1: sourcemov r1, #16dump_stat__tx_ring_loop:movx a, @dptrinc dptrinc dpsmovx @dptr, ainc dptrinc dpsdjnz r1, dump_stat__tx_ring_loopinc dpsmov a, #0xfcmovx @dptr, ainc dptr;; rx_ring[0..15]inc dpsmov dptr, #rx_ring ; DPTR1: sourcemov r1, #16dump_stat__rx_ring_loop:movx a, @dptrinc dptrinc dpsmovx @dptr, ainc dptrinc dpsdjnz r1, dump_stat__rx_ring_loop;; now send itclr amov dps, amov dptr, IN4BCmov a, #38movx @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_incjne a,tx_ring_out, start_tx__workret ; no workstart_tx__work:;; tx was not running. send the first character, setup the TI intinc tx_ring_out ; [++tx_ring_out]mov dph, #HIGH(tx_ring)mov dpl, tx_ring_outmovx a, @dptrmov sbuf, asetb TX_RUNNINGstart_tx_done:;; can we unthrottle the host tx process?;; step 1: do we care?mov a, #0cjne 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_outsetb csubb a, tx_ring_in;; a is now write_room. If thresh >= a, we can unthrottleclr csubb a, tx_unthrottle_thresholdjc start_tx_really_done ; nope;; yes, we can unthrottle. remove the threshold and mark a requestmov tx_unthrottle_threshold, #0setb DO_TX_UNTHROTTLE;; prod rx, which will actually send the message when in2 becomes freeljmp start_inserial_int:push dpspush dplpush dphpush dpl1push dph1push accjnb TI, serial_int__not_tx;; tx finished. send another character if we have oneclr TI ; clear intclr TX_RUNNINGlcall start_txserial_int__not_tx:jnb RI, serial_int__not_rxlcall get_rx_charclr RI ; clear intserial_int__not_rx:;; returnpop accpop dph1pop dpl1pop dphpop dplpop dpsretiget_rx_char:mov dph, #HIGH(rx_ring)mov dpl, rx_ring_ininc dpl ; target = rx_ring_in+1mov a, sbufmovx @dptr, a;; check for overflow before incrementing rx_ring_inmov a, dplcjne a, rx_ring_out, get_rx_char__no_overflow;; signal overflowretget_rx_char__no_overflow:inc rx_ring_in;; kick off USB INpipeacall start_inretstart_in:;; check if the inpipe is already running.mov a,#0x10mov dptr, OEAmovx @dptr,amov dptr, IN2CSmovx a, @dptrjb acc.1, start_in__done; int will handle itjb 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_incjne a, rx_ring_out, start_in__have_workret ; nopestart_in__have_work:;; now copy as much data as possible into the pipe. 63 bytes max.clr amov dps, amov dph, #HIGH(rx_ring) ; load DPTR0 with sourceinc dpsmov dptr, IN2BUF ; load DPTR1 with targetmovx @dptr, a ; in[0] signals that rest of IN is rx datainc dptrinc dps;; loop until we run out of data, or we have copied 64 bytesmov r1, #1 ; INbuf size counterstart_in__loop:mov a, rx_ring_incjne a, rx_ring_out, start_inlocal_irq_enablell_copyingsjmp start_in__kickstart_inlocal_irq_enablell_copying:inc rx_ring_outmov dpl, rx_ring_outmovx a, @dptrinc dpsmovx @dptr, a ; write into IN bufferinc dptrinc dpsinc r1cjne 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 INmov a, #0x10 ; Turn the green LEDmov dptr,OEAmovx @dptr, amov dptr, IN2BCmov a, r1jz start_in__donemovx @dptr, a;; donestart_in__done:;acall dump_statretstart_in__do_tx_unthrottle:;; special sequence: send a tx unthrottle messageclr DO_TX_UNTHROTTLEclr amov dps, amov dptr, IN2BUFmov a, #1movx @dptr, ainc dptrmov a, #2movx @dptr, amov dptr, IN2BCmovx @dptr, aretputchar:clr TImov SBUF, aputchar_wait:jnb TI, putchar_waitclr TIretbaud_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, 0x0104desc_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
Go to most recent revision | Compare with Previous | Blame | View Log
