[HOST]
void usb_host_init(void) Initializes the host module. Calling this function from the application software enables the host program.
int isUsbConnect(void) Returns the number of currently connected USB devices. If a HUB is simply connected, the number of devices will be 0. If a device is connected directly to the USB connector or to a HUB port connected to the USB connector, a value of 1 or more will be returned. However, if a device that is not supported by the host program is connected, it will not be counted. If a mouse is connected, it will be counted, but if a LAN adapter is connected, it will not be counted. Detection of the connection or removal of USB devices, and enumeration after connection is detected, are performed by calling this function. Application programs must call this function within the application loop. that after the target device is connected, if the device is not removed or a new device is not connected until the application system is stopped, it is not necessary to call this function after the connection of the target device has been confirmed. [MOUSE]
int get_nums_mouse_device(void) Returns the number of USB mice currently connected. If no mouse is connected, returns 0 .
int usb_mouse_read(int dev_no, uint8_t *buff, uint16_t buff_size) Data is read from the mouse specified by dev_no and stored in buff. dev_no starts from 0 and ranges from the value returned by the get_nums_mouse_device() function -1. If the size of the packet read is larger than buff_size, only buff_size data from the beginning of the packet is stored in buff. The rest is discarded. Returns the number of bytes of data read. Returns 0 if the mouse data could not be read. Returns -1 if the mouse specified by dev_no does not exist or an error occurs. There is usually no problem if 8 bytes are allocated for the size of buff, but if the amount of data to be read is unknown (e.g. for a special mouse), allocate 64 bytes.
int usb_mouse_set_protocol(int dev_no, uint8_t protocol) Changes the protocol of the mouse specified by dev_no to the protocol specified by protocol. dev_no starts from 0 and goes up to the value returned by the get_nums_mouse_device() function -1. Set protocol to 0 for the Boot protocol, and 1 for the Report protocol. Returns 0 if there is no error in changing the protocol, or -1 if an error occurs. Even if the return value is 0, some mice cannot change the protocol. Some mice cannot change back to the Report protocol once they have changed to the Boot protocol. [KEYBOARD]
int get_nums_keyboard_device(void) Returns the number of USB keyboards currently connected. If no keyboard is connected, it returns 0 .
int usb_keyboard_read(int dev_no, uint8_t *buff, uint16_t buff_size) Data is read from the keyboard specified by dev_no and stored in buff. dev_no starts from 0 and ranges from the value returned by the get_nums_keyboard_device() function -1. If the size of the read packet is larger than buff_size, only buff_size data from the beginning of the packet is stored in buff. The rest is discarded. Returns the number of bytes of data read. Returns 0 if the keyboard data could not be read. Returns -1 if the keyboard specified by dev_no does not exist or an error occurs. There is usually no problem if 8 bytes are allocated for the size of buff. If a special keyboard is connected and the amount of data is unknown, allocate 64 bytes.
int usb_keyboard_set_protocol(int dev_no, uint8_t protocol) Changes the protocol of the keyboard specified by dev_no to the protocol specified by protocol. dev_no ranges from 0 to the value returned by the get_nums_keyboard_device() function minus 1. Sets protocol to 0 for the Boot protocol and 1 for the Report protocol. Returns 0 if there is no error in changing the protocol, or -1 if an error occurs.
int usb_keyboard_set_report(int dev_no, uint8_t led_ctrl) This controls the lighting of the three LEDs NumLock, CapsLock, and ScrlLock of the keyboard specified by dev_no. dev_no starts from 0 and goes up to the value returned by the get_nums_keyboard_device() function -1. The values to set for led_ctrl are as follows: bit 0: NUM_LOC bit 1: CAPS_LOC bit 2: SCROLL_LOCK 0:消灯 1:点灯Returns -1 if the keyboard specified by dev_no does not exist or if an error occurs. Returns 0 if there is no error. Has no effect on keyboards that do not implement LEDs. [HID GENERIC]
int get_nums_hid_generic_device(void) Returns the number of currently connected HID generic devices. If no devices are connected, returns 0. A HID generic device is a device with class code (3), subclass code (0), and protocol code (0). Game controllers, etc., fall into this category.
int usb_hid_generic_read(int dev_no, uint8_t *buff, uint16_t buff_size) Data is read from the device specified by dev_no and stored in buff. dev_no starts from 0 and ranges from the value returned by the get_nums_hid_generic_device() function -1. If the size of the read packet is larger than buff_size, only buff_size bytes from the beginning of the packet are stored in buff. The rest is discarded. Returns the number of bytes of data read. Returns 0 if data could not be read from the device. Returns -1 if the device specified by dev_no does not exist or an error occurs. The size of buff should be 8 bytes for low speed devices, and 64 bytes for full speed or faster devices.
int usb_hid_generic_write(int dev_no, uint8_t *data, uint16_t data_size) Writes data_size bytes of data from the data buffer to the device specified by dev_no. dev_no starts from 0 and ranges from -1, the value returned by the get_nums_hid_generic_device() function. The size of the data to be written specified by data_size can exceed the USB packet size. If the device specified by dev_no does not exist or an error occurs, -1 is returned. Otherwise, the number of bytes sent (same as data_size) is returned. [AUDIO MIDI] int get_nums_midi_device(void) Returns the number of currently connected MIDI devices. If no MIDI devices are connected, returns 0 .
int usb_midi_read(int dev_no, uint8_t *buff, uint16_t buff_size) Reads data from the MIDI device specified by dev_no and stores it in buff. dev_no starts from 0 and ranges from the value returned by the get_nums_midi_device() function -1. If the size of the read packet is larger than buff_size, only buff_size bytes from the beginning of the packet are stored in buff. The rest is discarded. Returns the number of bytes read. Returns 0 if data could not be read from the device. Returns -1 if the device specified by dev_no does not exist or an error occurs. Ensure that the size of the data buffer is 64 bytes.
int usb_midi_write(int dev_no, uint8_t *data, uint16_t data_size) Writes data_size bytes of data from the data buffer to the MIDI device specified by dev_no. dev_no starts from 0 and ranges from the value returned by the get_nums_midi_device() function -1. The size of the data to be written specified by data_size can exceed the USB packet size. If the device specified by dev_no does not exist or an error occurs, -1 is returned. Otherwise, the number of bytes sent (same as data_size) is returned. [USB SERIAL]
int get_nums_serial_device(void) Returns the number of currently connected serial devices. If no serial devices are connected, returns 0 .
int set_serial_port_parameter(int dev_no, int32_t *param) Sets the parameter of the serial device specified by dev_no to the value specified by param. dev_no starts from 0 and goes up to the value returned by the get_nums_serial_device() function -1. The values to set in param are as follows: param[5]: param[0] = BAUDRATE; param[1] = DATABIT; param[2] = STOPBIT; param[3] = PARITYBIT; param[4] = FLOW BAUDRATE: 115200/57600/38400/19200/9600/4800/2400 STOPBIT: 0/1/2 0=1bit 1=1.5bit 2=2bit PARITYBIT: 0/1/2/3/4 0=none 1=odd 2=even 3=mark 4=space DATABIT: 8/7/6/5 FLOW: 0/1/2/4 0=none 1=rts/cts 2=dtr/dsr 4=xon/xoffIf the device specified by dev_no does not exist or an error occurs, it returns -1. Otherwise, it returns 0. Note that the FLOW control setting is invalid for CDC devices. It is valid for FTDI serial devices. During the process of recognizing the serial device, the HOST sets the following default parameters: 115200bps, data length 8 bits, stop bit 1, no parity, no flow control. int get_serial_port_parameter(int dev_no, int32_t *param) Reads the parameters of the serial device specified by dev_no and stores them in param. dev_no starts from 0 and goes up to the value returned by the get_nums_serial_device() function -1. If the device specified by dev_no does not exist or an error occurs, -1 is returned. Otherwise, 0 is returned. FTDI serial devices do not have the parameter reading function, so -1 is always returned. int set_serial_port_state(int dev_no, uint8_t ctrl) Sets the state of the serial device specified by dev_no to the value specified by ctrl. dev_no starts at 0 and goes up to the value returned by the get_nums_serial_device() function -1. The values you can set for ctrl are as follows: bit0=DTR 1=on 0=off bit1=RTS 1=on 0=offIf the device specified by dev_no does not exist or an error occurs, it returns -1. Otherwise, it returns 0. HOST sets DTR and RTS on during the process of identifying the serial device . int usb_serial_read(int dev_no, uint8_t *buff, uint16_t buff_size) Receives data from the serial device specified by dev_no and stores it in buff. dev_no starts from 0 to the value returned by the get_nums_serial_device() function -1. If the size of the received data is larger than buff_size, only buff_size is stored in buff and the rest is discarded. Returns the number of bytes of data received. Returns 0 if data could not be received from the device. Returns -1 if the device specified by dev_no does not exist or an error occurs. int usb_serial_write(int dev_no, uint8_t *data, uint16_t data_size) Sends data_size bytes of data from the data buffer to the serial device specified by dev_no. dev_no starts from 0 and ranges from the value returned by the get_nums_serial_device() function -1. The size of the data specified by data_size can exceed the USB packet size. If the device specified by dev_no does not exist or an error occurs, -1 is returned. Otherwise, the number of bytes sent (same as data_size) is returned. [MSC] int get_nums_msc_device(void) Returns the number of currently connected MSC devices (USB memory, USB card reader, etc.). If no MSC devices are connected, returns 0 . int usb_msc_sector_read(int dev_no, uint32_t read_sector, uint8_t *buff) One sector of data (512 bytes) is read from the sector specified by read_sector from the MSC device specified by dev_no and stored in buff. dev_no starts from 0 and ranges from the value returned by get_nums_msc_device() function -1. The sectors specified by read_sector range from 0 to the value returned by usb_msc_capacity_read(). The size of buff must be 512 bytes. -1 is returned if the device specified by dev_no does not exist or an error occurs. -2 is returned if the device is not ready. 0 is returned if the read was successful. The ready state of the device can be confirmed by calling the usb_msc_test_unit_ready() function. int usb_msc_sector_write(int dev_no, uint32_t write_sector, uint8_t *buff) Writes one sector of data from buff to the sector specified by write_sector of the MSC device specified by dev_no. dev_no starts from 0 and ranges from the value returned by the get_nums_msc_device() function -1. The sectors specified by write_sector range from 0 to the value returned by usb_msc_capacity_read(). -1 is returned if the device specified by dev_no does not exist or an error occurs. -2 is returned if the device is not ready. 0 is returned if the write was successful. The ready state of the device can be confirmed by calling the usb_msc_test_unit_ready() function. int usb_msc_test_unit_ready(int dev_no) Issues the SCSI command TEST_UNIT_READY to the MSC device specified by dev_no to check the ready state of the device. This command is issued repeatedly for 2.5 seconds until the device returns ready. Returns 0 when the device is confirmed as ready. Returns -1 when the device specified by dev_no does not exist or when the device is not confirmed as ready within 2.5 seconds. The result of the status check (ready or unready) is stored inside the host and is updated each time this function is called. The HOST calls this function during the MSC device recognition process to check the device's ready state. int usb_msc_request_sense(int dev_no) Issues the SCSI command REQUEST_SENSE to the MSC device specified by dev_no. Returns 0 if the command is executed successfully. Returns -1 if the device specified by dev_no does not exist or an error occurs. The sense data read by command execution is discarded internally and cannot be confirmed. uint32_t usb_msc_capacity_read(int dev_no) Issues the SCSI command READ_CAPACITY to the MSC device specified by dev_no. If the command is successful, it returns the last logical sector number. If the device specified by dev_no does not exist or an error occurs, it returns 0. |