In case anybody wants an example of USB Host HID Keypad, you can try this for the LPC54605J256BD100. I had it working like this: Using RTT you'll have to take out if you don't have a SEGGER probe.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_device_registers.h"
#include "fsl_power.h"
#include "SEGGER_RTT.h"
#include "FreeRTOS.h"
#include "task.h"
#include "usb_host_config.h"
#include "usb_host.h"
#include "usb.h"
#include "usb_host_hid.h"
#include "host_hid_generic.h"
#include "usb_host_ip3516hs.h"
#include "usb_host_hub.h"
#include "usb_host_interface_0_hid_keyboard.h"
#define PRINTF(...) SEGGER_RTT_printf(0, __VA_ARGS__)
#define CONTROLLER_ID kUSB_ControllerIp3516Hs0
static usb_host_handle g_HostHandle;
static usb_host_interface_handle g_HidKeyboardInterface = NULL;
static usb_host_class_handle g_HidKeyboardClassHandle = NULL;
static usb_device_handle g_HidKeyboardDeviceHandle = NULL;
static volatile bool g_KeyboardReady = false;
static uint8_t g_KeyboardReport[8];
static const char *keycode_to_ascii(uint8_t keycode, uint8_t modifiers)
{
(void)modifiers;
static char buf[20];
switch (keycode)
{
case 98: return "0";
case 89: return "1";
case 90: return "2";
case 91: return "3";
case 92: return "4";
case 93: return "5";
case 94: return "6";
case 95: return "7";
case 96: return "8";
case 97: return "9";
case 99: return ".";
case 85: return "*";
case 86: return "-";
case 87: return "+";
case 88: return "[ENTER]";
case 76: return "/";
case 83: return "[NUMLOCK]";
case 42: return "[BACKSPACE]";
case 79: return "[RIGHT]";
case 80: return "[LEFT]";
case 81: return "[DOWN]";
case 82: return "[UP]";
case 75: return "[HOME]";
case 78: return "[PGUP]";
case 77: return "[END]";
case 74: return "[INS]";
case 73: return "[DEL]";
default:
snprintf(buf, sizeof(buf), "[0x%02X]", keycode);
return buf;
}
}
static void USB_HostHidKeyboardRecvCallback(void *param,
uint8_t *buffer,
uint32_t length,
usb_status_t status)
{
(void)param;
if (status == kStatus_USB_Success && buffer != NULL && length > 0)
{
uint8_t modifiers = buffer[0];
uint8_t key = 0;
for (int i = 2; i < 8; i++)
{
if (buffer[i] != 0)
{
key = buffer[i];
break;
}
}
if (key != 0)
{
PRINTF("Key: 0x%02X -> %s", key, keycode_to_ascii(key, modifiers));
if (modifiers)
{
PRINTF(" (Mod:0x%02X)", modifiers);
}
PRINTF("\r\n");
}
else
{
PRINTF("Key released\r\n");
}
}
else if (status != kStatus_USB_Success)
{
PRINTF("HID receive error: %d\r\n", status);
}
if (g_HidKeyboardClassHandle != NULL)
{
USB_HostHidRecv(g_HidKeyboardClassHandle,
g_KeyboardReport,
sizeof(g_KeyboardReport),
USB_HostHidKeyboardRecvCallback,
NULL);
}
}
static void hid_set_interface_callback(void *param,
uint8_t *data,
uint32_t dataLength,
usb_status_t status)
{
(void)param;
(void)data;
(void)dataLength;
if (status != kStatus_USB_Success)
{
PRINTF("SetInterface failed: %d\r\n", status);
return;
}
PRINTF("Interface bound OK\r\n");
USB_HostHidSetProtocol(g_HidKeyboardClassHandle,
USB_HOST_HID_REQUEST_PROTOCOL_BOOT,
NULL,
NULL);
USB_HostHidRecv(g_HidKeyboardClassHandle,
g_KeyboardReport,
sizeof(g_KeyboardReport),
USB_HostHidKeyboardRecvCallback,
NULL);
}
static void USB_StartKeyboardIfReady(void)
{
usb_status_t status;
if (!g_KeyboardReady || g_HidKeyboardDeviceHandle == NULL)
{
return;
}
g_KeyboardReady = false;
status = USB_HostHidInit(g_HidKeyboardDeviceHandle, &g_HidKeyboardClassHandle);
PRINTF("HidInit = %d\r\n", status);
if (status != kStatus_USB_Success)
{
return;
}
status = USB_HostHidSetInterface(g_HidKeyboardClassHandle,
g_HidKeyboardInterface,
0,
hid_set_interface_callback,
NULL);
PRINTF("SetInterface submit = %d\r\n", status);
}
usb_status_t USB_HostEvent(usb_device_handle deviceHandle,
usb_host_configuration_handle configurationHandle,
uint32_t eventCode)
{
usb_host_configuration_t *config;
uint8_t interfaceIndex;
switch (eventCode)
{
case kUSB_HostEventAttach:
g_HidKeyboardDeviceHandle = deviceHandle;
PRINTF("USB Device Attached\r\n");
break;
case kUSB_HostEventEnumerationDone:
if (g_HidKeyboardDeviceHandle != deviceHandle)
{
break;
}
config = (usb_host_configuration_t *)configurationHandle;
for (interfaceIndex = 0; interfaceIndex < config->interfaceCount; interfaceIndex++)
{
usb_host_interface_t *interface = &config->interfaceList[interfaceIndex];
usb_descriptor_interface_t *desc = (usb_descriptor_interface_t *)interface->interfaceDesc;
if (desc == NULL)
{
continue;
}
if (desc->bInterfaceClass == USB_HOST_HID_CLASS_CODE)
{
g_HidKeyboardInterface = interface;
g_HidKeyboardDeviceHandle = deviceHandle;
g_KeyboardReady = true;
PRINTF("HID keyboard detected\r\n");
USB_StartKeyboardIfReady();
return kStatus_USB_Success;
}
}
break;
case kUSB_HostEventDetach:
if (g_HidKeyboardDeviceHandle == deviceHandle)
{
PRINTF("USB keyboard detached\r\n");
g_HidKeyboardDeviceHandle = NULL;
g_HidKeyboardInterface = NULL;
g_HidKeyboardClassHandle = NULL;
g_KeyboardReady = false;
}
break;
default:
break;
}
return kStatus_USB_Success;
}
void USB_HostClockInit(void)
{
#if ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 0U))
CLOCK_EnableUsbhs0HostClock(kCLOCK_UsbSrcUsbPll, 48000000U);
USBHSH->PORTMODE &= ~(1UL << 16);
USBHSH->PORTMODE |= (1UL << 17);
USBHSH->PORTMODE &= ~(1UL << 8);
for (volatile uint32_t d = 0; d < 500000; d++)
{
__NOP();
}
#if ((defined FSL_FEATURE_USBHSH_USB_RAM) && (FSL_FEATURE_USBHSH_USB_RAM > 0U))
for (int i = 0; i < (FSL_FEATURE_USBHSH_USB_RAM >> 2); i++)
{
((uint32_t *)FSL_FEATURE_USBHSH_USB_RAM_BASE_ADDRESS)[i] = 0U;
}
#endif
#endif
}
void USB_HostIsrEnable(void)
{
IRQn_Type irqNumber = USB1_IRQn;
NVIC_SetPriority(irqNumber, USB_HOST_INTERRUPT_PRIORITY);
EnableIRQ(irqNumber);
}
usb_status_t USB_HostApplicationInit(void)
{
usb_status_t status = kStatus_USB_Success;
USB_HostClockInit();
PRINTF("Clock Init done\r\n");
#if ((defined FSL_FEATURE_SOC_SYSMPU_COUNT) && (FSL_FEATURE_SOC_SYSMPU_COUNT))
SYSMPU_Enable(SYSMPU, 0);
#endif
status = USB_HostInit(CONTROLLER_ID, &g_HostHandle, USB_HostEvent);
if (status != kStatus_USB_Success)
{
PRINTF("USB_HostInit FAILED (status=%d)\r\n", status);
return status;
}
PRINTF("USB_HostInit SUCCESS\r\n");
USB_HostIsrEnable();
PRINTF("USB host ISR enabled\r\n");
return status;
}
void USB_HostTaskFn(void *param)
{
#if ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 0U))
USB_HostIp3516HsTaskFunction(param);
#endif
}
static void USB_HostTask(void *param)
{
while (1)
{
USB_HostTaskFn(param);
vTaskDelay(pdMS_TO_TICKS(1));
}
}
static void USB_HostApplicationTask(void *param)
{
while (1)
{
USB_HostHidGenericTask(param);
vTaskDelay(pdMS_TO_TICKS(1));
}
}
void BOARD_InitHardware(void)
{
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitPeripherals();
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY);
POWER_DisablePD(kPDRUNCFG_PD_USB1_PHY);
}
extern "C" void USB1_IRQHandler(void)
{
if (g_HostHandle != NULL)
{
USB_HostIp3516HsIsrFunction(g_HostHandle);
}
}
int main(void)
{
SEGGER_RTT_Init();
BOARD_InitHardware();
USB_HostApplicationInit();
xTaskCreate(USB_HostTask, "USB_Host", 2048, g_HostHandle, 3, NULL);
xTaskCreate(USB_HostApplicationTask, "HID_Generic", 2048, g_HostHandle, 2, NULL);
PRINTF("Starting scheduler...\r\n");
vTaskStartScheduler();
while (1)
{
}
}