fl { PropForth5.5(DevKernel) USBmouse driver by USB Bus I/F Chip CH376 2017/01/23 14:28:33 CH376 has 8bit-parallel/SPI/UART as communication. Selected UART(default:9600baud) CH376 Module ------------ Propeller | Vcc|--- 5V P0 ---------|Tx | P1 ---------|Rx | P2 ---------|INT | | Gnd | ------------ | GND serial 9600bps Noparity Startbit1 Databit9 Stopbit1 time ------------------------------------------------------------>> ------- --------- ------------ | | | | | | | | ----- ----------------------------- idle start bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 stop idle bit bit (always "0") (always "1") } \ =========================================================================== \ Constants \ =========================================================================== 0 wconstant Rx 1 wconstant Tx 2 wconstant INT 1 INT lshift constant INTm d2400 constant baud/4 \ 9600/4 d64 wconstant numBytes \ =========================================================================== \ Variables \ =========================================================================== wvariable inchar wvariable strBuf d14 allot wvariable token \ =========================================================================== \ Main \ =========================================================================== \ Start up CH376 \ ( -- ) : init_CH376 c" Tx Rx baud/4 serial" 5 cogx \ Start serial on cog5 d100 delms inchar 5 cogio 2+ W! \ Set output of cog5 to inchar h100 inchar W! \ Clear inchar ; \ Stop CH376 \ ( -- ) : stop_CH376 0 5 cogio 2+ W! 5 cogreset ; \ Transmit command and data \ ( m[n1].. n1 -- ) n1:number of elements : CH376_Tx >r hAB h57 r> 2+ 0 do begin 5 cogio W@ h100 and until \ Wait until input for serial-cog is under ready-state 5 cogio W! \ Write data to cog5's input loop ; \ Receive data and save them in free area \ ( n1 -- ) n1:number of data : CH376_Rx here W@ swap 0 do begin inchar W@ h100 and 0= until \ Wait until output for serial-cog is under ready-state inchar W@ over C! \ Save output-data of cog5 to free area h100 inchar W! \ Clear inchar 1+ \ Increment free space address loop drop ; \ Read 1byte from USB \ ( -- n1 ) n1:return code from USB : USB_byte 1 CH376_Rx here W@ C@ ; \ Check if USB is available \ ( -- n1 ) n1:not 0 if there is data from CH376S : USB_available inchar W@ hFF and ; \ Get ICchip version \ ( -- ) : chipVer 1 1 CH376_Tx \ Issue GET_IC_VER USB_byte st? h1F and ." IC chip version:" hex . decimal cr ; \ Reset CH376S \ ( -- ) : resetALL 5 1 CH376_Tx \ Issue RESET_ALL ." CH376S module has been reset." d40 delms cr ; \ \ ( n1 -- ) n1:mode code to work mode of USB : set_USB_Mode h15 2 CH376_Tx \ Issue SET_USB_MODE USB_byte h51 dup = \ CMD_RET_SUCCESS if drop ." CMD_SET_USB_MODE operation successfull." USB_byte h15 dup = \ USB_INIT_CONNECT if drop ." USB device is present." else ." USB device is not present. Error code:" . then else ." CH376S error! Error code:" . then cr ; : USBchk begin USB_available 0= if 1 else USB_byte . 0 then until ; \ Wait until INT-pin is active(Lo) \ ( -- ) : INTloop begin ina COG@ INTm and 0= until ." over" ; \ Get status \ ( -- n1 ) n1:state code : getStatus h22 1 CH376_Tx \ GET_STATUS USB_byte ; : getStatus1 h22 1 CH376_Tx \ GET_STATUS \ INTloop USB_byte d10 delms begin USB_available if USB_byte 1 else 0 then until { cnt COG@ begin USB_available if USB_byte ." XX" st? 1 else cnt COG@ over ." YY" st? - d800000 > then until st? nip } ; \ Wait ready-state \ ( -- n1 ) n1:status code : wait INTloop getStatus1 ; : test1 hex init_CH376 0 token W! \ Initialize device inUSB HOST mode[reset USB bus] 7 h15 2 CH376_Tx d10 delms USB_byte h51 = if ." Reset USBbus" cr \ Initialize device inUSB HOST mode[Produce SOF] 6 set_USB_Mode ." SOF" cr \ Set 1.5Mbps Low Speed mode hD8 h17 hB 3 CH376_Tx getStatus h15 = if ." mouse Low speed" cr \ Set USBaddres to 1 (SET_ADDRESS) 1 h45 2 CH376_Tx USB_byte h14 = if ." USB-address is 1" cr \ SET_USB_ADDR to CH376 1 h13 2 CH376_Tx getStatus h14 = if ." CH376-USB-address successful" cr \ Set Configure to 1 1 h49 2 CH376_Tx USB_byte h14 = if ." Configure successful" cr cr begin \ ISSUE_TKN_X h19 token W@ h4E 3 CH376_Tx d100 delms wait h14 = if cnt COG@ begin USB_available if USB_byte . then cnt COG@ over - d80000 > until drop st? cr token W@ h80 xor token W! h27 1 CH376_Tx \ RD_USB_DATA0 USB_byte dup CH376_Rx here W@ swap bounds do i C@ . loop cr else ." Token error" then fkey? swap drop until else ." Configure error" then else ." CH376 USB-address error" then else ." USB-address error" then else ." Low-speed error" then else ." USB-mode error" then cr resetALL stop_CH376 ;