Uart.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include "includes.h"
  2. #include "trace.h"
  3. #include "Uart.h"
  4. #include "../APP/command/uart_conf.h"
  5. #include "../APP/network_mgr/net_proc.h"
  6. #include "../APP/network/nettimer.h"
  7. extern int rs485_send_flag;
  8. uart_info g_uart_info[UART_MAX];
  9. uart_config_struct g_uart_cfg[] = {
  10. #ifdef GAS_STATION
  11. {GPIOA, GPIO_Pin_10,GPIOA, GPIO_Pin_9, 0, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA},
  12. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},
  13. {GPIOD, GPIO_Pin_9, GPIOD, GPIO_Pin_8, GPIO_FullRemap_USART3,USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  14. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO},
  15. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  16. #else
  17. #ifdef MSTS_C
  18. {GPIOB, GPIO_Pin_7, GPIOB, GPIO_Pin_6, GPIO_Remap_USART1, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO},
  19. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},//不可以和me3616链接
  20. {GPIOD, GPIO_Pin_9, GPIOD, GPIO_Pin_8, GPIO_FullRemap_USART3, USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  21. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC},
  22. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD},
  23. #else
  24. {GPIOA, GPIO_Pin_10,GPIOA, GPIO_Pin_9, 0, USART1_IRQn, USART1, RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA},
  25. {GPIOA, GPIO_Pin_3, GPIOA, GPIO_Pin_2, 0, USART2_IRQn, USART2, RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA},
  26. {GPIOB, GPIO_Pin_11,GPIOB, GPIO_Pin_10, 0, USART3_IRQn, USART3, RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB},
  27. {GPIOC, GPIO_Pin_11,GPIOC, GPIO_Pin_10, 0, UART4_IRQn, UART4, RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO},
  28. {GPIOD, GPIO_Pin_2, GPIOC, GPIO_Pin_12, 0, UART5_IRQn, UART5, RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO},
  29. #endif
  30. #endif
  31. };
  32. int uart_msg_send(u8 uartid, const char *buf, u32 buflen);
  33. int fputc(int ch, FILE *f)
  34. {
  35. uint16_t temp=1000;
  36. USART_TypeDef * uart_def = g_uart_cfg[UART_DEBUG].uart_def;
  37. if(g_ptTest.bTestStart==1) return 0;
  38. while((temp>0)&((uart_def->SR&0X40)==0))
  39. {
  40. temp--;
  41. };//循环发送,直到发送完毕
  42. //while((USART2->SR & USART_FLAG_TXE) == RESET);
  43. uart_def->DR = (u8) ch;
  44. return ch;
  45. }
  46. /*测试串口输出4G接收到的数据*/
  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(USARTx == UART4){
  143. timeout_start(&g_uart4Time);
  144. }
  145. else if((USARTx == UART5)){//||USARTx == USART2){
  146. timeout_start(&g_uart5Time);
  147. }
  148. if(!pinfo->rinfo.flag.bufof)
  149. {/*如果接收缓冲没有溢出*/
  150. index = pinfo->rinfo.rtbuf.pinput;
  151. pinfo->rinfo.rtbuf.buf[index] = val;
  152. index = (index+1)%UART_RCV_TEMPBUF_LEN;/*以datalen为模加index*/
  153. if(index == pinfo->rinfo.rtbuf.poutput)
  154. {/*接收的速度大于处理的速度了*/
  155. pinfo->rinfo.flag.bufof = ENABLE;/*溢出了*/
  156. }
  157. else
  158. {
  159. pinfo->rinfo.rtbuf.pinput = index;/*更新输入指针长度*/
  160. }
  161. }
  162. }
  163. if(USART_GetITStatus(USARTx, USART_IT_TXE) != RESET) {/*数据发送完成,可以发送新数据*/
  164. if(pinfo->sinfo.poutput != pinfo->sinfo.pinput)
  165. {/*如果有数据需要发送*/
  166. index = pinfo->sinfo.poutput;
  167. USART_SendData(USARTx, pinfo->sinfo.pdata[index]);/*新数据写入发送缓冲*/
  168. pinfo->sinfo.poutput = (index+1)%pinfo->sinfo.datalen;/*以datalen为模加index*/
  169. if(USARTx == RS485_UART)
  170. {
  171. if(rs485_send_flag)
  172. {
  173. rs485_send_flag = 0;
  174. rs485_rcv_flag = 1;
  175. }
  176. }
  177. } else {/*没有数据需要发送*/
  178. USART_ITConfig(USARTx, USART_IT_TXE, DISABLE);
  179. pinfo->sinfo.sending = FALSE;
  180. if(rs485_rcv_flag)
  181. {
  182. delay_ms(1);
  183. RS485_RX();
  184. rs485_rcv_flag = 0;
  185. }
  186. }
  187. }
  188. OS_CRITICAL_EXIT();
  189. OSIntExit();
  190. return;
  191. }
  192. /******************************************************************************************
  193. *uart_rs232_init --串口初始化函数
  194. *--
  195. *Input:
  196. * prt --协议类型
  197. * rcv --信息接收函数
  198. *Output:
  199. * 无
  200. *Return:
  201. * OK/ERROR 表示执行成功与否
  202. *
  203. */
  204. int uart_rs232_init(u8 uartid, uart_rcv_func rcv)
  205. {
  206. uart_info *pinfo = NULL;
  207. uart_config_struct *p_uart = NULL;
  208. pinfo = &g_uart_info[uartid];
  209. p_uart = &g_uart_cfg[uartid];
  210. /*清数据结构*/
  211. memset(pinfo, 0, sizeof(uart_info));
  212. /*记录接收报文处理函数*/
  213. pinfo->rinfo.rproc.rcv = rcv;
  214. /*lint -save -e611*/
  215. pinfo->uartid = uartid;
  216. pinfo->arg = (void *)p_uart->uart_def;
  217. /*lint -restore*/
  218. /*接收和发送指针执行相应的缓冲区*/
  219. //pinfo->rinfo.rproc.pdata = p_rx_data;
  220. pinfo->rinfo.rproc.datalen = 1024;
  221. //pinfo->sinfo.pdata = p_tx_data;
  222. pinfo->sinfo.datalen = 1024;
  223. //trace_otp_trace1(uart_msg_send, uartid, "My Friends, Uart%d!!!\n", uartid+1);
  224. return 0;
  225. }
  226. u8 uart_msg_send_disable(u8 uartid)
  227. {
  228. uart_info *pinfo = NULL;
  229. pinfo = &g_uart_info[uartid];
  230. return (pinfo->sinfo.poutput != pinfo->sinfo.pinput);
  231. }
  232. USART_TypeDef* uart_choice(uint8_t uartid)
  233. {
  234. USART_TypeDef *UART;
  235. switch(uartid)
  236. {
  237. case UART1_ID:
  238. UART = USART1;
  239. break;
  240. case UART2_ID:
  241. UART = USART2;
  242. break;
  243. case UART3_ID:
  244. UART = USART3;
  245. break;
  246. case UART4_ID:
  247. UART = UART4;
  248. break;
  249. case UART5_ID:
  250. UART = UART5;
  251. break;
  252. default:
  253. break;
  254. }
  255. return UART;
  256. }
  257. int uart_msg_send00(u8 uartid, const char *buf, u32 buflen)
  258. {
  259. uint32_t t = 0;
  260. USART_TypeDef* USARTx;
  261. if(uartid > UART5_ID)
  262. return -1;
  263. USARTx = uart_choice(uartid);
  264. while(buflen--)
  265. {
  266. IWDG_ReloadCounter();
  267. USARTx->DR = (*buf & (uint16_t)0x01FF);
  268. t = 1000;
  269. while((t--)&&(USART_GetFlagStatus(USARTx, USART_FLAG_TXE)!=SET)){}
  270. buf++;
  271. }
  272. return 0;
  273. }
  274. /*******************************************************************************
  275. *uart1_msg_send 发送报文处理
  276. *Input:
  277. * buf --需要发送的数据指针
  278. * buflen --发送的数据长度
  279. *Output:
  280. * None
  281. *Return:
  282. * OK/ERROR,表示执行成功和失败
  283. */
  284. int uart_msg_send(u8 uartid, const char *buf, u32 buflen)
  285. {
  286. u16 i = 0, index = 0;
  287. USART_TypeDef* USARTx;
  288. uart_info *pinfo = NULL;
  289. uart_config_struct *p_uart = NULL;
  290. OS_ERR err;
  291. CPU_SR_ALLOC();
  292. pinfo = &g_uart_info[uartid];
  293. p_uart = &g_uart_cfg[uartid];
  294. if(pinfo == NULL) {
  295. return -1;
  296. }
  297. if(uartid==UART2_ID){
  298. rs485_send_flag = 1;
  299. rs485_rcv_flag = 0;
  300. RS485_TX();
  301. }
  302. USARTx = (USART_TypeDef *)p_uart->uart_def;
  303. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  304. /*取缓冲区输入指针*/
  305. index = pinfo->sinfo.pinput;
  306. /*依次将需要发送的数据放入发送缓冲区*/
  307. for(i=0; i<buflen; i++) {
  308. pinfo->sinfo.pdata[index] = buf[i];
  309. /*模加index*/
  310. index = (index+1)%pinfo->sinfo.datalen;
  311. while(index == pinfo->sinfo.poutput) {/* 发送太慢了,等一等*/
  312. OS_CRITICAL_EXIT();
  313. OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_DLY, &err);
  314. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  315. }
  316. }
  317. /*更新输入指针*/
  318. pinfo->sinfo.pinput = index;
  319. /*启动发送*/
  320. if((pinfo->sinfo.pinput != pinfo->sinfo.poutput)) {//&&(!pinfo->sinfo.sending)
  321. USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
  322. pinfo->sinfo.sending = TRUE;
  323. //index = pinfo->sinfo.poutput;
  324. //USART_SendData(USARTx, pinfo->sinfo.pdata[index]);/*新数据写入发送缓冲*/
  325. //pinfo->sinfo.poutput = (index+1)%pinfo->sinfo.datalen;/*以datalen为模加index*/
  326. }
  327. OS_CRITICAL_EXIT();
  328. return 0;
  329. }
  330. /*******************************************************************************
  331. *uart_rcv_process 接收报文处理,按照相应的协议,对接收缓冲区的报文进行处理,并调用
  332. * 注册的rcv处理函数;
  333. *Input:
  334. * prt --报文处理使用的协议,根据协议类型进行报文接收;
  335. * prinfo --接收缓冲区相关信息指针;
  336. *Output:
  337. * None
  338. *Return:
  339. * OK/ERROR,表示执行成功和失败
  340. */
  341. int uart_rcv_process(u8 uartid)
  342. {
  343. u8 val = 0;
  344. u32 index = 0;
  345. u32 ucnt = 0;
  346. uart_info *pinfo = NULL;
  347. // uart_config_struct *p_uart = NULL;
  348. uart_rcv_info *prinfo = NULL;
  349. char *str = NULL, *str1 = NULL,*str2 = NULL;
  350. char *msg = NULL;
  351. int slen;
  352. uint8_t cregStatus = 0;
  353. static uint16_t dalen = 0,len = 0;
  354. CPU_SR_ALLOC();
  355. pinfo = &g_uart_info[uartid];
  356. // p_uart = &g_uart_cfg[uartid];
  357. prinfo = &pinfo->rinfo;
  358. /*参数检查*/
  359. if(prinfo == NULL) {
  360. return -1;
  361. }
  362. OS_CRITICAL_ENTER(); /* Tell uC/OS-II that we are starting an ISR */
  363. /*20090828modify,szb*/
  364. if(prinfo->flag.bufof==TRUE) {
  365. prinfo->flag.bufof=FALSE;
  366. prinfo->rtbuf.poutput = prinfo->rtbuf.pinput;
  367. prinfo->rproc.outcnt = 0;
  368. }
  369. /*看看接收暂存缓冲区当前有没有数据可以接收*/
  370. if(prinfo->rtbuf.poutput == prinfo->rtbuf.pinput) {/*没有新数据*/
  371. return 0;
  372. }
  373. /*记录接收暂存缓冲区开始处理的位置*/
  374. index = prinfo->rtbuf.poutput;
  375. ucnt = prinfo->rproc.outcnt;
  376. /*处理已经接收完毕但是还没有处理的报文内容*/
  377. while(index != prinfo->rtbuf.pinput) {
  378. /*获取接收缓冲区的内容*/
  379. val = prinfo->rtbuf.buf[index];
  380. prinfo->rproc.pdata[ucnt] = val;
  381. if(uartid == UART4_ID) {
  382. USART_SendData(UART5,val);
  383. }
  384. else if((uartid == UART5_ID) ){//||(uartid==UART2_ID)
  385. USART_SendData(UART4,val);
  386. //if(g_oilreadDa.bfinish == 0){
  387. if(dalen>=1023) dalen = 0;
  388. if(val == 0x01) {
  389. dalen = 0;
  390. g_oilreadDa.bstart = 1;
  391. }
  392. g_oilreadDa.oil_buff[dalen++] = val;
  393. g_oilreadDa.len1 = dalen;
  394. // if(dalen>7) {
  395. //if(strncmp((char*)(&g_oilreadDa.oil_buff[1]),"i20100",6)==0){
  396. if( g_oilreadDa.bstart == 1) {
  397. if((g_oilreadDa.oil_buff[dalen-1] == 0x03)){
  398. g_oilreadDa.bfinish = 1;
  399. g_oilreadDa.len = dalen;
  400. }
  401. }
  402. else g_oilreadDa.bstart = 0;
  403. // }
  404. //}
  405. }
  406. /*处理下一个字符*/
  407. index = (index+1)%UART_RCV_TEMPBUF_LEN;
  408. ucnt = (ucnt+1)%UART_RCV_PROCBUF_LEN;
  409. prinfo->rproc.pdata[ucnt] = 0;
  410. }
  411. if(uartid==UART3_ID){
  412. str = strstr(prinfo->rproc.pdata,"+MSUB:");
  413. if(str) {
  414. str1 = strstr(prinfo->rproc.pdata,"byte");
  415. str2 = strstr(str1,"\r\n");
  416. if(str1 && str2 ){//&&( ucnt-datalen>=(len+2))
  417. memset(sys_net.str, 0, sizeof(sys_net.str));
  418. slen = net_data_copy(str1+5,(uint8_t *)sys_net.str);
  419. net_rcv_len = net_rcvdata_ch((char *)sys_net.str,net_rcv_data,slen);
  420. data_dump("NET RECB", net_rcv_data, net_rcv_len);
  421. msg = (char *)net_queue_mem_calloc_must();
  422. if(msg) {
  423. memcpy(msg, net_rcv_data, net_rcv_len);
  424. net_queue_insert((char *)msg, net_rcv_len); //将数据放到消息队列中
  425. }
  426. else
  427. {
  428. net_queue_mem_free(msg);
  429. }
  430. prinfo->rproc.outcnt = 0;
  431. memset(str,0xFF,str2-str1);
  432. }
  433. }
  434. str = strstr(prinfo->rproc.pdata,"+CREG:");
  435. if(str){
  436. str1 = strstr(str,"\r\n");
  437. if(str1){
  438. str2 = strstr(str,",");
  439. if(str2){
  440. g_runData.cregStatus = atoi(str2+1);
  441. printf("\r\n check cregStatus = %d\r\n",g_runData.cregStatus);
  442. prinfo->rproc.outcnt = 0;
  443. memset(str,0xFF,str1-str);
  444. }
  445. else {
  446. g_runData.cregStatus = atoi(str+6);
  447. printf("\r\n auto cregStatus = %d\r\n",g_runData.cregStatus);
  448. prinfo->rproc.outcnt = 0;
  449. memset(str,0xFF,str1-str);
  450. }
  451. if(g_runData.cregStatus!=1){
  452. timeout_start(&g_usmTimeOut);
  453. }
  454. else {
  455. timeout_stop(&g_usmTimeOut);
  456. }
  457. }
  458. }
  459. }
  460. if(prinfo->rproc.rcv) {/* 注册接收函数 */
  461. prinfo->rproc.rcv(pinfo->uartid, pinfo->arg, prinfo->rproc.pdata, ucnt, NULL);
  462. ucnt = 0;
  463. }
  464. /*更新输出起始指针*/
  465. prinfo->rtbuf.poutput = index;
  466. prinfo->rproc.outcnt = ucnt;
  467. OS_CRITICAL_EXIT();
  468. return 0;
  469. }
  470. int uart_blocking_read(char *buffer, u8 uartid, u32 timeout)
  471. {
  472. OS_ERR err;
  473. uart_info *pinfo = NULL;
  474. // uart_config_struct *p_uart = NULL;
  475. uart_rcv_info *prinfo = NULL;
  476. int ret = 1;
  477. CPU_SR_ALLOC();
  478. pinfo = &g_uart_info[uartid];
  479. // p_uart = &g_uart_cfg[uartid];
  480. prinfo = &pinfo->rinfo;
  481. while(timeout--) {
  482. OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_DLY, &err);
  483. }
  484. OS_CRITICAL_ENTER();
  485. if(prinfo->rproc.outcnt) {
  486. memcpy(buffer, prinfo->rproc.pdata, prinfo->rproc.outcnt);
  487. ret = prinfo->rproc.outcnt;
  488. prinfo->rproc.outcnt = 0;
  489. } else {
  490. ret = 0;
  491. }
  492. OS_CRITICAL_EXIT();
  493. return ret;
  494. }
  495. int uart_blocking_read_len(char *buffer, u8 uartid)
  496. {
  497. OS_ERR err;
  498. uart_info *pinfo = NULL;
  499. uart_rcv_info *prinfo = NULL;
  500. int ret = 1;
  501. uint32_t revlen = 0;
  502. #if (1)
  503. CPU_SR_ALLOC();
  504. pinfo = &g_uart_info[uartid];
  505. prinfo = &pinfo->rinfo;
  506. OS_CRITICAL_ENTER();
  507. if(prinfo->rproc.outcnt) {
  508. memcpy(buffer, prinfo->rproc.pdata, prinfo->rproc.outcnt);
  509. ret = prinfo->rproc.outcnt;
  510. prinfo->rproc.outcnt = 0;
  511. } else {
  512. ret = 0;
  513. }
  514. OS_CRITICAL_EXIT();
  515. #else
  516. pinfo = &g_uart_info[uartid];
  517. prinfo = &pinfo->rinfo;
  518. revlen = prinfo->rproc.outcnt;
  519. prinfo->rproc.outcnt = 0;
  520. if(revlen) {
  521. memcpy(buffer, prinfo->rproc.pdata, revlen);
  522. ret = revlen;
  523. //prinfo->rproc.outcnt = 0;
  524. } else {
  525. ret = 0;
  526. }
  527. #endif
  528. return ret;
  529. }
  530. /*****************************************************************************
  531. ** Function name: USART1_IRQHandler
  532. **
  533. ** Descriptions: UART1 interrupt handler
  534. **
  535. ** parameters: None
  536. ** Returned value: None
  537. **
  538. *****************************************************************************/
  539. void USART1_IRQHandler(void)
  540. {
  541. uart_interrupt(UART1_ID);
  542. }
  543. void USART2_IRQHandler(void)
  544. {
  545. uart_interrupt(UART2_ID);
  546. }
  547. u8 USART3_RX_BUF[128]; //接收缓冲,最大USART_REC_LEN个字节.
  548. //接收状态
  549. //bit15, 接收完成标志
  550. //bit14, 接收到0x0d
  551. //bit13~0, 接收到的有效字节数目
  552. u16 USART3_RX_STA=0;
  553. int rcv_flag = 0;
  554. void USART3_IRQHandler(void)
  555. {
  556. // uint8_t Res;
  557. // if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  558. // {
  559. // Res =USART_ReceiveData(USART3);//(USART3->DR); //读取接收到的数据
  560. // USART3_RX_BUF[USART3_RX_STA] = Res;
  561. // USART3_RX_STA++;
  562. // if(Res == 16)
  563. // rcv_flag = 1;
  564. //
  565. // }
  566. uart_interrupt(UART3_ID);
  567. }
  568. void UART4_IRQHandler(void)
  569. {
  570. uart_interrupt(UART4_ID);
  571. }
  572. void UART5_IRQHandler(void)
  573. {
  574. uart_interrupt(UART5_ID);
  575. }