TMC32_QJB/USB/USB_init.c
2026-04-30 16:23:12 +08:00

117 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2019, Tongxin Microelectronics Co., Ltd.
* All rights reserved.
*
* FileName: usb_init.c
* SCFID:
* Feature:
* Version: V0.1
*
* History:
* 2019-06-04
* 1. Original version 0.1
* */
#include "global.h"
extern uint8_t gLicenseFlag; //标记是否有LicensegLicenseFlag = 0表明license有效
void USBClkInit(void)
{
int i;
RCC_Security2PeriphClockCmd(RCC_SEC2Periph_USB, ENABLE);
USBPHYCKCON1 |= Bit1_En;
USBPHYCKCON2 |= Bit12_En | Bit13_En | Bit16_En;
USBPHYCON = 0x00000004; // Disable Pull-up
for(i=0;i<100;i++)
{}
RCC_USBClockConfig(RCC_USB_CLKSRC_USBPHY,ENABLE);
for(i=0;i<100;i++)
{}
USBDCON |= Bit20_En;
for(i=0;i<10;i++) // Waiting for USB controller reset,16 cycles needed
{
__NOP();
__NOP();
__NOP();
}
USBPHYCON = 0x00000007; // Enable DP PU
}
/***************************************************************************
* Function: USBInit
* Description: usb Init
* Input: NULL
* Output: NULL
* Return: NULL
* Other: NULL
**************************************************************************/
void USBInit(void)
{
// USBDCON |= Bit20_En;
USBDCON |= Bit16_En | Bit0_En; //reset irq en, ep0 irq en
// SCUCM3 |= Bit8_En; // usb clock enable
USBPCON = 0x00000002; // USB physical layer power on and no suspend
USBPHYCON = 0x00000005; // Enable DP PU
USBPHYCKCON1 |= Bit1_En;
USBPHYCKCON2 |= Bit16_En;
// XTAL and PLL Init
// SCUXTAL = 0x03; // enable xtal in and out IO
// USB parameter init
st_usbDeviceRequest.bStdRqstFlag = 0;
st_usbDeviceRequest.bClsRqstFlag = 0;
st_usbDeviceRequest.bVdrRqstFlag = 0;
g_sUsbEp0TxLen = 0;
g_bUsbEp0TxIntFlag = 0;
g_bUsbEp0RcvDataFlag = 0;
g_bUsbEp1RcvDataFlag = 0;
g_sUsbEp2TxLen = 0;
#if CCID
st_ccidCmdMessage.bWaitForAbort = 0;
g_Icc1PowerOn = 0;
#endif
//usb端点初始化
usbEpInit(); // USB Eps initialize
}
void AppUSBInit(uint8_t usbType)
{
//如果产品没有SPRODUCT_FLAG置位设备为虚拟串口
if(gLicenseFlag) //如果不相等
{
g_bUsbDevType = USBDEVTYPE_VSP;
}
else
{
g_bUsbDevType = usbType;
}
//usb时钟初始化
USBClkInit();
//usb端点初始化
USBInit();
//usb中断使能
NVIC_ClearPendingIRQ(USB_IRQn);
NVIC_EnableIRQ(USB_IRQn);
}