Uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #include "includes.h"
  2. #include "Uart.h"
  3. #include "BSP.h"
  4. uint8_t UART3_RCV_PROT_BUF[UART_RCV_PROCBUF_LEN];//串口3接收原始数据
  5. uint16_t uart3_prot_index = 0;
  6. uart_info g_uart_info[UART_MAX];
  7. uart_config_struct g_uart_cfg[] = {
  8. #ifdef GAS_STATION
  9. {GPIOA, GPIO_Pin_10,GPIOA, GPIO_Pin_9, 0, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA},
  10. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},
  11. {GPIOD, GPIO_Pin_9, GPIOD, GPIO_Pin_8, GPIO_FullRemap_USART3, USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  12. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO},
  13. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  14. #else
  15. #ifdef MSTS_C
  16. {GPIOB, GPIO_Pin_7, GPIOB, GPIO_Pin_6, GPIO_Remap_USART1, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO},
  17. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},//不可以和me3616链接
  18. {GPIOD, GPIO_Pin_9, GPIOD, GPIO_Pin_8, GPIO_FullRemap_USART3, USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  19. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC},
  20. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD},
  21. #else
  22. {GPIOA, GPIO_Pin_10,GPIOA, GPIO_Pin_9, 0, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA},
  23. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},
  24. {GPIOB, GPIO_Pin_11,GPIOB, GPIO_Pin_10, 0, USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB},
  25. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO},
  26. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  27. #endif
  28. #endif
  29. };
  30. int uart_msg_send(u8 uartid, const char *buf, u32 buflen);
  31. int fputc(int ch, FILE *f)
  32. {
  33. uint16_t temp=1000;
  34. USART_TypeDef * uart_def = g_uart_cfg[UART_DEBUG].uart_def;
  35. if(g_ptTest.bTestStart == PT_OFF) //PT状态关闭
  36. {
  37. while((temp>0)&((uart_def->SR&0X40)==0))
  38. {
  39. temp--;
  40. };//循环发送,直到发送完毕
  41. //while((USART2->SR & USART_FLAG_TXE) == RESET);
  42. uart_def->DR = (u8) ch;
  43. while((USART_GetFlagStatus(uart_def, USART_FLAG_TXE)!=SET)){}
  44. }
  45. return ch;
  46. }
  47. void data_dump(const char *name, uint8_t *data, uint16_t length)
  48. {
  49. int index = 0;
  50. printf("%s Data Info: \r\n ", name);
  51. for(index = 0;index < length;index++) {
  52. if((index%4 == 0)&&index) {
  53. if((index%16 == 0)&&index) {
  54. printf("\r\n ");
  55. } else {
  56. printf(" ");
  57. }
  58. }
  59. printf("%02x ", *(data + index));
  60. }
  61. printf("\r\n");
  62. }
  63. void uart_init(u8 uartid, u32 baud, u16 wordlen, u16 parity)
  64. {
  65. uart_config_struct *p_uart = NULL;
  66. //GPIO端口设置
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. USART_InitTypeDef USART_InitStructure;
  69. NVIC_InitTypeDef NVIC_InitStructure;
  70. USART_TypeDef * USARTx = NULL;
  71. p_uart = &g_uart_cfg[uartid];
  72. USARTx = (USART_TypeDef *)p_uart->uart_def;
  73. RCC_APB2PeriphClockCmd(p_uart->gpio_clk, ENABLE);
  74. if(USARTx == USART1) {
  75. RCC_APB2PeriphClockCmd(p_uart->uart_clk, ENABLE);
  76. } else {
  77. RCC_APB1PeriphClockCmd(p_uart->uart_clk, ENABLE);
  78. }
  79. //USART1_TX GPIOA.9初始化
  80. GPIO_InitStructure.GPIO_Pin = p_uart->tx_pin;
  81. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  82. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  83. GPIO_Init((GPIO_TypeDef* )p_uart->tx_port, &GPIO_InitStructure);//初始化GPIOA.9
  84. //USART1_RX GPIOA.10初始化
  85. GPIO_InitStructure.GPIO_Pin = p_uart->rx_pin;
  86. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  87. GPIO_Init((GPIO_TypeDef* )p_uart->rx_port, &GPIO_InitStructure);//初始化GPIOA.10
  88. if(p_uart->remap) {
  89. GPIO_PinRemapConfig(p_uart->remap, ENABLE);
  90. }
  91. //Usart1 NVIC 配置
  92. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  93. NVIC_InitStructure.NVIC_IRQChannel = p_uart->irq_no;
  94. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
  95. NVIC_InitStructure.NVIC_IRQChannelSubPriority = uartid; //子优先级3
  96. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  97. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  98. //USART 初始化设置
  99. USART_InitStructure.USART_BaudRate = baud;//串口波特率
  100. USART_InitStructure.USART_WordLength = wordlen;//字长为8位数据格式
  101. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  102. USART_InitStructure.USART_Parity = parity;//无奇偶校验位
  103. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  104. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  105. USART_Init(USARTx, &USART_InitStructure); //初始化串口1
  106. USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);//开启串口接受中断
  107. // USART_ITConfig(USARTx, USART_IT_TC, ENABLE);//开启串口发送完成中断
  108. // USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
  109. // USART_ITConfig(USARTx, USART_IT_IDLE, ENABLE);//开启串口空闲中断
  110. USART_Cmd(USARTx, ENABLE); //使能串口1
  111. }
  112. /****************************************************************************
  113. * --uart_interrupt() UART0中断服务程序。
  114. * Input:
  115. * uid --串口编号
  116. * pinfo --串口信息处理结构指针
  117. *Output:
  118. * None
  119. *Return:
  120. * None
  121. */
  122. int rs485_rcv_flag = 0;
  123. void uart_interrupt(u8 uartid)
  124. {
  125. u16 index = 0;
  126. u8 val =0;
  127. uart_info *pinfo = NULL;
  128. uart_config_struct *p_uart = NULL;
  129. USART_TypeDef * USARTx = NULL;
  130. CPU_SR_ALLOC();
  131. pinfo = &g_uart_info[uartid];
  132. p_uart = &g_uart_cfg[uartid];
  133. USARTx = (USART_TypeDef *)p_uart->uart_def;
  134. OSIntEnter();
  135. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  136. // OSIntNestingCtr++;
  137. if(USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
  138. {/*有新数据需要接收,不管缓存区是否溢出,数据必须读出*/
  139. val = USART_ReceiveData(USARTx); /*接收新数据,同时清除中断挂起*/
  140. USART_ClearFlag(USARTx, USART_IT_RXNE);
  141. USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
  142. if(!pinfo->rinfo.flag.bufof)
  143. {/*如果接收缓冲没有溢出*/
  144. index = pinfo->rinfo.rtbuf.pinput;
  145. pinfo->rinfo.rtbuf.buf[index] = val;
  146. index = (index+1)%UART_RCV_TEMPBUF_LEN;/*以datalen为模加index*/
  147. if(index == pinfo->rinfo.rtbuf.poutput)
  148. {/*接收的速度大于处理的速度了*/
  149. pinfo->rinfo.flag.bufof = ENABLE;/*溢出了*/
  150. }
  151. else
  152. {
  153. pinfo->rinfo.rtbuf.pinput = index;/*更新输入指针长度*/
  154. }
  155. }
  156. }
  157. #if 0
  158. if(USART_GetITStatus(USARTx, USART_IT_TXE) != RESET) {/*数据发送完成,可以发送新数据*/
  159. if(pinfo->sinfo.poutput != pinfo->sinfo.pinput)
  160. {/*如果有数据需要发送*/
  161. index = pinfo->sinfo.poutput;
  162. USART_SendData(USARTx, pinfo->sinfo.pdata[index]);/*新数据写入发送缓冲*/
  163. pinfo->sinfo.poutput = (index+1)%pinfo->sinfo.datalen;/*以datalen为模加index*/
  164. } else {/*没有数据需要发送*/
  165. USART_ITConfig(USARTx, USART_IT_TXE, DISABLE);
  166. pinfo->sinfo.sending = FALSE;
  167. }
  168. }
  169. #endif
  170. OS_CRITICAL_EXIT();
  171. OSIntExit();
  172. return;
  173. }
  174. /******************************************************************************************
  175. *uart_rs232_init --串口初始化函数
  176. *--
  177. *Input:
  178. * prt --协议类型
  179. * rcv --信息接收函数
  180. *Output:
  181. * 无
  182. *Return:
  183. * OK/ERROR 表示执行成功与否
  184. *
  185. */
  186. int uart_rs232_init(u8 uartid, uart_rcv_func rcv)
  187. {
  188. uart_info *pinfo = NULL;
  189. uart_config_struct *p_uart = NULL;
  190. pinfo = &g_uart_info[uartid];
  191. p_uart = &g_uart_cfg[uartid];
  192. /*清数据结构*/
  193. memset(pinfo, 0, sizeof(uart_info));
  194. /*记录接收报文处理函数*/
  195. pinfo->rinfo.rproc.rcv = rcv;
  196. /*lint -save -e611*/
  197. pinfo->uartid = uartid;
  198. pinfo->arg = (void *)p_uart->uart_def;
  199. /*lint -restore*/
  200. /*接收和发送指针执行相应的缓冲区*/
  201. //pinfo->rinfo.rproc.pdata = p_rx_data;
  202. pinfo->rinfo.rproc.datalen = 1024;
  203. //pinfo->sinfo.pdata = p_tx_data;
  204. // pinfo->sinfo.datalen = 1024;
  205. //trace_otp_trace1(uart_msg_send, uartid, "My Friends, Uart%d!!!\n", uartid+1);
  206. return 0;
  207. }
  208. //u8 uart_msg_send_disable(u8 uartid)
  209. //{
  210. // uart_info *pinfo = NULL;
  211. // pinfo = &g_uart_info[uartid];
  212. // return (pinfo->sinfo.poutput != pinfo->sinfo.pinput);
  213. //}
  214. /**********************************************************/
  215. USART_TypeDef* uart_choice(uint8_t uartid)
  216. {
  217. USART_TypeDef *USARTx;
  218. switch(uartid)
  219. {
  220. case UART1_ID:
  221. USARTx = USART1;
  222. break;
  223. case UART2_ID:
  224. USARTx = USART2;
  225. break;
  226. case UART3_ID:
  227. USARTx = USART3;
  228. break;
  229. case UART4_ID:
  230. USARTx = UART4;
  231. break;
  232. case UART5_ID:
  233. USARTx = UART5;
  234. break;
  235. default:
  236. break;
  237. }
  238. return USARTx;
  239. }
  240. /**********************************************************/
  241. /*******************************************************************************
  242. *uart1_msg_send 发送报文处理
  243. *Input:
  244. * buf --需要发送的数据指针
  245. * buflen --发送的数据长度
  246. *Output:
  247. * None
  248. *Return:
  249. * OK/ERROR,表示执行成功和失败
  250. */
  251. int uart_msg_send(u8 uartid, const char *buf, u32 buflen)
  252. {
  253. #if 1
  254. uint32_t t = 0;
  255. USART_TypeDef* USARTx;
  256. if(uartid > UART5_ID)
  257. return -1;
  258. if (uartid == UART_485)
  259. RS485_TX();
  260. USARTx = uart_choice(uartid);
  261. while(buflen--)
  262. {
  263. USARTx->DR = (*buf & (uint16_t)0x01FF);
  264. t = 5000;
  265. while((t--)&&(USART_GetFlagStatus(USARTx, USART_FLAG_TXE)!=SET)){}
  266. buf++;
  267. }
  268. if (uartid == UART_485)
  269. {
  270. delay_us(120);//极限为210us,大于210us数据接收不完整。
  271. RS485_RX();
  272. }
  273. #endif
  274. #if 0
  275. u16 i = 0, index = 0;
  276. USART_TypeDef* USARTx;
  277. uart_info *pinfo = NULL;
  278. uart_config_struct *p_uart = NULL;
  279. OS_ERR err;
  280. CPU_SR_ALLOC();
  281. pinfo = &g_uart_info[uartid];
  282. p_uart = &g_uart_cfg[uartid];
  283. if(pinfo == NULL) {
  284. return -1;
  285. }
  286. USARTx = (USART_TypeDef *)p_uart->uart_def;
  287. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  288. /*取缓冲区输入指针*/
  289. index = pinfo->sinfo.pinput;
  290. /*依次将需要发送的数据放入发送缓冲区*/
  291. for(i=0; i<buflen; i++) {
  292. pinfo->sinfo.pdata[index] = buf[i];
  293. /*模加index*/
  294. index = (index+1)%pinfo->sinfo.datalen;
  295. while(index == pinfo->sinfo.poutput) {/* 发送太慢了,等一等*/
  296. OS_CRITICAL_EXIT();
  297. OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_DLY, &err);
  298. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  299. }
  300. }
  301. /*更新输入指针*/
  302. pinfo->sinfo.pinput = index;
  303. /*启动发送*/
  304. if((pinfo->sinfo.pinput != pinfo->sinfo.poutput)) {//&&(!pinfo->sinfo.sending)
  305. USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
  306. pinfo->sinfo.sending = TRUE;
  307. //index = pinfo->sinfo.poutput;
  308. //USART_SendData(USARTx, pinfo->sinfo.pdata[index]);/*新数据写入发送缓冲*/
  309. //pinfo->sinfo.poutput = (index+1)%pinfo->sinfo.datalen;/*以datalen为模加index*/
  310. }
  311. OS_CRITICAL_EXIT();
  312. #endif
  313. return 0;
  314. }
  315. /*******************************************************************************
  316. *uart_rcv_process 接收报文处理,按照相应的协议,对接收缓冲区的报文进行处理,并调用
  317. * 注册的rcv处理函数;
  318. *Input:
  319. * prt --报文处理使用的协议,根据协议类型进行报文接收;
  320. * prinfo --接收缓冲区相关信息指针;
  321. *Output:
  322. * None
  323. *Return:
  324. * OK/ERROR,表示执行成功和失败
  325. */
  326. int uart_rcv_process(u8 uartid)
  327. {
  328. u8 val = 0;
  329. u32 index = 0;
  330. u32 ucnt = 0;
  331. uart_info *pinfo = NULL;
  332. // uart_config_struct *p_uart = NULL;
  333. uart_rcv_info *prinfo = NULL;
  334. CPU_SR_ALLOC();
  335. // if(uartid == UART3_ID)
  336. // return 0;
  337. pinfo = &g_uart_info[uartid];
  338. // p_uart = &g_uart_cfg[uartid];
  339. prinfo = &pinfo->rinfo;
  340. /*参数检查*/
  341. if(prinfo == NULL) {
  342. return -1;
  343. }
  344. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  345. /*20090828modify,szb*/
  346. if(prinfo->flag.bufof==TRUE) {
  347. prinfo->flag.bufof=FALSE;
  348. prinfo->rtbuf.poutput = prinfo->rtbuf.pinput;
  349. prinfo->rproc.outcnt = 0;
  350. }
  351. /*看看接收暂存缓冲区当前有没有数据可以接收*/
  352. if(prinfo->rtbuf.poutput == prinfo->rtbuf.pinput) {/*没有新数据*/
  353. return 0;
  354. }
  355. /*记录接收暂存缓冲区开始处理的位置*/
  356. index = prinfo->rtbuf.poutput;
  357. ucnt = prinfo->rproc.outcnt;
  358. /*处理已经接收完毕但是还没有处理的报文内容*/
  359. while(index != prinfo->rtbuf.pinput) {
  360. /*获取接收缓冲区的内容*/
  361. val = prinfo->rtbuf.buf[index];
  362. prinfo->rproc.pdata[ucnt] = val;
  363. /*处理下一个字符*/
  364. index = (index+1)%UART_RCV_TEMPBUF_LEN;
  365. ucnt = (ucnt+1)%UART_RCV_PROCBUF_LEN;
  366. }
  367. if(prinfo->rproc.rcv) {/* 注册接收函数 */
  368. prinfo->rproc.rcv(pinfo->uartid, pinfo->arg, prinfo->rproc.pdata, ucnt, NULL);
  369. ucnt = 0;
  370. }
  371. /*更新输出起始指针*/
  372. prinfo->rtbuf.poutput = index;
  373. prinfo->rproc.outcnt = ucnt;
  374. OS_CRITICAL_EXIT();
  375. return 0;
  376. }
  377. int uart_blocking_read(char *buffer, u8 uartid, u32 timeout)
  378. {
  379. OS_ERR err;
  380. uart_info *pinfo = NULL;
  381. // uart_config_struct *p_uart = NULL;
  382. uart_rcv_info *prinfo = NULL;
  383. int ret = 1;
  384. CPU_SR_ALLOC();
  385. pinfo = &g_uart_info[uartid];
  386. // p_uart = &g_uart_cfg[uartid];
  387. prinfo = &pinfo->rinfo;
  388. while(timeout--) {
  389. OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_DLY, &err);
  390. }
  391. OS_CRITICAL_ENTER();
  392. if(prinfo->rproc.outcnt) {
  393. memcpy(buffer, prinfo->rproc.pdata, prinfo->rproc.outcnt);
  394. ret = prinfo->rproc.outcnt;
  395. prinfo->rproc.outcnt = 0;
  396. } else {
  397. ret = 0;
  398. }
  399. OS_CRITICAL_EXIT();
  400. return ret;
  401. }
  402. /*****************************************************************************
  403. ** Function name: USART1_IRQHandler
  404. **
  405. ** Descriptions: UART1 interrupt handler
  406. **
  407. ** parameters: None
  408. ** Returned value: None
  409. **
  410. *****************************************************************************/
  411. void USART1_IRQHandler(void)
  412. {
  413. uart_interrupt(UART1_ID);
  414. }
  415. void USART2_IRQHandler(void)
  416. {
  417. uart_interrupt(UART2_ID);
  418. }
  419. void USART3_IRQHandler(void)
  420. {
  421. uart_interrupt(UART3_ID);
  422. // uint8_t cnt = 0;
  423. // u16 index = 0;
  424. // u8 val =0;
  425. // uart_info *pinfo = NULL;
  426. // CPU_SR_ALLOC();
  427. // pinfo = &g_uart_info[UART3_ID];
  428. // OSIntEnter();
  429. // OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  430. // // uart_interrupt(UART3_ID);
  431. // if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  432. // {/*有新数据需要接收,不管缓存区是否溢出,数据必须读出*/
  433. // val = USART_ReceiveData(USART3); /*接收新数据,同时清除中断挂起*/
  434. // USART_ClearFlag(USART3, USART_IT_RXNE);
  435. // USART_ClearITPendingBit(USART3, USART_IT_RXNE);
  436. // /***********/
  437. // UART3_RCV_PROT_BUF[uart3_prot_index] = val;
  438. //
  439. // /***********/
  440. //
  441. // // if(!pinfo->rinfo.flag.bufof)
  442. // {/*如果接收缓冲没有溢出*/
  443. // index = pinfo->rinfo.rtbuf.pinput;
  444. // if(val == 0xbb)//接收到首字节
  445. // {
  446. // pinfo->irqinfo.head_cnt = index;
  447. // pinfo->irqinfo.rcv_over = 0;
  448. // pinfo->irqinfo.data_cnt = 0;
  449. // pinfo->irqinfo.data_len = 0;
  450. // pinfo->irqinfo.code = 0;
  451. //
  452. // pinfo->irqinfo.head_cnt2 = uart3_prot_index;
  453. // pinfo->irqinfo.data_len2 = 0;
  454. // }
  455. // else if(val == 0xba)//需转义
  456. // {
  457. // pinfo->irqinfo.code = 1;
  458. // cnt = 1;//不存储
  459. // }
  460. // else
  461. // {
  462. // if(pinfo->irqinfo.code)//转义
  463. // {
  464. // pinfo->irqinfo.code = 0;
  465. // if(val == 0)
  466. // {
  467. // val = 0xba;
  468. // }
  469. // else if (val == 1)
  470. // {
  471. // val = 0xbb;
  472. // }
  473. // }
  474. // if(index == ((pinfo->irqinfo.head_cnt+1)%UART_RCV_TEMPBUF_LEN))//第二个字节
  475. // {
  476. // pinfo->irqinfo.data_len = val;
  477. // pinfo->irqinfo.data_cnt = 0;
  478. // }
  479. // else
  480. // {
  481. // pinfo->irqinfo.data_cnt = (pinfo->irqinfo.data_cnt+1)%(UART_RCV_TEMPBUF_LEN+1);
  482. // if(pinfo->irqinfo.data_cnt == pinfo->irqinfo.data_len)
  483. // {
  484. // pinfo->irqinfo.rcv_over = 1;
  485. // }
  486. // }
  487. // }
  488. // if(cnt == 0)
  489. // {
  490. // pinfo->rinfo.rtbuf.buf[index] = val;
  491. // index = (index+1)%UART_RCV_TEMPBUF_LEN;/*以datalen为模加index*/
  492. // }
  493. // pinfo->rinfo.rtbuf.pinput = index;/*更新输入指针长度*/
  494. //
  495. // }
  496. //
  497. // pinfo->irqinfo.data_len2++;
  498. // uart3_prot_index = (uart3_prot_index+1)%UART_RCV_TEMPBUF_LEN;
  499. // }
  500. // OS_CRITICAL_EXIT();
  501. // OSIntExit();
  502. }
  503. void UART4_IRQHandler(void)
  504. {
  505. uart_interrupt(UART4_ID);
  506. }
  507. void UART5_IRQHandler(void)
  508. {
  509. uart_interrupt(UART5_ID);
  510. }
  511. //串口3接收
  512. uint16_t uart_rcv_uart3(uint8_t *buffer)
  513. {
  514. // OS_ERR err;
  515. uint16_t len1 = 0,len2 = 0;
  516. uart_info *pinfo = NULL;
  517. int ret = 1;
  518. CPU_SR_ALLOC();
  519. pinfo = &g_uart_info[UART3_ID];
  520. OS_CRITICAL_ENTER();
  521. if(pinfo->irqinfo.rcv_over && pinfo->irqinfo.data_len)
  522. {
  523. pinfo->irqinfo.rcv_over = 0;
  524. if((pinfo->irqinfo.head_cnt + pinfo->irqinfo.data_len + 2) > UART_RCV_TEMPBUF_LEN)
  525. {
  526. len1 = UART_RCV_TEMPBUF_LEN - pinfo->irqinfo.head_cnt;
  527. len2 = pinfo->irqinfo.head_cnt+pinfo->irqinfo.data_len+2 - UART_RCV_TEMPBUF_LEN;
  528. memcpy(buffer,&pinfo->rinfo.rtbuf.buf[pinfo->irqinfo.head_cnt],len1);
  529. memcpy(buffer+len1,pinfo->rinfo.rtbuf.buf,len2);
  530. }
  531. else
  532. memcpy(buffer,&pinfo->rinfo.rtbuf.buf[pinfo->irqinfo.head_cnt],pinfo->irqinfo.data_len+2);
  533. ret = pinfo->irqinfo.data_len+2;
  534. pinfo->irqinfo.data_len = 0;
  535. }
  536. else
  537. {
  538. ret = 0;
  539. }
  540. OS_CRITICAL_EXIT();
  541. return ret;
  542. }
  543. //串口3接收
  544. uint16_t uart_rcv_uart3_two(uint8_t *buffer1,uint16_t *buf_len1, uint8_t *buffer2, uint16_t *buf_len2)
  545. {
  546. // OS_ERR err;
  547. uint16_t len1 = 0,len2 = 0;
  548. uart_info *pinfo = NULL;
  549. int ret = 1;
  550. CPU_SR_ALLOC();
  551. pinfo = &g_uart_info[UART3_ID];
  552. OS_CRITICAL_ENTER();
  553. if(pinfo->irqinfo.rcv_over && pinfo->irqinfo.data_len)
  554. {
  555. pinfo->irqinfo.rcv_over = 0;
  556. if((pinfo->irqinfo.head_cnt + pinfo->irqinfo.data_len + 2) > UART_RCV_TEMPBUF_LEN)
  557. {
  558. len1 = UART_RCV_TEMPBUF_LEN - pinfo->irqinfo.head_cnt;
  559. len2 = pinfo->irqinfo.head_cnt+pinfo->irqinfo.data_len+2 - UART_RCV_TEMPBUF_LEN;
  560. memcpy(buffer1,&pinfo->rinfo.rtbuf.buf[pinfo->irqinfo.head_cnt],len1);
  561. memcpy(buffer1+len1,pinfo->rinfo.rtbuf.buf,len2);
  562. }
  563. else
  564. memcpy(buffer1,&pinfo->rinfo.rtbuf.buf[pinfo->irqinfo.head_cnt],pinfo->irqinfo.data_len+2);
  565. ret = pinfo->irqinfo.data_len+2;
  566. pinfo->irqinfo.data_len = 0;
  567. *buf_len1 = ret;//数组1的长度
  568. //原始数据读取
  569. if((pinfo->irqinfo.head_cnt2 + pinfo->irqinfo.data_len2) > UART_RCV_TEMPBUF_LEN)
  570. {
  571. len1 = UART_RCV_TEMPBUF_LEN - pinfo->irqinfo.head_cnt2;
  572. len2 = pinfo->irqinfo.head_cnt2+pinfo->irqinfo.data_len2 - UART_RCV_TEMPBUF_LEN;
  573. memcpy(buffer2,&UART3_RCV_PROT_BUF[pinfo->irqinfo.head_cnt2],len1);
  574. memcpy(buffer2+len1,UART3_RCV_PROT_BUF,len2);
  575. }
  576. else
  577. memcpy(buffer2,&UART3_RCV_PROT_BUF[pinfo->irqinfo.head_cnt2],pinfo->irqinfo.data_len2);
  578. *buf_len2 = pinfo->irqinfo.data_len2;
  579. pinfo->irqinfo.data_len2 = 0;
  580. }
  581. else
  582. {
  583. ret = 0;
  584. }
  585. OS_CRITICAL_EXIT();
  586. return ret;
  587. }