1

I've been trying to program a CH552 chip using assembly, however, I've ran into a bit of a problem when trying to implement USB. My device replies to the first GET_DESCRIPTOR (device) fine, as shown by the usbmon log, but the chip is not replying to requests on the new address. Here are the relevant usbmon logs (using grep "80 06" -A1):

ffffa0bf9a27f8c0 416853100 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 416853225 C Ci:1:000:0 0 18 = 12010002 ffffff40 34127856 01000102 0301
--
ffffa0bf9a27f8c0 416979126 S Ci:1:073:0 s 80 06 0100 0000 0012 18 <
ffffa0bf9a27f8c0 416979360 C Ci:1:073:0 -71 0
ffffa0bf9a27f8c0 416979386 S Ci:1:073:0 s 80 06 0100 0000 0012 18 <
ffffa0bf9a27f8c0 416979618 C Ci:1:073:0 -71 0
ffffa0bf9a27f8c0 416979645 S Ci:1:073:0 s 80 06 0100 0000 0012 18 <
ffffa0bf9a27f8c0 416979813 C Ci:1:073:0 -71 0
--
ffffa0bf9a27f8c0 417094197 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417094349 C Ci:1:000:0 0 0
ffffa0bf9a27f8c0 417094372 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417094481 C Ci:1:000:0 0 18 = 80060001 00004000 dd947856 01000102 0301
ffffa0bf9a27f8c0 417094489 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417094648 C Ci:1:000:0 -71 0
--
ffffa0bf9a27f8c0 417313132 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417313357 C Ci:1:000:0 -71 0
ffffa0bf9a27f8c0 417313384 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417313581 C Ci:1:000:0 -71 0
ffffa0bf9a27f8c0 417313608 S Ci:1:000:0 s 80 06 0100 0000 0040 64 <
ffffa0bf9a27f8c0 417313777 C Ci:1:000:0 -71 0

Here is the relevant output from dmesg | tail:

[ 4512.616604] usb 1-2: new full-speed USB device number 73 using xhci_hcd
[ 4512.743379] usb 1-2: device descriptor read/all, error -71
[ 4512.857686] usb 1-2: new full-speed USB device number 74 using xhci_hcd
[ 4512.972745] usb 1-2: device descriptor read/64, error -71
[ 4513.190744] usb 1-2: device descriptor read/64, error -71
[ 4513.292877] usb usb1-port2: attempt power cycle
[ 4513.680700] usb 1-2: new full-speed USB device number 75 using xhci_hcd
[ 4513.680890] usb 1-2: Device not responding to setup address.
[ 4513.885901] usb 1-2: Device not responding to setup address.
[ 4514.092785] usb 1-2: device not accepting address 75, error -71
[ 4514.092896] usb 1-2: WARN: invalid context state for evaluate context command.
[ 4514.206755] usb 1-2: new full-speed USB device number 76 using xhci_hcd
[ 4514.206901] usb 1-2: Device not responding to setup address.
[ 4514.412865] usb 1-2: Device not responding to setup address.
[ 4514.620786] usb 1-2: device not accepting address 76, error -71
[ 4514.620908] usb 1-2: WARN: invalid context state for evaluate context command.
[ 4514.620990] usb usb1-port2: unable to enumerate USB device

And here is my code:

b0      EQU 00000001b
b1      EQU 00000010b
b2      EQU 00000100b
b3      EQU 00001000b
b4      EQU 00010000b
b5      EQU 00100000b
b6      EQU 01000000b
b7      EQU 10000000b

;   GPIO
p1_mod_oc   EQU 092h
p1_dir_pu   EQU 093h
p3_mod_oc   EQU 096h
p3_dir_pu   EQU 097h

;   SFRs
dp      EQU 082h

;   Interrupts
; ie
    e_dis       EQU b6
ie_ex       EQU 0E8h
    ie_usb      BIT ie_ex.2
    
;   Interrupt vectors
int_no_usb  EQU 043h

;   USB
usb_int_fg  EQU 0D8h
    uif_transfer    BIT usb_int_fg.1
usb_int_st  equ 0D9h
    buis_token1 EQU b5
    buis_token0 EQU b4
    mask_uis_token  EQU b5 OR b4
    mask_uis_endp   EQU b3 OR b2 OR b1 OR b0
usb_int_en  EQU 0E1h
    buie_transfer   EQU b1
usb_ctrl    EQU 0E2h
    buc_sys_ctrl1   EQU b5
    buc_sys_ctrl0   EQU b4
    buc_dma_en  EQU b0
usb_dev_ad  EQU 0E3h
udev_ctrl   EQU 0D1h
    bud_port_en EQU b0
uep0_ctrl   EQU 0DCh
    buep_r_tog  EQU b7
    buep_t_tog  EQU b6
    buep_r_res1 EQU b3
    buep_r_res0 EQU b2
    buep_t_res1 EQU b1
    buep_t_res0 EQU b0
uep0_t_len  EQU 0DDh
uep0_dma_h  EQU 0EDh
uep0_dma_l  EQU 0ECh
uep0_dma    EQU 0ECh

;   Custom  USB
; r0
    buep0_stage EQU b0
; r5 used to store tx length


;   ============== MACROS ==============

MOVW    MACRO   dest,       src
    MOV dest,       src
    MOV dest+1,     src+1
ENDM

MOVSX   MACRO   src
    MOV a,      src
    MOVX    @dptr,      a
    INC dptr
    
ENDM

ADDBW   MACRO   dest,       src
    MOV a,      src
    ADD a,      dest
    MOV dest,       a
    JNC #2d
    INC dest+1
ENDM


;   ==============  CODE  ==============

vectors:
    ORG 000h
    LJMP    start

    ORG int_no_usb
    LJMP    usb_isr


start:
    MOV p1_mod_oc,  #00000000b
    MOV p1_dir_pu,  #11110011b
    
    MOV p3_mod_oc,  #00000000b
    MOV p3_dir_pu,  #00000011b
    
    MOV r0,     #0d
    MOV p1,     #00000000b
    MOV p3,     #00000000b
    
    SETB    ea
    CLR e_dis
    SETB    ie_usb
    ORL usb_int_en, #buie_transfer
    
    MOV usb_ctrl,   #(buc_sys_ctrl1 OR buc_dma_en)
    MOV udev_ctrl,  #bud_port_en
    
loop:
    SJMP    loop


usb_isr:
    MOV a,      usb_int_st
    ANL a,      #mask_uis_token
    SWAP    a
    RL  a
    MOV dptr,       #packet_type_jmptbl
    JMP @a+dptr
    
    packet_type_jmptbl:
    AJMP    packet_out
    AJMP    over
    AJMP    packet_in
    AJMP    packet_setup
    
    packet_out:
        MOV uep0_t_len, #0b
        AJMP    over
    
    packet_in:
        MOV a,      r0
        JZ  old_address
        MOV usb_dev_ad, a
        MOV a,      usb_dev_ad
        old_address:
        AJMP    over
    
    packet_setup:
        MOVW    dp,     uep0_dma
        INC dptr
        MOVX    a,      @dptr
        RL  a
        
        MOV dptr,       #brequest_jmptbl
        JMP @a+dptr
        
        brequest_jmptbl:
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    brequest_set_address
        AJMP    brequest_get_descriptor
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    over
        AJMP    over
        
        brequest_get_descriptor:
            MOV a,      #(NOT buep0_stage)
            ANL a,      r0
            XCH a,      r0
            MOV uep0_ctrl,  #11000000b
            MOV uep0_t_len, #18d
            MOVW    dp,     uep0_dma
            MOVSX   #18d
            MOVSX   #1d
            MOVSX   #000h
            MOVSX   #002h
            MOVSX   #0FFh
            MOVSX   #0FFh
            MOVSX   #0FFh
            MOVSX   #64d
            MOVSX   #034h
            MOVSX   #012h
            MOVSX   #078h
            MOVSX   #056h
            MOVSX   #001h
            MOVSX   #000h
            MOVSX   #1d
            MOVSX   #2d
            MOVSX   #3d
            MOVSX   #1d
            
            AJMP    over
            
        brequest_set_address:
            
            MOVW    dp,     uep0_dma
            INC dptr
            INC dptr
            MOVX    a,      @dptr
            MOV r0,     a           
            MOV uep0_ctrl,  #11000000b
            MOV uep0_t_len, #0b
            
            AJMP    over
        
        
    over:
    CLR uif_transfer
    RETI
END

Thanks!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.