218 lines
6.1 KiB
C
218 lines
6.1 KiB
C
/*
|
|
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
|
* Copyright 2016 - 2017 NXP
|
|
* All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include "usb_host_config.h"
|
|
#include "usb_host.h"
|
|
#include "fsl_device_registers.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "audio_recorder.h"
|
|
#include "fsl_common.h"
|
|
#include "pin_mux.h"
|
|
#include "clock_config.h"
|
|
#include "board.h"
|
|
#if (defined(FSL_FEATURE_SOC_SYSMPU_COUNT) && (FSL_FEATURE_SOC_SYSMPU_COUNT > 0U))
|
|
#include "fsl_sysmpu.h"
|
|
#endif /* FSL_FEATURE_SOC_SYSMPU_COUNT */
|
|
#include "app.h"
|
|
|
|
#if ((!USB_HOST_CONFIG_KHCI) && (!USB_HOST_CONFIG_EHCI) && (!USB_HOST_CONFIG_OHCI) && (!USB_HOST_CONFIG_IP3516HS))
|
|
#error Please enable USB_HOST_CONFIG_KHCI, USB_HOST_CONFIG_EHCI, USB_HOST_CONFIG_OHCI, or USB_HOST_CONFIG_IP3516HS in file usb_host_config.
|
|
#endif
|
|
|
|
#include "sdmmc_config.h"
|
|
#include "usb_phy.h"
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
usb_host_handle g_hostHandle;
|
|
/* Allocate the memory for the heap. */
|
|
#if defined(configAPPLICATION_ALLOCATED_HEAP) && (configAPPLICATION_ALLOCATED_HEAP)
|
|
USB_DMA_NONINIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE) uint8_t ucHeap[configTOTAL_HEAP_SIZE];
|
|
#endif
|
|
extern sd_card_t g_sd;
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
extern void USB_HostClockInit(void);
|
|
extern void USB_HostIsrEnable(void);
|
|
extern void USB_HostTaskFn(void *param);
|
|
void BOARD_InitHardware(void);
|
|
extern void USB_AudioTask(void *arg);
|
|
extern usb_status_t USB_HostAudioEvent(usb_device_handle deviceHandle,
|
|
usb_host_configuration_handle configurationHandle,
|
|
uint32_t eventCode);
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
|
|
void USB_OTG1_IRQHandler(void)
|
|
{
|
|
USB_HostEhciIsrFunction(g_hostHandle);
|
|
}
|
|
|
|
void USB_HostClockInit(void)
|
|
{
|
|
usb_phy_config_struct_t phyConfig = {
|
|
BOARD_USB_PHY_D_CAL,
|
|
BOARD_USB_PHY_TXCAL45DP,
|
|
BOARD_USB_PHY_TXCAL45DM,
|
|
};
|
|
|
|
CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
|
|
CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
|
|
USB_EhciPhyInit(CONTROLLER_ID, BOARD_XTAL0_CLK_HZ, &phyConfig);
|
|
}
|
|
|
|
void USB_HostIsrEnable(void)
|
|
{
|
|
uint8_t irqNumber;
|
|
|
|
uint8_t usbHOSTEhciIrq[] = USBHS_IRQS;
|
|
irqNumber = usbHOSTEhciIrq[CONTROLLER_ID - kUSB_ControllerEhci0];
|
|
/* USB_HOST_CONFIG_EHCI */
|
|
|
|
/* Install isr, set priority, and enable IRQ. */
|
|
#if defined(__GIC_PRIO_BITS)
|
|
GIC_SetPriority((IRQn_Type)irqNumber, USB_HOST_INTERRUPT_PRIORITY);
|
|
#else
|
|
NVIC_SetPriority((IRQn_Type)irqNumber, USB_HOST_INTERRUPT_PRIORITY);
|
|
#endif
|
|
EnableIRQ((IRQn_Type)irqNumber);
|
|
}
|
|
|
|
void USB_HostTaskFn(void *param)
|
|
{
|
|
USB_HostEhciTaskFunction(param);
|
|
}
|
|
/*!
|
|
* @brief host callback function.
|
|
*
|
|
* device attach/detach callback function.
|
|
*
|
|
* @param deviceHandle device handle.
|
|
* @param configurationHandle attached device's configuration descriptor information.
|
|
* @param eventCode callback event code, please reference to enumeration host_event_t.
|
|
*
|
|
* @retval kStatus_USB_Success The host is initialized successfully.
|
|
* @retval kStatus_USB_NotSupported The application don't support the configuration.
|
|
*/
|
|
usb_status_t USB_HostEvent(usb_device_handle deviceHandle,
|
|
usb_host_configuration_handle configurationHandle,
|
|
uint32_t eventCode)
|
|
{
|
|
usb_status_t status = kStatus_USB_Success;
|
|
switch (eventCode & 0x0000FFFFU)
|
|
{
|
|
case kUSB_HostEventAttach:
|
|
status = USB_HostAudioEvent(deviceHandle, configurationHandle, eventCode);
|
|
break;
|
|
|
|
case kUSB_HostEventNotSupported:
|
|
usb_echo("device not supported.\r\n");
|
|
break;
|
|
|
|
case kUSB_HostEventEnumerationDone:
|
|
status = USB_HostAudioEvent(deviceHandle, configurationHandle, eventCode);
|
|
break;
|
|
|
|
case kUSB_HostEventDetach:
|
|
status = USB_HostAudioEvent(deviceHandle, configurationHandle, eventCode);
|
|
break;
|
|
|
|
case kUSB_HostEventEnumerationFail:
|
|
usb_echo("enumeration failed\r\n");
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
/*!
|
|
* @brief app initialization.
|
|
*/
|
|
void APP_init(void)
|
|
{
|
|
usb_status_t status = kStatus_USB_Success;
|
|
|
|
BOARD_SD_Config(&g_sd, NULL, (USB_HOST_INTERRUPT_PRIORITY - 1U), NULL);
|
|
|
|
USB_HostClockInit();
|
|
|
|
#if ((defined FSL_FEATURE_SOC_SYSMPU_COUNT) && (FSL_FEATURE_SOC_SYSMPU_COUNT))
|
|
SYSMPU_Enable(SYSMPU, 0);
|
|
#endif /* FSL_FEATURE_SOC_SYSMPU_COUNT */
|
|
|
|
status = USB_HostInit(CONTROLLER_ID, &g_hostHandle, USB_HostEvent);
|
|
if (status != kStatus_USB_Success)
|
|
{
|
|
usb_echo("host init error\r\n");
|
|
return;
|
|
}
|
|
USB_HostIsrEnable();
|
|
|
|
usb_echo("host init done\r\n");
|
|
}
|
|
|
|
/*!
|
|
* @brief usb host controller driver task.
|
|
*/
|
|
void USB_HostTask(void *hostHandle)
|
|
{
|
|
while (1)
|
|
{
|
|
USB_HostTaskFn(g_hostHandle);
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* @brief audio task.
|
|
*/
|
|
void USB_HostApplicationTask(void *param)
|
|
{
|
|
USB_HostAudioAppSDcardInit();
|
|
while (1)
|
|
{
|
|
USB_AudioTask(param);
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* @brief main function.
|
|
*/
|
|
int main(void)
|
|
{
|
|
BOARD_ConfigMPU();
|
|
BOARD_InitPins();
|
|
BOARD_BootClockRUN();
|
|
BOARD_InitDebugConsole();
|
|
|
|
APP_init();
|
|
|
|
if (xTaskCreate(USB_HostTask, "usb host task", 2500L / sizeof(portSTACK_TYPE), g_hostHandle, 4U, NULL) != pdPASS)
|
|
{
|
|
usb_echo("create host task error\r\n");
|
|
}
|
|
if (xTaskCreate(USB_HostApplicationTask, "app task", 3000L / sizeof(portSTACK_TYPE), NULL, 3U, NULL) != pdPASS)
|
|
{
|
|
usb_echo("create audio task error\r\n");
|
|
}
|
|
|
|
vTaskStartScheduler();
|
|
|
|
while (1)
|
|
{
|
|
;
|
|
}
|
|
}
|