Serial.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. static 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. }
  164. isr_sem_send(&ModemUartSem);
  165. USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
  166. }
  167. if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
  168. {
  169. USART_ReceiveData(USART2); //
  170. USART_ClearFlag(USART2,USART_FLAG_ORE); //
  171. }
  172. }
  173. /**********************************************************************
  174. >112/r/n 最少6字节
  175. ************************************************************************/
  176. void Uart2RecvProcess(void)
  177. {
  178. unsigned short Rx2Len;
  179. unsigned short out;
  180. char preData[20];
  181. unsigned short i,j,len;
  182. char num[5];
  183. char data,flag;
  184. // //少于8字节不处理
  185. // if(g_usRx2Len<8)return;
  186. //缓冲区满,清空缓冲区
  187. if(g_usRx2Len>(UART2_RX_BUFFER_SIZE-2)){
  188. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  189. g_usRx2In=0;
  190. g_usRx2Len=0;
  191. g_usRx2Out=0;
  192. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  193. SlwTrace(DEBUG,"[Rx2Buf Over]",1);
  194. return;
  195. }
  196. //锁定环形缓冲区当前状态,
  197. Rx2Len=g_usRx2Len;
  198. // USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  199. // g_usRx2Len=g_usRx2Len-i;
  200. // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  201. // //少于8字节不处理
  202. // if(Rx2Len<4)return;
  203. //预读取前n字节 n最少4,最大16 为后续处理提供条件
  204. if(Rx2Len<16)len=Rx2Len;
  205. else len=16;
  206. out=g_usRx2Out;
  207. for(i=0;i<len;i++){
  208. preData[i]=RxBuffer2[out];
  209. if(++out>UART2_RX_BUFFER_SIZE)out=0;
  210. }
  211. RunMake(THIS_FILE_ID);
  212. //判断是否为OK
  213. //判断前8字节是否等于"+TCPRECV",如果等于则特殊处理,如果不等则找到"/r/n"作为结束符
  214. if(0!=ModemStrCmp(preData,"+TCP"))
  215. {
  216. //非"+TCPRECV",则直接找到最近的0x0A作为结束符,读取并形成一包消息
  217. out=g_usRx2Out;
  218. for(i=0;i<Rx2Len;i++)
  219. {
  220. if(RxBuffer2[out]==0x0A)break;
  221. if(++out>=UART2_RX_BUFFER_SIZE)out=0;
  222. }
  223. //123/n/0
  224. if(i<Rx2Len)
  225. {
  226. //找到了0x0A,从环形缓冲区中读取数据并拷贝到消息缓冲区,发送消息
  227. len=i+1;//消息长度
  228. RunMake(THIS_FILE_ID);
  229. if(len<(MODEM_AT_MSG_DATA_LEN-2))
  230. {
  231. sutAtm.MsgLen=len;
  232. for(i=0;i<len;i++)
  233. {
  234. sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
  235. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  236. }
  237. sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
  238. //更新g_usRx3Len,需要防止中断修改冲突
  239. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
  240. g_usRx2Len=g_usRx2Len-len;
  241. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
  242. RunMake(THIS_FILE_ID);
  243. //SlwTrace(INF,"/*********start*************/",1);
  244. SlwTrace(INF,sutAtm.MsgData,1);
  245. //发送消息
  246. sutAtm.MsgLen+=1;
  247. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full1",1);
  248. return;
  249. }else
  250. {//消息队列的内存块长度不够,需要特殊处理,扔掉后面部分
  251. sutAtm.MsgLen=len;
  252. for(i=0;i<len;i++)
  253. {
  254. data=RxBuffer2[g_usRx2Out];
  255. if(i<(MODEM_AT_MSG_DATA_LEN-2))sutAtm.MsgData[i]=data;
  256. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  257. }
  258. sutAtm.MsgData[MODEM_AT_MSG_DATA_LEN-1]=0;//消息最后补0作为结束符
  259. //更新g_usRx3Len,需要防止中断修改冲突
  260. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART2 RX IT
  261. g_usRx2Len=g_usRx2Len-len;
  262. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART2 RX IT
  263. RunMake(THIS_FILE_ID);
  264. //发送消息
  265. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full2",1);
  266. return;
  267. }
  268. }
  269. return;
  270. }
  271. //+TCPRECV:1,24,12
  272. if(Rx2Len<16)return;//最少16字节“+TCPRECV:0,1,d\r\n”
  273. out=g_usRx2Out;
  274. flag=0;//找到','的标志
  275. j=0;//用来存储xxx的下标,xxx范围"1" -> "9999"
  276. for(i=0;i<Rx2Len;i++){
  277. data=RxBuffer2[out];
  278. if(','==data){
  279. if(flag<1)flag++;
  280. else break;
  281. }else if(flag==1 && j<4){
  282. num[j++]=data;
  283. }
  284. if(++out>=UART2_RX_BUFFER_SIZE)out=0;
  285. }
  286. if(i>=Rx2Len){//未找到了2个','则直接忽略
  287. //SlwTrace(DEBUG,"<2<\r\n");
  288. return;
  289. }
  290. RunMake(THIS_FILE_ID);
  291. //找到了2个',' 计算xxx长度
  292. num[j]=0;
  293. len=atoi(num);
  294. if(len<10)j=1;
  295. else if(len<100)j=2;
  296. else if(len<1000)j=3;
  297. else j=4;
  298. len=len+j+14;
  299. if(Rx2Len<len){
  300. return;//长度不够,忽略
  301. }
  302. //长度够,产生消息
  303. if(len<(MODEM_AT_MSG_DATA_LEN-2)){
  304. sutAtm.MsgLen=len;
  305. for(i=0;i<len;i++){
  306. sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
  307. if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
  308. }
  309. sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
  310. //更新g_usRx3Len,需要防止中断修改冲突
  311. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
  312. g_usRx2Len=g_usRx2Len-len;
  313. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
  314. //发送消息
  315. sutAtm.MsgLen +=1;
  316. if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full3",1);
  317. }else{
  318. snprintf(preData, sizeof(preData),"AtMsgBuf Over [%u]",i);
  319. SlwTrace(DEBUG,preData,1);
  320. return;
  321. }
  322. RunMake(THIS_FILE_ID);
  323. }
  324. /********************************************************************************
  325. *Uart1Send
  326. *串口1启动发送
  327. *********************************************************************************/
  328. /*
  329. void Uart1Send(char *txbuf,int len)
  330. {
  331. int i;
  332. unsigned char data;
  333. unsigned char last_data=~UART_TRANSFORM;
  334. //Send Head
  335. USART_SendData(USART1, UART_HEAD);
  336. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  337. for(i=0;i<len;i++){
  338. data= txbuf[i];
  339. if(UART_TRANSFORM == data){
  340. USART_SendData(USART1,UART_TRANSFORM );
  341. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  342. USART_SendData(USART1,0x01 );
  343. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  344. }else if(UART_HEAD == data){
  345. USART_SendData(USART1,UART_TRANSFORM);
  346. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  347. USART_SendData(USART1,0x02 );
  348. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  349. }else if(UART_END == data){
  350. USART_SendData(USART1,UART_TRANSFORM);
  351. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  352. USART_SendData(USART1,0x03 );
  353. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  354. }else{
  355. USART_SendData(USART1,data);
  356. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  357. }
  358. }
  359. //Send End
  360. USART_SendData(USART1, UART_END);
  361. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  362. }
  363. */
  364. void Uart1Send(char *txbuf,int len)
  365. {
  366. int i;
  367. for(i=0;i<len;i++){
  368. USART_SendData(USART1,txbuf[i]);
  369. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  370. }
  371. }
  372. void Uart2Send(unsigned char *txbuf,unsigned short len)
  373. {
  374. int i;
  375. for(i=0;i<len;i++){
  376. USART_SendData(USART2,txbuf[i]);
  377. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  378. }
  379. }
  380. /*************************************************************************************
  381. * 函数名:USART1_Send
  382. * 描述 :串口1发送数据函数 通过DMA实现
  383. * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
  384. * 输出 : 无
  385. * 举例 :Usart1_Send("Hello word!",11); Usart1_Send(Txd1Buffer,11);
  386. **************************************************************************************/
  387. void USART1_Send(char *pbuffer, unsigned short size)
  388. {
  389. #if UART1_TX_USE_DMA==1
  390. char buf[20];
  391. unsigned short i;
  392. unsigned short len;
  393. char *pData;
  394. if(size==0)return;
  395. //等待上一次发送完成
  396. while(g_ucUart1Sending!=0){
  397. os_dly_wait(1);
  398. }
  399. #if 0 //增加时间戳
  400. sprintf((char *)TxBuffer1,"%010u: ",os_time_get());
  401. if(size>(UART1_TX_BUFFER_SIZE-12))len=UART1_TX_BUFFER_SIZE-12;
  402. else len=size;
  403. len+=12;
  404. pData=(char *)&TxBuffer1[12];
  405. #else
  406. if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  407. else len=size;
  408. pData=(char *)&TxBuffer1[0];
  409. #endif
  410. for(i=0;i<len;i++)pData[i]=pbuffer[i];
  411. DMA_Cmd (DMA1_Channel4,DISABLE);
  412. DMA1_Channel4->CMAR = (u32)TxBuffer1;
  413. DMA1_Channel4->CNDTR = len;
  414. g_ucUart1Sending=1;
  415. DMA_Cmd (DMA1_Channel4,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
  416. #endif
  417. }
  418. void USART1_SendNew(char *pbuffer, unsigned short size,char needEnd)
  419. {
  420. #if UART1_TX_USE_DMA==1
  421. char buf[20];
  422. unsigned short i;
  423. unsigned short len;
  424. char *pData;
  425. if(size==0)return;
  426. //等待上一次发送完成
  427. while(g_ucUart1Sending!=0){
  428. os_dly_wait(1);
  429. }
  430. if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  431. else len=size;
  432. pData=(char *)&TxBuffer1[0];
  433. for(i=0;i<len;i++)pData[i]=pbuffer[i];
  434. ///////////////////////////////
  435. if(0 != needEnd)
  436. {
  437. if(len <= (UART1_TX_BUFFER_SIZE-2))
  438. {
  439. pData[len++] = '\r';
  440. pData[len++] = '\n';
  441. }
  442. }
  443. ///////////////////////////////
  444. DMA_Cmd (DMA1_Channel4,DISABLE);
  445. DMA1_Channel4->CMAR = (u32)TxBuffer1;
  446. DMA1_Channel4->CNDTR = len;
  447. g_ucUart1Sending=1;
  448. DMA_Cmd (DMA1_Channel4,ENABLE);
  449. #endif
  450. }
  451. void USART1_SendWaitCompleted(void)
  452. {
  453. #if UART1_TX_USE_DMA==1
  454. while(g_ucUart1Sending!=0){
  455. os_dly_wait(1);
  456. }
  457. #endif
  458. }
  459. /*************************************************************************************
  460. * 函数名:USART2_Send
  461. * 描述 :串口1发送数据函数 通过DMA实现
  462. * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
  463. * 输出 : 无
  464. * 举例 :Usart3_Send("Hello word!",11); Usart3_Send(Txd3Buffer,11);
  465. **************************************************************************************/
  466. void USART2_Send(char *pbuffer, unsigned short size)
  467. {
  468. #if UART2_TX_USE_DMA==1
  469. unsigned short i;
  470. if(size==0 || size>UART2_TX_BUFFER_SIZE)return;
  471. //等待上一次发送完成
  472. while(g_ucUart2Sending!=0){
  473. os_dly_wait(1);
  474. }
  475. for(i=0;i<size;i++){
  476. TxBuffer2[i]=pbuffer[i];
  477. }
  478. DMA_Cmd (DMA1_Channel7,DISABLE);
  479. DMA1_Channel7->CMAR = (u32)TxBuffer2;
  480. DMA1_Channel7->CNDTR = size;
  481. g_ucUart2Sending=1;
  482. DMA_Cmd (DMA1_Channel7,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
  483. #endif
  484. }
  485. void USART3_SendWaitCompleted(void)
  486. {
  487. #if UART3_TX_USE_DMA==1
  488. while(g_ucUart3Sending!=0){
  489. os_dly_wait(1);
  490. }
  491. #endif
  492. }
  493. /*********************************************************************************************
  494. *UART1RxTxISRHandler
  495. *串口1中断处理函数
  496. 通讯协议
  497. RxBuffer1接收缓冲区
  498. sutMsg.Uart1Recv为1表明接受到一包数据,需要应用层处理数据后将其清零以便接受新的数据包
  499. 每包数据均以UART_HEAD开头,UART_END结尾。如解决数据包内容刚好有与UART_HEAD或UART_END相等的值,则需要转义
  500. 转义字符为UART_TRANSFORM,当收到UART_TRANSFORM开头时将根据紧挨其后的字符决定转义
  501. 如UART_HEAD=0x7a UART_END=0x7b UART_TRANSFORM=0x7c
  502. 如拟发送数据为: 01 02 7a 03 04 7b 05 06 7c 08 09 经过转换后的数据包为:
  503. 01 02 7c 02 03 04 7c 03 05 06 7c 01 08 09
  504. 加上包头包尾后:
  505. 7a 01 02 7c 02 03 04 7c 03 05 06 7c 01 08 09 7b
  506. **********************************************************************************************
  507. void UART1RxTxISRHandler(void)
  508. {
  509. __IO unsigned char uart_data;
  510. __IO static unsigned char last_data;
  511. //----接收中断--------
  512. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  513. {
  514. uart_data = USART_ReceiveData(USART1);
  515. if(sutMsg.Uart1Recv){//上一批没处理,不接受新数据
  516. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
  517. rx1_ct=0;
  518. return;
  519. }
  520. if(uart_data==UART_HEAD){//包头
  521. RxBuffer1[0]=UART_HEAD;
  522. rx1_ct=1;
  523. }else if(rx1_ct>=(UART1_RX_BUFFER_SIZE-1)){//超出接受缓冲区,丢掉此包数据,复位重新接收
  524. rx1_ct=0;
  525. }else if(rx1_ct>0){
  526. if(uart_data==UART_END){//包尾
  527. RxBuffer1[rx1_ct++]=uart_data;
  528. rx1_len=rx1_ct;
  529. rx1_ct=0;
  530. sutMsg.Uart1Recv=1;
  531. }else if(uart_data==UART_TRANSFORM){//转义字符,忽略
  532. }else{
  533. if(last_data==UART_TRANSFORM){
  534. if(0x01==uart_data)RxBuffer1[rx1_ct++]=UART_TRANSFORM;
  535. else if(0x02==uart_data)RxBuffer1[rx1_ct++]=UART_HEAD;
  536. else if(0x03==uart_data)RxBuffer1[rx1_ct++]=UART_END;
  537. }else{
  538. RxBuffer1[rx1_ct++]=uart_data;
  539. }
  540. }
  541. }
  542. last_data=uart_data;
  543. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
  544. }
  545. }
  546. */
  547. /*********************************************************************************************
  548. *UART1RxTxISRHandler
  549. *串口1中断处理函数
  550. **********************************************************************************************/
  551. void UART1RxTxISRHandler(void)
  552. {
  553. __IO unsigned char uart_data;
  554. __IO static unsigned char uart_last_data=0;
  555. char *p;
  556. unsigned char i;
  557. uint32_t temp = 0;
  558. //----接收中断--------
  559. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  560. {
  561. uart_data = USART_ReceiveData(USART1);
  562. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
  563. if(g_ucUARTSel==COM_SEL_MODEM){//Modem Trace
  564. USART_SendData(USART2, uart_data);
  565. }else if(g_ucUARTSel==COM_SEL_GPS){//GPS Trace
  566. USART_SendData(USART3, uart_data);
  567. }
  568. }
  569. //----接收空闲中断----
  570. if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET){
  571. temp = USART1->SR;//先读SR,再读DR
  572. temp = USART1->DR;//方可清USART_IT_IDLE标志
  573. temp = UART1_RX_BUFFER_SIZE - DMA1_Channel5->CNDTR;
  574. DMA_Cmd(DMA1_Channel5,DISABLE);
  575. //处理数据
  576. // if(temp>5 && RxBuffer1[0]=='G' && RxBuffer1[1]=='T' && RxBuffer1[2]=='+'){
  577. if(temp>3){
  578. g_usUart1RecvLen=temp;
  579. }else{
  580. g_usUart1RecvLen=0;
  581. DMA1_Channel5->CNDTR = UART1_RX_BUFFER_SIZE;//重设DMA传输数据长度
  582. DMA_Cmd(DMA1_Channel5,ENABLE);//打开DMA
  583. }
  584. }
  585. // //---异常中断---
  586. if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
  587. {
  588. USART_ReceiveData(USART1); //
  589. USART_ClearFlag(USART1,USART_FLAG_ORE); //
  590. }
  591. }
  592. /*******************************************************************************
  593. Uart1Init
  594. Use for Trace
  595. *******************************************************************************/
  596. void Uart1Init()
  597. {
  598. NVIC_InitTypeDef NVIC_InitStructure;
  599. GPIO_InitTypeDef GPIO_InitStructure;
  600. USART_InitTypeDef USART_InitStructure;
  601. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
  602. /*
  603. * USART1_TX -> PA9 , USART1_RX -> PA10
  604. */
  605. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  606. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  607. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  608. GPIO_Init(GPIOA, &GPIO_InitStructure);
  609. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  610. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;// GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
  611. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  612. GPIO_Init(GPIOA, &GPIO_InitStructure);
  613. USART_InitStructure.USART_BaudRate =9600;// 115200;
  614. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  615. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  616. USART_InitStructure.USART_Parity = USART_Parity_No;
  617. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  618. USART_InitStructure.USART_Mode = USART_Mode_Tx;//USART_Mode_Rx |
  619. USART_Init(USART1, &USART_InitStructure);
  620. USART_Cmd(USART1, ENABLE);
  621. USART_ClearFlag(USART1,USART_FLAG_TC);
  622. USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  623. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  624. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  625. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
  626. USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //使能USART1的发送DMA请求
  627. Uart1DMAInit();
  628. Uart1RxEnable(0);//1打开 如果为0 不使能该串口,可能导致串口接收异常而内存溢出,修改为默认不接收RX只发TX,按组合键才进入写号模式
  629. //FUN2 KEY Free 旋两格进入GT模式 现在0的时候要进入GT
  630. rx1_ct=0;
  631. g_usUart1RecvLen=0;
  632. g_ucUart1Sending=0;
  633. }
  634. void Uart1RxEnable(int en)
  635. {
  636. static unsigned char sucEn=0;
  637. USART_InitTypeDef USART_InitStructure;
  638. if(sucEn!=en)sucEn=en;
  639. else return;
  640. if(en){
  641. USART_InitStructure.USART_BaudRate =9600;// 115200;
  642. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  643. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  644. USART_InitStructure.USART_Parity = USART_Parity_No;
  645. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  646. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
  647. USART_Init(USART1, &USART_InitStructure);
  648. USART_Cmd(USART1, ENABLE);
  649. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  650. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能USART1的接收DMA请求ENABLE
  651. DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
  652. }else{
  653. USART_InitStructure.USART_BaudRate =9600;// 115200;
  654. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  655. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  656. USART_InitStructure.USART_Parity = USART_Parity_No;
  657. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  658. USART_InitStructure.USART_Mode = USART_Mode_Tx;//
  659. USART_Init(USART1, &USART_InitStructure);
  660. USART_Cmd(USART1, ENABLE);
  661. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  662. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
  663. //DMA_Cmd(DMA1_Channel5, DISABLE); //使能通道
  664. }
  665. }
  666. void Uart1RxEnable2(int en)
  667. {
  668. USART_InitTypeDef USART_InitStructure;
  669. if(en){
  670. USART_InitStructure.USART_BaudRate =115200;// 115200;
  671. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  672. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  673. USART_InitStructure.USART_Parity = USART_Parity_No;
  674. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  675. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
  676. USART_Init(USART1, &USART_InitStructure);
  677. USART_Cmd(USART1, ENABLE);
  678. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  679. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能USART1的接收DMA请求ENABLE
  680. DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
  681. }else{
  682. USART_InitStructure.USART_BaudRate =115200;// 115200;
  683. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  684. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  685. USART_InitStructure.USART_Parity = USART_Parity_No;
  686. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  687. USART_InitStructure.USART_Mode = USART_Mode_Tx;//
  688. USART_Init(USART1, &USART_InitStructure);
  689. USART_Cmd(USART1, ENABLE);
  690. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  691. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
  692. //DMA_Cmd(DMA1_Channel5, DISABLE); //使能通道
  693. }
  694. }
  695. /***************************************************************************
  696. Uart2Init
  697. Use for MainBoard
  698. ****************************************************************************/
  699. void Uart2Init(void)
  700. {
  701. GPIO_InitTypeDef GPIO_InitStructure;
  702. USART_InitTypeDef USART_InitStructure;
  703. /*使能串口2使用的GPIO时钟*/
  704. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  705. /*使能串口2时钟*/
  706. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  707. /*串口2 RX管脚配置*/
  708. /* Configure USART2 Rx as input floating */
  709. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  710. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  711. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  712. GPIO_Init(GPIOA, &GPIO_InitStructure);
  713. /*串口2 TX管脚配置*/
  714. /* Configure USART2 Tx as alternate function push-pull */
  715. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  716. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  717. GPIO_Init(GPIOA, &GPIO_InitStructure);
  718. USART_InitStructure.USART_BaudRate = 115200;
  719. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  720. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  721. USART_InitStructure.USART_Parity = USART_Parity_No;
  722. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  723. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  724. USART_Init(USART2, &USART_InitStructure);
  725. USART_ClearFlag(USART2,USART_FLAG_TC);
  726. USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  727. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  728. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  729. USART_ITConfig(USART2, USART_IT_PE, ENABLE);
  730. USART_ITConfig(USART2, USART_IT_ERR, ENABLE);
  731. USART_Cmd(USART2, ENABLE);
  732. Uart2DMAInit();
  733. #if UART2_TX_USE_DMA==1
  734. USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //使能USART3 TX 的DMA请求
  735. #endif
  736. USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
  737. rx2_ct=0;
  738. g_usRx2Len=0;
  739. g_usRx2In=0;
  740. g_usRx2Out=0;
  741. g_ucUart2Sending=0;
  742. memset(RxBuffer2,0,sizeof(RxBuffer2));
  743. }
  744. void UART3RxTxISRHandler(void)
  745. {
  746. uint16_t i = 0;
  747. __IO unsigned char uart_data;
  748. __IO static unsigned char uart_last_data=0;
  749. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
  750. uart_data = USART_ReceiveData(USART3);
  751. if(g_ucUARTSel==COM_SEL_GPS){
  752. USART_SendData(USART1, uart_data);
  753. }else if(0==g_usUart3RecvLen){
  754. RxBuffer3[rx3_ct]= uart_data;
  755. if(RxBuffer3[0]!='$')rx3_ct=0;
  756. else rx3_ct++;
  757. if(rx3_ct>=(UART3_RX_BUFFER_SIZE-1))rx3_ct=0;
  758. if(uart_last_data==0x0d && uart_data==0x0a){ //收到回车换行符
  759. if((RxBuffer3[1]=='G' && RxBuffer3[3]=='R' && RxBuffer3[5]=='C')||(RxBuffer3[1]=='G' && RxBuffer3[4]=='G' && RxBuffer3[5]=='A')){//$GPRMC $GNGGA
  760. //Send message
  761. // SlwTrace(DEBUG,"GPRMCisg_usUart2RecvLen=rx2_ct?Send message?!\r\n");
  762. RxBuffer3[rx3_ct-1]=0;
  763. g_usUart3RecvLen=rx3_ct;
  764. }else{
  765. rx3_ct=0;
  766. }
  767. }
  768. }else{
  769. rx3_ct=0;
  770. }
  771. USART_ClearITPendingBit(USART3, USART_IT_RXNE); //Clean interrupt flag
  772. uart_last_data=uart_data;
  773. }
  774. if(USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET)
  775. {
  776. USART_ReceiveData(USART3); //
  777. USART_ClearFlag(USART3,USART_FLAG_ORE); //
  778. }
  779. }
  780. /***************************************************************************
  781. Uart3Init
  782. ****************************************************************************/
  783. void Uart3Init(void)
  784. {
  785. GPIO_InitTypeDef GPIO_InitStructure;
  786. USART_InitTypeDef USART_InitStructure;
  787. /*使能串口3使用的GPIO时钟*/
  788. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  789. /*使能串口3时钟*/
  790. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  791. /*串口3 RX管脚配置*/
  792. /* Configure USART3 Rx as input floating */
  793. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  794. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  795. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  796. GPIO_Init(GPIOB, &GPIO_InitStructure);
  797. /*串口3 TX管脚配置*/
  798. /* Configure USART2 Tx as alternate function push-pull */
  799. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  800. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_AF_PP;
  801. // GPIO_Init(GPIOB, &GPIO_InitStructure);
  802. USART_InitStructure.USART_BaudRate = 9600;
  803. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  804. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  805. USART_InitStructure.USART_Parity = USART_Parity_No;;
  806. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  807. USART_InitStructure.USART_Mode = USART_Mode_Rx;
  808. USART_Init(USART3, &USART_InitStructure);
  809. USART_ClearFlag(USART3,USART_FLAG_TC);
  810. USART_ITConfig(USART3, USART_IT_TXE, DISABLE); //ENABLE DISABLE
  811. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  812. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
  813. USART_Cmd(USART3, ENABLE);
  814. rx3_ct=0;
  815. g_usUart3RecvLen=0;
  816. memset(RxBuffer3, 0, sizeof(RxBuffer3));
  817. }
  818. /***********************************************************************************
  819. *
  820. ************************************************************************************/
  821. void SlwTrace(TRACE_TYPE type,char *buf,char needEnd)
  822. {
  823. int len;
  824. #ifndef SLW_DEBUG
  825. if(type==DEBUG)return;
  826. #endif
  827. if(g_ucUARTSel!=COM_SEL_MCU)return;
  828. #if UART1_TX_USE_DMA==0
  829. len=strlen(buf);
  830. if(len>256)len=256;
  831. Uart1Send(buf,len);
  832. #else
  833. len=strlen(buf);
  834. if(len>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
  835. //USART1_Send(buf,len);
  836. USART1_SendNew(buf,len,needEnd);
  837. #endif
  838. }
  839. /*******************************************************************************
  840. ComSelect
  841. 调试口选择
  842. #define COM_SEL_MCU 0
  843. #define COM_SEL_GPS 1
  844. #define COM_SEL_MODEM 2
  845. *******************************************************************************/
  846. void ComSelect(unsigned char sel)
  847. {
  848. switch(sel)
  849. {
  850. case COM_SEL_MCU:
  851. SlwTrace(INF,"COM<-->MCU!",1);
  852. os_dly_wait(10);
  853. g_ucUARTSel=sel;
  854. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  855. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  856. USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
  857. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
  858. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
  859. USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
  860. USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
  861. USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
  862. break;
  863. case COM_SEL_GPS:
  864. SlwTrace(INF,"COM<-->GPS!",1);
  865. os_dly_wait(10);
  866. g_ucUARTSel=sel;
  867. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  868. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  869. USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
  870. USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
  871. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  872. USART_ITConfig(USART3, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  873. USART_DMACmd(USART3,USART_DMAReq_Tx,DISABLE);
  874. USART_DMACmd(USART3,USART_DMAReq_Rx,DISABLE);
  875. break;
  876. case COM_SEL_MODEM:
  877. SlwTrace(INF,"COM<-->Modem!",1);
  878. os_dly_wait(10);
  879. g_ucUARTSel=sel;
  880. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  881. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  882. USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
  883. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE);
  884. DMA_Cmd(DMA1_Channel4, DISABLE); //等到需要发送的时候再使能
  885. DMA_Cmd(DMA1_Channel5, DISABLE); //等到需要发送的时候再使能
  886. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
  887. USART_ITConfig(USART2, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
  888. USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
  889. USART_DMACmd(USART2,USART_DMAReq_Rx,DISABLE);
  890. DMA_Cmd(DMA1_Channel6, DISABLE); //等到需要发送的时候再使能
  891. DMA_Cmd(DMA1_Channel7, DISABLE); //等到需要发送的时候再使能
  892. break;
  893. }
  894. }
  895. /**************************************************************************
  896. * USART1_SendOK_IQ
  897. * 串口1发送数据完毕后,会调用的中断函数,将发送标志置0
  898. * 此函数需在stm32f10x_it.c 的 DMA1_Channel4_IRQHandler()中被调用
  899. ***************************************************************************/
  900. void USART1_SendOK_IQ(void)
  901. {
  902. //判断是否为DMA发送完成中断
  903. if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
  904. {
  905. DMA_ClearITPendingBit(DMA1_IT_GL4);
  906. DMA_ClearFlag(DMA1_FLAG_TC4);
  907. DMA_Cmd (DMA1_Channel4,DISABLE);
  908. g_ucUart1Sending = 0; //数据发送完毕
  909. }
  910. //printf("USART1_SendOK_IQ\r\n");
  911. }
  912. /**************************************************************************
  913. * USART2_SendOK_IQ
  914. * 串口3发送数据完毕后,会调用的中断函数,将发送标志置0
  915. * 此函数需在stm32f10x_it.c 的 DMA1_Channel2_IRQHandler()中被调用
  916. ***************************************************************************/
  917. void USART2_SendOK_IQ(void)
  918. {
  919. //判断是否为DMA发送完成中断
  920. if(DMA_GetFlagStatus(DMA1_FLAG_TC7)==SET)
  921. {
  922. DMA_ClearITPendingBit(DMA1_IT_GL7);
  923. DMA_ClearFlag(DMA1_FLAG_TC7);
  924. DMA_Cmd (DMA1_Channel7,DISABLE);
  925. g_ucUart2Sending = 0; //数据发送完毕
  926. }
  927. //printf("USART3_SendOK_IQ\r\n");
  928. }
  929. #ifdef __GNUC__
  930. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  931. set to 'Yes') calls __io_putchar() */
  932. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  933. #else
  934. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  935. #endif /* __GNUC__ */
  936. /**
  937. * @brief Retargets the C library printf function to the USART.
  938. * @param None
  939. * @retval None
  940. */
  941. PUTCHAR_PROTOTYPE
  942. {
  943. /* Place your implementation of fputc here */
  944. /* e.g. write a character to the USART */
  945. USART_SendData(USART1, (uint8_t) ch);
  946. /* Loop until the end of transmission */
  947. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  948. {
  949. }
  950. return ch;
  951. }