/* USB Device layer setup and powering on */ void Setup_USB(void) { /* Enables the CPU_CLK clock on USB */ RST_CLK_PCLKcmd(RST_CLK_PCLK_USB, ENABLE); /* Device layer initialization */ USB_Clock_InitStruct.USB_USBC1_Source = USB_C1HSEdiv2; USB_Clock_InitStruct.USB_PLLUSBMUL = USB_PLLUSBMUL12; USB_DeviceBUSParam.MODE = USB_SC_SCFSP_Full; USB_DeviceBUSParam.SPEED = USB_SC_SCFSR_12Mb; USB_DeviceBUSParam.PULL = USB_HSCR_DP_PULLUP_Set; USB_DeviceInit(&USB_Clock_InitStruct, &USB_DeviceBUSParam); /* Enable all USB interrupts */ USB_SetSIM(USB_SIS_Msk); USB_DevicePowerOn(); /* Enable interrupt on USB */ #ifdef USB_INT_HANDLE_REQUIRED NVIC_EnableIRQ(USB_IRQn); #endif /* USB_INT_HANDLE_REQUIRED */ USB_DEVICE_HANDLE_RESET; }
/* 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); }
/*! * @brief Initialize the common class and the supported classes. * * This function is used to initialize the common class and the supported classes. * * @param[in] controllerId The controller id of the USB IP. Please refer to the enumeration #usb_controller_index_t. * @param[in] configList The class configurations. The pointer must point to the goblal variable. * Please refer to the structure #usb_device_class_config_list_struct_t. * @param[out] handle It is out parameter, is used to return pointer of the device handle to the caller. * The value of parameter is a pointer points the device handle, and this design is uesd to * make simple device align with composite device. For composite device, there are many * kinds of class handle, but there is only one device handle. So the handle points to * a device instead of a class. And the class handle can be got from the * #usb_device_class_config_struct_t::classHandle after the the function successfully. * * @return A USB error code or kStatus_USB_Success. */ usb_status_t USB_DeviceClassInit( uint8_t controllerId, /*!< [IN] Controller ID */ usb_device_class_config_list_struct_t *configList, /*!< [IN] Pointer to class configuration list */ usb_device_handle *handle /*!< [OUT] Pointer to the device handle */ ) { usb_device_common_class_struct_t *classHandle; usb_status_t error = kStatus_USB_Error; uint8_t mapIndex; uint8_t classIndex; if ((NULL == handle) || (NULL == configList) || ((usb_device_callback_t)NULL == configList->deviceCallback)) { return kStatus_USB_InvalidParameter; } /* Allocate a common class driver handle. */ error = USB_DeviceClassAllocateHandle(controllerId, &classHandle); if (kStatus_USB_Success != error) { return error; } /* Save the configuration list */ classHandle->configList = configList; /* Initialize the device stack. */ error = USB_DeviceInit(controllerId, USB_DeviceClassCallback, &classHandle->handle); if (kStatus_USB_Success != error) { USB_DeviceDeinit(classHandle->handle); USB_DeviceClassFreeHandle(controllerId); return error; } /* Initialize the all supported classes according to the configuration list. */ for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++) { for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t)); mapIndex++) { if (classHandle->configList->config[classIndex].classInfomation->type == s_UsbDeviceClassInterfaceMap[mapIndex].type) { (void)s_UsbDeviceClassInterfaceMap[mapIndex].classInit( controllerId, &classHandle->configList->config[classIndex], &classHandle->configList->config[classIndex].classHandle); } } } *handle = classHandle->handle; return error; }
/* 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); }