static int f_usb_enable(int ena) { if (_argc != 1) { return -1; } if (ena) { print("Enable usb\n"); USB_Cable_Config(ENABLE); USB_ARC_init(); } else { print("Disable usb\n"); USB_Cable_Config(DISABLE); } return 0; }
/******************************************************************************* * Function Name : PowerOn * Description : * Input : None. * Output : None. * Return : USB_SUCCESS. *******************************************************************************/ RESULT PowerOn(void) { #ifndef STM32F10X_CL uint16_t wRegVal; /*** cable plugged-in ? ***/ /*while(!CablePluggedIn());*/ USB_Cable_Config(ENABLE); /*** CNTR_PWDN = 0 ***/ wRegVal = CNTR_FRES; _SetCNTR(wRegVal); /*** CNTR_FRES = 0 ***/ wInterrupt_Mask = 0; _SetCNTR(wInterrupt_Mask); /*** Clear pending interrupts ***/ _SetISTR(0); /*** Set interrupt mask ***/ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; _SetCNTR(wInterrupt_Mask); #endif /* STM32F10X_CL */ return USB_SUCCESS; }
int main(void) { delay_init(); //延时函数初始化 uart_init(9600); //串口初始化为9600 LED_Init(); //初始化与LED连接的硬件接口 LED0=1; LED1=1; LED2=1; USB_Cable_Config (ENABLE); delay_ms(500); USB_Port_Set(0); delay_ms(500); USB_Port_Set(1); Set_USBClock(); USB_Interrupts_Config(); USB_Init(); SPI_IO_Init(); DMA_configuration(); spi_conf(SPI_DEFAULT_SPEED); LED1=0; LED2=0; while(1) { /* Get command */ serprog_handle_command(usb_getc()); /* Flush output via USB */ usb_sync(); } }
/******************************************************************************* * Function Name : PowerOn * Description : * Input : None. * Output : None. * Return : USB_SUCCESS. *******************************************************************************/ RESULT PowerOn(void) { #ifndef STM32F10X_CL uint16_t wRegVal; /*** cable plugged-in ? ***/ /*while(!CablePluggedIn());*/ USB_Cable_Config(ENABLE);//使能usb控制引脚 PD9 改为PA8 /*** CNTR_PWDN = 0 ***/ wRegVal = CNTR_FRES; _SetCNTR(wRegVal);//清除CNTR寄存器上的PWDN P406 /*** CNTR_FRES = 0 ***/ wInterrupt_Mask = 0; _SetCNTR(wInterrupt_Mask);//清除CNTR寄存器上的FRES /*** Clear pending interrupts ***/ _SetISTR(0); /*** Set interrupt mask ***/ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;//P413 _SetCNTR(wInterrupt_Mask);//使能复位中断,唤醒中断,挂起中断 p412 #endif /* STM32F10X_CL */ return USB_SUCCESS; }
void HAL_USB_Attach(void) { if (USB_Configured) { // Do not attach if there are no USB classes registered if (USBD_Composite_Registered_Count(true) > 0) USB_Cable_Config(ENABLE); } }
void USB_Configs() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); USB_Cable_Config(ENABLE); USB_Interrupts_Config(); Set_USBClock(); USB_Init(); }
void HAL_USB_Detach(void) { if (USB_Configured) { USB_Cable_Config(DISABLE); if (USB_Composite_Instance) { USB_Composite_Instance->DeInit(&USB_OTG_dev, 0); } } }
/** * @brief handles switch-off conditions * @param None. * @retval USB_SUCCESS. */ RESULT PowerOff() { _SetCNTR(CNTR_FRES); /* disable all interrupts and force USB reset */ _SetISTR(0); /* clear interrupt status register */ USB_Cable_Config(DISABLE); /* Disable the Pull-Up*/ _SetCNTR(CNTR_FRES + CNTR_PDWN);/* switch-off device */ return USB_SUCCESS; }
void USBDevice_Connect() { DBG_MSG("called", 0); if(!InitMemoryInfo()) return; USB_Interrupts_Config(ENABLE); USB_Cable_Config(ENABLE); USB_Init(); }
/** * @brief Configures USB Cable. * @param None * @retval None */ void Set_System(void) { /* Unlock the internal flash */ FLASH_Unlock(); /* Configure the USB Disconnect Pin */ iNEMO_USB_Disconnet_Pin_Config(); /*Disable the USB Cable*/ USB_Cable_Config(DISABLE); /* Init the media interface */ MAL_Init(); /*Enable the USB Cable*/ USB_Cable_Config(ENABLE); }
/******************************************************************************* * Function Name : USB_USART_Init * Description : Start USB-USART protocol. * Input : baudRate (0 : disconnect usb else init usb only once). * Return : None. *******************************************************************************/ void USB_USART_Init(uint32_t baudRate) { if (linecoding.bitrate != baudRate) { if (!baudRate) { USB_Cable_Config(DISABLE); } else if (!linecoding.bitrate) { //Perform a Detach-Attach operation on USB bus USB_Cable_Config(DISABLE); USB_Cable_Config(ENABLE); //Initialize USB device only once (if linecoding.bitrate==0) SPARK_USB_Setup(); } //linecoding.bitrate will be overwritten by USB Host linecoding.bitrate = baudRate; } }
void usb_config(void) { GPIO_InitTypeDef gpioInitStructure; EXTI_InitTypeDef extiInitStructure; NVIC_InitTypeDef nvicInitStructure; print_info("usb_config\n"); ring_buffer_init(&usb_tx_ring_buffer, usb_tx_buffer, USB_TX_BUFFER_SIZE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Enable the USB disconnect GPIO clock */ RCC_APB2PeriphClockCmd(USB_DISCONNECT_RCC, ENABLE); /* USB_DISCONNECT used as USB pull-up */ gpioInitStructure.GPIO_Pin = USB_DISCONNECT_PIN; gpioInitStructure.GPIO_Mode = GPIO_Mode_Out_PP; gpioInitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(USB_DISCONNECT, &gpioInitStructure); /* Configure the EXTI line 18 connected internally to the USB IP */ EXTI_ClearITPendingBit(EXTI_Line18); extiInitStructure.EXTI_Mode = EXTI_Mode_Interrupt; extiInitStructure.EXTI_Line = EXTI_Line18; extiInitStructure.EXTI_Trigger = EXTI_Trigger_Rising; extiInitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&extiInitStructure); USB_Cable_Config(DISABLE); /* Enable USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); nvicInitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 2; nvicInitStructure.NVIC_IRQChannelSubPriority = 0; nvicInitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicInitStructure); /* Enable the USB Wake-up interrupt */ nvicInitStructure.NVIC_IRQChannel = USBWakeUp_IRQn; nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&nvicInitStructure); print_info("USB_Init\n"); USB_Init(); delay_ms(100); // TODO remove? }
void Set_USBPullUp() { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_APB2PeriphClockCmd(USB_PULLUP_RCC_APB2PERIPH, ENABLE); /* Setup USB pull-up */ GPIO_InitStructure.GPIO_Pin = USB_PULLUP_GPIO_BIT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(USB_PULLUP_GPIO_PORT, &GPIO_InitStructure); USB_Cable_Config(DISABLE); }
/******************************************************************************* * Function Name : main. * Description : main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { NVIC_InitTypeDef NVIC_InitStructure; DFU_Button_Config(); /* 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*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000) { /* Jump to user application */ JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4); Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) ApplicationAddress); Jump_To_Application(); } } /* Otherwise enters DFU mode to allow user to program his application */ /* Enter DFU mode */ DeviceState = STATE_dfuERROR; DeviceStatus[0] = STATUS_ERRFIRMWARE; DeviceStatus[4] = DeviceState; RCC_Configuration(); NVIC_Configuration(); NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); GPIO_Configuration(); /* Init the media interface */ MAL_Init(); /* potrebne ? */ USB_Cable_Config(ENABLE); //USB_Init(); USB_Init(&Device_Table, &Device_Property, &User_Standard_Requests); /* Main loop */ while (1) {} }
/******************************************************************************* * Function Name : PowerOff * Description : handles switch-off conditions * Input : None. * Output : None. * Return : USB_SUCCESS. *******************************************************************************/ RESULT PowerOff() { #ifndef STM32F10X_CL /* disable all interrupts and force USB reset */ _SetCNTR(CNTR_FRES); /* clear interrupt status register */ _SetISTR(0); /* Disable the Pull-Up*/ USB_Cable_Config(DISABLE); /* switch-off device */ _SetCNTR(CNTR_FRES + CNTR_PDWN); /* sw variables reset */ /* ... */ #endif /* STM32F10X_CL */ return USB_SUCCESS; }
/** * @brief PowerOn * @param None. * @retval USB_SUCCESS. */ RESULT PowerOn(void) { uint16_t wRegVal; USB_Cable_Config(ENABLE); /*** cable plugged-in ? ***/ wRegVal = CNTR_FRES; /*** CNTR_PWDN = 0 ***/ _SetCNTR(wRegVal); wInterrupt_Mask = 0; /*** CNTR_FRES = 0 ***/ _SetCNTR(wInterrupt_Mask); _SetISTR(0); /*** Clear pending interrupts ***/ /*** Set interrupt mask ***/ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; _SetCNTR(wInterrupt_Mask); return USB_SUCCESS; }
/******************************************************************************* * Function Name : PowerOn * Description : * Input : None. * Output : None. * Return : USB_SUCCESS. *******************************************************************************/ RESULT PowerOn(void) { u16 wRegVal; USB_Cable_Init(); USB_Cable_Config(ENABLE);//使能1.5K上拉 /*** CNTR_PWDN = 0 ***/ wRegVal = CNTR_FRES; _SetCNTR(wRegVal); /*** CNTR_FRES = 0 ***/ wInterrupt_Mask = 0; _SetCNTR(wInterrupt_Mask); /*** Clear pending interrupts ***/ _SetISTR(0); /*** Set interrupt mask ***/ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; _SetCNTR(wInterrupt_Mask); return USB_SUCCESS; }
void GlobalVariableInitialize(void) { u8 cChip; USB_Cable_Config(DISABLE); //Usb soft disconnect cUsbConfigured = FALSE; cFlagHostCommandDataReceived = FALSE; dwTotalSendBytes = 0; UsbOutDataBelongs = FREE_DATA; ClearSoftTimer0(); ClearSoftTimer1(); cUsbTxUnfinished = FALSE; for(cChip=0; cChip<TOTAL_CHIPS_INSTALLED; cChip++) { ChipMiningStatus[cChip].cEngineCount = 0; ChipMiningStatus[cChip].cFrequencySlect = __ASIC_FREQUENCY_ACTUAL_INDEX; ChipMiningStatus[cChip].dwCalcTimePerChip = 3000; //set calc time is 30s ClearSoftTimerEx(cChip); cGoodEngineCount[cChip] = 0; } }
/******************************************************************************* * Function Name : PowerOn * Description : * Input : None. * Output : None. * Return : USB_SUCCESS. *******************************************************************************/ RESULT PowerOn(void) { uint16_t wRegVal; DBG_USB_PRINT("PowerOn..1\r\n"); /*** cable plugged-in ? ***/ USB_Cable_Config(ENABLE); DBG_USB_PRINT("PowerOn..2\r\n"); /*** CNTR_PWDN = 0 ***/ wRegVal = CNTR_FRES; _SetCNTR(wRegVal); DBG_USB_PRINT("PowerOn..3\r\n"); /*** CNTR_FRES = 0 ***/ wInterrupt_Mask = 0; _SetCNTR(wInterrupt_Mask); DBG_USB_PRINT("PowerOn..4\r\n"); /*** Clear pending interrupts ***/ _SetISTR(0); DBG_USB_PRINT("PowerOn..5\r\n"); /*** Set interrupt mask ***/ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; _SetCNTR(wInterrupt_Mask); DBG_USB_PRINT("PowerOn..6\r\n"); return USB_SUCCESS; }
/******************************************************************************* * Function Name : Set_System * Description : Configures Main system clocks & power * Input : None. * Return : None. *******************************************************************************/ void Set_System(void) { GPIO_InitTypeDef GPIO_InitStructure; /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while (RCC_GetSYSCLKSource() != 0x08) {} } /* Configure USB pull-up */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); /* Configure USB pull-up */ #ifndef JOYSTICK 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 USB_Cable_Config(DISABLE); for(u32 i=1000000;--i;); USB_Cable_Config(ENABLE); }
/******************************************************************************* * Function Name : Mass_Storage_Init * Description : Initializes the peripherals used by the mass storage driver. * Input : None * Output : None * Return : None *******************************************************************************/ void Mass_Storage_Init(void) { /* Disable the Pull-Up*/ USB_Cable_Config(DISABLE); }
void USBDevice_Disconnect() { USB_Cable_Config(DISABLE); }
void USBDevice_Disconnect() { DBG_MSG("called", 0); USB_Interrupts_Config(DISABLE); USB_Cable_Config(DISABLE); }
/******************************************************************************* * Function Name : Set_System * Description : Configures Main system clocks & power * Input : None. * Return : None. *******************************************************************************/ void Set_System(void) { GPIO_InitTypeDef GPIO_InitStructure; /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/ /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); #ifdef STM32F10X_CL /* Configure PLLs *********************************************************/ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ RCC_PREDIV2Config(RCC_PREDIV2_Div5); RCC_PLL2Config(RCC_PLL2Mul_8); /* Enable PLL2 */ RCC_PLL2Cmd(ENABLE); /* Wait till PLL2 is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET) {} /* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */ RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5); RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9); #else /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); #endif /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } else { /* If HSE fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error */ /* Go to infinite loop */ while (1) { } } #ifdef USE_STM3210B_EVAL /* Enable GPIOB, TIM2 & TIM4 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4 , ENABLE); #endif /* USE_STM3210B_EVAL */ /* Configure USB pull-up */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE); /* Configure USB pull-up */ 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); USB_Cable_Config(DISABLE); USB_Cable_Config(ENABLE); }