/* * @brief This file contains USB HID Mouse example using USB ROM Drivers. * * @note * Copyright(C) NXP Semiconductors, 2013 * All rights reserved. * * @par * Software that is described herein is for illustrative purposes only * which provides customers with programming information regarding the * LPC products. This software is supplied "AS IS" without any warranties of * any kind, and NXP Semiconductors and its licensor disclaim any and * all warranties, express or implied, including all implied warranties of * merchantability, fitness for a particular purpose and non-infringement of * intellectual property rights. NXP Semiconductors assumes no responsibility * or liability for the use of the software, conveys no license or rights under any * patent, copyright, mask work right, or any other intellectual property rights in * or to any products. NXP Semiconductors reserves the right to make changes * in the software without notification. NXP Semiconductors also makes no * representation or warranty that such application will be suitable for the * specified use without further testing or modification. * * @par * Permission to use, copy, modify, and distribute this software and its * documentation is hereby granted, under NXP Semiconductors' and its * licensor's relevant copyrights in the software, without fee, provided that it * is used in conjunction with NXP Semiconductors microcontrollers. This * copyright, permission, and disclaimer notice must appear in all copies of * this code. */ #include "board.h" #include #include #include "app_usbd_cfg.h" #include "hid_mouse.h" #include "cdc_vcom.h" /***************************************************************************** * Private types/enumerations/variables ****************************************************************************/ /***************************************************************************** * Public types/enumerations/variables ****************************************************************************/ static USBD_HANDLE_T g_hUsb; static uint8_t g_rxBuff[256]; const USBD_API_T *g_pUsbApi; /***************************************************************************** * Private functions ****************************************************************************/ /***************************************************************************** * Public functions ****************************************************************************/ /** * @brief Handle interrupt from USB0 * @return Nothing */ void USB_IRQHandler(void) { USBD_API->hw->ISR(g_hUsb); } /* Find the address of interface descriptor for given class type. */ USB_INTERFACE_DESCRIPTOR *find_IntfDesc(const uint8_t *pDesc, uint32_t intfClass) { USB_COMMON_DESCRIPTOR *pD; USB_INTERFACE_DESCRIPTOR *pIntfDesc = 0; uint32_t next_desc_adr; pD = (USB_COMMON_DESCRIPTOR *) pDesc; next_desc_adr = (uint32_t) pDesc; while (pD->bLength) { /* is it interface descriptor */ if (pD->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE) { pIntfDesc = (USB_INTERFACE_DESCRIPTOR *) pD; /* did we find the right interface descriptor */ if (pIntfDesc->bInterfaceClass == intfClass) { break; } } pIntfDesc = 0; next_desc_adr = (uint32_t) pD + pD->bLength; pD = (USB_COMMON_DESCRIPTOR *) next_desc_adr; } return pIntfDesc; } /** * @brief main routine for blinky example * @return Function should not exit. */ int main(void) { USBD_API_INIT_PARAM_T usb_param; USB_CORE_DESCS_T desc; ErrorCode_t ret = LPC_OK; uint32_t prompt = 0, rdCnt = 0; /* Initialize board and chip */ Board_Init(); /* enable clocks */ Chip_USB_Init(); /* initialize USBD ROM API pointer. */ g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->pUSBD; /* initialize call back structures */ memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T)); usb_param.usb_reg_base = LPC_USB0_BASE; /* WORKAROUND for artf44835 ROM driver BUG: Code clearing STALL bits in endpoint reset routine corrupts memory area next to the endpoint control data. For example When EP0, EP1_IN, EP1_OUT, EP2_IN are used we need to specify 3 here. But as a workaround for this issue specify 4. So that extra EPs control structure acts as padding buffer to avoid data corruption. Corruption of padding memory doesn’t affect the stack/program behaviour. */ usb_param.max_num_ep = 4 + 1; usb_param.mem_base = USB_STACK_MEM_BASE; usb_param.mem_size = USB_STACK_MEM_SIZE; /* Set the USB descriptors */ desc.device_desc = (uint8_t *) USB_DeviceDescriptor; desc.string_desc = (uint8_t *) USB_StringDescriptor; /* Note, to pass USBCV test full-speed only devices should have both * descriptor arrays point to same location and device_qualifier set * to 0. */ desc.high_speed_desc = USB_FsConfigDescriptor; desc.full_speed_desc = USB_FsConfigDescriptor; desc.device_qualifier = 0; /* USB Initialization */ ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { ret = Mouse_Init(g_hUsb, (USB_INTERFACE_DESCRIPTOR *) find_IntfDesc(desc.high_speed_desc, USB_DEVICE_CLASS_HUMAN_INTERFACE), &usb_param.mem_base, &usb_param.mem_size); if (ret == LPC_OK) { /* Init VCOM interface */ ret = vcom_init(g_hUsb, &desc, &usb_param); if (ret == LPC_OK) { /* enable USB interrupts */ NVIC_EnableIRQ(USB0_IRQn); /* now connect */ USBD_API->hw->Connect(g_hUsb, 1); } } } while (1) { /* If everything went well with stack init do the tasks or else sleep */ if (ret == LPC_OK) { /* Do Mouse tasks */ Mouse_Tasks(); /* Check if host has connected and opened the VCOM port */ if ((vcom_connected() != 0) && (prompt == 0)) { vcom_write("Hello World!!\r\n", 15); prompt = 1; } /* If VCOM port is opened echo whatever we receive back to host. */ if (prompt) { rdCnt = vcom_bread(&g_rxBuff[0], 256); if (rdCnt) { vcom_write(&g_rxBuff[0], rdCnt); } } } /* Sleep until next IRQ happens */ __WFI(); } }