Already ported code download: http://download.csdn.net/detail/lxj_com2006/3793887
Welcome Exchange: qq:67016879 email:lxj_com@163.com
Directory
1 Overview: ... 1
2 source file list: ... 2
3 Structure diagram: ... 3
4 circuit schematic diagram reference: ... 4
4.1 Choice of communication interface: ... 5
5 Demo program Detailed: ... 5
5.1 Header file: ... 5
5.2 Buffer Buffer: ... 6
5.3 Debugging Information: ... 6
5.4 SPI communication mode: ... 6
5.4.1 SPI MCU io foot configuration: ... 7
5.5 UART Communication mode: ... 7
5.5.1 Serial port parameter setting: ... 7
5.5.2 Ch376datasheet serial Port Description: ... 8
5.6 Detection u disk Insert: ... 8
5.7 u disk pull-out: ... 9
5.8 Create directory: ... 10
5.9 Write File: ... 10
5.10 Read the file: ... 11
5.11 Delete File: ... 12
5.12 Main () function: ... 13
6 Summary: ... 14
6.1 Advantages: ... 14
6.2 Insufficient: ... 14
1 Overview:
CH376 as a domestic USB chip (Nanjing Qin Heng Electronic), support Host,slave mode, compared to the previous version ch375,ch376 internal FAT12/16/32 file system, to avoid the MCU need to deal with a large file system library, the system cost to very low, Even a 89c51 can run smoothly, memory consumption is very low. and provides partial upward compatibility. Price with similar products on the market almost (such as: pb375,sl811, etc.), about more than 10 yuan, the advantage is slightly better stability.
Compiler: Keil3 C51 v8.18
Single-chip microcomputer: SST89X516RD
USB chip: ch376s (Sop-28pin)
Communication interface: SPI, serial port, bus and port (slightly)
Because there is no special requirements for reading and writing speed, plus the use of IO foot considerations, this article does not explain the CH376 bus mode and IO analog bus mode, using SPI or serial communication.
This article mainly explains CH376 to host mode to read and write USB disk for example, CH376 function is very powerful, such as: with SD card, keyboard, mouse, printer and other communications, slave mode, and did not describe.
CH376 is bad for case support, all paths, the file name must be all uppercase (datasheet), with small, mixed name test, the function will return success, but actually no effect, to the PC to view and did not produce a file or read and write success, but also resulting in a file with the same name.
2 source file list:
HAL. H declares some device interface functions, such as ch376_port_init,minitch376host,xwritech376cmd,xreadch376data,xwritech376data, and these functions are in SPI_SW. C,UART_HW. c, such as the implementation of these files, is tantamount to the communication interface Unified encapsulation once, for the upper-level unified call, this is done because the CH376 support 3 Total communication mode: MCU local buses (bus), SPI, UART, when users choose a different mode of communication and the upper application is irrelevant, It is convenient to compile different communication modules (detailed below). So like SPI_SW. C,UART_HW. C and so these files can only be compiled, can not compile at the same time, will produce errors, function redefined.
CH376 official solution is to use the #include contains different C files for different communication module selection, this article is not recommended, this will destroy the C language file structure (in general) Include only. h header files, add c files to Keil project management as needed (#include C file mode cannot debug breakpoint rows).
3 structure diagram:
From the machine generally refers to: U disk, SD card, mouse, printer and other specific functions of the USB device, in response to the USB host command, host equipment generally refers to the PC, SCM, CPU and other active issue of command equipment. Its purpose is to carry on the USB bus communication, the specification just.
4 circuit schematic diagram reference:
4.1 Choice of communication interface:
5 DEMO program detailed:
HOST. C 5.1 header files:
5.2 Buffer buffer:
BUF size According to the actual need to apply
5.3 Debugging information:
Only a serial port in the case of serial communication will be unable to use the serial port debugging information
5.4 SPI communication mode:
The SPI communication mode is selected and the UART communication mode cannot be selected at the same time
5.4.1 SPI MCU io foot configuration:
SPI_SW. C
Software simulation SPI, Hardware SPI (abbreviated).
5.5 UART communication mode:
The UART traffic mode is selected and the SPI communication mode cannot be selected at the same time, and debug debugging information must be turned off (only one serial port condition).
5.5.1 Serial port parameter setting:
UART_HW. C
Hardware serial port, baud rate settings, CH376 support hardware, software baud rate settings.
5.5.2 CH376 datasheet serial port description:
5.6 Testing u disk insert:
This article adopts the way of inquiry, interrupt mode (slightly)
The Ch376diskconnect function can be a wait or timeout return, by Def_int_timeout (see File_system. H) macro control, in the actual product application, generally will not always wait, need to set a timeout, by the SCM clock frequency decision, timeout can call callback function, waiting for users to retry or exit.
If you return successfully, you can call a callback function, CH376 official program does not provide such a model, you can make simple changes.
5.7 u disk pull out:
5.8 Creating a directory:
The default starts at the root directory.
5.9 Writing files:
This example simplifies the error handling, in the practical application, must analyze the error code, and provides the user retries, exits or the corresponding method, S is the error code, may inquire the CH376INC.H file. In the safest case, the 3rd parameter of the Ch376bytewrite function is the actual number of bytes written, preferably compared to the number of bytes to write, in this brief.
5.10 Read the file:
Block read files, large and small files are adapted to read a piece can call a callback function, please determine whether the realsize and readsize size is equal, the situation is theoretically.
5.11 Delete files:
Deleting files will not be recoverable
5.12 Main () function:
In practical applications, if there are multiple devices using polling mode to detect the status or data, the main cycle time slice control, allocate the time slice of each device and delay (the U disk detection delay mentioned above), can not enter the waiting state.
If only the user initiative to operate a single mode, just consider timeout, that is, do not have to deal with the passive state (U disk insert interrupt), such as: Insert U disk after the user must press or other operation to read and write U disk.
6 Summary : 6.1 Advantages:
From the test results, CH376 on U disk Read and write effect is not bad, the network has published some test speed results, and mouth mode can reach dozens of K per second, it is simple to use, resources occupy very few, inexpensive, good stability.
6.2 Less :
From the demo program, the overall structure is not very good, users need to deal with a lot of work, such as: callback mechanism, main cycle time slice control, events and error handling.
Too many file library functions, but also to distinguish between the root directory, multi-level directory, the use of functions are not the same (such as: New multi-level directory is not convenient to use), unlike the operating system encapsulated similar: Open,read,write,del and other common functions.
Bad capitalization, can only handle all uppercase directories or file names (also support lowercase file names, examples EXAM11 have instructions, do not recommend the use of long file names). The FAT file system is case-insensitive, but CH376 can build the same file in the same directory with a large, lowercase name that should be a bug.