294 lines
10 KiB
C
294 lines
10 KiB
C
/*
|
|
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
|
* Copyright 2016 NXP
|
|
* All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
/*${standard_header_anchor}*/
|
|
#include "usb_device_config.h"
|
|
#include "usb.h"
|
|
#include "usb_device.h"
|
|
|
|
#include "usb_device_class.h"
|
|
#include "usb_device_hid.h"
|
|
#include "usb_device_ch9.h"
|
|
#include "usb_device_descriptor.h"
|
|
#include "device_mouse.h"
|
|
|
|
#include "fsl_device_registers.h"
|
|
#include "clock_config.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "board.h"
|
|
|
|
#if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
|
|
#include "usb_phy.h"
|
|
#endif
|
|
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
|
|
usb_status_t USB_DeviceHidMouseAction(uint32_t moveAction);
|
|
static usb_status_t USB_DeviceHidMouseCallback(class_handle_t handle, uint32_t event, void *param);
|
|
static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param);
|
|
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
|
|
extern usb_hid_mouse_struct_t g_UsbDeviceHidMouse;
|
|
|
|
extern usb_device_class_struct_t g_UsbDeviceHidMouseConfig;
|
|
|
|
/* Set class configurations */
|
|
usb_device_class_config_struct_t g_UsbDeviceHidConfig[1] = {{
|
|
USB_DeviceHidMouseCallback, /* HID mouse class callback pointer */
|
|
(class_handle_t)NULL, /* The HID class handle, This field is set by USB_DeviceClassInit */
|
|
&g_UsbDeviceHidMouseConfig, /* The HID mouse configuration, including class code, subcode, and protocol, class type,
|
|
transfer type, endpoint address, max packet size, etc.*/
|
|
}};
|
|
|
|
/* Set class configuration list */
|
|
usb_device_class_config_list_struct_t g_UsbDeviceHidConfigList = {
|
|
g_UsbDeviceHidConfig, /* Class configurations */
|
|
USB_DeviceCallback, /* Device callback pointer */
|
|
1U, /* Class count */
|
|
};
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
|
|
/* Update mouse pointer location. */
|
|
usb_status_t USB_DeviceHidMouseAction(uint32_t moveAction)
|
|
{
|
|
static int8_t x = 0U;
|
|
static int8_t y = 0U;
|
|
|
|
if (moveAction & (0x01))
|
|
{
|
|
/* Move up. Discrease Y value. */
|
|
g_UsbDeviceHidMouse.buffer[1] = 0U;
|
|
g_UsbDeviceHidMouse.buffer[2] = (uint8_t)(-10);
|
|
y--;
|
|
}
|
|
if (moveAction & (0x01 << 1))
|
|
{
|
|
/* Move down. Increase Y value. */
|
|
g_UsbDeviceHidMouse.buffer[1] = 0U;
|
|
g_UsbDeviceHidMouse.buffer[2] = 10U;
|
|
y++;
|
|
}
|
|
if (moveAction & (0x01 << 2))
|
|
{
|
|
/* Move left. Discrease X value. */
|
|
g_UsbDeviceHidMouse.buffer[1] = (uint8_t)(-10);
|
|
g_UsbDeviceHidMouse.buffer[2] = 0U;
|
|
x--;
|
|
}
|
|
if (moveAction & (0x01 << 3))
|
|
{
|
|
/* Move left. Discrease X value. */
|
|
g_UsbDeviceHidMouse.buffer[1] = (uint8_t)(10);
|
|
g_UsbDeviceHidMouse.buffer[2] = 0U;
|
|
x--;
|
|
}
|
|
|
|
if (g_UsbDeviceHidMouse.attach != 0)
|
|
{
|
|
/* Send mouse report to the host */
|
|
return USB_DeviceHidSend(g_UsbDeviceHidMouse.hidHandle, USB_HID_MOUSE_ENDPOINT_IN, g_UsbDeviceHidMouse.buffer,
|
|
USB_HID_MOUSE_REPORT_LENGTH);
|
|
}
|
|
else
|
|
{
|
|
return kStatus_USB_Error;
|
|
}
|
|
}
|
|
|
|
/* The hid class callback */
|
|
static usb_status_t USB_DeviceHidMouseCallback(class_handle_t handle, uint32_t event, void *param)
|
|
{
|
|
usb_status_t error = kStatus_USB_InvalidRequest;
|
|
|
|
switch (event)
|
|
{
|
|
case kUSB_DeviceHidEventSendResponse:
|
|
error = kStatus_USB_Success;
|
|
|
|
break;
|
|
case kUSB_DeviceHidEventGetReport:
|
|
case kUSB_DeviceHidEventSetReport:
|
|
case kUSB_DeviceHidEventRequestReportBuffer:
|
|
break;
|
|
case kUSB_DeviceHidEventGetIdle:
|
|
case kUSB_DeviceHidEventGetProtocol:
|
|
case kUSB_DeviceHidEventSetIdle:
|
|
case kUSB_DeviceHidEventSetProtocol:
|
|
error = kStatus_USB_Success;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
/* The device callback */
|
|
static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param)
|
|
{
|
|
usb_status_t error = kStatus_USB_InvalidRequest;
|
|
uint16_t *temp16 = (uint16_t *)param;
|
|
uint8_t *temp8 = (uint8_t *)param;
|
|
|
|
switch (event)
|
|
{
|
|
case kUSB_DeviceEventBusReset:
|
|
{
|
|
/* USB bus reset signal detected */
|
|
g_UsbDeviceHidMouse.attach = 0U;
|
|
g_UsbDeviceHidMouse.currentConfiguration = 0U;
|
|
error = kStatus_USB_Success;
|
|
#if (defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
|
|
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
|
|
/* Get USB speed to configure the device, including max packet size and interval of the endpoints. */
|
|
if (kStatus_USB_Success == USB_DeviceClassGetSpeed(DEVICE_CONTROLLER_ID, &g_UsbDeviceHidMouse.speed))
|
|
{
|
|
USB_DeviceSetSpeed(handle, g_UsbDeviceHidMouse.speed);
|
|
}
|
|
#endif
|
|
}
|
|
break;
|
|
#if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U))
|
|
case kUSB_DeviceEventAttach:
|
|
{
|
|
usb_echo("USB device attached.\r\n");
|
|
/*Add one delay here to make the DP pull down long enough to allow host to detect the previous
|
|
* disconnection.*/
|
|
SDK_DelayAtLeastUs(5000, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
|
|
USB_DeviceRun(g_UsbDeviceHidMouse.deviceHandle);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventDetach:
|
|
{
|
|
usb_echo("USB device detached.\r\n");
|
|
g_UsbDeviceHidMouse.attach = 0;
|
|
USB_DeviceStop(g_UsbDeviceHidMouse.deviceHandle);
|
|
}
|
|
break;
|
|
#endif
|
|
case kUSB_DeviceEventSetConfiguration:
|
|
if (0U == (*temp8))
|
|
{
|
|
g_UsbDeviceHidMouse.attach = 0;
|
|
g_UsbDeviceHidMouse.currentConfiguration = 0U;
|
|
error = kStatus_USB_Success;
|
|
}
|
|
else if (USB_HID_MOUSE_CONFIGURE_INDEX == (*temp8))
|
|
{
|
|
/* Set device configuration request */
|
|
g_UsbDeviceHidMouse.attach = 1U;
|
|
g_UsbDeviceHidMouse.currentConfiguration = *temp8;
|
|
error = kStatus_USB_Success;
|
|
}
|
|
else
|
|
{
|
|
/* no action */
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventSetInterface:
|
|
if (g_UsbDeviceHidMouse.attach)
|
|
{
|
|
/* Set device interface request */
|
|
uint8_t interface = (uint8_t)((*temp16 & 0xFF00U) >> 0x08U);
|
|
uint8_t alternateSetting = (uint8_t)(*temp16 & 0x00FFU);
|
|
|
|
if (interface < USB_HID_MOUSE_INTERFACE_COUNT)
|
|
{
|
|
if (alternateSetting < USB_HID_MOUSE_INTERFACE_ALTERNATE_COUNT)
|
|
{
|
|
g_UsbDeviceHidMouse.currentInterfaceAlternateSetting[interface] = alternateSetting;
|
|
error = kStatus_USB_Success;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetConfiguration:
|
|
if (param)
|
|
{
|
|
/* Get current configuration request */
|
|
*temp8 = g_UsbDeviceHidMouse.currentConfiguration;
|
|
error = kStatus_USB_Success;
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetInterface:
|
|
if (param)
|
|
{
|
|
/* Get current alternate setting of the interface request */
|
|
uint8_t interface = (uint8_t)((*temp16 & 0xFF00U) >> 0x08U);
|
|
if (interface < USB_HID_MOUSE_INTERFACE_COUNT)
|
|
{
|
|
*temp16 = (*temp16 & 0xFF00U) | g_UsbDeviceHidMouse.currentInterfaceAlternateSetting[interface];
|
|
error = kStatus_USB_Success;
|
|
}
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetDeviceDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get device descriptor request */
|
|
error = USB_DeviceGetDeviceDescriptor(handle, (usb_device_get_device_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetConfigurationDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get device configuration descriptor request */
|
|
error = USB_DeviceGetConfigurationDescriptor(handle,
|
|
(usb_device_get_configuration_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetStringDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get device string descriptor request */
|
|
error = USB_DeviceGetStringDescriptor(handle, (usb_device_get_string_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetHidDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get hid descriptor request */
|
|
error = USB_DeviceGetHidDescriptor(handle, (usb_device_get_hid_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetHidReportDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get hid report descriptor request */
|
|
error =
|
|
USB_DeviceGetHidReportDescriptor(handle, (usb_device_get_hid_report_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
case kUSB_DeviceEventGetHidPhysicalDescriptor:
|
|
if (param)
|
|
{
|
|
/* Get hid physical descriptor request */
|
|
error = USB_DeviceGetHidPhysicalDescriptor(handle,
|
|
(usb_device_get_hid_physical_descriptor_struct_t *)param);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return error;
|
|
}
|