|
TIVA C launchpad试用心得2.-usb HID键盘设备
[复制链接]
功能,插上侧面usb口,自动识别驱动后。打开记事本,按下SW1按键,即会向记事本输出一行文字。
用的TI USB库,主要是端口初始化和枚举描述符
源代码下载
2.0_usb_dev_KB.rar
(136.21 KB, 下载次数: 21)
- //*****************************************************************************
- //
- // The HID keyboard device initialization and customization structures.
- //
- //*****************************************************************************
- tHIDKeyboardInstance g_KeyboardInstance;
- const tUSBDHIDKeyboardDevice g_sKeyboardDevice =
- {
- USB_VID_STELLARIS,
- USB_PID_KEYBOARD,
- 500,
- USB_CONF_ATTR_SELF_PWR | USB_CONF_ATTR_RWAKE,
- KeyboardHandler,
- (void *)&g_sKeyboardDevice,
- g_pStringDescriptors,
- NUM_STRING_DESCRIPTORS,
- &g_KeyboardInstance
- };
复制代码- // Set the USB stack mode to Device mode with VBUS monitoring.
- //
- USBStackModeSet(0, USB_MODE_FORCE_DEVICE, 0);
- // Pass our device information to the USB HID device class driver,
- // initialize the USB
- // controller and connect the device to the bus.
- //
- USBDHIDKeyboardInit(0, &g_sKeyboardDevice);
- //
- // Wait for initial configuration to complete.
- //
- UARTprintf("Waiting for host...\n");
- //
- // Main application loop.
- //
-
- while(1)
- {
- // unsigned char ucButtons;
- // unsigned char ucButtonsChanged;
- //
- // Tell the user what we are doing and provide some basic instructions.
- //
- UARTprintf("Waiting for host...\n");
- //
- // Wait here until USB device is connected to a host.
- //
- while(!g_bConnected)
- {
- }
- //
- // Update the status.
- //
- UARTprintf("Host connected...\n");
- //
- // Enter the idle state.
- //
- g_eKeyboardState = STATE_IDLE;
- //
- // Assume that the bus is not currently suspended if we have just been
- // configured.
- //
- bLastSuspend = false;
- //
- // Keep transferring characters from the UART to the USB host for as
- // long as we are connected to the host.
- //
- while(g_bConnected)
- {
- //
- // Remember the current time.
- //
- ulLastTickCount = g_ulSysTickCount;
- //
- // Has the suspend state changed since last time we checked?
- //
- if(bLastSuspend != g_bSuspended)
- {
- //
- // Yes - the state changed so update the display.
- //
- bLastSuspend = g_bSuspended;
- if(bLastSuspend)
- {
- UARTprintf("Bus suspended...\n");
- // Turn on the Green LED.
- //
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
- // Turn off the Green LED.
- //
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
- }
- else
- {
- UARTprintf("Host connected...\n");
- // Turn on the Green LED.
- //
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
- // Turn off the Green LED.
- //
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
- }
- }
- //
- // See if the button was just pressed.
- //
- if(!GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4))
- {
- //
- // If the bus is suspended then resume it. Otherwise, type
- // some "random" characters.
- //
- if(g_bSuspended)
- {
- USBDHIDKeyboardRemoteWakeupRequest(
- (void *)&g_sKeyboardDevice);
- }
- else
- {
- //SendString("Make the Switch to TI Microcontrollers!");
- }
- SendString(KeyChar);
- }
- //
- // Wait for at least 1 system tick to have gone by before we poll
- // the buttons again.
- //
- while(g_ulSysTickCount == ulLastTickCount)
- {
- }
- }
- }
- }
复制代码
|
|