/* See virtual_com.h for documentation of this function. */ usb_device_handle USB_VcomInit(void) { usb_device_handle deviceHandle = NULL; USB_DeviceClockInit(); #if (defined(FSL_FEATURE_SOC_SYSMPU_COUNT) && (FSL_FEATURE_SOC_SYSMPU_COUNT > 0U)) SYSMPU_Enable(SYSMPU, 0); #endif /* FSL_FEATURE_SOC_SYSMPU_COUNT */ s_cdcVcom.speed = USB_SPEED_FULL; s_cdcVcom.attach = 0; s_cdcVcom.deviceHandle = NULL; if (kStatus_USB_Success != USB_DeviceInit(CONTROLLER_ID, USB_DeviceCallback, &s_cdcVcom.deviceHandle)) { deviceHandle = NULL; } else { deviceHandle = s_cdcVcom.deviceHandle; USB_DeviceIsrEnable(); USB_DeviceRun(s_cdcVcom.deviceHandle); } return deviceHandle; }
/*! * @brief Application initialization function. * * This function initializes the application. * * @return None. */ void APPInit(void) { uint8_t irqNo; #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0) uint8_t ehciIrq[] = USBHS_IRQS; irqNo = ehciIrq[CONTROLLER_ID - kUSB_ControllerEhci0]; CLOCK_EnableUsbhs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); USB_EhciPhyInit(CONTROLLER_ID, BOARD_XTAL0_CLK_HZ); #endif #if defined(USB_DEVICE_CONFIG_KHCI) && (USB_DEVICE_CONFIG_KHCI > 0) uint8_t khciIrq[] = USB_IRQS; irqNo = khciIrq[CONTROLLER_ID - kUSB_ControllerKhci0]; SystemCoreClockUpdate(); #if ((defined FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED) && (FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED)) CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U); #else CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); #endif /* FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED */ #endif #if (defined(FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT > 0U)) MPU_Enable(MPU, 0); #endif /* FSL_FEATURE_SOC_MPU_COUNT */ /* * If the SOC has USB KHCI dedicated RAM, the RAM memory needs to be clear after * the KHCI clock is enabled. When the demo uses USB EHCI IP, the USB KHCI dedicated * RAM can not be used and the memory can't be accessed. */ #if (defined(FSL_FEATURE_USB_KHCI_USB_RAM) && (FSL_FEATURE_USB_KHCI_USB_RAM > 0U)) #if (defined(FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS) && (FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS > 0U)) for (int i = 0; i < FSL_FEATURE_USB_KHCI_USB_RAM; i++) { ((uint8_t *)FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS)[i] = 0x00U; } #endif /* FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS */ #endif /* FSL_FEATURE_USB_KHCI_USB_RAM */ s_cdcVcom.speed = USB_SPEED_FULL; s_cdcVcom.attach = 0; s_cdcVcom.deviceHandle = NULL; if (kStatus_USB_Success != USB_DeviceInit(CONTROLLER_ID, USB_DeviceCallback, &s_cdcVcom.deviceHandle)) { usb_echo("USB device vcom failed\r\n"); return; } else { usb_echo("USB device CDC vcom demo\r\n"); } NVIC_SetPriority((IRQn_Type)irqNo, USB_DEVICE_INTERRUPT_PRIORITY); NVIC_EnableIRQ((IRQn_Type)irqNo); USB_DeviceRun(s_cdcVcom.deviceHandle); }
/* Application initialization */ static void USB_DeviceApplicationInit(void) { uint8_t irqNumber; #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U) uint8_t usbDeviceEhciIrq[] = USBHS_IRQS; irqNumber = usbDeviceEhciIrq[CONTROLLER_ID - kUSB_ControllerEhci0]; CLOCK_EnableUsbhs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); USB_EhciPhyInit(CONTROLLER_ID, BOARD_XTAL0_CLK_HZ); #endif #if defined(USB_DEVICE_CONFIG_KHCI) && (USB_DEVICE_CONFIG_KHCI > 0U) uint8_t usbDeviceKhciIrq[] = USB_IRQS; irqNumber = usbDeviceKhciIrq[CONTROLLER_ID - kUSB_ControllerKhci0]; SystemCoreClockUpdate(); #if ((defined FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED) && (FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED)) CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U); #else CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); #endif /* FSL_FEATURE_USB_KHCI_IRC48M_MODULE_CLOCK_ENABLED */ #endif #if (defined(FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT > 0U)) MPU_Enable(MPU, 0); #endif /* FSL_FEATURE_SOC_MPU_COUNT */ /* * If the SOC has USB KHCI dedicated RAM, the RAM memory needs to be clear after * the KHCI clock is enabled. When the demo uses USB EHCI IP, the USB KHCI dedicated * RAM can not be used and the memory can't be accessed. */ #if (defined(FSL_FEATURE_USB_KHCI_USB_RAM) && (FSL_FEATURE_USB_KHCI_USB_RAM > 0U)) #if (defined(FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS) && (FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS > 0U)) for (int i = 0; i < FSL_FEATURE_USB_KHCI_USB_RAM; i++) { ((uint8_t *)FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS)[i] = 0x00U; } #endif /* FSL_FEATURE_USB_KHCI_USB_RAM_BASE_ADDRESS */ #endif /* FSL_FEATURE_USB_KHCI_USB_RAM */ /* Set composite device default state */ g_UsbDeviceComposite.speed = USB_SPEED_FULL; g_UsbDeviceComposite.attach = 0U; g_UsbDeviceComposite.deviceHandle = NULL; /* Initialize the usb stack and class drivers */ if (kStatus_USB_Success != USB_DeviceInit(CONTROLLER_ID, USB_DeviceCallback, &g_UsbDeviceComposite.deviceHandle)) { usb_echo("USB device composite demo init failed\r\n"); return; } else { usb_echo("USB device composite demo\r\n"); } USB_DeviceHidKeyboardInit(&g_UsbDeviceComposite); USB_DeviceHidMouseInit(&g_UsbDeviceComposite); /* Install isr, set priority, and enable IRQ. */ NVIC_SetPriority((IRQn_Type)irqNumber, USB_DEVICE_INTERRUPT_PRIORITY); NVIC_EnableIRQ((IRQn_Type)irqNumber); /* Start USB device composite */ USB_DeviceRun(g_UsbDeviceComposite.deviceHandle); }
/* 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; error = kStatus_USB_Success; #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U) /* Get USB speed to configure the device, including max packet size and interval of the endpoints. */ if (kStatus_USB_Success == USB_DeviceClassGetSpeed(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"); 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 (param) { /* Set device configuration request */ g_UsbDeviceHidMouse.attach = 1U; g_UsbDeviceHidMouse.currentConfiguration = *temp8; if (USB_HID_MOUSE_CONFIGURE_INDEX == (*temp8)) { error = USB_DeviceHidMouseAction(); } } 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; default: break; }