123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- #include "usb_bsp.h"
- #define USE_ACCURATE_TIME
- #define TIM_MSEC_DELAY 0x01
- #define TIM_USEC_DELAY 0x02
- #if 0
- #define HOST_OVRCURR_PORT GPIOE
- #define HOST_OVRCURR_LINE GPIO_Pin_1
- #define HOST_OVRCURR_PORT_SOURCE GPIO_PortSourceGPIOE
- #define HOST_OVRCURR_PIN_SOURCE GPIO_PinSource1
- #define HOST_OVRCURR_PORT_RCC RCC_APB2Periph_GPIOE
- #define HOST_OVRCURR_EXTI_LINE EXTI_Line1
- #define HOST_OVRCURR_IRQn EXTI1_IRQn
- #ifdef USE_STM3210C_EVAL
- #define HOST_POWERSW_PORT_RCC RCC_APB2Periph_GPIOC
- #define HOST_POWERSW_PORT GPIOC
- #define HOST_POWERSW_VBUS GPIO_Pin_9
- #else
- #ifdef USE_USB_OTG_FS
- #define HOST_POWERSW_PORT_RCC RCC_AHB1Periph_GPIOH
- #define HOST_POWERSW_PORT GPIOH
- #define HOST_POWERSW_VBUS GPIO_Pin_5
- #endif
- #endif
- #define HOST_SOF_OUTPUT_RCC RCC_APB2Periph_GPIOA
- #define HOST_SOF_PORT GPIOA
- #define HOST_SOF_SIGNAL GPIO_Pin_8
- #endif
- ErrorStatus HSEStartUpStatus;
- #ifdef USE_ACCURATE_TIME
- __IO uint32_t BSP_delay = 0;
- #endif
- #ifdef USE_ACCURATE_TIME
- static void BSP_SetTime(uint8_t Unit);
- static void BSP_Delay(uint32_t nTime, uint8_t Unit);
- static void USB_OTG_BSP_TimeInit(void);
- #endif
- void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
- {
-
- #ifdef USE_STM3210C_EVAL
- RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
- #else
- GPIO_InitTypeDef GPIO_InitStructure;
- #ifdef USE_USB_OTG_FS
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_OTG1_FS);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_OTG1_FS);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
- #else
- #ifdef USE_ULPI_PHY
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
- RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH |
- RCC_AHB1Periph_GPIOI,
- ENABLE);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOH, GPIO_PinSource4, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOI, GPIO_PinSource11, GPIO_AF_OTG2_HS);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_OTG2_HS);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |
- GPIO_Pin_5 | GPIO_Pin_10 |
- GPIO_Pin_11 | GPIO_Pin_12 |
- GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOH, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOI, &GPIO_InitStructure);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS |
- RCC_AHB1Periph_OTG_HS_ULPI,
- ENABLE);
- #else
- #if 1
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
- #else
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |
- GPIO_Pin_13 |
- GPIO_Pin_14 |
- GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_FS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_FS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
- #endif
- #endif
- #endif
- #endif
-
- USB_OTG_BSP_TimeInit();
- }
- void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef USE_USB_OTG_HS
- NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn;
- #else
- NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;
- #endif
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
- {
- #if 0
- #ifndef USE_USB_OTG_HS
- if (0 == state)
- {
-
- GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
- }
- else
- {
-
- GPIO_ResetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
- }
- #endif
- #endif
- }
- void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
- {
- #if 0
- #ifndef USE_USB_OTG_HS
- GPIO_InitTypeDef GPIO_InitStructure;
- #ifdef USE_STM3210C_EVAL
- RCC_APB2PeriphClockCmd(HOST_POWERSW_PORT_RCC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(HOST_POWERSW_PORT, &GPIO_InitStructure);
- #else
- #ifdef USE_USB_OTG_FS
- RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOH , ENABLE);
- GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- GPIO_Init(HOST_POWERSW_PORT,&GPIO_InitStructure);
- #endif
- #endif
- GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
- USB_OTG_BSP_mDelay(200);
- #endif
- #endif
- }
- static void USB_OTG_BSP_TimeInit(void)
- {
- #ifdef USE_ACCURATE_TIME
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- #endif
- }
- void USB_OTG_BSP_uDelay(const uint32_t usec)
- {
- #ifdef USE_ACCURATE_TIME
- BSP_Delay(usec, TIM_USEC_DELAY);
- #else
- __IO uint32_t count = 0;
- const uint32_t utime = (120 * usec / 7);
- do
- {
- if (++count > utime)
- {
- return;
- }
- } while (1);
- #endif
- }
- void USB_OTG_BSP_mDelay(const uint32_t msec)
- {
- #ifdef USE_ACCURATE_TIME
- BSP_Delay(msec, TIM_MSEC_DELAY);
- #else
- USB_OTG_BSP_uDelay(msec * 1000);
- #endif
- }
- void USB_OTG_BSP_TimerIRQ(void)
- {
- #ifdef USE_ACCURATE_TIME
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- if (BSP_delay > 0x00)
- {
- BSP_delay--;
- }
- else
- {
- TIM_Cmd(TIM2, DISABLE);
- }
- }
- #endif
- }
- #ifdef USE_ACCURATE_TIME
- static void BSP_Delay(uint32_t nTime, uint8_t unit)
- {
- BSP_delay = nTime;
- BSP_SetTime(unit);
- while (BSP_delay != 0)
- ;
- TIM_Cmd(TIM2, DISABLE);
- }
- static void BSP_SetTime(uint8_t unit)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_Cmd(TIM2, DISABLE);
- TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
- if (unit == TIM_USEC_DELAY)
- {
- TIM_TimeBaseStructure.TIM_Period = 11;
- }
- else if (unit == TIM_MSEC_DELAY)
- {
- TIM_TimeBaseStructure.TIM_Period = 11999;
- }
- TIM_TimeBaseStructure.TIM_Prescaler = 5;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- TIM_ARRPreloadConfig(TIM2, ENABLE);
-
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
-
- TIM_Cmd(TIM2, ENABLE);
- }
- #endif
|