您现在的位置: 主页 > 嵌入式开发入门到精通 > 单片机技术进阶 > STM32 USB虚拟串口 -
本文所属标签:
为本文创立个标签吧:

STM32 USB虚拟串口 -

来源: 网络用户发布,如有版权联系网管删除 2018-09-07 

[导读]串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出。很多本本人群,更是深有体会,不准备一个USB转串口工具就没办法进行开发。本章节来简单概述STM32低端芯片上的USB虚拟串口的移植。在官方DEMO中已经提

串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出。很多本本人群,更是深有体会,不准备一个USB转串口工具就没办法进行开发。本章节来简单概述STM32低端芯片上的USB虚拟串口的移植。在官方DEMO中已经提供了现成的程序,这里对修改方法做简单说明。

本文引用地址: http://www.21ic.com/app/mcu/201808/784487.htm

首先打开官方demo我们开始进行移植,第一步复制我们可用的文件,操作如下:

ProjectsVirtual_COM_Port文件夹下,复制红线部分

图1

图2

我为了方便演示统放在usb/src文件夹下:

图3

现在复制USB的库文件,这些文件不需要我们修改:

图4

上图中的文件统一放在usb/lib文件夹下:

图5

好了现在所需要的文件我们以复制完了。这里先讲一下DEMO程序的主要工作流程:

图6

由上图可知,PC通过虚拟串口发送数据到STM32 usb口,STM32再通过usart1发送数据到PC串口。我们做项目时,只用USB虚拟串口即可。所以我们现在需要把串口发送部分删除。把USB做为一个COM口来使用。我们要如何使用这个USB口呢?demo中是把USB发送数据做了一个缓存,先把要发送的数据存入缓存中,然后由USB自动发送出去。而接收部分是直接通过串口透传。我们在应用时就需要用到两个FIFO,1是发送,这个和demo方式是样;2是接收,接收也做一个缓存,我们通过查询来判断是否收到新数据。这下大家应该明白为什么使用两个FIFO了。 我这里有写好的FIFO库函数可直接使用Queue.c文件。

现在开始修改:

1,stm32_it.c 更名为usb_it.c删除无用代码,只保留usb中断函数,和唤醒函数。代码如下:

代码1

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

2 #include "hw_config.h"

3 #include "usb_lib.h"

4 #include "usb_istr.h"

5

6

7 /*******************************************************************************

8 * Function Name : USB_IRQHandler

9 * Description : This function handles USB Low Priority interrupts

10 * requests.

11 * Input : None

12 * Output : None

13 * Return : None

14 *******************************************************************************/

15 #if defined(STM32L1XX_MD) "| defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)|| defined (STM32F37X)

16 void USB_LP_IRQHandler(void)

17 #else

18 void USB_LP_CAN1_RX0_IRQHandler(void)

19 #endif

20 {

21 USB_Istr();

22 }

23

24 /*******************************************************************************

25 * Function Name : USB_FS_WKUP_IRQHandler

26 * Description : This function handles USB WakeUp interrupt request.

27 * Input : None

28 * Output : None

29 * Return : None

30 *******************************************************************************/

31

32 #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)

33 void USB_FS_WKUP_IRQHandler(void)

34 #else

35 void USBWakeUp_IRQHandler(void)

36 #endif

37 {

38 EXTI_ClearITPendingBit(EXTI_Line18);

39 }

2,修改代码hw_config.c删除无用代码,新建立2组,读FIFO和写FIFO的函数。后面会用到。

代码如下:

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

2

3 #include "usb_lib.h"

4 #include "usb_prop.h"

5 #include "usb_desc.h"

6 #include "hw_config.h"

7 #include "usb_pwr.h"

8 #include "Queue.h"

9

10

11 /* Private typedef -----------------------------------------------------------*/

12 /* Private define ------------------------------------------------------------*/

13 /* Private macro -------------------------------------------------------------*/

14 /* Private variables ---------------------------------------------------------*/

15 ErrorStatus HSEStartUpStatus;

16 USART_InitTypeDef USART_InitStructure;

17 EXTI_InitTypeDef EXTI_InitStructure;

18

19

20 #define USB_COM_RX_BUF_SIZE (1024 + 256)

21 #define USB_COM_TX_BUF_SIZE (1024 + 256)

22

23 static QUEUE8_t m_QueueUsbComRx = {0};

24 static QUEUE8_t m_QueueUsbComTx = {0};

25 static uint8_t m_UsbComRxBuf[USB_COM_RX_BUF_SIZE] = {0};

26 static uint8_t m_UsbComTxBuf[USB_COM_TX_BUF_SIZE] = {0};

27

28 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);

29 /* Extern variables ----------------------------------------------------------*/

30

31 extern LINE_CODING linecoding;

32

33 /* Private function prototypes -----------------------------------------------*/

34 /* Private functions ---------------------------------------------------------*/

35 /*******************************************************************************

36 * Function Name : Set_System

37 * Description : Configures Main system clocks & power

38 * Input : None.

39 * Return : None.

40 *******************************************************************************/

41 void Set_System(void)

42 {

43 GPIO_InitTypeDef GPIO_InitStructure;

44

45 QUEUE_PacketCreate(&m_QueueUsbComRx, m_UsbComRxBuf, sizeof(m_UsbComRxBuf));

46 QUEUE_PacketCreate(&m_QueueUsbComTx, m_UsbComTxBuf, sizeof(m_UsbComTxBuf));

47

48 /* Enable USB_DISCONNECT GPIO clock */

49 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);

50

51 /* Configure USB pull-up pin */

52 GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;

53 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

54 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

55 GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);

56

57 /* Configure the EXTI line 18 connected internally to the USB IP */

58 EXTI_ClearITPendingBit(EXTI_Line18);

59 EXTI_InitStructure.EXTI_Line = EXTI_Line18;

60 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

61 EXTI_InitStructure.EXTI_LineCmd = ENABLE;

62 EXTI_Init(&EXTI_InitStructure);

63

64

65 }

66

67 /*******************************************************************************

68 * Function Name : Set_USBClock

69 * Description : Configures USB Clock input (48MHz)

70 * Input : None.

71 * Return : None.

72 *******************************************************************************/

73 void Set_USBClock(void)

74 {

75 /* Select USBCLK source */

76 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

77

78 /* Enable the USB clock */

79 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);

80 }

81

82 /*******************************************************************************

83 * Function Name : Enter_LowPowerMode

84 * Description : Power-off system clocks and power while entering suspend mode

85 * Input : None.

86 * Return : None.

87 *******************************************************************************/

88 void Enter_LowPowerMode(void)

89 {

90 /* Set the device state to suspend */

91 bDeviceState = SUSPENDED;

92 }

93

94 /*******************************************************************************

95 * Function Name : Leave_LowPowerMode

96 * Description : Restores system clocks and power while exiting suspend mode

97 * Input : None.

98 * Return : None.

99 *******************************************************************************/

100 void Leave_LowPowerMode(void)

101 {

102 DEVICE_INFO *pInfo = &Device_Info;

103

104 /* Set the device state to the correct state */

105 if (pInfo->Current_Configuration != 0)

106 {

107 /* Device configured */

108 bDeviceState = CONFIGURED;

109 }

110 else

111 {

112 bDeviceState = ATTACHED;

113 }

114 /*Enable SystemCoreClock*/

115 // SystemInit();

116 }

117

118 /*******************************************************************************

119 * Function Name : USB_Interrupts_Config

120 * Description : Configures the USB interrupts

121 * Input : None.

122 * Return : None.

123 *******************************************************************************/

124 void USB_Interrupts_Config(void)

125 {

126 NVIC_InitTypeDef NVIC_InitStructure;

127

128 NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

129 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

130 NVIC



              查看评论 回复



 

"STM32 USB虚拟串口 -"的相关文章

网站地图

围观()