Serial.c 19 KB

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