Serial.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #include "includes.h"
  2. unsigned char g_ucUARTSel=COM_SEL_MCU;//0--MCU 1--Modem 2--GPS
  3. unsigned char g_ucUART1RxMode=0;
  4. //Uart1 for debug
  5. unsigned char RxBuffer1[UART1_RX_BUFFER_SIZE];
  6. unsigned short rx1_ct;
  7. unsigned short g_usUart1RecvLen;
  8. /********************************************************************
  9. *Uart1DMAInit
  10. *串口1 DMA设置
  11. *串口1 发送必须用通道4,接收必须用通道5
  12. *********************************************************************/
  13. void Uart1DMAInit(void)
  14. {
  15. NVIC_InitTypeDef NVIC_InitStructure;
  16. DMA_InitTypeDef DMA_InitStructure;
  17. //启动DMA时钟
  18. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  19. //DMA 通道5用于UART1 RX
  20. DMA_DeInit(DMA1_Channel5);
  21. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//外设地址
  22. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1;//内存地址
  23. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMA传输方向单向
  24. DMA_InitStructure.DMA_BufferSize = UART1_RX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
  25. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
  26. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
  27. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
  28. DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
  29. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
  30. DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
  31. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
  32. DMA_Init(DMA1_Channel5,&DMA_InitStructure);
  33. //DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
  34. }
  35. void Uart1Init(void){
  36. NVIC_InitTypeDef NVIC_InitStructure;
  37. GPIO_InitTypeDef GPIO_InitStructure;
  38. USART_InitTypeDef USART_InitStructure;
  39. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
  40. /*
  41. * USART1_TX -> PA9 , USART1_RX -> PA10
  42. */
  43. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  45. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  46. GPIO_Init(GPIOA, &GPIO_InitStructure);
  47. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  48. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;// GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
  49. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  50. GPIO_Init(GPIOA, &GPIO_InitStructure);
  51. USART_InitStructure.USART_BaudRate =115200;// 115200;
  52. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  53. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  54. USART_InitStructure.USART_Parity = USART_Parity_No;
  55. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  56. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  57. USART_Init(USART1, &USART_InitStructure);
  58. USART_Cmd(USART1, ENABLE);
  59. USART_ClearFlag(USART1,USART_FLAG_TC);
  60. USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  61. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE
  62. }
  63. void Uart1RxEnable(int en)
  64. {
  65. USART_InitTypeDef USART_InitStructure;
  66. USART_InitStructure.USART_BaudRate =115200;
  67. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  68. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  69. USART_InitStructure.USART_Parity = USART_Parity_No;
  70. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  71. USART_InitStructure.USART_Mode = USART_Mode_Tx;//
  72. if(en){
  73. USART_InitStructure.USART_Mode |= USART_Mode_Rx;//
  74. USART_Init(USART1, &USART_InitStructure);
  75. USART_Cmd(USART1, ENABLE);
  76. USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
  77. }else{
  78. USART_Init(USART1, &USART_InitStructure);
  79. USART_Cmd(USART1, ENABLE);
  80. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
  81. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
  82. }
  83. }
  84. void Uart1Send(char *txbuf,int len)
  85. {
  86. int i;
  87. for(i=0;i<len;i++){
  88. USART_SendData(USART1,txbuf[i]);
  89. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  90. }
  91. }
  92. void SlwTrace(TRACE_TYPE type,char *buf,char needEnd)
  93. {
  94. int len;
  95. #ifndef SLW_DEBUG
  96. if(type==DEBUG)return;
  97. #endif
  98. if(g_ucUARTSel!=COM_SEL_MCU)return;
  99. len=strlen(buf);
  100. if(len>256)len=256;
  101. Uart1Send(buf,len);
  102. if(needEnd) Uart1Send("\r\n",2);
  103. }
  104. #ifdef __GNUC__
  105. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  106. set to 'Yes') calls __io_putchar() */
  107. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  108. #else
  109. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  110. #endif /* __GNUC__ */
  111. /**
  112. * @brief Retargets the C library printf function to the USART.
  113. * @param None
  114. * @retval None
  115. */
  116. PUTCHAR_PROTOTYPE{
  117. /* Place your implementation of fputc here */
  118. /* e.g. write a character to the USART */
  119. USART_SendData(USART1, (uint8_t) ch);
  120. /* Loop until the end of transmission */
  121. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  122. {}
  123. return ch;
  124. }
  125. /*********************************************************************************************
  126. *UART1RxTxISRHandler
  127. *串口1中断处理函数
  128. **********************************************************************************************/
  129. void UART1RxTxISRHandler(void)
  130. {
  131. __IO unsigned char uart_data;
  132. __IO static unsigned char uart_last_data=0;
  133. __IO unsigned long temp;
  134. //----接收中断--------
  135. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  136. {
  137. uart_data = USART_ReceiveData(USART1);
  138. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
  139. if(g_ucUARTSel==COM_SEL_MODEM){//Modem Trace
  140. USART_SendData(USART2, uart_data);
  141. }else if(g_ucUARTSel==COM_SEL_GPS){//GPS Trace
  142. USART_SendData(USART3, uart_data);
  143. }else if(g_ucUARTSel==COM_SEL_MCU){
  144. // USART_SendData(USART1, uart_data);
  145. if(g_usUart1RecvLen!=0){//上一包数据未处理完,不处理新来的数据,直接扔掉!
  146. rx1_ct=0;
  147. RxBuffer1[0]=0;
  148. return;
  149. }
  150. RxBuffer1[rx1_ct++]=uart_data;
  151. if(Authed){
  152. if(uart_last_data==0x0d &&uart_data==0x0a){
  153. if(RxBuffer1[0]=='G' && RxBuffer1[1]=='T'){
  154. g_usUart1RecvLen=rx1_ct;
  155. RxBuffer1[rx1_ct]=0;
  156. }else{
  157. rx1_ct=0;
  158. RxBuffer1[0]=0;
  159. }
  160. }
  161. }else{
  162. if(RxBuffer1[0] == PRO_HEADER){
  163. if(rx1_ct>=3){
  164. temp=RxBuffer1[2]+3;
  165. if(rx1_ct >= temp){
  166. g_usUart1RecvLen=rx1_ct;
  167. rx1_ct=0;
  168. }
  169. }
  170. }else{
  171. rx1_ct=0;
  172. RxBuffer1[0]=0;
  173. }
  174. }
  175. if(rx1_ct>=(UART1_RX_BUFFER_SIZE-1)){//数据超过缓冲区的数据,过滤不处理
  176. rx1_ct=0;
  177. g_usUart1RecvLen=0;
  178. }
  179. }
  180. }
  181. uart_last_data=uart_data;
  182. // //---异常中断---
  183. if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
  184. {
  185. USART_ReceiveData(USART1); //
  186. USART_ClearFlag(USART1,USART_FLAG_ORE); //
  187. }
  188. }
  189. //Usart2
  190. unsigned char RxBuffer2[UART2_RX_BUFFER_SIZE];
  191. static unsigned short rx2_ct;
  192. unsigned short g_usRx2Len;
  193. unsigned short g_usRx2In;
  194. unsigned short g_usRx2Out;
  195. void Uart2Init(void)
  196. {
  197. GPIO_InitTypeDef GPIO_InitStructure;
  198. USART_InitTypeDef USART_InitStructure;
  199. /*使能串口2使用的GPIO时钟*/
  200. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  201. /*使能串口2时钟*/
  202. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  203. /*串口2 RX管脚配置*/
  204. /* Configure USART2 Rx as input floating */
  205. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  206. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  207. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  208. GPIO_Init(GPIOA, &GPIO_InitStructure);
  209. /*串口2 TX管脚配置*/
  210. /* Configure USART2 Tx as alternate function push-pull */
  211. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  212. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  213. GPIO_Init(GPIOA, &GPIO_InitStructure);
  214. USART_InitStructure.USART_BaudRate = 115200;
  215. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  216. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  217. USART_InitStructure.USART_Parity = USART_Parity_No;
  218. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  219. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  220. USART_Init(USART2, &USART_InitStructure);
  221. USART_ClearFlag(USART2,USART_FLAG_TC);
  222. USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  223. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  224. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  225. USART_ITConfig(USART2, USART_IT_PE, ENABLE);
  226. USART_ITConfig(USART2, USART_IT_ERR, ENABLE);
  227. USART_Cmd(USART2, ENABLE);
  228. rx2_ct=0;
  229. g_usRx2Len=0;
  230. g_usRx2In=0;
  231. g_usRx2Out=0;
  232. memset(RxBuffer2,0,sizeof(RxBuffer2));
  233. }
  234. void Uart2RecvProcess(void)
  235. {
  236. unsigned short Rx2Len;
  237. unsigned short out;
  238. char preData[20];
  239. unsigned short i,j,len;
  240. char num[5];
  241. char data,flag;
  242. //少于8字节不处理
  243. if(g_usRx2Len<8)return;
  244. //缓冲区满,清空缓冲区
  245. if(g_usRx2Len>(UART2_RX_BUFFER_SIZE-2)){
  246. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  247. g_usRx2In=0;
  248. g_usRx2Len=0;
  249. g_usRx2Out=0;
  250. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  251. SlwTrace(DEBUG,"[Rx2Buf Over]",1);
  252. return;
  253. }
  254. //锁定环形缓冲区当前状态,
  255. Rx2Len=g_usRx2Len;
  256. // USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  257. // g_usRx2Len=g_usRx2Len-i;
  258. // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  259. //少于8字节不处理
  260. if(Rx2Len<4)return;
  261. //预读取前n字节 n最少4,最大16 为后续处理提供条件
  262. if(Rx2Len<16)len=Rx2Len;
  263. else len=16;
  264. out=g_usRx2Out;
  265. for(i=0;i<len;i++){
  266. preData[i]=RxBuffer2[out];
  267. if(++out>UART2_RX_BUFFER_SIZE)out=0;
  268. }
  269. //判断是否为OK
  270. //判断前8字节是否等于"+TCPRECV",如果等于则特殊处理,如果不等则找到"/r/n"作为结束符
  271. if(0!=ModemStrCmp(preData,"+TCPRECV"))
  272. {
  273. //非"+TCPRECV",则直接找到最近的0x0A作为结束符,读取并形成一包消息
  274. out=g_usRx2Out;
  275. for(i=0;i<Rx2Len;i++)
  276. {
  277. if(RxBuffer2[out]==0x0A)break;
  278. if(++out>=UART2_RX_BUFFER_SIZE)out=0;
  279. }
  280. //123/n/0
  281. if(i<Rx2Len)
  282. {//找到了0x0A,从环形缓冲区中读取数据并拷贝到消息缓冲区,发送消息
  283. len=i+1;//消息长度
  284. if(len<(MODEM_AT_MSG_DATA_LEN-2))
  285. {
  286. sutAtm.MsgLen=len;
  287. for(i=0;i<len;i++)
  288. {
  289. sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
  290. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  291. }
  292. sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
  293. //更新g_usRx3Len,需要防止中断修改冲突
  294. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
  295. g_usRx2Len=g_usRx2Len-len;
  296. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
  297. //发送消息
  298. sutAtm.MsgLen+=1;
  299. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full1",1);
  300. return;
  301. }else
  302. {//消息队列的内存块长度不够,需要特殊处理,扔掉后面部分
  303. sutAtm.MsgLen=len;
  304. for(i=0;i<len;i++)
  305. {
  306. data=RxBuffer2[g_usRx2Out];
  307. if(i<(MODEM_AT_MSG_DATA_LEN-2))sutAtm.MsgData[i]=data;
  308. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  309. }
  310. sutAtm.MsgData[MODEM_AT_MSG_DATA_LEN-1]=0;//消息最后补0作为结束符
  311. //更新g_usRx3Len,需要防止中断修改冲突
  312. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART2 RX IT
  313. g_usRx2Len=g_usRx2Len-len;
  314. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART2 RX IT
  315. //发送消息
  316. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full2",1);
  317. return;
  318. }
  319. }
  320. return;
  321. }
  322. //+TCPRECV:1,24,12
  323. if(Rx2Len<16)return;//最少16字节“+TCPRECV:0,1,d\r\n”
  324. out=g_usRx2Out;
  325. flag=0;//找到','的标志
  326. j=0;//用来存储xxx的下标,xxx范围"1" -> "9999"
  327. for(i=0;i<Rx2Len;i++){
  328. data=RxBuffer2[out];
  329. if(','==data){
  330. if(flag<1)flag++;
  331. else break;
  332. }else if(flag==1 && j<4){
  333. num[j++]=data;
  334. }
  335. if(++out>=UART2_RX_BUFFER_SIZE)out=0;
  336. }
  337. if(i>=Rx2Len){//未找到了2个','则直接忽略
  338. //SlwTrace(DEBUG,"<2<\r\n");
  339. return;
  340. }
  341. //找到了2个',' 计算xxx长度
  342. num[j]=0;
  343. len=atoi(num);
  344. if(len<10)j=1;
  345. else if(len<100)j=2;
  346. else if(len<1000)j=3;
  347. else j=4;
  348. len=len+j+14;
  349. if(Rx2Len<len){
  350. return;//长度不够,忽略
  351. }
  352. //长度够,产生消息
  353. if(len<(MODEM_AT_MSG_DATA_LEN-2)){
  354. sutAtm.MsgLen=len;
  355. for(i=0;i<len;i++){
  356. sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
  357. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  358. }
  359. sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
  360. //更新g_usRx3Len,需要防止中断修改冲突
  361. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
  362. g_usRx2Len=g_usRx2Len-len;
  363. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
  364. //发送消息
  365. sutAtm.MsgLen +=1;
  366. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full3",1);
  367. }else{
  368. snprintf(preData, sizeof(preData),"AtMsgBuf Over [%u]",i);
  369. SlwTrace(DEBUG,preData,1);
  370. return;
  371. }
  372. }
  373. void UART2RxTxISRHandler(void)
  374. {
  375. __IO unsigned char uart_data;
  376. //----接收中断--------
  377. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  378. {
  379. uart_data = USART_ReceiveData(USART2); // Read one byte from the receive data register
  380. //Modem Trace
  381. if(g_ucUARTSel==COM_SEL_MODEM){
  382. USART_SendData(USART1, uart_data);
  383. USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
  384. return;
  385. }
  386. //MCU 处理Modem
  387. if(g_usRx2Len<UART2_RX_BUFFER_SIZE){
  388. RxBuffer2[g_usRx2In]=uart_data;
  389. if(++g_usRx2In>=UART2_RX_BUFFER_SIZE)g_usRx2In=0;
  390. g_usRx2Len++;
  391. }
  392. USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
  393. }
  394. if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
  395. {
  396. USART_ReceiveData(USART2); //
  397. USART_ClearFlag(USART2,USART_FLAG_ORE); //
  398. }
  399. }
  400. void Uart2Send(unsigned char *txbuf,unsigned short len)
  401. {
  402. int i;
  403. for(i=0;i<len;i++){
  404. USART_SendData(USART2,txbuf[i]);
  405. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  406. }
  407. }
  408. //Usart 3
  409. unsigned short rx3_ct;
  410. unsigned short g_usUart3RecvLen;
  411. unsigned char RxBuffer3[UART3_RX_BUFFER_SIZE];
  412. void Uart3Init(void)
  413. {
  414. GPIO_InitTypeDef GPIO_InitStructure;
  415. USART_InitTypeDef USART_InitStructure;
  416. /*使能串口3使用的GPIO时钟*/
  417. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  418. /*使能串口3时钟*/
  419. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  420. /*串口3 RX管脚配置*/
  421. /* Configure USART3 Rx as input floating */
  422. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  423. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  424. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  425. GPIO_Init(GPIOB, &GPIO_InitStructure);
  426. /*串口3 TX管脚配置*/
  427. /* Configure USART2 Tx as alternate function push-pull */
  428. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  429. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_AF_PP;
  430. // GPIO_Init(GPIOB, &GPIO_InitStructure);
  431. USART_InitStructure.USART_BaudRate = 9600;
  432. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  433. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  434. USART_InitStructure.USART_Parity = USART_Parity_No;;
  435. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  436. USART_InitStructure.USART_Mode = USART_Mode_Rx;
  437. USART_Init(USART3, &USART_InitStructure);
  438. USART_ClearFlag(USART3,USART_FLAG_TC);
  439. USART_ITConfig(USART3, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  440. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  441. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  442. USART_Cmd(USART3, ENABLE);
  443. rx3_ct=0;
  444. g_usUart3RecvLen=0;
  445. memset(RxBuffer3, 0, sizeof(RxBuffer3));
  446. }
  447. void UART3RxTxISRHandler(void)
  448. {
  449. //uint32_t temp = 0;
  450. uint16_t i = 0;
  451. //char *p;
  452. //char *msg;
  453. __IO unsigned char uart_data;
  454. __IO static unsigned char uart_last_data=0;
  455. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
  456. uart_data = USART_ReceiveData(USART3);
  457. if(g_ucUARTSel==COM_SEL_GPS){
  458. USART_SendData(USART1, uart_data);
  459. }else if(0==g_usUart3RecvLen){
  460. RxBuffer3[rx3_ct]= uart_data;
  461. if(RxBuffer3[0]!='$')rx3_ct=0;
  462. else rx3_ct++;
  463. if(rx3_ct>=(UART3_RX_BUFFER_SIZE-1))rx3_ct=0;
  464. if(uart_last_data==0x0d && uart_data==0x0a){ //收到回车换行符
  465. if(RxBuffer3[1]=='G' && RxBuffer3[3]=='R' && RxBuffer3[5]=='C'){//$GPRMC
  466. //Send message
  467. // SlwTrace(DEBUG,"GPRMCisg_usUart2RecvLen=rx2_ct?Send message?!\r\n");
  468. RxBuffer3[rx3_ct-1]=0;
  469. g_usUart3RecvLen=rx3_ct;
  470. }else{
  471. rx3_ct=0;
  472. }
  473. }
  474. }else{
  475. rx3_ct=0;
  476. }
  477. USART_ClearITPendingBit(USART3, USART_IT_RXNE); //Clean interrupt flag
  478. uart_last_data=uart_data;
  479. }
  480. if(USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET)
  481. {
  482. USART_ReceiveData(USART3); //
  483. USART_ClearFlag(USART3,USART_FLAG_ORE); //
  484. }
  485. }
  486. ///////////////////////////////////////////////////////////////////////////////
  487. /*******************************************************************************
  488. ComSelect
  489. 调试口选择
  490. #define COM_SEL_MCU 0
  491. #define COM_SEL_GPS 1
  492. #define COM_SEL_MODEM 2
  493. *******************************************************************************/
  494. void ComSelect(unsigned char sel)
  495. {
  496. switch(sel)
  497. {
  498. case COM_SEL_MCU:
  499. SlwTrace(INF,"COM<-->MCU!",1);
  500. g_ucUARTSel=sel;
  501. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  502. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  503. USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
  504. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
  505. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  506. USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  507. USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
  508. USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
  509. break;
  510. case COM_SEL_GPS:
  511. SlwTrace(INF,"COM<-->GPS!",1);
  512. g_ucUARTSel=sel;
  513. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  514. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  515. USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
  516. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
  517. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  518. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  519. USART_DMACmd(USART3,USART_DMAReq_Tx,DISABLE);
  520. USART_DMACmd(USART3,USART_DMAReq_Rx,DISABLE);
  521. break;
  522. case COM_SEL_MODEM:
  523. SlwTrace(INF,"COM<-->Modem!",1);
  524. g_ucUARTSel=sel;
  525. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  526. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  527. USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
  528. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE);
  529. DMA_Cmd(DMA1_Channel4, DISABLE); //等到需要发送的时候再使能
  530. DMA_Cmd(DMA1_Channel5, DISABLE); //等到需要发送的时候再使能
  531. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  532. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  533. USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
  534. USART_DMACmd(USART2,USART_DMAReq_Rx,DISABLE);
  535. DMA_Cmd(DMA1_Channel6, DISABLE); //等到需要发送的时候再使能
  536. DMA_Cmd(DMA1_Channel7, DISABLE); //等到需要发送的时候再使能
  537. break;
  538. }
  539. }