/Examples/APP/VirtualCom.C

https://github.com/wegam/STM32F10xUSBE · C · 197 lines · 60 code · 26 blank · 111 comment · 4 complexity · 63878bcd0a8f5abbac7688401f92354b MD5 · raw file

  1. #ifdef VirtualCom
  2. #include "VirtualCom.H"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_rcc.h"
  5. #include "stm32f10x_type.h"
  6. #include "STM32F10x_BitBand.H"
  7. #include "STM32_SYS.H"
  8. #include "STM32_PWM.H"
  9. #include "STM32_SYSTICK.H"
  10. #include "STM32_GPIO.H"
  11. #include "STM32_MEM.H"
  12. #include "hw_config.h"
  13. #include "usb_lib.h"
  14. #include "usb_desc.h"
  15. #include "usb_pwr.h"
  16. #include "stdio.h" //ÓÃÓÚprintf
  17. #include "string.h" //ÓÃÓÚprintf
  18. #include "stdarg.h" //ÓÃÓÚ»ñÈ¡²»È·¶¨¸öÊýµÄ²ÎÊý
  19. #include "stdlib.h" //malloc¶¯Ì¬ÉêÇëÄÚ´æ¿Õ¼ä
  20. #include "STM32_USART.H"
  21. //extern u8 buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];
  22. //extern u8 buffer_rx[VIRTUAL_COM_PORT_DATA_SIZE];
  23. //extern u8 buffer_tx[VIRTUAL_COM_PORT_DATA_SIZE];
  24. extern u32 count_out;
  25. u8 *VirtualCom_Buffer;
  26. u32 VirtualCom_BufferSize=0;
  27. /*******************************************************************************
  28. * º¯ÊýÃû :
  29. * ¹¦ÄÜÃèÊö :
  30. * ÊäÈë :
  31. * Êä³ö :
  32. * ·µ»Ø :
  33. *******************************************************************************/
  34. void VirtualCom_Configuration(void)
  35. {
  36. SYS_Configuration(); //ϵͳÅäÖà STM32_SYS.H
  37. Set_System(); //hw_config.h
  38. VirtualCom_LedConfiguration();
  39. Set_USBClock(); //hw_config.h
  40. USB_Interrupts_Config(); //hw_config.h
  41. USB_Init(); //usb_lib.h->usb_init.h //ÓÃÓڶ˵ãÊý¾ÝÊäÈëÊäÈëÖжϴ¦Àí
  42. // PWM_Configuration(TIM2,7200,5000,50);
  43. PWM_OUT(TIM2,PWM_OUTChannel1,200000,2); //PWMÉ趨-20161127°æ±¾
  44. SysTick_Configuration(100); //ϵͳàÖàªÊ±ÖÓÅäÖÃ72MHz,µ¥Î»ÎªuS
  45. }
  46. /*******************************************************************************
  47. * º¯ÊýÃû :
  48. * ¹¦ÄÜÃèÊö :
  49. * ÊäÈë :
  50. * Êä³ö :
  51. * ·µ»Ø :
  52. *******************************************************************************/
  53. void VirtualCom_Server(void)
  54. {
  55. //---DMA
  56. if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
  57. {
  58. /* Send the received data to the PC Host*/
  59. USART_To_USB_Send_Data();
  60. USART_ClearITPendingBit(USART1, USART_IT_IDLE);
  61. }
  62. //---DMA
  63. if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  64. {
  65. /* Send the received data to the PC Host*/
  66. USART_To_USB_Send_Data();
  67. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  68. }
  69. // //----Ô­³ÌÐò
  70. // if ((count_out != 0) && (bDeviceState == CONFIGURED))
  71. // {
  72. // USB_To_USART_Send_Data(&buffer_out[0], count_out);
  73. // count_out = 0;
  74. // }
  75. // VirtualCom_LedRun();
  76. // if (count_out > 0&&VirtualCom_BufferSize==0)
  77. // {
  78. // //1£©USB½ÓÊÕÊý¾Ý´®¿Ú·¢³ö
  79. // VirtualCom_LedRun();
  80. // VirtualCom_BufferSize=count_out;
  81. // count_out = 0;
  82. // VirtualCom_Buffer = (unsigned char*)malloc(sizeof(char) * VirtualCom_BufferSize);
  83. // memcpy(VirtualCom_Buffer,buffer_out,VirtualCom_BufferSize);
  84. // USB_To_USART_Send_Data(VirtualCom_Buffer, VirtualCom_BufferSize); //WOW
  85. // free(VirtualCom_Buffer); //ÊͷŶ¯Ì¬¿Õ¼ä
  86. // memset(VirtualCom_Buffer,0,VirtualCom_BufferSize); //³õʼ»¯»º³å
  87. // VirtualCom_BufferSize=0;
  88. // }
  89. // if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  90. // {
  91. // /* Send the received data to the PC Host*/
  92. // USART_To_USB_Send_Data();
  93. // }
  94. //2£©USB½ÓÊÕÊý¾Ý´®¿Ú·¢³ö
  95. // if(bDeviceState == CONFIGURED)
  96. // {
  97. // USB_To_USART_Send_Data(buffer_out, count_out); //WOW
  98. // count_out = 0;
  99. // memset(VirtualCom_Buffer,0,VirtualCom_BufferSize); //³õʼ»¯»º³å
  100. // }
  101. //3£©USB½ÓÊÕÊý¾ÝÔÙͨ¹ýUSB·µ»Ø
  102. // USART_To_USB_Send_Data();
  103. // USART_TX_DMAFlagClear(USART1);
  104. // VirtualCom_LedRun();
  105. // }
  106. }
  107. /*******************************************************************************
  108. * º¯ÊýÃû :
  109. * ¹¦ÄÜÃèÊö :
  110. * ÊäÈë :
  111. * Êä³ö :
  112. * ·µ»Ø :
  113. *******************************************************************************/
  114. void VirtualCom_LedConfiguration(void)
  115. {
  116. // GPIO_InitTypeDef GPIO_InitStructure;
  117. //
  118. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  119. //
  120. // /* ×óLED */
  121. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  122. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  123. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  124. // GPIO_Init(GPIOB, &GPIO_InitStructure);
  125. //
  126. // /* ÓÒLED */
  127. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  128. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  129. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  130. // GPIO_Init(GPIOB, &GPIO_InitStructure);
  131. //
  132. // /* VBUS */
  133. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  134. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  135. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  136. // GPIO_Init(GPIOA, &GPIO_InitStructure);
  137. //
  138. //// GPIO_SetBits(GPIOB,GPIO_Pin_0);
  139. // GPIO_ResetBits(GPIOB,GPIO_Pin_0); //µãÁÁLEDÓÒ
  140. // GPIO_ResetBits(GPIOB,GPIO_Pin_10); //µãÁÁLED×ó
  141. // GPIO_Configuration_AOD2(GPIOB,GPIO_Pin_9); //VBUS
  142. GPIO_Configuration_OPP2(LED_CONNECTED_PORT,LED_CONNECTED_PIN); //ÓÒLED
  143. GPIO_Configuration_OPP2(LED_RUNNING_PORT,LED_RUNNING_PIN); //×óLED
  144. GPIO_Configuration_OPP2(GPIOB,GPIO_Pin_10); //CMSISµçÔ´¿ª¹Ø
  145. PB10=1;
  146. }
  147. /*******************************************************************************
  148. * º¯ÊýÃû :
  149. * ¹¦ÄÜÃèÊö :
  150. * ÊäÈë :
  151. * Êä³ö :
  152. * ·µ»Ø :
  153. *******************************************************************************/
  154. void VirtualCom_LedRun(void)
  155. {
  156. // if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_0))
  157. // {
  158. // PB0=0; //µãÁÁLEDÓÒ
  159. // PB10=1;
  160. // }
  161. // else
  162. // {
  163. // PB0=1; //µãÁÁLED×ó
  164. // PB10=0;
  165. // }
  166. // PB0=~PB0; //µãÁÁLEDÓÒ
  167. // PB0=~PB10; //µãÁÁLEDÓÒ
  168. PB0=PB10;
  169. PB10=~PB10;
  170. }
  171. #endif