Hi,
Zhang
Thank You
the below given code will help some person who search it,
#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 "mouse.h"
#include "fsl_device_registers.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include <stdio.h>
#include <stdlib.h>
#include "pin_mux.h"
#include <stdbool.h>
#include "fsl_power.h"
#include "fsl_mrt.h"
void BOARD_InitHardware(void);
void USB_DeviceClockInit(void);
void USB_DeviceIsrEnable(void);
static usb_status_t USB_DeviceHidMouseAction(void);//Update mouse pointer location. Draw a rectangular rotation
static usb_status_t USB_DeviceHidMouseCallback(class_handle_t handle, uint32_t event, void *param);//The hid class callback.send response if the device is attached
static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param);//The device callback-Get hid physical descriptor request- USB bus reset signal detected-Set device configuration request-Set device interface request-Get current configuration request-Get device descriptor request-
static void USB_DeviceApplicationInit(void);// Initialize the usb stack and class drivers-Get the HID mouse class handle
uint32_t timerInterval;
uint32_t isConnectedToFsHost = 0U;
uint32_t isConnectedToHsHost = 0U;
USB_DMA_NONINIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE) static uint8_t s_MouseBuffer[USB_HID_MOUSE_REPORT_LENGTH];
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
******************************************************************************/
void USB_TimerInt(uint8_t instance, uint8_t enable)
{
MRT_Type *instanceList[] = MRT_BASE_PTRS;
uint32_t mrt_clock;
mrt_clock = CLOCK_GetFreq(kCLOCK_BusClk);
if (enable)
{
/* Start channel 0 */
MRT_StartTimer(instanceList[instance], kMRT_Channel_0, USEC_TO_COUNT(timerInterval, mrt_clock));
}
else
{
/* Stop channel 0 */
MRT_StopTimer(instanceList[instance], kMRT_Channel_0);
/* Clear interrupt flag.*/
MRT_ClearStatusFlags(instanceList[instance], kMRT_Channel_0, kMRT_TimerInterruptFlag);
}
}
void USB0_IRQHandler(void)
{
USB_DeviceLpcIp3511IsrFunction(g_UsbDeviceHidMouse.deviceHandle);
}
void USB_DeviceClockInit(void)
{
/* enable USB IP clock */
CLOCK_EnableUsbfs0DeviceClock(kCLOCK_UsbSrcFro, CLOCK_GetFroHfFreq());
for (int i = 0; i < FSL_FEATURE_USB_USB_RAM; i++)
{
((uint8_t *)FSL_FEATURE_USB_USB_RAM_BASE_ADDRESS)[i] = 0x00U;
}
}
void USB_DeviceIsrEnable(void)
{
uint8_t irqNumber;
uint8_t usbDeviceIP3511Irq[] = USB_IRQS;
irqNumber = usbDeviceIP3511Irq[CONTROLLER_ID - kUSB_ControllerLpcIp3511Fs0];
EnableIRQ((IRQn_Type)irqNumber);
}
/* Update mouse pointer location. Draw a rectangular rotation*/
static usb_status_t USB_DeviceHidMouseAction(void)
{
static int8_t x = 0U;
static int8_t y = 0U;
enum
{
RIGHT,
DOWN,
LEFT,
UP
};
static uint8_t dir = RIGHT;
switch (dir)
{
case RIGHT:
/* Move right. Increase X value. */
g_UsbDeviceHidMouse.buffer[1] = 2U;
g_UsbDeviceHidMouse.buffer[2] = 0U;
x++;
if (x > 99U)
{
dir++;
}
break;
case DOWN:
/* Move down. Increase Y value. */
g_UsbDeviceHidMouse.buffer[1] = 0U;
g_UsbDeviceHidMouse.buffer[2] = 2U;
y++;
if (y > 99U)
{
dir++;
}
break;
case LEFT:
/* Move left. Discrease X value. */
g_UsbDeviceHidMouse.buffer[1] = (uint8_t)(-2);
g_UsbDeviceHidMouse.buffer[2] = 0U;
x--;
if (x < 2U)
{
dir++;
}
break;
case UP:
/* Move up. Discrease Y value. */
g_UsbDeviceHidMouse.buffer[1] = 0U;
g_UsbDeviceHidMouse.buffer[2] = (uint8_t)(-2);
y--;
if (y < 2U)
{
dir = RIGHT;
}
break;
default:
break;
}
/* 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);
}
/* 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_Error;
usb_device_endpoint_callback_message_struct_t *message = (usb_device_endpoint_callback_message_struct_t *)param;
switch (event)
{
case kUSB_DeviceHidEventSendResponse:
/* Resport sent */
if (g_UsbDeviceHidMouse.attach)
{
if ((NULL != message) && (message->length == USB_UNINITIALIZED_VAL_32))
{
return error;
}
error = USB_DeviceHidMouseAction();
}
}
}
/* The device callback */
static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param)
{
usb_status_t error = kStatus_USB_Error;
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;
}
break;
case kUSB_DeviceEventSetConfiguration:
if (0U == (*temp8))
{
g_UsbDeviceHidMouse.attach = 0;
g_UsbDeviceHidMouse.currentConfiguration = 0U;
}
else if (USB_HID_MOUSE_CONFIGURE_INDEX == (*temp8))
{
/* Set device configuration request */
g_UsbDeviceHidMouse.attach = 1U;
g_UsbDeviceHidMouse.currentConfiguration = *temp8;
error = USB_DeviceHidMouseAction();
}
else
{
error = kStatus_USB_InvalidRequest;
}
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)
{
g_UsbDeviceHidMouse.currentInterfaceAlternateSetting[interface] = alternateSetting;
if (alternateSetting == 0U)
{
error = USB_DeviceHidMouseAction();
}
}
}
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;
}
else
{
error = kStatus_USB_InvalidRequest;
}
}
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;
}
}
static void USB_DeviceApplicationInit(void)
{
USB_DeviceClockInit();
/* Set HID mouse to default state */
g_UsbDeviceHidMouse.speed = USB_SPEED_FULL;
g_UsbDeviceHidMouse.attach = 0U;
g_UsbDeviceHidMouse.hidHandle = (class_handle_t)NULL;
g_UsbDeviceHidMouse.deviceHandle = NULL;
g_UsbDeviceHidMouse.buffer = s_MouseBuffer;
/* Initialize the usb stack and class drivers */
if (kStatus_USB_Success !=
USB_DeviceClassInit(CONTROLLER_ID, &g_UsbDeviceHidConfigList, &g_UsbDeviceHidMouse.deviceHandle))
{
usb_echo("USB device mouse failed\r\n");
return;
}
else
{
usb_echo("USB device DCD + HID mouse demo\r\n");
usb_echo("USB device HID mouse demo\r\n");
/* Get the HID mouse class handle */
g_UsbDeviceHidMouse.hidHandle = g_UsbDeviceHidConfigList.config->classHandle;
}
USB_DeviceIsrEnable();
/*USB_DeviceRun could not be called here to avoid DP/DM confliction between DCD function and USB function in case
DCD is enabled. Instead, USB_DeviceRun should be called after the DCD is finished immediately*/
/* Start USB device HID mouse */
/*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);
}
/*!
* @brief Application task function.
*
* This function runs the task for application.
*
* @return None.
*/
void USB_DeviceAppTask(void *parameter)
{
usb_hid_mouse_struct_t *usbDeviceHid = (usb_hid_mouse_struct_t *)parameter;
if (usbDeviceHid->connectStateChanged)
{
usbDeviceHid->connectStateChanged = 0;
if (g_UsbDeviceHidMouse.connectState)
{
/*user need call USB_DeviceRun here to usb function run if dcd function is disabled*/
/*USB_DeviceRun(g_UsbDeviceHidMouse.deviceHandle);*/
usb_echo("USB device attached.\r\n");
}
else
{
usb_echo("USB device detached.\r\n");
}
}
}
void main(void)
{
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
BOARD_InitPins();
BOARD_BootClockFROHF96M();
BOARD_InitDebugConsole();
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*< Turn on USB Phy */
CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
CLOCK_AttachClk(kFRO_HF_to_USB0_CLK);
/* enable usb0 host clock */
CLOCK_EnableClock(kCLOCK_Usbhsl0);
/*According to reference mannual, device mode setting has to be set by access usb host register */
*((uint32_t *)(USBFSH_BASE + 0x5C)) |= USBFSH_PORTMODE_DEV_ENABLE_MASK;
/* disable usb0 host clock */
CLOCK_DisableClock(kCLOCK_Usbhsl0);
USB_DeviceApplicationInit();
}
the output will be seen in the PC, that the mouse cursor will rotate in a square shape.
Thanking You,
Karthikeyan.