usart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**
  2. ******************************** STM32F10x *********************************
  3. * @文件名 : usart.c
  4. * @作者 : sun
  5. * @库版本 : V3.5.0
  6. * @文件版本 : V1.0.0
  7. * @日期 : 2016年05月09日
  8. * @摘要 : USART源文件
  9. ******************************************************************************/
  10. /*----------------------------------------------------------------------------
  11. 更新日志:
  12. 2016-05-09 V1.0.0:初始版本
  13. ----------------------------------------------------------------------------*/
  14. /* 包含的头文件 --------------------------------------------------------------*/
  15. #include "usart.h"
  16. #include "led.h"
  17. #include "systick.h"
  18. #include <string.h>
  19. /********************uart1****************************/
  20. volatile uint8_t m_state_code=M_STATE_IDLE;
  21. uint32_t usart2_cmd_time_out=0;
  22. uint32_t usart2_cmd_time_counter=0;
  23. #if 1
  24. #pragma import(__use_no_semihosting)
  25. //标准库需要的支持函数
  26. struct __FILE
  27. {
  28. int handle;
  29. };
  30. FILE __stdout;
  31. //定义_sys_exit()以避免使用半主机模式
  32. void _sys_exit(int x)
  33. {
  34. x = x;
  35. }
  36. //重定义fputc函数
  37. int fputc(int ch, FILE *f)
  38. {
  39. uint16_t temp=1000;
  40. while((temp>0)&((USART1->SR&0X40)==0))
  41. {
  42. temp--;
  43. };//循环发送,直到发送完毕
  44. //while((USART2->SR & USART_FLAG_TXE) == RESET);
  45. USART1->DR = (u8) ch;
  46. return ch;
  47. }
  48. #endif
  49. /************************************************
  50. 函数名称 : USART1_GPIO_Configuration
  51. 功 能 : USART所使用管脚输出输入定义
  52. 参 数 : 无
  53. 返 回 值 : 无
  54. 作 者 : sun
  55. *************************************************/
  56. void USART1_GPIO_Configuration(void)
  57. {
  58. GPIO_InitTypeDef GPIO_InitStructure;
  59. /* 定义 USART1-TX 引脚为复用输出 */
  60. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //IO口的第A9脚
  61. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  62. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  63. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
  64. /* 定义 USART1-Rx 引脚为悬空输入 */
  65. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第A10脚
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  67. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
  68. }
  69. /************************************************
  70. 函数名称 : USART_Configuration
  71. 功 能 : 配置USART
  72. 参 数 : 无
  73. 返 回 值 : 无
  74. 作 者 : sun
  75. *************************************************/
  76. void USART1_Configuration(void)
  77. {
  78. USART_InitTypeDef USART_InitStructure;
  79. /******************************************************************
  80. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  81. 115200 8 1 0(NO)
  82. *******************************************************************/
  83. USART_InitStructure.USART_BaudRate = USART1_BAUDRATE; //设定传输速率
  84. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
  85. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  86. USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  87. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  88. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  89. USART_Init(USART1, &USART_InitStructure); //初始化USART1
  90. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能USART1接收中断
  91. USART_Cmd(USART1, ENABLE); //使能USART1
  92. }
  93. /************************************************
  94. 函数名称 : USART1_GPIO_Configuration
  95. 功 能 : USART所使用管脚输出输入定义
  96. 参 数 : 无
  97. 返 回 值 : 无
  98. 作 者 : sun
  99. *************************************************/
  100. void USART2_GPIO_Configuration(void)
  101. {
  102. GPIO_InitTypeDef GPIO_InitStructure;
  103. /* 定义 USART1-TX 引脚为复用输出 */
  104. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第A9脚
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  106. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  107. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
  108. /* 定义 USART1-Rx 引脚为悬空输入 */
  109. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //IO口的第A10脚
  110. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IPU GPIO_Mode_IN_FLOATING; //IO口悬空输入
  111. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
  112. }
  113. /************************************************
  114. 函数名称 : USART_Configuration
  115. 功 能 : 配置USART
  116. 参 数 : 无
  117. 返 回 值 : 无
  118. 作 者 : sun
  119. *************************************************/
  120. void USART2_Configuration(void)
  121. {
  122. USART_InitTypeDef USART_InitStructure;
  123. /******************************************************************
  124. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  125. 115200 8 1 0(NO)
  126. *******************************************************************/
  127. USART_InitStructure.USART_BaudRate = USART2_BAUDRATE; //设定传输速率
  128. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
  129. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  130. USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  131. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  132. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  133. USART_Init(USART2, &USART_InitStructure); //初始化USART1
  134. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能USART1接收中断
  135. USART_ClearFlag(USART2, USART_FLAG_TC);
  136. USART_Cmd(USART2, ENABLE); //使能USART1
  137. }
  138. /************************************************
  139. 函数名称 : USART1_GPIO_Configuration
  140. 功 能 : USART所使用管脚输出输入定义
  141. 参 数 : 无
  142. 返 回 值 : 无
  143. 作 者 : sun
  144. *************************************************/
  145. void USART4_GPIO_Configuration(void)
  146. {
  147. GPIO_InitTypeDef GPIO_InitStructure;
  148. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  149. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  150. /* 定义 USART1-TX 引脚为复用输出 */
  151. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第C10脚
  152. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  153. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  154. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
  155. /* 定义 USART1-Rx 引脚为悬空输入 */
  156. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //IO口的第C11脚
  157. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  158. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输入IO口
  159. }
  160. /************************************************
  161. 函数名称 : USART_Configuration
  162. 功 能 : 配置USART
  163. 参 数 : 无
  164. 返 回 值 : 无
  165. 作 者 : sun
  166. *************************************************/
  167. void USART4_Configuration(void)
  168. {
  169. USART_InitTypeDef USART_InitStructure;
  170. /******************************************************************
  171. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  172. 115200 8 1 0(NO)
  173. *******************************************************************/
  174. USART_InitStructure.USART_BaudRate = UART4_BAUDRATE; //设定传输速率
  175. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
  176. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  177. USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  178. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  179. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  180. USART_Init(UART4, &USART_InitStructure); //初始化UART4
  181. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能UART4接收中断
  182. USART_Cmd(UART4, ENABLE); //使能UART4
  183. }
  184. /************************************************
  185. 函数名称 : USART1_GPIO_Configuration
  186. 功 能 : USART所使用管脚输出输入定义
  187. 参 数 : 无
  188. 返 回 值 : 无
  189. 作 者 : sun
  190. *************************************************/
  191. void USART5_GPIO_Configuration(void)
  192. {
  193. GPIO_InitTypeDef GPIO_InitStructure;
  194. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  195. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  196. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
  197. /* 定义 USART1-TX 引脚为复用输出 */
  198. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //IO口的第PC12脚
  199. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  200. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  201. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
  202. /* 定义 USART1-Rx 引脚为悬空输入 */
  203. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第PD2脚
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  205. GPIO_Init(GPIOD, &GPIO_InitStructure); //USART输入IO口
  206. }
  207. /************************************************
  208. 函数名称 : USART_Configuration
  209. 功 能 : 配置USART
  210. 参 数 : 无
  211. 返 回 值 : 无
  212. 作 者 : sun
  213. *************************************************/
  214. void USART5_Configuration(void)
  215. {
  216. USART_InitTypeDef USART_InitStructure;
  217. /******************************************************************
  218. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  219. 115200 8 1 0(NO)
  220. *******************************************************************/
  221. USART_InitStructure.USART_BaudRate = UART5_BAUDRATE; //设定传输速率
  222. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
  223. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  224. USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  225. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  226. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  227. USART_Init(UART5, &USART_InitStructure); //初始化USART5
  228. USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //使能USART5接收中断
  229. USART_Cmd(UART5, ENABLE); //使能USART5
  230. }
  231. /************************************************
  232. 函数名称 : USART_Initializes
  233. 功 能 : 串口初始化
  234. 参 数 : 无
  235. 返 回 值 : 无
  236. 作 者 : sun
  237. *************************************************/
  238. void USART_Initializes(void)
  239. {
  240. USART1_GPIO_Configuration();
  241. USART1_Configuration();
  242. }
  243. /************************************************
  244. 函数名称 : USART1_SendChar
  245. 功 能 : 串口1发送一个字符
  246. 参 数 : Data --- 数据
  247. 返 回 值 : 无
  248. 作 者 : sun
  249. *************************************************/
  250. void USART1_SendByte(uint8_t Data)
  251. {
  252. while((USART1->SR & USART_FLAG_TXE) == RESET);
  253. USART1->DR = (Data & (uint16_t)0x01FF);
  254. }
  255. /************************************************
  256. 函数名称 : USART1_SendNByte
  257. 功 能 : 串口1发送N个字符
  258. 参 数 : pData ----- 字符串
  259. Length --- 长度
  260. 返 回 值 : 无
  261. 作 者 : sun
  262. *************************************************/
  263. void USART1_SendNByte(uint8_t *pData, uint16_t Length)
  264. {
  265. while(Length--)
  266. {
  267. USART1_SendByte(*pData);
  268. pData++;
  269. }
  270. }
  271. /************************************************
  272. 函数名称 : USART1_Printf
  273. 功 能 : 串口1打印输出
  274. 参 数 : string --- 字符串
  275. 返 回 值 : 无
  276. 作 者 : sun
  277. *************************************************/
  278. void USART1_Printf(uint8_t *String)
  279. {
  280. while((*String) != '\0')
  281. {
  282. USART1_SendByte(*String);
  283. String++;
  284. }
  285. }
  286. /************************************************
  287. 函数名称 : USART1_SendChar
  288. 功 能 : 串口1发送一个字符
  289. 参 数 : Data --- 数据
  290. 返 回 值 : 无
  291. 作 者 : sun
  292. *************************************************/
  293. void USART2_SendByte(uint8_t Data)
  294. {
  295. while((USART2->SR & USART_FLAG_TXE) == RESET);
  296. USART2->DR = (Data & (uint16_t)0x01FF);
  297. }
  298. /************************************************
  299. 函数名称 : USART1_SendNByte
  300. 功 能 : 串口1发送N个字符
  301. 参 数 : pData ----- 字符串
  302. Length --- 长度
  303. 返 回 值 : 无
  304. 作 者 : sun
  305. *************************************************/
  306. void USART2_SendNByte(uint8_t *pData, uint16_t Length)
  307. {
  308. while(Length--)
  309. {
  310. USART2_SendByte(*pData);
  311. pData++;
  312. }
  313. }
  314. /************************************************
  315. 函数名称 : USART1_Printf
  316. 功 能 : 串口1打印输出
  317. 参 数 : string --- 字符串
  318. 返 回 值 : 无
  319. 作 者 : sun
  320. *************************************************/
  321. void USART2_Printf(uint8_t *String)
  322. {
  323. while((*String) != '\0')
  324. {
  325. USART2_SendByte(*String);
  326. String++;
  327. }
  328. }
  329. /************************************************
  330. 函数名称 : UART4_SendChar
  331. 功 能 : 串口4发送一个字符
  332. 参 数 : Data --- 数据
  333. 返 回 值 : 无
  334. 作 者 : sun
  335. *************************************************/
  336. void UART4_SendByte(uint8_t Data)
  337. {
  338. while((UART4->SR & USART_FLAG_TXE) == RESET);
  339. UART4->DR = (Data & (uint16_t)0x01FF);
  340. }
  341. /************************************************
  342. 函数名称 : USART4_SendNByte
  343. 功 能 : 串口4发送N个字符
  344. 参 数 : pData ----- 字符串
  345. Length --- 长度
  346. 返 回 值 : 无
  347. 作 者 : sun
  348. *************************************************/
  349. void UART4_SendNByte(uint8_t *pData, uint16_t Length)
  350. {
  351. while(Length--)
  352. {
  353. UART4_SendByte(*pData);
  354. pData++;
  355. }
  356. }
  357. /************************************************
  358. 函数名称 : USART4_Printf
  359. 功 能 : 串口4打印输出
  360. 参 数 : string --- 字符串
  361. 返 回 值 : 无
  362. 作 者 : sun
  363. *************************************************/
  364. void UART4_Printf(uint8_t *String)
  365. {
  366. while((*String) != '\0')
  367. {
  368. UART4_SendByte(*String);
  369. String++;
  370. }
  371. }
  372. /************************************************
  373. 函数名称 : UART5_SendChar
  374. 功 能 : 串口5发送一个字符
  375. 参 数 : Data --- 数据
  376. 返 回 值 : 无
  377. 作 者 : sun
  378. *************************************************/
  379. void UART5_SendByte(uint8_t Data)
  380. {
  381. while((UART5->SR & USART_FLAG_TXE) == RESET);
  382. UART5->DR = (Data & (uint16_t)0x01FF);
  383. }
  384. /************************************************
  385. 函数名称 : USART4_SendNByte
  386. 功 能 : 串口5发送N个字符
  387. 参 数 : pData ----- 字符串
  388. Length --- 长度
  389. 返 回 值 : 无
  390. 作 者 : sun
  391. *************************************************/
  392. void UART5_SendNByte(uint8_t *pData, uint16_t Length)
  393. {
  394. while(Length--)
  395. {
  396. UART5_SendByte(*pData);
  397. pData++;
  398. }
  399. }
  400. /************************************************
  401. 函数名称 : USART4_Printf
  402. 功 能 : 串口5打印输出
  403. 参 数 : string --- 字符串
  404. 返 回 值 : 无
  405. 作 者 : sun
  406. *************************************************/
  407. void UART5_Printf(uint8_t *String)
  408. {
  409. while((*String) != '\0')
  410. {
  411. UART5_SendByte(*String);
  412. String++;
  413. }
  414. }
  415. void USART_printfHex(const uint8_t* SendChars,uint16_t len)
  416. {
  417. uint16_t i = 0;
  418. // uint8_t temp = 0;
  419. for(i=0;i<len;i++)
  420. {
  421. printf("%02X",SendChars[i]);
  422. }
  423. printf("\r\n");
  424. }
  425. #define CHAR_TO_UPPER(ch) ((ch >= 'a' && ch <= 'z')?(ch-0x20):ch)
  426. /**
  427. * @brief ascii convert hex
  428. * @par param[in] *hex:hex data
  429. * @par param[in] *ascii:ascii data
  430. * @par param[in] asciiLen:length of ascii
  431. * @retval length
  432. */
  433. uint8_t Ascii2Hex(uint8_t *hex, uint8_t *ascii, uint8_t asciiLen)
  434. {
  435. uint8_t i,ch,value;
  436. value = 0;
  437. for(i=0;i<(asciiLen>>1);i++) {
  438. ch = CHAR_TO_UPPER(ascii[i*2]);
  439. if(ch >= '0' && ch <= '9') {
  440. value = ch -'0';
  441. }
  442. else if(ch >= 'A' && ch <= 'F') {
  443. value = ch - 'A' + 0x0A;
  444. }
  445. else {
  446. return i;
  447. }
  448. hex[i] = (value<<4);
  449. ch = CHAR_TO_UPPER(ascii[i*2+1]);
  450. if(ch >= '0' && ch <= '9') {
  451. value = ch -'0';
  452. }
  453. else if(ch >= 'A' && ch <= 'F') {
  454. value = ch - 'A' + 0x0A;
  455. }
  456. else {
  457. return i;
  458. }
  459. hex[i] += value;
  460. }
  461. return i;
  462. }
  463. /**
  464. * @brief hex convert ascii
  465. * @par param[in] *ascii:ascii data
  466. * @par param[in] *hex:hex data
  467. * @par param[in] hexLen:length of hex
  468. * @retval length
  469. */
  470. uint8_t Hex2Ascii(uint8_t *ascii, uint8_t *hex, uint8_t hexLen)
  471. {
  472. uint8_t i, value;
  473. for(i=0;i<hexLen;i++) {
  474. value = (hex[i]>>4);
  475. if(value > 9) {
  476. value += 0x07;
  477. }
  478. ascii[2*i] = value+0x30;
  479. value = (hex[i]&0x0F);
  480. if(value > 9) {
  481. value += 0x07;
  482. }
  483. ascii[2*i+1] = value+0x30;
  484. }
  485. return hexLen*2;
  486. }
  487. /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/