void stm32_usbc_early_init(stm32_usb_clk_t clock_source) { // TODO(konkers): add usb clock source to rcc. #if 0 if (clock_source == STM32_USB_CLK_HSI48) { RCC_USBCLKConfig(RCC_USBCLK_HSI48); } else { RCC_USBCLKConfig(RCC_USBCLK_PLLCLK); } RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); #endif }
/******************************************************************************* * Function Name : main. * Description : main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { board_init(); DFU_Button_Config(); usb.bDeviceState = UNCONNECTED; usb.fSuspendEnabled = TRUE; /* Check if the Key push-button on STM3210x-EVAL Board is pressed */ if (DFU_Button_Read() != 0x00) { /* Test if user code is programmed starting from address 0x8003000 */ if (((*(__IO uint32_t*)AppAddress) & 0x2FFE0000 ) == 0x20000000) { /* Jump to user application */ JumpAddress = *(__IO uint32_t*) (AppAddress + 4); Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) AppAddress); Jump_To_Application(); } } /* Otherwise enters DFU mode to allow user to program his application */ /* Config and enable clock */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); /* Enter DFU mode */ dfu.bStatus = STATUS_ERRFIRMWARE; dfu.bState = STATE_dfuERROR; /* Unlock the internal flash */ FLASH_Unlock(); /* Init the media interface */ MAL_Init(); /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); USB_Init(); /* Main loop */ while (1) {} }
void Clk_Init (void) { // 1. Cloking the controller from internal HSI RC (8 MHz) RCC_HSICmd(ENABLE); // wait until the HSI is ready while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // 2. Enable ext. high frequency OSC RCC_HSEConfig(RCC_HSE_ON); // wait until the HSE is ready while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // 3. Init PLL RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz // RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); // 72MHz RCC_PLLCmd(ENABLE); // wait until the PLL is ready while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // 4. Set system clock divders RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_ADCCLKConfig(RCC_PCLK2_Div8); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); // Flash 1 wait state *(vu32 *)0x40022000 = 0x12; // 5. Clock system from PLL RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); }
/******************************************************************************* * Function Name : Set_USBClock * Description : Configures USB Clock input (48MHz) * Input : None. * Return : None. *******************************************************************************/ void Set_USBClock(void) { /* USBCLK = PLLCLK / 1.5 */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
int32_t PIOS_USB_Reenumerate() { /* Force USB reset and power-down (this will also release the USB pins for direct GPIO control) */ _SetCNTR(CNTR_FRES | CNTR_PDWN); /* Using a "dirty" method to force a re-enumeration: */ /* Force DPM (Pin PA12) low for ca. 10 mS before USB Tranceiver will be enabled */ /* This overrules the external Pull-Up at PA12, and at least Windows & MacOS will enumerate again */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); PIOS_DELAY_WaitmS(50); /* Release power-down, still hold reset */ _SetCNTR(CNTR_PDWN); PIOS_DELAY_WaituS(5); /* CNTR_FRES = 0 */ _SetCNTR(0); /* Clear pending interrupts */ _SetISTR(0); /* Configure USB clock */ /* USBCLK = PLLCLK / 1.5 */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); return 0; }
/******************************************************************************* * Function Name : Set_USBClock * Description : Configures USB Clock input (48MHz) * Input : None. * Return : None. *******************************************************************************/ void Set_USBClock(void) { /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
/******************************************************************************* * Function Name : USB_Cable_Config * Description : Software Connection/Disconnection of USB Cable * Input : None. * Return : Status *******************************************************************************/ void USB_Cable_Config (FunctionalState NewState) { #ifdef JOYSTICK if(NewState != DISABLE){ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }else{ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, DISABLE); GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIOA->BRR = GPIO_Pin_12; } #else if (NewState != DISABLE) { GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN); } else { GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN); } #endif }
int main(void) { /* Configure Clocks (GPIO and DMA clocks already enabled by startup.c) */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_APB2PeriphClockCmd( SPI_ENGINE_RCC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); /* Configure On-board LED */ GPIO_Init(PORT_LED, &GPIO_InitStructure_LED); /* Configure SPI Port */ GPIO_Init( GPIOA, &GPIO_InitStructure_SPIOUT); GPIO_Init( GPIOA, &GPIO_InitStructure_SPIIN); GPIO_Init( PORT_SS, &GPIO_InitStructure_SPISS); /* Configure SPI Engine with DMA */ spi_conf(SPI_DEFAULT_SPEED); /* Configure USB Interrupt */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_Init(&NVIC_InitStructure); /* Enable all peripherals */ USB_Init(); /* Main loop */ while(1) { /* Get command */ serprog_handle_command(usb_getc()); /* Flush output via USB */ usb_sync(); } return 0; }
/******************************************************************************* * System Clock Configuration *******************************************************************************/ void Set_System(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2 RCC_ADCCLKConfig(RCC_PCLK2_Div4); //ADCCLK = PCLK2/4 = 18MHz RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 72 MHz RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource()!=0x08){} } RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE); RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_SPI2, ENABLE); RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); //USBCLK = 48MHz RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); //Enable USB clock }
void RCC_Configuration(void) { SystemInit(); SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK ); RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); // enable usb RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); }
/******************************************************************************* * Function Name : Set_USBClock * Description : Configures USB Clock input (48MHz). * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Set_USBClock(void) { // Select USBCLK source RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); // 72MHZ sysclk // RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_Div1); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
/******************************************************************************* * Function Name : Set_USBClock * Description : Configures USB Clock input (48MHz) * Input : None. * Return : None. *******************************************************************************/ static void usb_SetClock(void) { /* Enable GPIOA, GPIOD and USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE); /* USBCLK = PLLCLK / 1.5 */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
void Set_USBClock(void) { if( SystemCoreClock == 72000000ul ) { RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); } else if( SystemCoreClock == 48000000ul ) { RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_Div1); } else { /* unsupported clock */ for(;;); } /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
int32_t PIOS_USB_Init(uint32_t * usb_id, const struct pios_usb_cfg * cfg) { PIOS_Assert(usb_id); PIOS_Assert(cfg); struct pios_usb_dev * usb_dev; usb_dev = (struct pios_usb_dev *) PIOS_USB_alloc(); if (!usb_dev) goto out_fail; /* Bind the configuration to the device instance */ usb_dev->cfg = cfg; PIOS_USB_Reenumerate(); /* * This is a horrible hack to make this available to * the interrupt callbacks. This should go away ASAP. */ pios_usb_com_id = (uint32_t) usb_dev; /* Enable the USB Interrupts */ NVIC_Init((NVIC_InitTypeDef*)&usb_dev->cfg->irq.init); /* Configure USB D-/D+ (DM/DP) pins */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 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_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_14); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_14); /* Configure VBUS sense pin */ if (usb_dev->cfg->vsense.gpio) GPIO_Init(usb_dev->cfg->vsense.gpio, (GPIO_InitTypeDef*)&usb_dev->cfg->vsense.init); /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); USB_Init(); USB_SIL_Init(); *usb_id = (uint32_t) usb_dev; return 0; /* No error */ out_fail: return(-1); }
/******************************************************************************* * Function Name : Set_USBClock * Description : Configures USB Clock input (48MHz) * Input : None. * Return : None. *******************************************************************************/ void Set_USBClock(void) { #ifdef STM32F10X_CL /* Select USBCLK source */ RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3); /* Enable the USB clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE) ; #else /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); #endif /* STM32F10X_CL */ }
void cmtInitUsb() { /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); /* Enable USB Interrupts */ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void RCC_Configuration(void) { SystemInit(); SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK ); RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); // enable usb RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); // Enable GPIO modules RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO, ENABLE); // RTC clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); }
void Startup::clkInit() { // Set STKALIGN in NVIC SCB->CCR |= 0x200; // 1. Clocking the controller from internal HSI RC (8 MHz) RCC_HSICmd(ENABLE); // wait until the HSI is ready while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // 2. Enable ext. high frequency OSC RCC_HSEConfig(RCC_HSE_ON); // wait until the HSE is ready while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // 3. Init PLL RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz // RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); // 72MHz RCC_PLLCmd(ENABLE); // wait until the PLL is ready while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // 4. Set system clock dividers RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_ADCCLKConfig(RCC_PCLK2_Div8); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); // Flash 1 wait state // *(vu32 *)0x40022000 = 0x12; // *(vu32 *)0x40022000 = 0x01; // Enable Prefetch Buffer FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Flash 2 wait state FLASH_SetLatency(FLASH_Latency_2); // Select PLL as system clock source RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Wait till PLL is used as system clock source while(RCC_GetSYSCLKSource() != 0x08) { } }
int32_t PIOS_USB_Init(uint32_t *usb_id, const struct pios_usb_cfg *cfg) { PIOS_Assert(usb_id); PIOS_Assert(cfg); struct pios_usb_dev *usb_dev; usb_dev = (struct pios_usb_dev *)PIOS_USB_alloc(); if (!usb_dev) { goto out_fail; } /* Bind the configuration to the device instance */ usb_dev->cfg = cfg; PIOS_USB_Reenumerate(); /* * This is a horrible hack to make this available to * the interrupt callbacks. This should go away ASAP. */ pios_usb_com_id = (uint32_t)usb_dev; /* Enable the USB Interrupts */ NVIC_Init(&usb_dev->cfg->irq.init); /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); USB_Init(); USB_SIL_Init(); *usb_id = (uint32_t)usb_dev; return 0; /* No error */ out_fail: return -1; }
void USB_BSP_Init(USB_CORE_HANDLE *pdev) { EXTI_InitTypeDef EXTI_InitStructure; /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); #if defined USB_CLOCK_SOURCE_CRS /*For using CRS, you need to do the following: - Enable HSI48 (managed by the SystemInit() function at the application startup) - Select HSI48 as USB clock - Enable CRS clock - Set AUTOTRIMEN - Set CEN */ /* Select HSI48 as USB clock */ RCC_USBCLKConfig(RCC_USBCLK_HSI48); /* Configure the Clock Recovery System */ CRS_Config(); #else /* Configure PLL to be used as USB clock: - Enable HSE external clock (for this example the system is clocked by HSI48 managed by the SystemInit() function at the application startup) - Enable PLL - Select PLL as USB clock */ /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) {} /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} /* Configure USBCLK from PLL clock */ RCC_USBCLKConfig(RCC_USBCLK_PLLCLK); #endif /*USB_CLOCK_SOURCE_CRS */ /* Configure the Tamper button in EXTI mode */ STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI); /* Configure Tamper EXTI line to generate an interrupt on rising & falling edges */ EXTI_InitStructure.EXTI_Line = TAMPER_BUTTON_EXTI_LINE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Clear the Tamper EXTI line pending bit */ EXTI_ClearITPendingBit(TAMPER_BUTTON_EXTI_LINE); #ifdef USB_DEVICE_LOW_PWR_MGMT_SUPPORT /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* EXTI line 18 is connected to the USB Wakeup from suspend event */ EXTI_ClearITPendingBit(EXTI_Line18); EXTI_InitStructure.EXTI_Line = EXTI_Line18; /*Must Configure the EXTI Line 18 to be sensitive to rising edge*/ EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); #endif /*USB_DEVICE_LOW_PWR_MGMT_SUPPORT */ }
/************************************************************************* * Function Name: USB_HwInit * Parameters: none * * Return: none * * Description: Init USB * *************************************************************************/ void USB_HwInit(void) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // Reset USB Engine RCC_APB1PeriphResetCmd(RCC_APB1Periph_USB,ENABLE); // Init USB Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB,ENABLE); // Release reset USB Engine RCC_APB1PeriphResetCmd(RCC_APB1Periph_USB,DISABLE); // Enable 48MHz clock RCC_USBCLKConfig(USB_DIVIDER); // Force USB Reset & Disable USB interrupts USB_CNTR = 1; // GPIO assign to the USB engine RCC_APB2PeriphResetCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, DISABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE); // xxx // Configure PA11, PA12 as USB lines GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure PC11 USB connect. GPIO_WriteBit(GPIOC,GPIO_Pin_11,Bit_SET); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); // Disconnect device USB_ConnectRes(FALSE); // Init controls endpoints USB_HwReset(); // Clear spurious pending interrupt USB_ISTR = 0; // USB interrupt connect to NVIC #if USB_HIGH_PRIORITY_EVENT > 0 NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USB_INTR_HIGH_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif // USB_HIGH_PRIORITY_EVENT > 0 NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USB_INTR_LOW_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/****************************************************************************** * @brief Configures Main system clocks & power */ void USB_Config(void) { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; #if !defined(STM32L1XX_MD) && !defined(STM32L1XX_HD) && !defined(STM32L1XX_MD_PLUS) GPIO_InitTypeDef GPIO_InitStructure; #endif /* STM32L1XX_MD && STM32L1XX_XD */ #if defined(USB_USE_EXTERNAL_PULLUP) GPIO_InitTypeDef GPIO_InitStructure; #endif /* USB_USE_EXTERNAL_PULLUP */ #if defined(STM32L1XX_MD) \ || defined(STM32L1XX_HD) \ || defined(STM32L1XX_MD_PLUS) \ || defined(STM32F37X) \ || defined(STM32F30X) /* Enable the SYSCFG module clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); #endif /* STM32L1XX_XD */ #if !defined(STM32L1XX_MD) \ && !defined(STM32L1XX_HD) \ && !defined(STM32L1XX_MD_PLUS) \ && !defined(STM32F37X) \ && !defined(STM32F30X) /* Enable USB_DISCONNECT GPIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE); /* Configure USB pull-up pin */ GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure); #endif /* STM32L1XX_MD && STM32L1XX_XD */ #if defined(USB_USE_EXTERNAL_PULLUP) /* Enable the USB disconnect GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE); /* USB_DISCONNECT used as USB pull-up */ GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure); #endif /* USB_USE_EXTERNAL_PULLUP */ #if defined(STM32F37X) || defined(STM32F30X) /* Enable the USB disconnect GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE); /*Set PA11,12 as IN - USB_DM,DP*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 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); /* SET PA11,12 for USB: USB_DM,DP*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_14); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_14); /* USB_DISCONNECT used as USB pull-up */ GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure); #endif /* STM32F37X && STM32F30X */ /* Configure the EXTI line 18 connected internally to the USB IP */ EXTI_ClearITPendingBit(EXTI_Line18); EXTI_InitStructure.EXTI_Line = EXTI_Line18; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD) || defined(STM32L1XX_MD_PLUS) /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); #else /* Select USBCLK source */ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); #endif /* STM32L1XX_MD */ /* 2 bit for pre-emption priority, 2 bits for subpriority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS) NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn; NVIC_Init(&NVIC_InitStructure); /* Enable the USB Wake-up interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USB_FS_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); #elif defined(STM32F37X) /* Enable the USB interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn; NVIC_Init(&NVIC_InitStructure); /* Enable the USB Wake-up interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); #else NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_Init(&NVIC_InitStructure); /* Enable the USB Wake-up interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); #endif /* STM32L1XX_XD */ }
/******************************************************************************* Set_System : Configures Main system clocks & power *******************************************************************************/ void Set_System(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state RCC_CFGR = 0x001D8402;/*AHB peripheral clock enable |||||||+--Bits1:0 =10 PLL used as system clock ||||||+---Bits7:4 =0000 AHB clock=SYSCLK |||||+----Bits10:8 =100 PCLK1=HCLK divided by 2 ||||++----Bits13:11 =000 PCLK2=HCLK ||||+-----Bits15:14 =10 ADC prescaler PCLK2 divided by 6 |||+------Bit16 =1 HSE oscillator clock selected as PLL input clock |||+------Bit17 =0 HSE clock not divided ||++------Bits21:18 =0111 PLL input clock x 9 ||+-------Bit22=0 USB prescaler is PLL clock divided by 1.5 |+--------Bits26:24 =000 MCO as No clock |+--------Bits26:24 =100 MCO as SYSCLK |+--------Bits26:24 =111 MCO as SYSCLK/2 ++--------Bits31:27 Reserved*/ RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource()!=0x08){} } RCC_AHBENR = 0x00000117;/*AHB peripheral clock enable |||||||+--Bit0 =1 DMA1 clock enabled |||||||+--Bit1 =1 DMA2 clock enabled |||||||+--Bit2 =1 SRAM interface clock enabled during Sleep mode ||||||+---Bit4 =1 FLITF clock enabled during Sleep mode ||||||+---Bit6 =0 CRC clock disabled |||||+----Bit8 =1 FSMC clock enabled |||||+----Bit10=0 SDIO clock disabled ++++++----Bits31:11=0 Reserved*/ RCC_APB2ENR = 0x0000AE7D;/* APB2 peripheral clock enable |||||||+--Bit0 =1 1:Alternate Function I/O clock enabled |||||||+--Bit2 =1 I/O port A clock enabled |||||||+--Bit3 =1 I/O port B clock enabled ||||||+---Bit4 =1 I/O port C clock enabled ||||||+---Bit5 =1 I/O port D clock enabled ||||||+---Bit6 =1 I/O port E clock enabled ||||||+---Bit7 =0 I/O port F clock disabled |||||+----Bit8 =0 I/O port G clock disabled |||||+----Bit9 =1 ADC 1 interface clock disabled |||||+----Bit10=1 ADC 2 interface clock disabled |||||+----Bit11=1 TIM1 timer clock enabled ||||+-----Bit12=0 SPI 1 clock disabled ||||+-----Bit13=1 TIM8 timer clock enabled ||||+-----Bit14=0 USART1 clock disabled ||||+-----Bit15=1 ADC 3 interface clock enabled ++++------Bit31:17=0 Reserved*/ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); //USBCLK = 48MHz RCC_APB1ENR = 0x2080802F;/* APB1 peripheral clock enable |||||||+--Bit0 =1 Timer 2 clock enabled |||||||+--Bit1 =1 Timer 3 clock enabled |||||||+--Bit2 =1 Timer 4 clock enabled |||||||+--Bit3 =1 Timer 5 clock enabled ||||||+---Bit4 =0 Timer 6 clock disabled ||||||+---Bit5 =1 Timer 7 clock ensabled |||||+----Bit11=0 Window watchdog clock disabled ||||+-----Bit14=0 SPI 2 clock disabled ||||+-----Bit15=1 SPI 3 clock enabled |||+------Bit17=0 USART 2 clock disabled |||+------Bit18=0 USART 3 clock disabled |||+------Bit19=0 USART 4 clock disabled ||+-------Bit20=0 USART 5 clock disabled ||+-------Bit21=0 I2C 1 clock disabled ||+-------Bit22=0 I2C 2 clock enabled ||+-------Bit23=1 USB clock enabled |+--------Bit25=0 CAN clock disabled |+--------Bit27=0 Backup interface clock disabled +---------Bit28=0 Power interface clock disabled +---------Bit29=1 DAC interface clock enable*/ }
void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd) { #ifdef STM32F4 if(hpcd->Instance==USB_OTG_FS) { /* Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; // __USB_OTG_FS_CLK_ENABLE(); /**USB_OTG_FS GPIO Configuration PA9 ------> USB_OTG_FS_VBUS PA11 ------> USB_OTG_FS_DM PA12 ------> USB_OTG_FS_DP */ /* Configure DM DP Pins */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = 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_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* Configure VBUS Pin */ 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_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12; // set this quite low. it can take a while to fill RX/TX buffers and don't let it break other stuff NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } #endif #ifdef STM32F1 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USB interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12; // set this quite low. it can take a while to fill RX/TX buffers and don't let it break other stuff NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the USB Wake-up interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); jshPinOutput(USB_DISCONNECT_PIN, 0); // turn on USB #endif }