/Examples/APP/VirtualCom.C
https://github.com/wegam/STM32F10xUSBE · C · 197 lines · 60 code · 26 blank · 111 comment · 4 complexity · 63878bcd0a8f5abbac7688401f92354b MD5 · raw file
- #ifdef VirtualCom
- #include "VirtualCom.H"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_type.h"
- #include "STM32F10x_BitBand.H"
- #include "STM32_SYS.H"
- #include "STM32_PWM.H"
- #include "STM32_SYSTICK.H"
- #include "STM32_GPIO.H"
- #include "STM32_MEM.H"
- #include "hw_config.h"
- #include "usb_lib.h"
- #include "usb_desc.h"
- #include "usb_pwr.h"
- #include "stdio.h" //ÓÃÓÚprintf
- #include "string.h" //ÓÃÓÚprintf
- #include "stdarg.h" //ÓÃÓÚ»ñÈ¡²»È·¶¨¸öÊýµÄ²ÎÊý
- #include "stdlib.h" //malloc¶¯Ì¬ÉêÇëÄÚ´æ¿Õ¼ä
- #include "STM32_USART.H"
- //extern u8 buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];
- //extern u8 buffer_rx[VIRTUAL_COM_PORT_DATA_SIZE];
- //extern u8 buffer_tx[VIRTUAL_COM_PORT_DATA_SIZE];
- extern u32 count_out;
- u8 *VirtualCom_Buffer;
- u32 VirtualCom_BufferSize=0;
- /*******************************************************************************
- * º¯ÊýÃû :
- * ¹¦ÄÜÃèÊö :
- * ÊäÈë :
- * Êä³ö :
- * ·µ»Ø :
- *******************************************************************************/
- void VirtualCom_Configuration(void)
- {
- SYS_Configuration(); //ϵͳÅäÖà STM32_SYS.H
-
- Set_System(); //hw_config.h
-
- VirtualCom_LedConfiguration();
-
- Set_USBClock(); //hw_config.h
-
- USB_Interrupts_Config(); //hw_config.h
-
- USB_Init(); //usb_lib.h->usb_init.h //ÓÃÓڶ˵ãÊý¾ÝÊäÈëÊäÈëÖжϴ¦Àí
-
- // PWM_Configuration(TIM2,7200,5000,50);
- PWM_OUT(TIM2,PWM_OUTChannel1,200000,2); //PWMÉ趨-20161127°æ±¾
-
- SysTick_Configuration(100); //ϵͳàÖàªÊ±ÖÓÅäÖÃ72MHz,µ¥Î»ÎªuS
- }
- /*******************************************************************************
- * º¯ÊýÃû :
- * ¹¦ÄÜÃèÊö :
- * ÊäÈë :
- * Êä³ö :
- * ·µ»Ø :
- *******************************************************************************/
- void VirtualCom_Server(void)
- {
- //---DMA
- if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
- {
- /* Send the received data to the PC Host*/
- USART_To_USB_Send_Data();
- USART_ClearITPendingBit(USART1, USART_IT_IDLE);
- }
- //---DMA
- if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- /* Send the received data to the PC Host*/
- USART_To_USB_Send_Data();
- USART_ClearITPendingBit(USART1, USART_IT_RXNE);
- }
- // //----Ô³ÌÐò
- // if ((count_out != 0) && (bDeviceState == CONFIGURED))
- // {
- // USB_To_USART_Send_Data(&buffer_out[0], count_out);
- // count_out = 0;
- // }
-
- // VirtualCom_LedRun();
- // if (count_out > 0&&VirtualCom_BufferSize==0)
- // {
- // //1£©USB½ÓÊÕÊý¾Ý´®¿Ú·¢³ö
- // VirtualCom_LedRun();
- // VirtualCom_BufferSize=count_out;
- // count_out = 0;
- // VirtualCom_Buffer = (unsigned char*)malloc(sizeof(char) * VirtualCom_BufferSize);
- // memcpy(VirtualCom_Buffer,buffer_out,VirtualCom_BufferSize);
- // USB_To_USART_Send_Data(VirtualCom_Buffer, VirtualCom_BufferSize); //WOW
- // free(VirtualCom_Buffer); //ÊͷŶ¯Ì¬¿Õ¼ä
- // memset(VirtualCom_Buffer,0,VirtualCom_BufferSize); //³õʼ»¯»º³å
- // VirtualCom_BufferSize=0;
- // }
- // if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- // {
- // /* Send the received data to the PC Host*/
- // USART_To_USB_Send_Data();
- // }
-
- //2£©USB½ÓÊÕÊý¾Ý´®¿Ú·¢³ö
- // if(bDeviceState == CONFIGURED)
- // {
- // USB_To_USART_Send_Data(buffer_out, count_out); //WOW
- // count_out = 0;
- // memset(VirtualCom_Buffer,0,VirtualCom_BufferSize); //³õʼ»¯»º³å
- // }
- //3£©USB½ÓÊÕÊý¾ÝÔÙͨ¹ýUSB·µ»Ø
-
- // USART_To_USB_Send_Data();
- // USART_TX_DMAFlagClear(USART1);
- // VirtualCom_LedRun();
- // }
- }
- /*******************************************************************************
- * º¯ÊýÃû :
- * ¹¦ÄÜÃèÊö :
- * ÊäÈë :
- * Êä³ö :
- * ·µ»Ø :
- *******************************************************************************/
- void VirtualCom_LedConfiguration(void)
- {
- // GPIO_InitTypeDef GPIO_InitStructure;
- //
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- //
- // /* ×óLED */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOB, &GPIO_InitStructure);
- //
- // /* ÓÒLED */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // GPIO_Init(GPIOB, &GPIO_InitStructure);
- //
- // /* VBUS */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- // GPIO_Init(GPIOA, &GPIO_InitStructure);
- //
- //// GPIO_SetBits(GPIOB,GPIO_Pin_0);
- // GPIO_ResetBits(GPIOB,GPIO_Pin_0); //µãÁÁLEDÓÒ
- // GPIO_ResetBits(GPIOB,GPIO_Pin_10); //µãÁÁLED×ó
- // GPIO_Configuration_AOD2(GPIOB,GPIO_Pin_9); //VBUS
- GPIO_Configuration_OPP2(LED_CONNECTED_PORT,LED_CONNECTED_PIN); //ÓÒLED
- GPIO_Configuration_OPP2(LED_RUNNING_PORT,LED_RUNNING_PIN); //×óLED
- GPIO_Configuration_OPP2(GPIOB,GPIO_Pin_10); //CMSISµçÔ´¿ª¹Ø
- PB10=1;
-
- }
- /*******************************************************************************
- * º¯ÊýÃû :
- * ¹¦ÄÜÃèÊö :
- * ÊäÈë :
- * Êä³ö :
- * ·µ»Ø :
- *******************************************************************************/
- void VirtualCom_LedRun(void)
- {
- // if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_0))
- // {
- // PB0=0; //µãÁÁLEDÓÒ
- // PB10=1;
- // }
- // else
- // {
- // PB0=1; //µãÁÁLED×ó
- // PB10=0;
- // }
- // PB0=~PB0; //µãÁÁLEDÓÒ
- // PB0=~PB10; //µãÁÁLEDÓÒ
- PB0=PB10;
- PB10=~PB10;
- }
- #endif