usart.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* 包含头文件-----------------------------------------------------------------*/
  2. #include "usart.h"
  3. /* 私有宏定义-----------------------------------------------------------------*/
  4. /* 串口收发引脚重映射功能开关 0:关闭 1:开启 */
  5. #define USART1_REMAP_EN (0u) /* 默认引脚PA9PA10 重映射引脚PB6PB7 */
  6. #define USART2_REMAP_EN (0u) /* 默认引脚PA2PA3 重映射引脚PD5PD6 */
  7. /* 串口3特殊 取值为 0:关闭 1:开启部分重映射 2:开启完全重映射 */
  8. #define USART3_REMAP_EN (0u) /* 默认引脚PB10PB11 部分重映射引脚PC10PC11 完全重映射引脚PD8PD9 */
  9. /* 私有类型定义---------------------------------------------------------------*/
  10. /* 私有变量-------------------------------------------------------------------*/
  11. /* 全局变量-------------------------------------------------------------------*/
  12. /* 私有函数原型---------------------------------------------------------------*/
  13. /* 在别的地方复现此函数 串口1中断函数
  14. void USART1_IRQHandler(void)
  15. {
  16. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  17. {
  18. // user handle write
  19. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  20. }
  21. }
  22. */
  23. /* 在别的地方复现此函数 串口2中断函数
  24. void USART2_IRQHandler(void)
  25. {
  26. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  27. {
  28. // user handle write
  29. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  30. }
  31. }
  32. */
  33. /* 在别的地方复现此函数 串口3中断函数
  34. void USART3_IRQHandler(void)
  35. {
  36. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  37. {
  38. // user handle write
  39. USART_ClearITPendingBit(USART3, USART_IT_RXNE);
  40. }
  41. }
  42. */
  43. /**
  44. * @brief USART1初始化
  45. * @param baud : 串口波特率
  46. * @retval None
  47. */
  48. void usart1_config(uint32_t baud)
  49. {
  50. GPIO_InitTypeDef GPIO_InitStructure;
  51. USART_InitTypeDef USART_InitStructure;
  52. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* 打开串口时钟 */
  53. #if (USART1_REMAP_EN)
  54. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); /* 打开GPIO时钟 */
  55. GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); /* 开启重映射 */
  56. /* USART Tx的GPIO配置 */
  57. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /* 设置引脚号 */
  58. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  59. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  60. GPIO_Init(GPIOB, &GPIO_InitStructure); /* 初始化GPIO */
  61. /* USART Rx的GPIO配置 */
  62. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; /* 设置引脚号 */
  63. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  64. GPIO_Init(GPIOB, &GPIO_InitStructure); /* 初始化GPIO */
  65. #else
  66. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* 打开GPIO时钟 */
  67. /* USART Tx的GPIO配置 */
  68. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /* 设置引脚号 */
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  70. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  71. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  72. /* USART Rx的GPIO配置 */
  73. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* 设置引脚号 */
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  75. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  76. #endif
  77. USART_DeInit(USART1);
  78. USART_InitStructure.USART_BaudRate = baud; /* 波特率 */
  79. USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 数据长度 8bit */
  80. USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 停止位 1bit */
  81. USART_InitStructure.USART_Parity = USART_Parity_No; /* 校验位 无 */
  82. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* 硬件流控 无 */
  83. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 工作模式 接收和发送 */
  84. USART_Init(USART1, &USART_InitStructure); /* 初始化串口 */
  85. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* 使能接收中断 */
  86. USART_Cmd(USART1, ENABLE); /* 开启串口 */
  87. }
  88. /**
  89. * @brief USART2初始化
  90. * @param baud : 串口波特率
  91. * @retval None
  92. */
  93. void usart2_config(uint32_t baud)
  94. {
  95. GPIO_InitTypeDef GPIO_InitStructure;
  96. USART_InitTypeDef USART_InitStructure;
  97. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* 打开串口时钟 */
  98. #if (USART2_REMAP_EN)
  99. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); /* 打开GPIO时钟 */
  100. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); /* 开启重映射 */
  101. /* USART Tx的GPIO配置 */
  102. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /* 设置引脚号 */
  103. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  104. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  105. GPIO_Init(GPIOB, &GPIO_InitStructure); /* 初始化GPIO */
  106. /* USART Rx的GPIO配置 */
  107. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /* 设置引脚号 */
  108. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  109. GPIO_Init(GPIOD, &GPIO_InitStructure); /* 初始化GPIO */
  110. #else
  111. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* 打开GPIO时钟 */
  112. /* USART Tx的GPIO配置 */
  113. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /* 设置引脚号 */
  114. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  115. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  116. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  117. /* USART Rx的GPIO配置 */
  118. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; /* 设置引脚号 */
  119. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  120. GPIO_Init(GPIOA, &GPIO_InitStructure); /* 初始化GPIO */
  121. #endif
  122. USART_DeInit(USART2);
  123. USART_InitStructure.USART_BaudRate = baud; /* 波特率 */
  124. USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 数据长度 8bit */
  125. USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 停止位 1bit */
  126. USART_InitStructure.USART_Parity = USART_Parity_No; /* 校验位 无 */
  127. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* 硬件流控 无 */
  128. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 工作模式 接收和发送 */
  129. USART_Init(USART2, &USART_InitStructure); /* 初始化串口 */
  130. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /* 使能接收中断 */
  131. USART_Cmd(USART2, ENABLE); /* 开启串口 */
  132. }
  133. /**
  134. * @brief USART3初始化
  135. * @param baud : 串口波特率
  136. * @retval None
  137. */
  138. void usart3_config(uint32_t baud)
  139. {
  140. GPIO_InitTypeDef GPIO_InitStructure;
  141. USART_InitTypeDef USART_InitStructure;
  142. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* 打开串口时钟 */
  143. #if (1 == USART3_REMAP_EN)
  144. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); /* 打开GPIO时钟 */
  145. GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); /* 开启部分重映射 */
  146. /* USART Tx的GPIO配置 */
  147. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* 设置引脚号 */
  148. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  149. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  150. GPIO_Init(GPIOC, &GPIO_InitStructure); /* 初始化GPIO */
  151. /* USART Rx的GPIO配置 */
  152. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; /* 设置引脚号 */
  153. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  154. GPIO_Init(GPIOC, &GPIO_InitStructure); /* 初始化GPIO */
  155. #elif (2 == USART3_REMAP_EN)
  156. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); /* 打开GPIO时钟 */
  157. GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); /* 开启完全重映射 */
  158. /* USART Tx的GPIO配置 */
  159. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; /* 设置引脚号 */
  160. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  161. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  162. GPIO_Init(GPIOD, &GPIO_InitStructure); /* 初始化GPIO */
  163. /* USART Rx的GPIO配置 */
  164. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /* 设置引脚号 */
  165. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  166. GPIO_Init(GPIOD, &GPIO_InitStructure); /* 初始化GPIO */
  167. #else
  168. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* 打开GPIO时钟 */
  169. /* USART Tx的GPIO配置 */
  170. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* 设置引脚号 */
  171. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* 设置引脚模式 复用推挽 */
  172. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  173. GPIO_Init(GPIOB, &GPIO_InitStructure); /* 初始化GPIO */
  174. /* USART Rx的GPIO配置 */
  175. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; /* 设置引脚号 */
  176. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 设置引脚模式 浮空输入 */
  177. GPIO_Init(GPIOB, &GPIO_InitStructure); /* 初始化GPIO */
  178. #endif
  179. USART_DeInit(USART3);
  180. USART_InitStructure.USART_BaudRate = baud; /* 波特率 */
  181. USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 数据长度 8bit */
  182. USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 停止位 1bit */
  183. USART_InitStructure.USART_Parity = USART_Parity_No; /* 校验位 无 */
  184. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* 硬件流控 无 */
  185. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 工作模式 接收和发送 */
  186. USART_Init(USART3, &USART_InitStructure); /* 初始化串口 */
  187. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* 使能接收中断 */
  188. USART_Cmd(USART3, ENABLE); /* 开启串口 */
  189. }