device.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /**
  2. ******************************** STM32F10x *********************************
  3. * @文件名 : device.c
  4. * @作者 : sun
  5. * @库版本 : V3.5.0
  6. * @文件版本 : V1.0.0
  7. * @日期 : 2016年05月09日
  8. * @摘要 : device源文件
  9. ******************************************************************************/
  10. /*----------------------------------------------------------------------------
  11. 更新日志:
  12. 2016-05-09 V1.0.0:初始版本
  13. ----------------------------------------------------------------------------*/
  14. /* 包含的头文件 --------------------------------------------------------------*/
  15. #include "usart.h"
  16. #include "led.h"
  17. #include "queue.h"
  18. #include <string.h>
  19. /********************uart1****************************/
  20. uint8_t USART1_receive_buf[USART1_BUF_SIZE],USART1_ready_buf[USART1_BUF_SIZE];
  21. static int32_t USART1_ReceiveTimeCounter = 0;
  22. volatile uint16_t USART1_receive_index=0;
  23. volatile uint8_t USART1_ready_buf_ok = 0;
  24. volatile uint16_t USART1_ready_buf_len = 0;
  25. uint8_t USART2_receive_buf[USART2_BUF_SIZE],USART2_ready_buf[USART2_BUF_SIZE];
  26. static int32_t USART2_ReceiveTimeCounter = 0;
  27. volatile uint16_t USART2_receive_index=0;
  28. volatile uint8_t USART2_ready_buf_ok = 0;
  29. volatile uint16_t USART2_ready_buf_len = 0;
  30. uint8_t UART4_receive_buf[UART4_BUF_SIZE],UART4_ready_buf[UART4_BUF_SIZE];
  31. static int32_t UART4_ReceiveTimeCounter = 0;
  32. volatile uint16_t UART4_receive_index=0;
  33. volatile uint8_t UART4_ready_buf_ok = 0;
  34. volatile uint8_t UART4_ready_buf_len = 0;
  35. sequeue_t *usart2_rx_sq;
  36. sequeue_t *uart4_rx_sq;
  37. sequeue_t *uart4_tx_sq;
  38. sequeue_t *usart1_rx_sq;
  39. sequeue_t *usart1_tx_sq;
  40. sequeue_t *uart5_rx_sq;
  41. sequeue_t *uart5_tx_sq;
  42. uint8_t UART5_receive_buf[UART5_BUF_SIZE],UART5_ready_buf[UART5_BUF_SIZE];
  43. static int32_t UART5_ReceiveTimeCounter = 0;
  44. volatile uint16_t UART5_receive_index=0;
  45. volatile uint8_t UART5_ready_buf_ok = 0;
  46. volatile uint16_t UART5_ready_buf_len = 0;
  47. volatile uint8_t m_state_code=M_STATE_IDLE;
  48. #if 1
  49. #pragma import(__use_no_semihosting)
  50. //标准库需要的支持函数
  51. struct __FILE
  52. {
  53. int handle;
  54. };
  55. FILE __stdout;
  56. //定义_sys_exit()以避免使用半主机模式
  57. void _sys_exit(int x)
  58. {
  59. x = x;
  60. }
  61. //重定义fputc函数
  62. int fputc(int ch, FILE *f)
  63. {
  64. uint16_t temp=1000;
  65. while((temp>0)&((USART2->SR&0X40)==0))
  66. {
  67. temp--;
  68. };//循环发送,直到发送完毕
  69. //while((USART2->SR & USART_FLAG_TXE) == RESET);
  70. USART2->DR = (u8) ch;
  71. return ch;
  72. }
  73. #endif
  74. /************************************************
  75. 函数名称 : USART1_GPIO_Configuration
  76. 功 能 : USART所使用管脚输出输入定义
  77. 参 数 : 无
  78. 返 回 值 : 无
  79. 作 者 : sun
  80. *************************************************/
  81. void USART1_GPIO_Configuration(void)
  82. {
  83. GPIO_InitTypeDef GPIO_InitStructure;
  84. /* 定义 USART1-TX 引脚为复用输出 */
  85. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //IO口的第A9脚
  86. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  88. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
  89. /* 定义 USART1-Rx 引脚为悬空输入 */
  90. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第A10脚
  91. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  92. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
  93. }
  94. /************************************************
  95. 函数名称 : USART_Configuration
  96. 功 能 : 配置USART
  97. 参 数 : 无
  98. 返 回 值 : 无
  99. 作 者 : sun
  100. *************************************************/
  101. void USART1_Configuration(void)
  102. {
  103. USART_InitTypeDef USART_InitStructure;
  104. /******************************************************************
  105. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  106. 115200 8 1 0(NO)
  107. *******************************************************************/
  108. USART_InitStructure.USART_BaudRate = USART1_BAUDRATE; //设定传输速率
  109. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
  110. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  111. USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  112. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  113. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  114. USART_Init(USART1, &USART_InitStructure); //初始化USART1
  115. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能USART1接收中断
  116. USART_Cmd(USART1, ENABLE); //使能USART1
  117. }
  118. /************************************************
  119. 函数名称 : USART1_GPIO_Configuration
  120. 功 能 : USART所使用管脚输出输入定义
  121. 参 数 : 无
  122. 返 回 值 : 无
  123. 作 者 : sun
  124. *************************************************/
  125. void USART2_GPIO_Configuration(void)
  126. {
  127. GPIO_InitTypeDef GPIO_InitStructure;
  128. /* 定义 USART1-TX 引脚为复用输出 */
  129. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第A9脚
  130. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  131. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  132. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输出IO口
  133. /* 定义 USART1-Rx 引脚为悬空输入 */
  134. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //IO口的第A10脚
  135. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  136. GPIO_Init(GPIOA, &GPIO_InitStructure); //USART输入IO口
  137. }
  138. /************************************************
  139. 函数名称 : USART_Configuration
  140. 功 能 : 配置USART
  141. 参 数 : 无
  142. 返 回 值 : 无
  143. 作 者 : sun
  144. *************************************************/
  145. void USART2_Configuration(void)
  146. {
  147. USART_InitTypeDef USART_InitStructure;
  148. /******************************************************************
  149. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  150. 115200 8 1 0(NO)
  151. *******************************************************************/
  152. USART_InitStructure.USART_BaudRate = USART2_BAUDRATE; //设定传输速率
  153. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设定传输数据位数,USART_WordLength_8b USART_WordLength_9b
  154. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  155. USART_InitStructure.USART_Parity = USART_Parity_No ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  156. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  157. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  158. USART_Init(USART2, &USART_InitStructure); //初始化USART1
  159. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能USART1接收中断
  160. USART_ClearFlag(USART2, USART_FLAG_TC);
  161. USART_Cmd(USART2, ENABLE); //使能USART1
  162. }
  163. /************************************************
  164. 函数名称 : USART1_GPIO_Configuration
  165. 功 能 : USART所使用管脚输出输入定义
  166. 参 数 : 无
  167. 返 回 值 : 无
  168. 作 者 : sun
  169. *************************************************/
  170. void USART4_GPIO_Configuration(void)
  171. {
  172. GPIO_InitTypeDef GPIO_InitStructure;
  173. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  174. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  175. /* 定义 USART1-TX 引脚为复用输出 */
  176. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO口的第C10脚
  177. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  178. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  179. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
  180. /* 定义 USART1-Rx 引脚为悬空输入 */
  181. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //IO口的第C11脚
  182. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  183. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输入IO口
  184. }
  185. /************************************************
  186. 函数名称 : USART_Configuration
  187. 功 能 : 配置USART
  188. 参 数 : 无
  189. 返 回 值 : 无
  190. 作 者 : sun
  191. *************************************************/
  192. void USART4_Configuration(void)
  193. {
  194. USART_InitTypeDef USART_InitStructure;
  195. /******************************************************************
  196. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  197. 115200 8 1 0(NO)
  198. *******************************************************************/
  199. USART_InitStructure.USART_BaudRate = UART4_BAUDRATE; //设定传输速率
  200. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
  201. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  202. USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  203. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  204. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  205. USART_Init(UART4, &USART_InitStructure); //初始化UART4
  206. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能UART4接收中断
  207. USART_Cmd(UART4, ENABLE); //使能UART4
  208. }
  209. /************************************************
  210. 函数名称 : USART1_GPIO_Configuration
  211. 功 能 : USART所使用管脚输出输入定义
  212. 参 数 : 无
  213. 返 回 值 : 无
  214. 作 者 : sun
  215. *************************************************/
  216. void USART5_GPIO_Configuration(void)
  217. {
  218. GPIO_InitTypeDef GPIO_InitStructure;
  219. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  220. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE端口时钟 | RCC_APB2Periph_AFIO
  221. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
  222. /* 定义 USART1-TX 引脚为复用输出 */
  223. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //IO口的第PC12脚
  224. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
  225. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
  226. GPIO_Init(GPIOC, &GPIO_InitStructure); //USART输出IO口
  227. /* 定义 USART1-Rx 引脚为悬空输入 */
  228. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第PD2脚
  229. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO口悬空输入
  230. GPIO_Init(GPIOD, &GPIO_InitStructure); //USART输入IO口
  231. }
  232. /************************************************
  233. 函数名称 : USART_Configuration
  234. 功 能 : 配置USART
  235. 参 数 : 无
  236. 返 回 值 : 无
  237. 作 者 : sun
  238. *************************************************/
  239. void USART5_Configuration(void)
  240. {
  241. USART_InitTypeDef USART_InitStructure;
  242. /******************************************************************
  243. USART参数初始化: 波特率 传输位数 停止位数 校验位数
  244. 115200 8 1 0(NO)
  245. *******************************************************************/
  246. USART_InitStructure.USART_BaudRate = UART5_BAUDRATE; //设定传输速率
  247. USART_InitStructure.USART_WordLength = USART_WordLength_9b; //设定传输数据位数
  248. USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
  249. USART_InitStructure.USART_Parity = USART_Parity_Even ; //不用校验位USART_Parity_No,偶校验USART_Parity_Even,奇校验USART_Parity_Odd
  250. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
  251. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
  252. USART_Init(UART5, &USART_InitStructure); //初始化USART5
  253. USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //使能USART5接收中断
  254. USART_Cmd(UART5, ENABLE); //使能USART5
  255. }
  256. /************************************************
  257. 函数名称 : USART_Initializes
  258. 功 能 : 串口初始化
  259. 参 数 : 无
  260. 返 回 值 : 无
  261. 作 者 : sun
  262. *************************************************/
  263. void USART_Initializes(void)
  264. {
  265. USART1_GPIO_Configuration();
  266. USART1_Configuration();
  267. USART2_GPIO_Configuration();
  268. USART2_Configuration();
  269. USART4_GPIO_Configuration();
  270. USART4_Configuration();
  271. USART5_GPIO_Configuration();
  272. USART5_Configuration();
  273. }
  274. /************************************************
  275. 函数名称 : USART1_SendChar
  276. 功 能 : 串口1发送一个字符
  277. 参 数 : Data --- 数据
  278. 返 回 值 : 无
  279. 作 者 : sun
  280. *************************************************/
  281. void USART1_SendByte(uint8_t Data)
  282. {
  283. while((USART1->SR & USART_FLAG_TXE) == RESET);
  284. USART1->DR = (Data & (uint16_t)0x01FF);
  285. }
  286. /************************************************
  287. 函数名称 : USART1_SendNByte
  288. 功 能 : 串口1发送N个字符
  289. 参 数 : pData ----- 字符串
  290. Length --- 长度
  291. 返 回 值 : 无
  292. 作 者 : sun
  293. *************************************************/
  294. void USART1_SendNByte(uint8_t *pData, uint16_t Length)
  295. {
  296. while(Length--)
  297. {
  298. USART1_SendByte(*pData);
  299. pData++;
  300. }
  301. }
  302. /************************************************
  303. 函数名称 : USART1_Printf
  304. 功 能 : 串口1打印输出
  305. 参 数 : string --- 字符串
  306. 返 回 值 : 无
  307. 作 者 : sun
  308. *************************************************/
  309. void USART1_Printf(uint8_t *String)
  310. {
  311. while((*String) != '\0')
  312. {
  313. USART1_SendByte(*String);
  314. String++;
  315. }
  316. }
  317. /*UART1中断处理函数*/
  318. void USART1_IRQHandler(void)
  319. {
  320. if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  321. {
  322. USART1_ReceiveTimeCounter = USART1_RECEIVE_OVERTIME;
  323. if(USART1_receive_index >= USART1_BUF_SIZE)
  324. USART1_receive_index = 0;
  325. USART1_receive_buf[USART1_receive_index++] = (uint8_t)USART_ReceiveData(USART1);
  326. }
  327. if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
  328. {
  329. (void)USART_ReceiveData(USART1);
  330. }
  331. USART_ClearFlag(USART1,USART_FLAG_TC);
  332. }
  333. /************************************************
  334. 函数名称 : USART1_SendChar
  335. 功 能 : 串口1发送一个字符
  336. 参 数 : Data --- 数据
  337. 返 回 值 : 无
  338. 作 者 : sun
  339. *************************************************/
  340. void USART2_SendByte(uint8_t Data)
  341. {
  342. while((USART2->SR & USART_FLAG_TXE) == RESET);
  343. USART2->DR = (Data & (uint16_t)0x01FF);
  344. }
  345. /************************************************
  346. 函数名称 : USART1_SendNByte
  347. 功 能 : 串口1发送N个字符
  348. 参 数 : pData ----- 字符串
  349. Length --- 长度
  350. 返 回 值 : 无
  351. 作 者 : sun
  352. *************************************************/
  353. void USART2_SendNByte(uint8_t *pData, uint16_t Length)
  354. {
  355. while(Length--)
  356. {
  357. USART2_SendByte(*pData);
  358. pData++;
  359. }
  360. }
  361. /************************************************
  362. 函数名称 : USART1_Printf
  363. 功 能 : 串口1打印输出
  364. 参 数 : string --- 字符串
  365. 返 回 值 : 无
  366. 作 者 : sun
  367. *************************************************/
  368. void USART2_Printf(uint8_t *String)
  369. {
  370. while((*String) != '\0')
  371. {
  372. USART2_SendByte(*String);
  373. String++;
  374. }
  375. }
  376. /*UART1中断处理函数*/
  377. void USART2_IRQHandler(void)
  378. {
  379. uint8_t temp=0;
  380. //LED0=!LED0;
  381. if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  382. {
  383. USART2_ReceiveTimeCounter = USART2_RECEIVE_OVERTIME;
  384. if(USART2_receive_index >= USART2_BUF_SIZE)
  385. USART2_receive_index = 0;
  386. temp = (uint8_t)USART_ReceiveData(USART2);
  387. USART2_receive_buf[USART2_receive_index++] = temp;
  388. Update_Queue(usart2_rx_sq,0,temp);
  389. if(temp==0x0d)
  390. Update_Queue(usart2_rx_sq,1,0);
  391. }
  392. if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)
  393. {
  394. (void)USART_ReceiveData(USART2);
  395. }
  396. USART_ClearFlag(USART2,USART_FLAG_TC);
  397. }
  398. /************************************************
  399. 函数名称 : UART4_SendChar
  400. 功 能 : 串口4发送一个字符
  401. 参 数 : Data --- 数据
  402. 返 回 值 : 无
  403. 作 者 : sun
  404. *************************************************/
  405. void UART4_SendByte(uint8_t Data)
  406. {
  407. while((UART4->SR & USART_FLAG_TXE) == RESET);
  408. UART4->DR = (Data & (uint16_t)0x01FF);
  409. }
  410. /************************************************
  411. 函数名称 : USART4_SendNByte
  412. 功 能 : 串口4发送N个字符
  413. 参 数 : pData ----- 字符串
  414. Length --- 长度
  415. 返 回 值 : 无
  416. 作 者 : sun
  417. *************************************************/
  418. void UART4_SendNByte(uint8_t *pData, uint16_t Length)
  419. {
  420. while(Length--)
  421. {
  422. UART4_SendByte(*pData);
  423. pData++;
  424. }
  425. }
  426. /************************************************
  427. 函数名称 : USART4_Printf
  428. 功 能 : 串口4打印输出
  429. 参 数 : string --- 字符串
  430. 返 回 值 : 无
  431. 作 者 : sun
  432. *************************************************/
  433. void UART4_Printf(uint8_t *String)
  434. {
  435. while((*String) != '\0')
  436. {
  437. UART4_SendByte(*String);
  438. String++;
  439. }
  440. }
  441. /*UART4中断处理函数*/
  442. void UART4_IRQHandler(void)
  443. {
  444. //LED0=!LED0;
  445. if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
  446. {
  447. UART4_ReceiveTimeCounter = UART4_RECEIVE_OVERTIME;
  448. if(UART4_receive_index >= UART4_BUF_SIZE)
  449. UART4_receive_index = 0;
  450. UART4_receive_buf[UART4_receive_index++] = (uint8_t)USART_ReceiveData(UART4);
  451. }
  452. if (USART_GetFlagStatus(UART4, USART_FLAG_ORE) != RESET)
  453. {
  454. (void)USART_ReceiveData(UART4);
  455. }
  456. USART_ClearFlag(UART4,USART_FLAG_TC);
  457. }
  458. /************************************************
  459. 函数名称 : UART5_SendChar
  460. 功 能 : 串口5发送一个字符
  461. 参 数 : Data --- 数据
  462. 返 回 值 : 无
  463. 作 者 : sun
  464. *************************************************/
  465. void UART5_SendByte(uint8_t Data)
  466. {
  467. while((UART5->SR & USART_FLAG_TXE) == RESET);
  468. UART5->DR = (Data & (uint16_t)0x01FF);
  469. }
  470. /************************************************
  471. 函数名称 : USART4_SendNByte
  472. 功 能 : 串口5发送N个字符
  473. 参 数 : pData ----- 字符串
  474. Length --- 长度
  475. 返 回 值 : 无
  476. 作 者 : sun
  477. *************************************************/
  478. void UART5_SendNByte(uint8_t *pData, uint16_t Length)
  479. {
  480. while(Length--)
  481. {
  482. UART5_SendByte(*pData);
  483. pData++;
  484. }
  485. }
  486. /************************************************
  487. 函数名称 : USART4_Printf
  488. 功 能 : 串口5打印输出
  489. 参 数 : string --- 字符串
  490. 返 回 值 : 无
  491. 作 者 : sun
  492. *************************************************/
  493. void UART5_Printf(uint8_t *String)
  494. {
  495. while((*String) != '\0')
  496. {
  497. UART5_SendByte(*String);
  498. String++;
  499. }
  500. }
  501. /*UART5中断处理函数*/
  502. void UART5_IRQHandler(void)
  503. {
  504. //LED0=!LED0;
  505. if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
  506. {
  507. UART5_ReceiveTimeCounter = UART5_RECEIVE_OVERTIME;
  508. if(UART5_receive_index >= UART5_BUF_SIZE)
  509. UART5_receive_index = 0;
  510. UART5_receive_buf[UART5_receive_index++] = (uint8_t)USART_ReceiveData(UART5);
  511. }
  512. if (USART_GetFlagStatus(UART5, USART_FLAG_ORE) != RESET)
  513. {
  514. (void)USART_ReceiveData(UART5);
  515. }
  516. USART_ClearFlag(UART5,USART_FLAG_TC);
  517. }
  518. /************************************************
  519. 函数名称 : USART_ReceiveOvertimeProcess
  520. 功 能 : 串口超时接收
  521. 参 数 :
  522. 返 回 值 : 无
  523. 作 者 : sun
  524. *************************************************/
  525. void USART_ReceiveOvertimeProcess(void)
  526. {
  527. uint16_t i;
  528. // uint16_t j;
  529. // uint16_t h,t;
  530. uint8_t frame_num=0;
  531. uint8_t frame_head=0;
  532. uint8_t frame_len=0;
  533. /********************uart1****************************/
  534. if(USART1_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
  535. {
  536. USART1_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
  537. if(USART1_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
  538. {
  539. //若1包里面含有多个帧,拆开入队
  540. frame_head = 0;
  541. frame_num = 0;
  542. frame_len = 0;
  543. //
  544. start_led(3,USART1_receive_index,99,10*10);//(led,delay*0.1ms,duty,period*0.1ms)
  545. for(i = 0;i < USART1_receive_index; i ++){
  546. if(USART1_receive_buf[i] == 0xBB){
  547. if(frame_len>0)
  548. En_Queue(usart1_rx_sq,USART1_receive_buf+frame_head,sizeof(USART1_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  549. frame_head = i;
  550. frame_num++;
  551. frame_len=1;
  552. }
  553. else
  554. frame_len++;
  555. }
  556. if(frame_num > 0)
  557. En_Queue(usart1_rx_sq,USART1_receive_buf+frame_head,sizeof(USART1_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  558. DEBUG1("\r\nUSART1 rx=");
  559. DEBUG2(USART1_receive_buf,USART1_receive_index);
  560. //DEBUG("\r\n");
  561. USART1_receive_index = 0;
  562. USART1_ReceiveTimeCounter = 0;
  563. }
  564. }
  565. if(USART2_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
  566. {
  567. USART2_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
  568. if(USART2_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
  569. {
  570. if(USART2_ready_buf_ok == 0)
  571. {
  572. USART2_ready_buf_len = USART2_receive_index;
  573. for(i = 0;i <= USART2_ready_buf_len; i ++)
  574. USART2_ready_buf[i] = USART2_receive_buf[i];
  575. USART2_ready_buf[USART2_ready_buf_len] = '\0';
  576. USART2_ready_buf_ok = 1;
  577. USART2_receive_index=0;
  578. USART2_ReceiveTimeCounter = 0;
  579. }
  580. else
  581. {
  582. USART2_ReceiveTimeCounter = 0;
  583. }
  584. }
  585. }
  586. if(UART4_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
  587. {
  588. UART4_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
  589. if(UART4_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
  590. {
  591. //若1包里面含有多个帧,拆开入队
  592. frame_head = 0;
  593. frame_num = 0;
  594. frame_len = 0;
  595. for(i = 0;i < UART4_receive_index; i ++){
  596. if(UART4_receive_buf[i] == 0xBB){
  597. if(frame_len>0)
  598. En_Queue(uart4_rx_sq,UART4_receive_buf+frame_head,sizeof(UART4_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  599. frame_head = i;
  600. frame_num++;
  601. frame_len=1;
  602. }
  603. else
  604. frame_len++;
  605. }
  606. if(frame_num > 0)
  607. En_Queue(uart4_rx_sq,UART4_receive_buf+frame_head,sizeof(UART4_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  608. DEBUG1("\r\nUART4 rx=");
  609. DEBUG2(UART4_receive_buf,UART4_receive_index);
  610. //DEBUG("\r\n");
  611. UART4_receive_index = 0;
  612. UART4_ReceiveTimeCounter = 0;
  613. }
  614. }
  615. if(UART5_ReceiveTimeCounter>=SYSTEMTICK_PERIOD_MS)
  616. {
  617. UART5_ReceiveTimeCounter -= SYSTEMTICK_PERIOD_MS;
  618. if(UART5_ReceiveTimeCounter<SYSTEMTICK_PERIOD_MS)
  619. {
  620. //若1包里面含有多个帧,拆开入队
  621. frame_head = 0;
  622. frame_num = 0;
  623. frame_len = 0;
  624. //
  625. start_led(2,UART5_receive_index,99,10*10);//(led,delay*0.1ms,duty,period*0.1ms)
  626. for(i = 0;i < UART5_receive_index; i ++){
  627. if(UART5_receive_buf[i] == 0xBB){
  628. if(frame_len>0)
  629. En_Queue(uart5_rx_sq,UART5_receive_buf+frame_head,sizeof(UART5_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  630. frame_head = i;
  631. frame_num++;
  632. frame_len=1;
  633. }
  634. else
  635. frame_len++;
  636. }
  637. if(frame_num > 0)
  638. En_Queue(uart5_rx_sq,UART5_receive_buf+frame_head,sizeof(UART5_receive_buf)-frame_head,frame_len);//数据入队,可多次调用
  639. DEBUG1("\r\nUART5 rx=");
  640. DEBUG2(UART5_receive_buf,UART5_receive_index);
  641. //DEBUG("\r\n");
  642. UART5_receive_index = 0;
  643. UART5_ReceiveTimeCounter = 0;
  644. }
  645. }
  646. }
  647. void USART_printfHex(const uint8_t* SendChars,uint16_t len)
  648. {
  649. uint16_t i = 0;
  650. // uint8_t temp = 0;
  651. for(i=0;i<len;i++)
  652. {
  653. printf("%02X",SendChars[i]);
  654. }
  655. printf("\r\n");
  656. }
  657. #define CHAR_TO_UPPER(ch) ((ch >= 'a' && ch <= 'z')?(ch-0x20):ch)
  658. /**
  659. * @brief ascii convert hex
  660. * @par param[in] *hex:hex data
  661. * @par param[in] *ascii:ascii data
  662. * @par param[in] asciiLen:length of ascii
  663. * @retval length
  664. */
  665. uint8_t Ascii2Hex(uint8_t *hex, uint8_t *ascii, uint8_t asciiLen)
  666. {
  667. uint8_t i,ch,value;
  668. value = 0;
  669. for(i=0;i<(asciiLen>>1);i++) {
  670. ch = CHAR_TO_UPPER(ascii[i*2]);
  671. if(ch >= '0' && ch <= '9') {
  672. value = ch -'0';
  673. }
  674. else if(ch >= 'A' && ch <= 'F') {
  675. value = ch - 'A' + 0x0A;
  676. }
  677. else {
  678. return i;
  679. }
  680. hex[i] = (value<<4);
  681. ch = CHAR_TO_UPPER(ascii[i*2+1]);
  682. if(ch >= '0' && ch <= '9') {
  683. value = ch -'0';
  684. }
  685. else if(ch >= 'A' && ch <= 'F') {
  686. value = ch - 'A' + 0x0A;
  687. }
  688. else {
  689. return i;
  690. }
  691. hex[i] += value;
  692. }
  693. return i;
  694. }
  695. /**
  696. * @brief hex convert ascii
  697. * @par param[in] *ascii:ascii data
  698. * @par param[in] *hex:hex data
  699. * @par param[in] hexLen:length of hex
  700. * @retval length
  701. */
  702. uint8_t Hex2Ascii(uint8_t *ascii, uint8_t *hex, uint8_t hexLen)
  703. {
  704. uint8_t i, value;
  705. for(i=0;i<hexLen;i++) {
  706. value = (hex[i]>>4);
  707. if(value > 9) {
  708. value += 0x07;
  709. }
  710. ascii[2*i] = value+0x30;
  711. value = (hex[i]&0x0F);
  712. if(value > 9) {
  713. value += 0x07;
  714. }
  715. ascii[2*i+1] = value+0x30;
  716. }
  717. return hexLen*2;
  718. }
  719. sequeue_t *usart2_Rcv_sq_init(void)
  720. {
  721. usart2_rx_sq = Create_Empty_Sequeue(2,128);
  722. DEBUG_F("usart2_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart2_rx_sq,usart2_rx_sq->depth,usart2_rx_sq->width);
  723. if(usart2_rx_sq==NULL)
  724. DEBUG_F("error,usart2_rx_sq\r\n");
  725. return usart2_rx_sq;
  726. }
  727. sequeue_t *uart4_Rcv_sq_init(void)
  728. {
  729. uart4_rx_sq = Create_Empty_Sequeue(2,128);
  730. DEBUG_F("uart4_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart4_rx_sq,uart4_rx_sq->depth,uart4_rx_sq->width);
  731. if(uart4_rx_sq==NULL)
  732. DEBUG_F("error,uart4_rx_sq\r\n");
  733. return uart4_rx_sq;
  734. }
  735. sequeue_t *uart4_Send_sq_init(void)
  736. {
  737. uart4_tx_sq = Create_Empty_Sequeue(2,128);
  738. DEBUG_F("uart4_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart4_rx_sq,uart4_rx_sq->depth,uart4_rx_sq->width);
  739. if(uart4_tx_sq==NULL)
  740. DEBUG_F("error,uart4_tx_sq\r\n");
  741. return uart4_rx_sq;
  742. }
  743. sequeue_t *usart1_Rcv_sq_init(void)
  744. {
  745. usart1_rx_sq = Create_Empty_Sequeue(2,128);
  746. DEBUG_F("usart1_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart1_rx_sq,usart1_rx_sq->depth,usart1_rx_sq->width);
  747. if(usart1_rx_sq==NULL)
  748. DEBUG_F("error,usart1_rx_sq\r\n");
  749. return uart4_rx_sq;
  750. }
  751. sequeue_t *usart1_Send_sq_init(void)
  752. {
  753. usart1_tx_sq = Create_Empty_Sequeue(2,128);
  754. DEBUG_F("usart1_tx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)usart1_tx_sq,usart1_tx_sq->depth,usart1_tx_sq->width);
  755. if(usart1_tx_sq==NULL)
  756. DEBUG_F("error,usart1_tx_sq\r\n");
  757. return uart4_rx_sq;
  758. }
  759. sequeue_t *uart5_Rcv_sq_init(void)
  760. {
  761. uart5_rx_sq = Create_Empty_Sequeue(2,128);
  762. DEBUG_F("uart5_rx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart5_rx_sq,uart5_rx_sq->depth,uart5_rx_sq->width);
  763. if(uart5_rx_sq==NULL)
  764. DEBUG_F("error,uart5_rx_sq\r\n");
  765. return uart4_rx_sq;
  766. }
  767. sequeue_t *uart5_Send_sq_init(void)
  768. {
  769. uart5_tx_sq = Create_Empty_Sequeue(2,128);
  770. DEBUG_F("uart5_tx_sq: SQ=0x%08x,depth=%d,width=%d\r\n",(uint32_t)uart5_tx_sq,uart5_tx_sq->depth,uart5_tx_sq->width);
  771. if(uart5_tx_sq==NULL)
  772. DEBUG_F("error,uart5_Send_sq_init\r\n");
  773. return uart4_rx_sq;
  774. }
  775. /************************************************
  776. 函数名称 : 应用层函数,UsartToLora(非阻塞函数)
  777. 功 能 : 串口与LOra透出啊数据
  778. 参 数 :
  779. 返 回 值 :
  780. 作 者 : sun
  781. *************************************************/
  782. void UsartToLora(void )
  783. {
  784. // uint8_t BUFFER[MAXLOADLEN] ;
  785. uint8_t sout[128];
  786. int8_t lout=0;
  787. // uint8_t tmp;
  788. // uint8_t i;
  789. // int16_t bufferLen = 0;
  790. if(!Check_Seqeue_Empty(usart2_rx_sq))
  791. {
  792. lout=0;
  793. memset(sout,0,128);
  794. De_Queue(usart2_rx_sq,sout,sizeof(sout),(uint8_t *)&lout);
  795. if(strstr((char *)sout,"help")||strstr((char *)sout,"?")){
  796. printf("指令后面必须加回车:\r\n");
  797. printf("输入\"help or ?\",打印帮助信息\r\n");
  798. printf("输入\"erase app1\",擦除,app1\r\n");
  799. printf("输入\"erase app2\",擦除,app2\r\n");
  800. printf("输入\"erase app3\",擦除,app3\r\n");
  801. printf("输入\"copy app1->app2\",复制并校验,从app1 到app2\r\n");
  802. printf("输入\"copy app3->app1\",复制并校验,从app3 到app1\r\n");
  803. printf("输入\"copy app2->app1\",复制并校验,从app2 到app1\r\n");
  804. printf("输入\"run app1\",运行,run app1\r\n");
  805. printf("输入\"get info\",获取信息\r\n");
  806. printf("输入\"reset\",重启\r\n");
  807. }
  808. if(strstr((char *)sout,"erase app1")){
  809. printf("擦除,app1\r\n");
  810. m_state_code = M_STATE_ERASE_APP1;
  811. }
  812. if(strstr((char *)sout,"erase app2")){
  813. printf("擦除,app2\r\n");
  814. m_state_code = M_STATE_ERASE_APP2;
  815. }
  816. if(strstr((char *)sout,"erase app3")){
  817. printf("擦除,app3\r\n");
  818. m_state_code = M_STATE_ERASE_APP3;
  819. }
  820. if(strstr((char *)sout,"copy app1->app2")){
  821. printf("复制,copy app1->app2\r\n");
  822. m_state_code = M_STATE_CP_APP1_APP2;
  823. }
  824. if(strstr((char *)sout,"copy app3->app1")){
  825. printf("复制,copy app3->app1\r\n");
  826. m_state_code = M_STATE_CP_APP3_APP1;
  827. }
  828. if(strstr((char *)sout,"copy app2->app1")){
  829. printf("复制,copy app2->app1\r\n");
  830. m_state_code = M_STATE_CP_APP2_APP1;
  831. }
  832. if(strstr((char *)sout,"run app1")){
  833. printf("运行,run app1\r\n");
  834. m_state_code = M_STATE_RUN_APP1;
  835. }
  836. if(strstr((char *)sout,"get info")){
  837. printf("获取信息,get info\r\n");
  838. m_state_code = M_STATE_GET_INFO;
  839. }
  840. if(strstr((char *)sout,"reset")){
  841. printf("重启,reset\r\n");
  842. m_state_code = M_STATE_RST;
  843. }
  844. }
  845. }
  846. /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/