fl { PropForth5.5(DevKernel) USB Bus I/F Chip CH376 2017/01/19 13:48:54 CH376 has 8bit-parallel/SPI/UART as communication. Selected UART(default:9600baud) CH376 Module ------------ Propeller | Vcc|--- 5V P0 ---------|Tx | P1 ---------|Rx | | 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 d2400 constant baud/4 \ 9600/4 d64 wconstant numBytes \ =========================================================================== \ Variables \ =========================================================================== wvariable inchar wvariable strBuf d14 allot \ =========================================================================== \ 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 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 ; \ Check device \ ( n1 -- ) n1:any data n2:inverted n1 : chkDevice dup 6 2 CH376_Tx \ Issue CHECK_EXIST ." Connection to CH376 is " \ 1 CH376_Rx invert hFF and here W@ C@ = invert hFF and USB_byte = if ." ok." else ." failed " then cr ." Receive data from CH376:h" here W@ C@ hex . decimal cr ; \ Get ICchip version \ ( -- ) : chipVer 1 1 CH376_Tx \ Issue GET_IC_VER \ 1 CH376_Rx here W@ C@ h1F and ." IC chip version:" USB_byte st? h1F and ." IC chip version:" hex . decimal cr ; \ Get status \ ( -- n1 ) n1:state code : getStatus h22 1 CH376_Tx \ GET_STATUS USB_byte ; \ Wait ready-status \ ( -- ) : ready? begin getStatus h14 = until ; \ 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 cide:" . then cr ; \ Check disk connection status \ ( -- ) : DiscConnectionStatus ." Checking USB Disk connection status:" h30 1 CH376_Tx \ Issue DISK_CONNECT USB_byte h14 = \ USB_INIT_SUCCESS if ." >Connection to USB -- ok" else ." >Connection to USB -- Failed" then ready? cr ; \ Get data-block from endpoint of USB by RD_USB_DATA0 \ ( -- n1 ) n1:data : copyData h27 1 CH376_Tx \ RD_USB_DATA0 USB_byte CH376_Rx \ Copy data-block into buffer ; \ Print string \ ( n1 n2 -- ) n1:address n2:number of character : prtString bounds do i C@ emit loop cr ; \ Mount USB disk \ ( -- ) : diskMount ." Mounting USB disk" begin h31 1 CH376_Tx \ Issue DISK_MOUNT USB_byte st? h14 = \ USB_INIT_SUCCESS until ." >USB Mounted -- ok" cr copyData here W@ d36 dump here W@ 8 + d28 prtString cr ; \ Print kb \ ( n1 -- ) n1:address : prtVolume here W@ \ Get address dup W@ over 2+ W@ d16 lshift or nip \ Get sector number 2/ . ." kb" cr \ Print volume ; \ Disk Capacity \ ( -- ) : diskCapacity h3E 1 CH376_Tx USB_byte h14 = if copyData \ here W@ 4 dump ." Totale Capacity:" prtVolume else ." error" then cr ; \ Disk Query \ ( -- ) : diskQuery h3F 1 CH376_Tx USB_byte h14 = if copyData ." Logical Total Capacity:" prtVolume ." Free Capacity:" prtVolume ." FAT file system type:FAT" here W@ 8 + C@ 3 over = if ." 32" else 2 over = if ." 16" else ." 12" then then drop cr else ." error" then cr ; \ Copy string to strBuf from parsenw \ ( -- t/f ) true if string length is not 0 : copyBuf parsenw C@++ dup 0= if 2drop 0 else dup strBuf C! \ Save string length bounds strBuf 1+ rot2 do i C@ over C! 1+ loop drop \ Copy string 1 then ; \ Send filename \ ( -- ) : sendFileName h2F 1 CH376_Tx \ SET_FILE_NAME strBuf 1+ strBuf C@ bounds do i C@ begin 5 cogio W@ h100 and until \ Wait until input for serial-cog is under ready-state 5 cogio W! \ Write data(file name) to cog5's input loop begin 5 cogio W@ h100 and until \ Wait until input for serial-cog is under ready-state 0 5 cogio W! \ Write data(end character) to cog5's input ; \ Set file name [How to use:setFileName FILENAME] \ ( -- ) : setFileName copyBuf if sendFileName else ." no filename" then cr ; \ Clear strBuf \ ( -- ) : clr_strBuf 0 strBuf C! ; \ Get filesize \ ( -- ) : getFileSize h68 hC 2 CH376_Tx \ GET_FILE_SIZE 4 CH376_Rx here W@ \ Get address dup 2+ W@ d16 lshift swap W@ or ." FileSize=" . ." byte" cr ; \ Delete file (how to use:deleteFile FILENAME) \ ( -- ) : deleteFile setFileName strBuf C@ 0 > if h35 1 CH376_Tx \ FILE_ERACE USB_byte dup h14 = if drop ." Successfully to delete file" else ." Failed to delete file. Erro code:" . then clr_strBuf then cr ; \ Open file or directory \ ( -- n1 ) n1:status code : openFile h32 1 CH376_Tx \ FILE_OPEN USB_byte dup dup h14 = \ USB_INT_SUCCESS (when opening file) if drop ." >File opened successfully" else h41 = \ ERR_OPEN_DIR (when opening directory) if ." >Directory opened successfully." else dup h1D = if dup ." >Readable inside directory" else dup ." >Failed to open file. Error code:" . thens cr ; \ Close file [How to use:closeFile FILENAME] \ ( n1 -- ) n1:0=close without updating file siza 1=close with updating file size : closeFile ." Closing file" h36 2 CH376_Tx \ FILE_CLOSE USB_byte dup h14 = if drop ." >File closed successfully." else ." >Failed to close file. Error code:" . then cr ; \ Print all file/sub-directory inside directory \ ( --) : printDir openFile h1D = if begin copyData \ Print file name here W@ 8 bounds do i C@ dup h20 <> if emit else drop then loop h2E emit here W@ 8 + 3 bounds do i C@ emit loop \ Next file? h33 1 CH376_Tx \ FILE_ENUM_GO USB_byte h1D = if 0 else 1 then 9 emit until else ." error" then drop cr ; \ Search inside directory \ root directory[ dir /* ] \ sub directory[ dir * ] \ ( -- ) : dir setFileName printDir clr_strBuf ; \ Move to sub-directory (how to use:subDir DIRECTORY-NAME) \ ( -- ) : cd setFileName openFile drop 0 h2A h2F 3 CH376_Tx printDir clr_strBuf ; \ Create file [How to use:createFile FILENAME] \ ( -- ) : createFile setFileName h32 1 CH376_Tx \ FILE_OPEN USB_byte h14 <> \ USB_INT_SUCCESS (when opening file) if sendFileName h34 1 CH376_Tx \ FILE_CRETATE USB_byte h14 = \ USB_INIT_SUCCESS if ." Created file" then else ." This file already exist." 0 closeFile then clr_strBuf cr cr ; \ Read byte \ ( -- n2 ) n1:number of byte n2:true if : readByte 0 d64 h3A 3 CH376_Tx \ BYTE_READ USB_byte h1D = if 1 else 0 then ; \ Read contents of file \ ( -- ) : readFile ." Reading file:" begin readByte if h27 1 CH376_Tx \ RD_USB_DATA0 USB_byte dup CH376_Rx \ Copy data-block into buffer \ Print string here W@ swap bounds do i C@ emit loop 0 else 1 then h3B 1 CH376_Tx \ BYTE_RD_GO USB_byte h14 = and until cr cr ; \ Print string inside file [How to use:printFile FILENAME] \ ( -- ) : printFile setFileName openFile h14 = if readFile 0 closeFile then ; \ Set filepointer position \ ( n1 -- ) n1:1[begining of file] , 0[end of file] : filePoint ." Setting file pointer:Pointer " if 0 else hFF then dup 2dup h39 5 CH376_Tx \ BYTE_LOCATE USB_byte h14 = if ." successfully applied" else ." failed to set" then cr ; \ Write file \ ( n1 -- ) n1:cstr : writeFile ." Writing to file" C@++ dup dup \ ( cstr-addr length length length) 8 rshift swap hFF and h3C 3 CH376_Tx \ BYTE_WRITE ( cstr-addr length ) ." Setting data length" cr USB_byte h1E = if h2D 1 CH376_Tx \ WR_REQ_DATA bounds do begin 5 cogio W@ h100 and until \ Wait until input for serial-cog is under ready-state i C@ 5 cogio W! \ Write data to cog5's input loop ." Writing data to file:" USB_byte . cr h3D 1 CH376_Tx \ BYTE_WR_GO ." Updating file size:" USB_byte . else 2drop ." Error!" then cr ; \ Create sub-directory [How to use:createDir DIRECTORYNAME] \ ( -- ) : createDir setFileName h40 1 CH376_Tx \ DIR_CREATE USB_byte h14 = if ." Creating sub-directory is successful" else ." Creating sub-directory fault" then cr ; \ Initilize USB memory \ ( -- ) : init_USBmemory init_CH376 6 set_USB_Mode DiscConnectionStatus diskMount diskCapacity diskQuery clr_strBuf ; \ [How to use:readDemo FILENAME] \ ( -- ) : readDemo setFileName strBuf C@ 0> if openFile drop readFile 0 closeFile clr_strBuf then ; \ Add string to end [How to use:appendDemo FILENAME] \ ( -- ) : appendDemo setFileName strBuf C@ 0> if openFile drop 0 filePoint c" -append" writeFile 1 closeFile clr_strBuf then ; \ Insert string to top [How to use:overwriteDemo FILENAME] \ ( -- ) : overwriteDemo setFileName strBuf C@ 0> if openFile drop c" -overwrite" writeFile 1 closeFile clr_strBuf then ; { -root directory- dir /* -sub directort- cd /TEST4 }