fl { PropForth5.5(DevKernel) USB Bus I/F Chip CH376 2016/12/31 9:53:49 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 \ =========================================================================== \ 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" { h31 1 CH376_Tx \ Issue DISK_MOUNT USB_byte st? h14 = \ USB_INIT_SUCCESS } begin h31 1 CH376_Tx \ Issue DISK_MOUNT USB_byte st? h14 = \ USB_INIT_SUCCESS until \ if ." >USB Mounted -- ok" cr copyData here W@ d36 dump here W@ 8 + d28 prtString \ else \ ." >USB Mounted -- Failed" cr \ then 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 1 over = if ." 12" then then drop cr else ." error" then cr ; \ Get status \ ( -- n1 ) n1:state code : getStatus h22 1 CH376_Tx \ GET_STATUS USB_byte ; \ Set file name [How to use:setFileName FILENAME] \ ( -- ) : setFileName h2F 1 CH376_Tx \ SET_FILE_NAME \ Copy filename to free space parsenw C@++ dup dup >r \ Push length of string if \ Copy file name to free space bounds here W@ rot2 do i C@ over C! 1+ loop drop else \ Drop because ther is no character 2drop then r> \ Pop number of string \ Do when length of string is 0 dup 0= if drop 2 h2F here W@ C! h2A here W@ 1+ C! then \ Send filename here W@ swap 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 ; \ 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 h35 1 CH376_Tx \ FILE_ERACE USB_byte dup h14 = if drop ." Successfully to delete file" else ." Failed to delete file. Erro code:" . 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 ; \ 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 /* ] \ ( -- ) : dir setFileName printDir ; \ Search sub-directory \ ( -- ) : dir_* 0 h2A h2F 4 CH376_Tx printDir ; \ Print sub-directory (how to use:subDir DIRECTORY-NAME) \ ( -- ) : subDir setFileName openFile dir_* ; \ Create file [How to use:createFile FILENAME] \ ( -- ) : createFile setFileName h34 1 CH376_Tx \ FILE_CRETATE USB_byte h14 = \ USB_INIT_SUCCESS if ." Creating file" then 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 ; \ 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 0 0 0 else hFF hFF hFF hFF then 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 ; \ Initilize USB memory \ ( -- ) : init_USBmemory init_CH376 6 set_USB_Mode DiscConnectionStatus diskMount diskCapacity diskQuery ; \ Append data to file end \ ( n1 -- ) n1:cstr : appendFile 0 filePoint \ Set pointer to file end writeFile ; { setFileName TEST1.TXT hex openFile . st? readFile st? 1 filePoint d1000 delms 0 closeFile st? setFileName cr st? openFile cr st? getFileSize st? setFileName TEST8.TXT openFile . c" 12345" writeFile 1 filePoint d1000 delms 1 closeFile st? setFileName TEST8.TXT openFile . c" Parallax" appendFile 1 filePoint d1000 delms 1 closeFile st? }