Serial.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. //UART1 Trace
  25. unsigned char RxBuffer1[UART1_RX_BUFFER_SIZE];
  26. unsigned char TxBuffer1[UART1_TX_BUFFER_SIZE];
  27. unsigned short rx1_ct;
  28. unsigned short g_usUart1RecvLen;
  29. unsigned char g_ucUart1Sending;
  30. //UART3 用于与GPS通讯
  31. unsigned char RxBuffer3[UART3_RX_BUFFER_SIZE];
  32. unsigned char TxBuffer3[UART3_TX_BUFFER_SIZE];
  33. static unsigned short rx3_ct;
  34. unsigned short g_usUart3RecvLen;
  35. //UART2 用于与MODEM通讯
  36. unsigned char RxBuffer2[UART2_RX_BUFFER_SIZE];
  37. unsigned char TxBuffer2[UART2_TX_BUFFER_SIZE];
  38. static unsigned short rx2_ct;
  39. unsigned short g_usRx2Len;
  40. unsigned short g_usRx2In;
  41. unsigned short g_usRx2Out;
  42. unsigned char g_ucUart2Sending;
  43. /********************************************************************
  44. *Uart1DMAInit
  45. *串口1 DMA设置
  46. *串口1 发送必须用通道4,接收必须用通道5
  47. *********************************************************************/
  48. void Uart1DMAInit(void)
  49. {
  50. NVIC_InitTypeDef NVIC_InitStructure;
  51. DMA_InitTypeDef DMA_InitStructure;
  52. //启动DMA时钟
  53. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  54. #if UART1_TX_USE_DMA!=0
  55. //DMA 通道4用于UART1 TX
  56. DMA_DeInit(DMA1_Channel4);
  57. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//外设地址
  58. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;//内存地址
  59. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA传输方向单向
  60. DMA_InitStructure.DMA_BufferSize = UART1_TX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
  61. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
  62. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
  63. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
  64. DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
  65. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
  66. DMA_InitStructure.DMA_Priority = DMA_Priority_Low; //设置DMA的优先级别
  67. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
  68. DMA_Init(DMA1_Channel4,&DMA_InitStructure);
  69. // //DMA中断设置
  70. // NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  71. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  72. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  73. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  74. // NVIC_Init(&NVIC_InitStructure);
  75. DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);//中断设置
  76. DMA_Cmd(DMA1_Channel4, DISABLE); //等到需要发送的时候再使能
  77. #endif
  78. //DMA 通道5用于UART1 RX
  79. DMA_DeInit(DMA1_Channel5);
  80. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//外设地址
  81. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1;//内存地址
  82. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMA传输方向单向
  83. DMA_InitStructure.DMA_BufferSize = UART1_RX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
  84. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
  85. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
  86. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
  87. DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
  88. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
  89. DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
  90. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
  91. DMA_Init(DMA1_Channel5,&DMA_InitStructure);
  92. //DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
  93. }
  94. /********************************************************************
  95. *Uart2DMAInit
  96. *串口2 DMA设置
  97. *串口2 发送必须用通道7,接收必须用通道6
  98. *********************************************************************/
  99. void Uart2DMAInit(void)
  100. {
  101. NVIC_InitTypeDef NVIC_InitStructure;
  102. DMA_InitTypeDef DMA_InitStructure;
  103. //启动DMA时钟
  104. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  105. #if UART2_TX_USE_DMA==1
  106. //DMA 通道2用于UART3 TX
  107. DMA_DeInit(DMA1_Channel7);
  108. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR);//外设地址
  109. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2;//内存地址
  110. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA传输方向单向
  111. DMA_InitStructure.DMA_BufferSize = UART3_TX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
  112. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
  113. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
  114. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
  115. DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
  116. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
  117. DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
  118. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
  119. DMA_Init(DMA1_Channel7,&DMA_InitStructure);
  120. DMA_ITConfig(DMA1_Channel7,DMA_IT_TC,ENABLE);//中断设置
  121. #else
  122. DMA_ITConfig(DMA1_Channel7,DMA_IT_TC,DISABLE);
  123. #endif
  124. DMA_Cmd(DMA1_Channel7, DISABLE);
  125. //DMA 通道3用于UART3 RX
  126. DMA_DeInit(DMA1_Channel6);
  127. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR);//外设地址
  128. DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer2;//内存地址
  129. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMA传输方向单向
  130. DMA_InitStructure.DMA_BufferSize = UART3_RX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
  131. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
  132. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
  133. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
  134. DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
  135. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
  136. DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //设置DMA的优先级别
  137. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
  138. DMA_Init(DMA1_Channel6,&DMA_InitStructure);
  139. DMA_Cmd(DMA1_Channel6, DISABLE); //使能通道
  140. }
  141. /*********************************************************************************************
  142. *UART2RxTxISRHandler
  143. *串口2中断处理函数
  144. **********************************************************************************************/
  145. void UART2RxTxISRHandler(void)
  146. {
  147. __IO unsigned char uart_data;
  148. //----接收中断--------
  149. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  150. {
  151. uart_data = USART_ReceiveData(USART2); // Read one byte from the receive data register
  152. //Modem Trace
  153. if(g_ucUARTSel==COM_SEL_MODEM){
  154. USART_SendData(USART1, uart_data);
  155. USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
  156. return;
  157. }
  158. //MCU 处理Modem
  159. if(g_usRx2Len<UART2_RX_BUFFER_SIZE){
  160. RxBuffer2[g_usRx2In]=uart_data;
  161. if(++g_usRx2In>=UART2_RX_BUFFER_SIZE)g_usRx2In=0;
  162. g_usRx2Len++;
  163. if(uart_data==0x0a)isr_sem_send(&ModemUartSem);
  164. }
  165. //isr_sem_send(&ModemUartSem);
  166. USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
  167. }
  168. if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
  169. {
  170. USART_ReceiveData(USART2); //
  171. USART_ClearFlag(USART2,USART_FLAG_ORE); //
  172. }
  173. }
  174. /**********************************************************************
  175. >112/r/n 最少6字节
  176. ************************************************************************/
  177. void Uart2RecvProcess(void)
  178. {
  179. unsigned short Rx2Len;
  180. unsigned short out;
  181. char preData[20];
  182. unsigned short i,j,len;
  183. char num[5];
  184. char data,flag;
  185. // //少于8字节不处理
  186. // if(g_usRx2Len<8)return;
  187. //缓冲区满,清空缓冲区
  188. if(g_usRx2Len>(UART2_RX_BUFFER_SIZE-2)){
  189. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  190. g_usRx2In=0;
  191. g_usRx2Len=0;
  192. g_usRx2Out=0;
  193. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  194. SlwTrace(DEBUG,"[Rx2Buf Over]",1);
  195. return;
  196. }
  197. //锁定环形缓冲区当前状态,
  198. Rx2Len=g_usRx2Len;
  199. // USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  200. // g_usRx2Len=g_usRx2Len-i;
  201. // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  202. // //少于8字节不处理
  203. // if(Rx2Len<4)return;
  204. //预读取前n字节 n最少4,最大16 为后续处理提供条件
  205. if(Rx2Len<16)len=Rx2Len;
  206. else len=16;
  207. out=g_usRx2Out;
  208. for(i=0;i<len;i++){
  209. preData[i]=RxBuffer2[out];
  210. if(++out>UART2_RX_BUFFER_SIZE)out=0;
  211. }
  212. RunMake(THIS_FILE_ID);
  213. //判断是否为OK
  214. //判断前8字节是否等于"+TCPRECV",如果等于则特殊处理,如果不等则找到"/r/n"作为结束符
  215. if(0!=ModemStrCmp(preData,"+TCP"))
  216. {
  217. //非"+TCPRECV",则直接找到最近的0x0A作为结束符,读取并形成一包消息
  218. out=g_usRx2Out;
  219. for(i=0;i<Rx2Len;i++)
  220. {
  221. if(RxBuffer2[out]==0x0A)break;
  222. if(++out>=UART2_RX_BUFFER_SIZE)out=0;
  223. }
  224. //123/n/0
  225. if(i<Rx2Len)
  226. {
  227. //找到了0x0A,从环形缓冲区中读取数据并拷贝到消息缓冲区,发送消息
  228. len=i+1;//消息长度
  229. RunMake(THIS_FILE_ID);
  230. if(len<(MODEM_AT_MSG_DATA_LEN-2))
  231. {
  232. sutAtm.MsgLen=len;
  233. for(i=0;i<len;i++)
  234. {
  235. sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
  236. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  237. }
  238. sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
  239. //更新g_usRx3Len,需要防止中断修改冲突
  240. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
  241. g_usRx2Len=g_usRx2Len-len;
  242. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
  243. RunMake(THIS_FILE_ID);
  244. //SlwTrace(INF,"/*********start*************/",1);
  245. SlwTrace(INF,sutAtm.MsgData,1);
  246. //发送消息
  247. sutAtm.MsgLen+=1;
  248. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full1",1);
  249. return;
  250. }else
  251. {//消息队列的内存块长度不够,需要特殊处理,扔掉后面部分
  252. sutAtm.MsgLen=len;
  253. for(i=0;i<len;i++)
  254. {
  255. data=RxBuffer2[g_usRx2Out];
  256. if(i<(MODEM_AT_MSG_DATA_LEN-2))sutAtm.MsgData[i]=data;
  257. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  258. }
  259. sutAtm.MsgData[MODEM_AT_MSG_DATA_LEN-1]=0;//消息最后补0作为结束符
  260. //更新g_usRx3Len,需要防止中断修改冲突
  261. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART2 RX IT
  262. g_usRx2Len=g_usRx2Len-len;
  263. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART2 RX IT
  264. RunMake(THIS_FILE_ID);
  265. //发送消息
  266. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full2",1);
  267. return;
  268. }
  269. }
  270. return;
  271. }
  272. }
  273. void Uart1Send(char *txbuf,int len)
  274. {
  275. int i;
  276. for(i=0;i<len;i++){
  277. USART_SendData(USART1,txbuf[i]);
  278. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  279. }
  280. }
  281. void Uart2Send(unsigned char *txbuf,unsigned short len)
  282. {
  283. int i;
  284. for(i=0;i<len;i++){
  285. USART_SendData(USART2,txbuf[i]);
  286. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  287. }
  288. }
  289. /*************************************************************************************
  290. * 函数名:USART1_Send
  291. * 描述 :串口1发送数据函数 通过DMA实现
  292. * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
  293. * 输出 : 无
  294. * 举例 :Usart1_Send("Hello word!",11); Usart1_Send(Txd1Buffer,11);
  295. **************************************************************************************/
  296. void USART1_Send(char *pbuffer, unsigned short size)
  297. {
  298. #if UART1_TX_USE_DMA==1
  299. char buf[20];
  300. unsigned short i;
  301. unsigned short len;
  302. char *pData;
  303. if(size==0)return;
  304. //等待上一次发送完成
  305. while(g_ucUart1Sending!=0){
  306. os_dly_wait(1);
  307. }
  308. #if 0 //增加时间戳
  309. sprintf((char *)TxBuffer1,"%010u: ",os_time_get());
  310. if(size>(UART1_TX_BUFFER_SIZE-12))len=UART1_TX_BUFFER_SIZE-12;
  311. else len=size;
  312. len+=12;
  313. pData=(char *)&TxBuffer1[12];
  314. #else
  315. if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  316. else len=size;
  317. pData=(char *)&TxBuffer1[0];
  318. #endif
  319. for(i=0;i<len;i++)pData[i]=pbuffer[i];
  320. DMA_Cmd (DMA1_Channel4,DISABLE);
  321. DMA1_Channel4->CMAR = (u32)TxBuffer1;
  322. DMA1_Channel4->CNDTR = len;
  323. g_ucUart1Sending=1;
  324. DMA_Cmd (DMA1_Channel4,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
  325. #endif
  326. }
  327. void USART1_SendNew(char *pbuffer, unsigned short size,char needEnd)
  328. {
  329. #if UART1_TX_USE_DMA==1
  330. char buf[20];
  331. unsigned short i;
  332. unsigned short len;
  333. char *pData;
  334. if(size==0)return;
  335. //等待上一次发送完成
  336. while(g_ucUart1Sending!=0){
  337. os_dly_wait(1);
  338. }
  339. if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  340. else len=size;
  341. pData=(char *)&TxBuffer1[0];
  342. for(i=0;i<len;i++)pData[i]=pbuffer[i];
  343. ///////////////////////////////
  344. if(0 != needEnd)
  345. {
  346. if(len <= (UART1_TX_BUFFER_SIZE-2))
  347. {
  348. pData[len++] = '\r';
  349. pData[len++] = '\n';
  350. }
  351. }
  352. ///////////////////////////////
  353. DMA_Cmd (DMA1_Channel4,DISABLE);
  354. DMA1_Channel4->CMAR = (u32)TxBuffer1;
  355. DMA1_Channel4->CNDTR = len;
  356. g_ucUart1Sending=1;
  357. DMA_Cmd (DMA1_Channel4,ENABLE);
  358. #endif
  359. }
  360. void USART1_SendWaitCompleted(void)
  361. {
  362. #if UART1_TX_USE_DMA==1
  363. while(g_ucUart1Sending!=0){
  364. os_dly_wait(1);
  365. }
  366. #endif
  367. }
  368. /*************************************************************************************
  369. * 函数名:USART2_Send
  370. * 描述 :串口1发送数据函数 通过DMA实现
  371. * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
  372. * 输出 : 无
  373. * 举例 :Usart3_Send("Hello word!",11); Usart3_Send(Txd3Buffer,11);
  374. **************************************************************************************/
  375. void USART2_Send(char *pbuffer, unsigned short size)
  376. {
  377. #if UART2_TX_USE_DMA==1
  378. unsigned short i;
  379. if(size==0 || size>UART2_TX_BUFFER_SIZE)return;
  380. //等待上一次发送完成
  381. while(g_ucUart2Sending!=0){
  382. os_dly_wait(1);
  383. }
  384. for(i=0;i<size;i++){
  385. TxBuffer2[i]=pbuffer[i];
  386. }
  387. DMA_Cmd (DMA1_Channel7,DISABLE);
  388. DMA1_Channel7->CMAR = (u32)TxBuffer2;
  389. DMA1_Channel7->CNDTR = size;
  390. g_ucUart2Sending=1;
  391. DMA_Cmd (DMA1_Channel7,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
  392. #endif
  393. }
  394. void USART3_SendWaitCompleted(void)
  395. {
  396. #if UART3_TX_USE_DMA==1
  397. while(g_ucUart3Sending!=0){
  398. os_dly_wait(1);
  399. }
  400. #endif
  401. }
  402. /*********************************************************************************************
  403. *UART1RxTxISRHandler
  404. *串口1中断处理函数
  405. **********************************************************************************************/
  406. void UART1RxTxISRHandler(void)
  407. {
  408. __IO unsigned char uart_data;
  409. __IO static unsigned char uart_last_data=0;
  410. char *p;
  411. unsigned char i;
  412. uint32_t temp = 0;
  413. //----接收中断--------
  414. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  415. {
  416. uart_data = USART_ReceiveData(USART1);
  417. if(g_ucUARTSel==COM_SEL_MODEM){//Modem Trace
  418. USART_SendData(USART2, uart_data);
  419. }else if(g_ucUARTSel==COM_SEL_GPS){//GPS Trace
  420. USART_SendData(USART3, uart_data);
  421. }else {
  422. if(g_usUart1RecvLen!=0){//上一包数据未处理完,不处理新来的数据,直接扔掉!
  423. rx1_ct=0;
  424. RxBuffer1[0]=0;
  425. return;
  426. }
  427. RxBuffer1[rx1_ct++]=uart_data;
  428. if(uart_last_data==0x0d &&uart_data==0x0a){
  429. if(RxBuffer1[0]=='>'||RxBuffer1[0]=='A'||RxBuffer1[0]=='G'){
  430. g_usUart1RecvLen=rx1_ct;
  431. RxBuffer1[rx1_ct]=0;
  432. }else{
  433. rx1_ct=0;
  434. RxBuffer1[0]=0;
  435. }
  436. }
  437. }
  438. if(rx1_ct>=(UART1_RX_BUFFER_SIZE-1)){//数据超过缓冲区的数据,过滤不处理
  439. rx1_ct=0;
  440. g_usUart1RecvLen=0;
  441. }
  442. }
  443. uart_last_data=uart_data;
  444. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
  445. // //---异常中断---
  446. if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
  447. {
  448. USART_ReceiveData(USART1); //
  449. USART_ClearFlag(USART1,USART_FLAG_ORE); //
  450. }
  451. }
  452. /*******************************************************************************
  453. Uart1Init
  454. Use for Trace
  455. *******************************************************************************/
  456. void Uart1Init()
  457. {
  458. GPIO_InitTypeDef GPIO_InitStructure;
  459. USART_InitTypeDef USART_InitStructure;
  460. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
  461. /*
  462. * USART1_TX -> PA9 , USART1_RX -> PA10
  463. */
  464. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  465. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  466. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  467. GPIO_Init(GPIOA, &GPIO_InitStructure);
  468. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  469. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;// GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
  470. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  471. GPIO_Init(GPIOA, &GPIO_InitStructure);
  472. USART_InitStructure.USART_BaudRate =9600;// 115200;
  473. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  474. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  475. USART_InitStructure.USART_Parity = USART_Parity_No;
  476. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  477. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  478. USART_Init(USART1, &USART_InitStructure);
  479. USART_Cmd(USART1, ENABLE);
  480. USART_ClearFlag(USART1,USART_FLAG_TC);
  481. USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  482. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE//DISABLE
  483. }
  484. void Uart1RxEnable(int en)
  485. {
  486. static unsigned char sucEn=0;
  487. USART_InitTypeDef USART_InitStructure;
  488. if(sucEn!=en)sucEn=en;
  489. else return;
  490. if(en){
  491. USART_InitStructure.USART_BaudRate =9600;// 115200;
  492. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  493. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  494. USART_InitStructure.USART_Parity = USART_Parity_No;
  495. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  496. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
  497. USART_Init(USART1, &USART_InitStructure);
  498. USART_Cmd(USART1, ENABLE);
  499. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE
  500. }else{
  501. USART_InitStructure.USART_BaudRate =9600;// 115200;
  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_Tx;//
  507. USART_Init(USART1, &USART_InitStructure);
  508. USART_Cmd(USART1, ENABLE);
  509. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE
  510. }
  511. }
  512. void Uart1RxEnable2(int en)
  513. {
  514. USART_InitTypeDef USART_InitStructure;
  515. if(en){
  516. USART_InitStructure.USART_BaudRate =115200;// 115200;
  517. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  518. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  519. USART_InitStructure.USART_Parity = USART_Parity_No;
  520. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  521. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
  522. USART_Init(USART1, &USART_InitStructure);
  523. USART_Cmd(USART1, ENABLE);
  524. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE //USART_IT_IDLE USART_IT_RXNE
  525. }else{
  526. USART_InitStructure.USART_BaudRate =115200;// 115200;
  527. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  528. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  529. USART_InitStructure.USART_Parity = USART_Parity_No;
  530. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  531. USART_InitStructure.USART_Mode = USART_Mode_Tx;//
  532. USART_Init(USART1, &USART_InitStructure);
  533. USART_Cmd(USART1, ENABLE);
  534. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  535. }
  536. }
  537. /***************************************************************************
  538. Uart2Init
  539. Use for MainBoard
  540. ****************************************************************************/
  541. void Uart2Init(void)
  542. {
  543. GPIO_InitTypeDef GPIO_InitStructure;
  544. USART_InitTypeDef USART_InitStructure;
  545. /*使能串口2使用的GPIO时钟*/
  546. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  547. /*使能串口2时钟*/
  548. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  549. /*串口2 RX管脚配置*/
  550. /* Configure USART2 Rx as input floating */
  551. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  552. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  553. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  554. GPIO_Init(GPIOA, &GPIO_InitStructure);
  555. /*串口2 TX管脚配置*/
  556. /* Configure USART2 Tx as alternate function push-pull */
  557. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  558. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  559. GPIO_Init(GPIOA, &GPIO_InitStructure);
  560. USART_InitStructure.USART_BaudRate = 115200;
  561. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  562. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  563. USART_InitStructure.USART_Parity = USART_Parity_No;
  564. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  565. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  566. USART_Init(USART2, &USART_InitStructure);
  567. USART_ClearFlag(USART2,USART_FLAG_TC);
  568. USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  569. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  570. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  571. USART_ITConfig(USART2, USART_IT_PE, ENABLE);
  572. USART_ITConfig(USART2, USART_IT_ERR, ENABLE);
  573. USART_Cmd(USART2, ENABLE);
  574. rx2_ct=0;
  575. g_usRx2Len=0;
  576. g_usRx2In=0;
  577. g_usRx2Out=0;
  578. g_ucUart2Sending=0;
  579. memset(RxBuffer2,0,sizeof(RxBuffer2));
  580. }
  581. void UART3RxTxISRHandler(void)
  582. {
  583. uint16_t i = 0;
  584. __IO unsigned char uart_data;
  585. __IO static unsigned char uart_last_data=0;
  586. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
  587. uart_data = USART_ReceiveData(USART3);
  588. if(g_ucUARTSel==COM_SEL_GPS){
  589. USART_SendData(USART1, uart_data);
  590. }else if(0==g_usUart3RecvLen){
  591. RxBuffer3[rx3_ct]= uart_data;
  592. if(RxBuffer3[0]!='$')rx3_ct=0;
  593. else rx3_ct++;
  594. if(rx3_ct>=(UART3_RX_BUFFER_SIZE-1))rx3_ct=0;
  595. if(uart_last_data==0x0d && uart_data==0x0a){ //收到回车换行符
  596. if((RxBuffer3[1]=='G' && RxBuffer3[3]=='R' && RxBuffer3[5]=='C')||(RxBuffer3[1]=='G' && RxBuffer3[4]=='G' && RxBuffer3[5]=='A')){//$GPRMC $GNGGA
  597. //Send message
  598. // SlwTrace(DEBUG,"GPRMCisg_usUart2RecvLen=rx2_ct?Send message?!\r\n");
  599. RxBuffer3[rx3_ct-1]=0;
  600. g_usUart3RecvLen=rx3_ct;
  601. }else{
  602. rx3_ct=0;
  603. }
  604. }
  605. }else{
  606. rx3_ct=0;
  607. }
  608. USART_ClearITPendingBit(USART3, USART_IT_RXNE); //Clean interrupt flag
  609. uart_last_data=uart_data;
  610. }
  611. if(USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET)
  612. {
  613. USART_ReceiveData(USART3); //
  614. USART_ClearFlag(USART3,USART_FLAG_ORE); //
  615. }
  616. }
  617. /***************************************************************************
  618. Uart3Init
  619. ****************************************************************************/
  620. void Uart3Init(void)
  621. {
  622. GPIO_InitTypeDef GPIO_InitStructure;
  623. USART_InitTypeDef USART_InitStructure;
  624. /*使能串口3使用的GPIO时钟*/
  625. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  626. /*使能串口3时钟*/
  627. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  628. /*串口3 RX管脚配置*/
  629. /* Configure USART3 Rx as input floating */
  630. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  631. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  632. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  633. GPIO_Init(GPIOB, &GPIO_InitStructure);
  634. /*串口3 TX管脚配置*/
  635. /* Configure USART2 Tx as alternate function push-pull */
  636. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  637. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_AF_PP;
  638. // GPIO_Init(GPIOB, &GPIO_InitStructure);
  639. USART_InitStructure.USART_BaudRate = 9600;
  640. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  641. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  642. USART_InitStructure.USART_Parity = USART_Parity_No;;
  643. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  644. USART_InitStructure.USART_Mode = USART_Mode_Rx;
  645. USART_Init(USART3, &USART_InitStructure);
  646. USART_ClearFlag(USART3,USART_FLAG_TC);
  647. USART_ITConfig(USART3, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  648. USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  649. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  650. USART_Cmd(USART3, ENABLE);
  651. rx3_ct=0;
  652. g_usUart3RecvLen=0;
  653. memset(RxBuffer3, 0, sizeof(RxBuffer3));
  654. }
  655. /***********************************************************************************
  656. *
  657. ************************************************************************************/
  658. void SlwTrace(TRACE_TYPE type,char *buf,char needEnd)
  659. {
  660. int len;
  661. #if UART1_TX_USE_DMA==0
  662. len=strlen(buf);
  663. //if(len>256)len=256;
  664. Uart1Send(buf,len);
  665. #else
  666. len=strlen(buf);
  667. if(len>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  668. //USART1_Send(buf,len);
  669. USART1_SendNew(buf,len,needEnd);
  670. #endif
  671. }
  672. /*******************************************************************************
  673. ComSelect
  674. 调试口选择
  675. #define COM_SEL_MCU 0
  676. #define COM_SEL_GPS 1
  677. #define COM_SEL_MODEM 2
  678. *******************************************************************************/
  679. void ComSelect(unsigned char sel)
  680. {
  681. switch(sel)
  682. {
  683. case COM_SEL_MCU:
  684. SlwTrace(INF,"COM<-->MCU!",1);
  685. os_dly_wait(10);
  686. g_ucUARTSel=sel;
  687. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  688. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  689. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  690. USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  691. break;
  692. case COM_SEL_GPS:
  693. SlwTrace(INF,"COM<-->GPS!",1);
  694. os_dly_wait(10);
  695. g_ucUARTSel=sel;
  696. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  697. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  698. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  699. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  700. break;
  701. case COM_SEL_MODEM:
  702. SlwTrace(INF,"COM<-->Modem!",1);
  703. os_dly_wait(10);
  704. g_ucUARTSel=sel;
  705. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  706. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  707. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  708. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  709. break;
  710. }
  711. }
  712. /**************************************************************************
  713. * USART1_SendOK_IQ
  714. * 串口1发送数据完毕后,会调用的中断函数,将发送标志置0
  715. * 此函数需在stm32f10x_it.c 的 DMA1_Channel4_IRQHandler()中被调用
  716. ***************************************************************************/
  717. void USART1_SendOK_IQ(void)
  718. {
  719. //判断是否为DMA发送完成中断
  720. if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
  721. {
  722. DMA_ClearITPendingBit(DMA1_IT_GL4);
  723. DMA_ClearFlag(DMA1_FLAG_TC4);
  724. DMA_Cmd (DMA1_Channel4,DISABLE);
  725. g_ucUart1Sending = 0; //数据发送完毕
  726. }
  727. //printf("USART1_SendOK_IQ\r\n");
  728. }
  729. /**************************************************************************
  730. * USART2_SendOK_IQ
  731. * 串口3发送数据完毕后,会调用的中断函数,将发送标志置0
  732. * 此函数需在stm32f10x_it.c 的 DMA1_Channel2_IRQHandler()中被调用
  733. ***************************************************************************/
  734. void USART2_SendOK_IQ(void)
  735. {
  736. //判断是否为DMA发送完成中断
  737. if(DMA_GetFlagStatus(DMA1_FLAG_TC7)==SET)
  738. {
  739. DMA_ClearITPendingBit(DMA1_IT_GL7);
  740. DMA_ClearFlag(DMA1_FLAG_TC7);
  741. DMA_Cmd (DMA1_Channel7,DISABLE);
  742. g_ucUart2Sending = 0; //数据发送完毕
  743. }
  744. //printf("USART3_SendOK_IQ\r\n");
  745. }
  746. #ifdef __GNUC__
  747. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  748. set to 'Yes') calls __io_putchar() */
  749. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  750. #else
  751. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  752. #endif /* __GNUC__ */
  753. /**
  754. * @brief Retargets the C library printf function to the USART.
  755. * @param None
  756. * @retval None
  757. */
  758. PUTCHAR_PROTOTYPE
  759. {
  760. /* Place your implementation of fputc here */
  761. /* e.g. write a character to the USART */
  762. USART_SendData(USART1, (uint8_t) ch);
  763. /* Loop until the end of transmission */
  764. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  765. {
  766. }
  767. return ch;
  768. }