Uart.c 19 KB

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