[STM32F1] stm32的虚拟usb读FIFO和写FIFO

[复制链接]
762|0
 楼主| febgxu 发表于 2024-9-19 17:00 | 显示全部楼层 |阅读模式


  1. /* Includes ------------------------------------------------------------------*/

  2. #include "usb_lib.h"
  3. #include "usb_prop.h"
  4. #include "usb_desc.h"
  5. #include "hw_config.h"
  6. #include "usb_pwr.h"
  7. #include "Queue.h"


  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. /* Private macro -------------------------------------------------------------*/
  11. /* Private variables ---------------------------------------------------------*/
  12. ErrorStatus HSEStartUpStatus;
  13. USART_InitTypeDef USART_InitStructure;
  14. EXTI_InitTypeDef EXTI_InitStructure;


  15. #define USB_COM_RX_BUF_SIZE         (1024 + 256)
  16. #define USB_COM_TX_BUF_SIZE         (1024 + 256)

  17. static QUEUE8_t m_QueueUsbComRx         = {0};
  18. static QUEUE8_t m_QueueUsbComTx         = {0};
  19. static uint8_t  m_UsbComRxBuf[USB_COM_RX_BUF_SIZE]      = {0};
  20. static uint8_t  m_UsbComTxBuf[USB_COM_TX_BUF_SIZE]      = {0};

  21. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  22. /* Extern variables ----------------------------------------------------------*/

  23. extern LINE_CODING linecoding;

  24. /* Private function prototypes -----------------------------------------------*/
  25. /* Private functions ---------------------------------------------------------*/
  26. /*******************************************************************************
  27. * Function Name  : Set_System
  28. * Description    : Configures Main system clocks & power
  29. * Input          : None.
  30. * Return         : None.
  31. *******************************************************************************/
  32. void Set_System(void)
  33. {
  34.   GPIO_InitTypeDef GPIO_InitStructure;

  35.   QUEUE_PacketCreate(&m_QueueUsbComRx, m_UsbComRxBuf, sizeof(m_UsbComRxBuf));
  36.   QUEUE_PacketCreate(&m_QueueUsbComTx, m_UsbComTxBuf, sizeof(m_UsbComTxBuf));

  37.   /* Enable USB_DISCONNECT GPIO clock */
  38.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);

  39.   /* Configure USB pull-up pin */
  40.   GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
  41.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  42.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  43.   GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);

  44.   /* Configure the EXTI line 18 connected internally to the USB IP */
  45.   EXTI_ClearITPendingBit(EXTI_Line18);
  46.   EXTI_InitStructure.EXTI_Line = EXTI_Line18;
  47.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  48.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  49.   EXTI_Init(&EXTI_InitStructure);


  50. }

  51. /*******************************************************************************
  52. * Function Name  : Set_USBClock
  53. * Description    : Configures USB Clock input (48MHz)
  54. * Input          : None.
  55. * Return         : None.
  56. *******************************************************************************/
  57. void Set_USBClock(void)
  58. {
  59.   /* Select USBCLK source */
  60.   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  61.   /* Enable the USB clock */
  62.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  63. }

  64. /*******************************************************************************
  65. * Function Name  : Enter_LowPowerMode
  66. * Description    : Power-off system clocks and power while entering suspend mode
  67. * Input          : None.
  68. * Return         : None.
  69. *******************************************************************************/
  70. void Enter_LowPowerMode(void)
  71. {
  72.   /* Set the device state to suspend */
  73.   bDeviceState = SUSPENDED;
  74. }

  75. /*******************************************************************************
  76. * Function Name  : Leave_LowPowerMode
  77. * Description    : Restores system clocks and power while exiting suspend mode
  78. * Input          : None.
  79. * Return         : None.
  80. *******************************************************************************/
  81. void Leave_LowPowerMode(void)
  82. {
  83.   DEVICE_INFO *pInfo = &Device_Info;

  84.   /* Set the device state to the correct state */
  85.   if (pInfo->Current_Configuration != 0)
  86.   {
  87.     /* Device configured */
  88.     bDeviceState = CONFIGURED;
  89.   }
  90.   else
  91.   {
  92.     bDeviceState = ATTACHED;
  93.   }
  94.   /*Enable SystemCoreClock*/
  95. //  SystemInit();
  96. }

  97. /*******************************************************************************
  98. * Function Name  : USB_Interrupts_Config
  99. * Description    : Configures the USB interrupts
  100. * Input          : None.
  101. * Return         : None.
  102. *******************************************************************************/
  103. void USB_Interrupts_Config(void)
  104. {
  105.   NVIC_InitTypeDef NVIC_InitStructure;

  106.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  107.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  108.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  109.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  110.   NVIC_Init(&NVIC_InitStructure);

  111.     /* Enable the USB Wake-up interrupt */
  112.   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  113.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  114.   NVIC_Init(&NVIC_InitStructure);
  115. }

  116. /*******************************************************************************
  117. * Function Name  : USB_Cable_Config
  118. * Description    : Software Connection/Disconnection of USB Cable
  119. * Input          : None.
  120. * Return         : Status
  121. *******************************************************************************/
  122. void USB_Cable_Config (FunctionalState NewState)
  123. {
  124.   if (NewState == DISABLE)
  125.   {
  126.     GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  127.   }
  128.   else
  129.   {
  130.     GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  131.   }
  132. }

  133. /*******************************************************************************
  134. * Function Name : void USB_Config(void)
  135. * Description   : USB系统初始化
  136. * Input         :
  137. * Output        :
  138. * Other         :
  139. * Date          : 2014.11.28
  140. *******************************************************************************/
  141. void USB_Config(void)
  142. {
  143.     Set_System();

  144.     Set_USBClock();

  145.     USB_Interrupts_Config();

  146.     USB_Init();
  147. }

  148. /*******************************************************************************
  149. * Function Name : uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
  150. * Description   : 从USB接收缓存中读数据
  151. * Input         :
  152. * Output        :
  153. * Other         :
  154. * Date          : 2014.11.28
  155. *******************************************************************************/
  156. uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
  157. {
  158.     return QUEUE_PacketOut(&m_QueueUsbComRx, buffter, buffterSize);
  159. }
  160. /*******************************************************************************
  161. * Function Name : uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
  162. * Description   : 写数据到USB接收缓存中
  163. * Input         :
  164. * Output        :
  165. * Other         :
  166. * Date          : 2014.11.28
  167. *******************************************************************************/
  168. uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
  169. {
  170.     return QUEUE_PacketIn(&m_QueueUsbComRx, buffter, writeLen);
  171. }
  172. /*******************************************************************************
  173. * Function Name : uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
  174. * Description   : 从USB发送缓存中读数据
  175. * Input         :
  176. * Output        :
  177. * Other         :
  178. * Date          : 2014.11.28
  179. *******************************************************************************/
  180. uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
  181. {
  182.     return QUEUE_PacketOut(&m_QueueUsbComTx, buffter, buffterSize);;
  183. }
  184. /*******************************************************************************
  185. * Function Name : uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
  186. * Description   : 写数据到USB发送缓存中
  187. * Input         :
  188. * Output        :
  189. * Other         :
  190. * Date          : 2014.11.28
  191. *******************************************************************************/
  192. uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
  193. {
  194.     return QUEUE_PacketIn(&m_QueueUsbComTx, buffter, writeLen);
  195. }



  196. /*******************************************************************************
  197. * Function Name  : Get_SerialNum.
  198. * Description    : Create the serial number string descriptor.
  199. * Input          : None.
  200. * Output         : None.
  201. * Return         : None.
  202. *******************************************************************************/
  203. void Get_SerialNum(void)
  204. {
  205.   uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

  206.   Device_Serial0 = *(uint32_t*)ID1;
  207.   Device_Serial1 = *(uint32_t*)ID2;
  208.   Device_Serial2 = *(uint32_t*)ID3;

  209.   Device_Serial0 += Device_Serial2;

  210.   if (Device_Serial0 != 0)
  211.   {
  212.     IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
  213.     IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
  214.   }
  215. }

  216. /*******************************************************************************
  217. * Function Name  : HexToChar.
  218. * Description    : Convert Hex 32Bits value into char.
  219. * Input          : None.
  220. * Output         : None.
  221. * Return         : None.
  222. *******************************************************************************/
  223. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  224. {
  225.   uint8_t idx = 0;

  226.   for( idx = 0 ; idx < len ; idx ++)
  227.   {
  228.     if( ((value >> 28)) < 0xA )
  229.     {
  230.       pbuf[ 2* idx] = (value >> 28) + '0';
  231.     }
  232.     else
  233.     {
  234.       pbuf[2* idx] = (value >> 28) + 'A' - 10;
  235.     }

  236.     value = value << 4;

  237.     pbuf[ 2* idx + 1] = 0;
  238.   }
  239. }

  240. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


您需要登录后才可以回帖 登录 | 注册

本版积分规则

59

主题

5120

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部