STM32F4的usb cdc host通信
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "usbd_cdc_core.h"
- #include "usbd_usr.h"
- #include "usbd_desc.h"
- #include "config.h"
- #include "base_driver.h"
- #include "delay.h"
- /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
- * @{
- */
- /** @defgroup APP_HID
- * @brief Mass storage application module
- * @{
- */
- /** @defgroup APP_HID_Private_TypesDefinitions
- * @{
- */
- /**
- * @}
- */
- /** @defgroup APP_HID_Private_Defines
- * @{
- */
- /**
- * @}
- */
- /** @defgroup APP_HID_Private_Macros
- * @{
- */
- extern uint8_t USB_StatusDataSended;
- extern uint32_t USB_ReceivedCount;
- extern uint8_t USB_Tx_Buffer[];
- extern uint8_t USB_Rx_Buffer[];
- extern __IO uint8_t DeviceConfigured;
- /**
- * @}
- */
- /** @defgroup APP_HID_Private_Variables
- * @{
- */
-
- #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
- #if defined ( __ICCARM__ ) /*!< IAR Compiler */
- #pragma data_alignment=4
- #endif
- #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
- __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
- /**
- * @}
- */
- /** @defgroup APP_HID_Private_FunctionPrototypes
- * @{
- */
- /**
- * @}
- */
- /** @defgroup APP_HID_Private_Functions
- * @{
- */
- /**
- * @brief Program entry point
- * @param None
- * @retval None
- */
- int main(void)
- {
- static uint32_t SendDataNum = 0;
- static uint8_t SendDataBuff[64*1024];
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32fxxx_xx.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32fxxx.c file
- */
- // LED_Config();
- RCC_ClocksTypeDef SYS_Clocks;
- RCC_GetClocksFreq(&SYS_Clocks);
- USBD_Init(&USB_OTG_dev,
- #ifdef USE_USB_OTG_HS
- USB_OTG_HS_CORE_ID,
- #else
- USB_OTG_FS_CORE_ID,
- #endif
- &USR_desc,
- &USBD_CDC_cb,
- &USR_cb);
- // while(DeviceConfigured==0);
- //LED_Config();
- //LED_On(LED_B);
- //POWER_Config(500);
- // FSMC_Config();
- //FSMC_DMA_Configuration((uint8_t *)SendDataBuff,(uint8_t *)FSMC_READ_ADDR,128);
- delay_init(SystemCoreClock/1000000);
- while(1)
- {
- if(USB_ReceivedCount > 0){
- USB_ReceivedCount = 0;
- //?????????????,???1K
- SendDataNum = (USB_Rx_Buffer[0]<<24)|(USB_Rx_Buffer[1]<<16)|(USB_Rx_Buffer[2]<<8)|(USB_Rx_Buffer[3]<<0);
- do{
- //??????????
- USB_StatusDataSended = 0;
- //????
- DCD_EP_Tx(&USB_OTG_dev,CDC_IN_EP,SendDataBuff,sizeof(SendDataBuff));
- //????????,?????????????????????
- while(USB_StatusDataSended == 0){
- delay_us(1);
- }
- SendDataNum--;
- }while(SendDataNum > 0);
- }
- #ifdef LED_TEST
- /* LED_Off(LED_B);
- delay_ms(500);
- LED_On(LED_B);
- delay_ms(500);*/
- #endif
- }
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief assert_failed
- * Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param File: pointer to the source file name
- * @param Line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
-
- /* Infinite loop */
- while (1)
- {}
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
code_F429.7z
(1.01 MB, 下载次数: 42)
|