USB Examples

Table of Contents

Function Sub-Function APIs Description Update Status
irq_handler() none interrupt handle function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() usb_set_pin_en() open the DP and DM of USB and enable 1.5k internal pull-up resistor
usb_irq_init() enable manual interrupt
irq_enable() enable global interrupt
gpio_set_func()
gpio_set_input_en()
gpio_set_output_en()
gpio_set_up_down_resistor()
gpio_set_func(GPIO_PD1, AS_GPIO)
gpio_set_input_en(GPIO_PD1,1)
gpio_set_output_en(GPIO_PD1,0)
gpio_set_up_down_resistor(GPIO_PD1, PM_PIN_PULLUP_10K)
set pin for simulate pressing the left or right key of Mouse
gpio_set_func(GPIO_PD2, AS_GPIO)
gpio_set_input_en(GPIO_PD2,1)
gpio_set_output_en(GPIO_PD2,0)
gpio_set_up_down_resistor(GPIO_PD2, PM_PIN_PULLUP_10K)
set pin for simulate releasing the left or right key of Mouse
main_loop() usb_handle_irq_process() handle with USB interrupt
refer to Driver Demo check whether key is pressed
usb_mouse_hid_report() usb_mouse_hid_report(mouse) send key value of Mouse to USB host

Variables above are defined as below

unsigned char mouse[4];

API-USB-CASE2:KEYBOARD DEVICE

Function Sub-Function APIs Description Update Status
irq_handler() none interrupt handle function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() usb_set_pin_en() open the DP and DM of USB and enable 1.5k internal pull-up resistor
usb_irq_init() enable manual interrupt
usbhw_data_ep_ack() usbhw_data_ep_ack(USB_EDP_KEYBOARD_OUT) set ep3 BUSY(BUSY same as ACK) bit as 1 means output endpoint buffer can receive data from USB host
irq_enable() enable global interrupt
gpio_set_func()
gpio_set_input_en()
gpio_set_output_en()
gpio_set_up_down_resistor()
gpio_set_func(GPIO_PD1, AS_GPIO)
gpio_set_input_en(GPIO_PD1,1)
gpio_set_output_en(GPIO_PD1,0)
gpio_set_up_down_resistor(GPIO_PD1, PM_PIN_PULLUP_10K)
set pin for simulate pressing the left or right key of Mouse
gpio_set_func(GPIO_PD2, AS_GPIO)
gpio_set_input_en(GPIO_PD2,1)
gpio_set_output_en(GPIO_PD2,0)
gpio_set_up_down_resistor(GPIO_PD2, PM_PIN_PULLUP_10K)
set pin for simulate releasing the left or right key of Mouse
main_loop() usb_handle_irq_process() handle with USB interrupt
refer to Driver Demo check whether key is pressed
usb_keyboard_hid_report() usb_keyboard_hid_report(kb_data) send key value of Keyboard to USB host

Variables above are defined as below

unsigned char kb_data[8];

API-USB-CASE3:CDC DEVICE

Function Sub-Function APIs Description Update Status
irq_handler() none interrupt handle function 2019-1-10
main() cpu_wakeup_init() CPU initialization function [Mandatory]
clock_init() clock_init(SYS_CLK_24M_Crystal) Clock initialization function, System Clock is 24M RC by default [optional]
rf_drv_init() rf_drv_init(RF_MODE_BLE_1M) RF mode initialization [optional]
gpio_init() gpio_init(1) GPIO initialization: set the initialization status of all GPIOs [optional]
user_init() usb_set_pin_en() open the DP and DM of USB and enable 1.5k internal pull-up resistor
usb_irq_init() enable manual interrupt
usbhw_data_ep_ack() usbhw_data_ep_ack(USB_EDP_CDC_OUT) set ep5 BUSY(BUSY same as ACK) bit as 1 means output endpoint buffer can receive data from USB host
irq_enable() enable global interrupt
main_loop() usb_handle_irq_process() handle with USB interrupt
refer to Driver Demo check whether data is reveived
usb_cdc_tx_data_to_host() usb_cdc_tx_data_to_host(usb_cdc_data,usb_cdc_data_len) send data from USB host to USB host

Variables above are defined as below

#define USB_CDC_TX_RX_EPSIZE 64
unsigned short usb_cdc_data_len;
unsigned int usb_cdc_tx_cnt;
// Line Coding Structure is as follows:
// Offset | Field | Size | Value | Description
// 0 | dwDTERate | 4 | Number | Data terminal rate, in bits per second
// 4 | bCharFormat | 1 | Number | Stop bits: 0-1/1-1.5/2-2.
// 5 | bParityType | 1 | Number | Parity:0-None/1-Odd/2-Even/3-Mark/4-Space
// 6 | bDataBits | 1 | Number | Data bits (5, 6, 7, 8 or 16).
unsigned char LineCoding[7]={0x00,0xC2,0x01,0x00,0x00,0x00,0x08};

API-USB-CASE4:MICROPHONE DEVICE

    refer to API-AUDIO-CASE1:AMIC To USB

API-USB-CASE5:SPEAKER DEVICE

    refer to API-AUDIO-CASE9:USB To SDM

History Record

Date Description Author
2019-1-10 initial release SP/YY/LJW