diff options
Diffstat (limited to 'source/main.c')
-rw-r--r-- | source/main.c | 135 |
1 files changed, 131 insertions, 4 deletions
diff --git a/source/main.c b/source/main.c index ec2c101..64f5bcc 100644 --- a/source/main.c +++ b/source/main.c @@ -38,6 +38,12 @@ #include "pin_mux.h" #include "clock_config.h" #include "fsl_debug_console.h" +#include "fsl_mpu.h" +#include "usb_host_config.h" +#include "usb.h" +#include "usb_host.h" +#include "usb_host_hci.h" +#include "usb_host_devices.h" /* FreeRTOS kernel includes. */ #include "FreeRTOS.h" @@ -46,20 +52,131 @@ #include "timers.h" +#if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI)) +#define CONTROLLER_ID kUSB_ControllerKhci0 +#endif +#if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI)) +#define CONTROLLER_ID kUSB_ControllerEhci0 +#endif + /* Task priorities. */ #define hello_task_PRIORITY (configMAX_PRIORITIES - 1) +#define USB_HOST_INTERRUPT_PRIORITY (5U) /*! * @brief Task responsible for printing of "Hello world." message. */ static void hello_task(void *pvParameters) { for (;;) { - /*PRINTF("Hello world.\r\n");*/ + PRINTF("Hello world.\r\n"); /* Add your code here */ - vTaskSuspend(NULL); + vTaskDelay(5000); } } +usb_host_handle g_HostHandle; + +static usb_status_t USB_HostEvent(usb_device_handle deviceHandle, + usb_host_configuration_handle configurationHandle, + uint32_t eventCode) +{ + usb_status_t status = kStatus_USB_Success; + uint32_t infoValue; + + switch (eventCode) + { + case kUSB_HostEventAttach: + usb_echo("device attached.\r\n"); + // status = USB_HostMsdEvent(deviceHandle, configurationHandle, eventCode); + break; + + case kUSB_HostEventNotSupported: + usb_echo("device not supported.\r\n"); + break; + + case kUSB_HostEventEnumerationDone: + usb_echo("device enumerated.\r\n"); + USB_HostHelperGetPeripheralInformation(deviceHandle, kUSB_HostGetDevicePID, &infoValue); + usb_echo("PID = 0x%x ", infoValue); + USB_HostHelperGetPeripheralInformation(deviceHandle, kUSB_HostGetDeviceVID, &infoValue); + usb_echo("VID = 0x%x \r\n", infoValue); + // status = USB_HostMsdEvent(deviceHandle, configurationHandle, eventCode); + break; + + case kUSB_HostEventDetach: + usb_echo("device removed.\r\n"); + USB_HostCloseDeviceInterface(deviceHandle, NULL); + // status = USB_HostMsdEvent(deviceHandle, configurationHandle, eventCode); + break; + + default: + break; + } + return status; +} + +/*! + * @brief USB isr function. + */ +#if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI)) +void USB0_IRQHandler(void) +{ + USB_HostKhciIsrFunction(g_HostHandle); +} +#endif /* USB_HOST_CONFIG_KHCI */ +#if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI)) +void USBHS_IRQHandler(void) +{ + USB_HostEhciIsrFunction(g_HostHandle); +} +#endif /* USB_HOST_CONFIG_EHCI */ + +static void USB_HostApplicationInit(void) +{ + usb_status_t status = kStatus_USB_Success; + +#if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI)) + #if (defined(FSL_FEATURE_SOC_SCG_COUNT) && (FSL_FEATURE_SOC_SCG_COUNT > 0U)) + CLOCK_EnableUsbfs0Clock(kCLOCK_IpSrcFircAsync, CLOCK_GetFreq(kCLOCK_ScgFircAsyncDiv1Clk)); + #else + CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); + #endif +#endif /* USB_HOST_CONFIG_KHCI */ +#if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI)) + IRQn_Type usbHsIrqs[] = USBHS_IRQS; + usbIrq = usbHsIrqs[CONTROLLER_ID - kUSB_ControllerEhci0]; + CLOCK_EnableUsbhs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); + USB_EhciPhyInit(CONTROLLER_ID, BOARD_XTAL0_CLK_HZ); +#endif /* USB_HOST_CONFIG_EHCI */ +#if ((defined FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT)) + MPU_Enable(MPU, 0); +#endif /* FSL_FEATURE_SOC_MPU_COUNT */ + + status = USB_HostInit(CONTROLLER_ID, &g_HostHandle, USB_HostEvent); + if (status != kStatus_USB_Success) + { + usb_echo("host init error\r\n"); + return; + } + NVIC_SetPriority(USB0_IRQn, USB_HOST_INTERRUPT_PRIORITY); + NVIC_EnableIRQ(USB0_IRQn); + + usb_echo("host init done\r\n"); +} + +static void USB_HostTask(void *param) +{ + while (1) + { +#if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI)) + USB_HostKhciTaskFunction(param); +#endif /* USB_HOST_CONFIG_KHCI */ +#if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI)) + USB_HostEhciTaskFunction(param); +#endif /* USB_HOST_CONFIG_EHCI */ + } +} + /*! * @brief Application entry point. */ @@ -68,10 +185,20 @@ int main(void) { BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); - /* Add your code here */ PRINTF("Hello!\r\n"); + USB_HostApplicationInit(); + + /* Add your code here */ + /* Create RTOS task */ - xTaskCreate(hello_task, "Hello_task", configMINIMAL_STACK_SIZE, NULL, hello_task_PRIORITY, NULL); + if (xTaskCreate(USB_HostTask, "usb host task", 2000L / sizeof(portSTACK_TYPE), g_HostHandle, 4, NULL) != pdPASS) + { + usb_echo("create host task error\r\n"); + } + if(xTaskCreate(hello_task, "Hello_task", configMINIMAL_STACK_SIZE, NULL, hello_task_PRIORITY, NULL) != pdPASS) + { + usb_echo("create hello task error\r\n"); + } vTaskStartScheduler(); for(;;) { /* Infinite loop to avoid leaving the main function */ |