12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- /*******************************************************************************************
- * File Name: Serial.c
- * Function Describe:device for serial
- * Relate Module:
- * Explain:
- 接收采用中断方式
- UART1: 连接尾插,用于与PC相连 另外,printf映射到UART1
- UART2: 连接GPS
- UART3: 连接Modem
- 全局变量g_ucUARTSel 调试选择控制,决定了3个串口的转发关系,其值通过以下3个宏控分别代表:
- COM_SEL_MCU 正常使用不转发,尾插用于MCU打印LOG或 MCU软件升级
- COM_SEL_GPS UART1<-->UART2,尾插用于GPS LOG打印
- COM_SEL_MODEM UART1<-->UART3,尾插用于MODEM AT momand 或模块软件升级
- * Writer: ShiLiangWen
- * Date: 2015.1.20
- ********************************************************************************************/
- #define THIS_FILE_ID 4
- /* Includes ------------------------------------------------------------------*/
- #include "includes.h"
- #define USART1_DR_Base 0x40013804
- #define USART2_DR_Base 0x40004404
- #define USART3_DR_Base (u32)&USART3->DR
- unsigned char g_ucUARTSel=0;//0--MCU 1--Modem 2--GPS
- //UART1 Trace
- unsigned char RxBuffer1[UART1_RX_BUFFER_SIZE];
- unsigned char TxBuffer1[UART1_TX_BUFFER_SIZE];
- static unsigned short rx1_ct;
- unsigned short g_usUart1RecvLen;
- unsigned char g_ucUart1Sending;
- //UART3 用于与GPS通讯
- unsigned char RxBuffer3[UART3_RX_BUFFER_SIZE];
- unsigned char TxBuffer3[UART3_TX_BUFFER_SIZE];
- static unsigned short rx3_ct;
- unsigned short g_usUart3RecvLen;
- //UART2 用于与MODEM通讯
- unsigned char RxBuffer2[UART2_RX_BUFFER_SIZE];
- unsigned char TxBuffer2[UART2_TX_BUFFER_SIZE];
- static unsigned short rx2_ct;
- unsigned short g_usRx2Len;
- unsigned short g_usRx2In;
- unsigned short g_usRx2Out;
- unsigned char g_ucUart2Sending;
- /********************************************************************
- *Uart1DMAInit
- *串口1 DMA设置
- *串口1 发送必须用通道4,接收必须用通道5
- *********************************************************************/
- void Uart1DMAInit(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- //启动DMA时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- #if UART1_TX_USE_DMA!=0
- //DMA 通道4用于UART1 TX
- DMA_DeInit(DMA1_Channel4);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;//内存地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA传输方向单向
- DMA_InitStructure.DMA_BufferSize = UART1_TX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
- DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_Low; //设置DMA的优先级别
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
- DMA_Init(DMA1_Channel4,&DMA_InitStructure);
- // //DMA中断设置
- // NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
- // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- // NVIC_Init(&NVIC_InitStructure);
- DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);//中断设置
- DMA_Cmd(DMA1_Channel4, DISABLE); //等到需要发送的时候再使能
- #endif
- //DMA 通道5用于UART1 RX
- DMA_DeInit(DMA1_Channel5);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1;//内存地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMA传输方向单向
- DMA_InitStructure.DMA_BufferSize = UART1_RX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
- DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
- DMA_Init(DMA1_Channel5,&DMA_InitStructure);
- //DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
- }
- /********************************************************************
- *Uart2DMAInit
- *串口2 DMA设置
- *串口2 发送必须用通道7,接收必须用通道6
- *********************************************************************/
- void Uart2DMAInit(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- //启动DMA时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- #if UART2_TX_USE_DMA==1
- //DMA 通道2用于UART3 TX
- DMA_DeInit(DMA1_Channel7);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR);//外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2;//内存地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//DMA传输方向单向
- DMA_InitStructure.DMA_BufferSize = UART3_TX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
- DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_High; //设置DMA的优先级别
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
- DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA_ITConfig(DMA1_Channel7,DMA_IT_TC,ENABLE);//中断设置
- #else
- DMA_ITConfig(DMA1_Channel7,DMA_IT_TC,DISABLE);
- #endif
- DMA_Cmd(DMA1_Channel7, DISABLE);
- //DMA 通道3用于UART3 RX
- DMA_DeInit(DMA1_Channel6);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR);//外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer2;//内存地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMA传输方向单向
- DMA_InitStructure.DMA_BufferSize = UART3_RX_BUFFER_SIZE; //设置DMA在传输时缓冲区的长度
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的外设递增模式
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //设置DMA的内存递增模式
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据字长
- DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的传输模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //设置DMA的优先级别
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//设置DMA的2个memory中的变量互相访问
- DMA_Init(DMA1_Channel6,&DMA_InitStructure);
- DMA_Cmd(DMA1_Channel6, DISABLE); //使能通道
-
- }
- /*********************************************************************************************
- *UART2RxTxISRHandler
- *串口2中断处理函数
- **********************************************************************************************/
- void UART2RxTxISRHandler(void)
- {
- __IO unsigned char uart_data;
- //----接收中断--------
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- uart_data = USART_ReceiveData(USART2); // Read one byte from the receive data register
- //Modem Trace
- if(g_ucUARTSel==COM_SEL_MODEM){
- USART_SendData(USART1, uart_data);
- USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
- return;
- }
- //MCU 处理Modem
- if(g_usRx2Len<UART2_RX_BUFFER_SIZE){
- RxBuffer2[g_usRx2In]=uart_data;
- if(++g_usRx2In>=UART2_RX_BUFFER_SIZE)g_usRx2In=0;
- g_usRx2Len++;
- }
-
- isr_sem_send(&ModemUartSem);
- USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志
- }
- if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
- {
- USART_ReceiveData(USART2); //
- USART_ClearFlag(USART2,USART_FLAG_ORE); //
- }
-
- }
- /**********************************************************************
- >112/r/n 最少6字节
- ************************************************************************/
- void Uart2RecvProcess(void)
- {
- unsigned short Rx2Len;
- unsigned short out;
- char preData[20];
- unsigned short i,j,len;
- char num[5];
- char data,flag;
-
-
- // //少于8字节不处理
- // if(g_usRx2Len<8)return;
-
- //缓冲区满,清空缓冲区
- if(g_usRx2Len>(UART2_RX_BUFFER_SIZE-2)){
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
- g_usRx2In=0;
- g_usRx2Len=0;
- g_usRx2Out=0;
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- SlwTrace(DEBUG,"[Rx2Buf Over]",1);
- return;
- }
- //锁定环形缓冲区当前状态,
- Rx2Len=g_usRx2Len;
-
- // USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
- // g_usRx2Len=g_usRx2Len-i;
- // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- // //少于8字节不处理
- // if(Rx2Len<4)return;
- //预读取前n字节 n最少4,最大16 为后续处理提供条件
- if(Rx2Len<16)len=Rx2Len;
- else len=16;
-
- out=g_usRx2Out;
- for(i=0;i<len;i++){
- preData[i]=RxBuffer2[out];
- if(++out>UART2_RX_BUFFER_SIZE)out=0;
- }
- RunMake(THIS_FILE_ID);
- //判断是否为OK
- //判断前8字节是否等于"+TCPRECV",如果等于则特殊处理,如果不等则找到"/r/n"作为结束符
- if(0!=ModemStrCmp(preData,"+TCP"))
- {
- //非"+TCPRECV",则直接找到最近的0x0A作为结束符,读取并形成一包消息
- out=g_usRx2Out;
- for(i=0;i<Rx2Len;i++)
- {
- if(RxBuffer2[out]==0x0A)break;
- if(++out>=UART2_RX_BUFFER_SIZE)out=0;
- }
-
- //123/n/0
-
- if(i<Rx2Len)
- {
-
- //找到了0x0A,从环形缓冲区中读取数据并拷贝到消息缓冲区,发送消息
- len=i+1;//消息长度
- RunMake(THIS_FILE_ID);
-
- if(len<(MODEM_AT_MSG_DATA_LEN-2))
- {
- sutAtm.MsgLen=len;
- for(i=0;i<len;i++)
- {
- sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
- if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
- }
- sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
-
- //更新g_usRx3Len,需要防止中断修改冲突
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
- g_usRx2Len=g_usRx2Len-len;
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
- RunMake(THIS_FILE_ID);
- //SlwTrace(INF,"/*********start*************/",1);
- SlwTrace(INF,sutAtm.MsgData,1);
-
- //发送消息
- sutAtm.MsgLen+=1;
- if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full1",1);
- return;
- }else
- {//消息队列的内存块长度不够,需要特殊处理,扔掉后面部分
- sutAtm.MsgLen=len;
- for(i=0;i<len;i++)
- {
- data=RxBuffer2[g_usRx2Out];
- if(i<(MODEM_AT_MSG_DATA_LEN-2))sutAtm.MsgData[i]=data;
- if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
- }
- sutAtm.MsgData[MODEM_AT_MSG_DATA_LEN-1]=0;//消息最后补0作为结束符
- //更新g_usRx3Len,需要防止中断修改冲突
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART2 RX IT
- g_usRx2Len=g_usRx2Len-len;
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART2 RX IT
- RunMake(THIS_FILE_ID);
- //发送消息
- if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full2",1);
- return;
- }
- }
- return;
- }
- //+TCPRECV:1,24,12
- if(Rx2Len<16)return;//最少16字节“+TCPRECV:0,1,d\r\n”
- out=g_usRx2Out;
- flag=0;//找到','的标志
- j=0;//用来存储xxx的下标,xxx范围"1" -> "9999"
- for(i=0;i<Rx2Len;i++){
- data=RxBuffer2[out];
- if(','==data){
- if(flag<1)flag++;
- else break;
- }else if(flag==1 && j<4){
- num[j++]=data;
- }
- if(++out>=UART2_RX_BUFFER_SIZE)out=0;
- }
- if(i>=Rx2Len){//未找到了2个','则直接忽略
- //SlwTrace(DEBUG,"<2<\r\n");
- return;
- }
- RunMake(THIS_FILE_ID);
- //找到了2个',' 计算xxx长度
- num[j]=0;
- len=atoi(num);
- if(len<10)j=1;
- else if(len<100)j=2;
- else if(len<1000)j=3;
- else j=4;
- len=len+j+14;
- if(Rx2Len<len){
- return;//长度不够,忽略
- }
- //长度够,产生消息
- if(len<(MODEM_AT_MSG_DATA_LEN-2)){
- sutAtm.MsgLen=len;
- for(i=0;i<len;i++){
- sutAtm.MsgData[i]=RxBuffer2[g_usRx2Out];
- if(++g_usRx2Out>=UART2_RX_BUFFER_SIZE)g_usRx2Out=0;
- }
- sutAtm.MsgData[i]=0;//消息最后补0作为结束符,方便打印
- //更新g_usRx3Len,需要防止中断修改冲突
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //DISABLE UART3 RX IT
- g_usRx2Len=g_usRx2Len-len;
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE UART3 RX IT
- //发送消息
- sutAtm.MsgLen +=1;
- if(0 != MsgQueuePost(sutAtm.MsgData,sutAtm.MsgLen)) SlwTrace(INF, "MQueue Full3",1);
- }else{
- snprintf(preData, sizeof(preData),"AtMsgBuf Over [%u]",i);
- SlwTrace(DEBUG,preData,1);
- return;
- }
- RunMake(THIS_FILE_ID);
- }
- /********************************************************************************
- *Uart1Send
- *串口1启动发送
- *********************************************************************************/
- /*
- void Uart1Send(char *txbuf,int len)
- {
- int i;
- unsigned char data;
- unsigned char last_data=~UART_TRANSFORM;
- //Send Head
- USART_SendData(USART1, UART_HEAD);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- for(i=0;i<len;i++){
- data= txbuf[i];
- if(UART_TRANSFORM == data){
- USART_SendData(USART1,UART_TRANSFORM );
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- USART_SendData(USART1,0x01 );
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }else if(UART_HEAD == data){
- USART_SendData(USART1,UART_TRANSFORM);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- USART_SendData(USART1,0x02 );
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }else if(UART_END == data){
- USART_SendData(USART1,UART_TRANSFORM);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- USART_SendData(USART1,0x03 );
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }else{
- USART_SendData(USART1,data);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }
- }
- //Send End
- USART_SendData(USART1, UART_END);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }
- */
- void Uart1Send(char *txbuf,int len)
- {
- int i;
- for(i=0;i<len;i++){
- USART_SendData(USART1,txbuf[i]);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }
- }
- void Uart2Send(unsigned char *txbuf,unsigned short len)
- {
- int i;
- for(i=0;i<len;i++){
- USART_SendData(USART2,txbuf[i]);
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
- }
- }
- /*************************************************************************************
- * 函数名:USART1_Send
- * 描述 :串口1发送数据函数 通过DMA实现
- * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
- * 输出 : 无
- * 举例 :Usart1_Send("Hello word!",11); Usart1_Send(Txd1Buffer,11);
- **************************************************************************************/
- void USART1_Send(char *pbuffer, unsigned short size)
- {
-
- #if UART1_TX_USE_DMA==1
- char buf[20];
- unsigned short i;
- unsigned short len;
- char *pData;
- if(size==0)return;
- //等待上一次发送完成
- while(g_ucUart1Sending!=0){
- os_dly_wait(1);
- }
-
- #if 0 //增加时间戳
- sprintf((char *)TxBuffer1,"%010u: ",os_time_get());
- if(size>(UART1_TX_BUFFER_SIZE-12))len=UART1_TX_BUFFER_SIZE-12;
- else len=size;
- len+=12;
- pData=(char *)&TxBuffer1[12];
- #else
- if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
- else len=size;
- pData=(char *)&TxBuffer1[0];
- #endif
-
-
- for(i=0;i<len;i++)pData[i]=pbuffer[i];
- DMA_Cmd (DMA1_Channel4,DISABLE);
- DMA1_Channel4->CMAR = (u32)TxBuffer1;
- DMA1_Channel4->CNDTR = len;
- g_ucUart1Sending=1;
- DMA_Cmd (DMA1_Channel4,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
- #endif
- }
- void USART1_SendNew(char *pbuffer, unsigned short size,char needEnd)
- {
-
- #if UART1_TX_USE_DMA==1
- char buf[20];
- unsigned short i;
- unsigned short len;
- char *pData;
- if(size==0)return;
- //等待上一次发送完成
- while(g_ucUart1Sending!=0){
- os_dly_wait(1);
- }
-
- if(size>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
- else len=size;
- pData=(char *)&TxBuffer1[0];
-
- for(i=0;i<len;i++)pData[i]=pbuffer[i];
- ///////////////////////////////
- if(0 != needEnd)
- {
- if(len <= (UART1_TX_BUFFER_SIZE-2))
- {
- pData[len++] = '\r';
- pData[len++] = '\n';
- }
- }
- ///////////////////////////////
- DMA_Cmd (DMA1_Channel4,DISABLE);
- DMA1_Channel4->CMAR = (u32)TxBuffer1;
- DMA1_Channel4->CNDTR = len;
- g_ucUart1Sending=1;
- DMA_Cmd (DMA1_Channel4,ENABLE);
- #endif
- }
- void USART1_SendWaitCompleted(void)
- {
- #if UART1_TX_USE_DMA==1
- while(g_ucUart1Sending!=0){
- os_dly_wait(1);
- }
- #endif
- }
- /*************************************************************************************
- * 函数名:USART2_Send
- * 描述 :串口1发送数据函数 通过DMA实现
- * 输入 :要发送数据的首地址,发送数据的个数。发送数据的地址任意,并不用固定为Txd1Buffer
- * 输出 : 无
- * 举例 :Usart3_Send("Hello word!",11); Usart3_Send(Txd3Buffer,11);
- **************************************************************************************/
- void USART2_Send(char *pbuffer, unsigned short size)
- {
- #if UART2_TX_USE_DMA==1
- unsigned short i;
- if(size==0 || size>UART2_TX_BUFFER_SIZE)return;
- //等待上一次发送完成
- while(g_ucUart2Sending!=0){
- os_dly_wait(1);
- }
- for(i=0;i<size;i++){
- TxBuffer2[i]=pbuffer[i];
- }
- DMA_Cmd (DMA1_Channel7,DISABLE);
- DMA1_Channel7->CMAR = (u32)TxBuffer2;
- DMA1_Channel7->CNDTR = size;
- g_ucUart2Sending=1;
- DMA_Cmd (DMA1_Channel7,ENABLE); //使能DMA,开始发送 //数据发送中 //灯亮
- #endif
- }
- void USART3_SendWaitCompleted(void)
- {
- #if UART3_TX_USE_DMA==1
- while(g_ucUart3Sending!=0){
- os_dly_wait(1);
- }
- #endif
- }
- /*********************************************************************************************
- *UART1RxTxISRHandler
- *串口1中断处理函数
- 通讯协议
- RxBuffer1接收缓冲区
- sutMsg.Uart1Recv为1表明接受到一包数据,需要应用层处理数据后将其清零以便接受新的数据包
- 每包数据均以UART_HEAD开头,UART_END结尾。如解决数据包内容刚好有与UART_HEAD或UART_END相等的值,则需要转义
- 转义字符为UART_TRANSFORM,当收到UART_TRANSFORM开头时将根据紧挨其后的字符决定转义
- 如UART_HEAD=0x7a UART_END=0x7b UART_TRANSFORM=0x7c
- 如拟发送数据为: 01 02 7a 03 04 7b 05 06 7c 08 09 经过转换后的数据包为:
- 01 02 7c 02 03 04 7c 03 05 06 7c 01 08 09
- 加上包头包尾后:
- 7a 01 02 7c 02 03 04 7c 03 05 06 7c 01 08 09 7b
- **********************************************************************************************
- void UART1RxTxISRHandler(void)
- {
- __IO unsigned char uart_data;
- __IO static unsigned char last_data;
- //----接收中断--------
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- uart_data = USART_ReceiveData(USART1);
- if(sutMsg.Uart1Recv){//上一批没处理,不接受新数据
- USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
- rx1_ct=0;
- return;
- }
-
- if(uart_data==UART_HEAD){//包头
- RxBuffer1[0]=UART_HEAD;
- rx1_ct=1;
- }else if(rx1_ct>=(UART1_RX_BUFFER_SIZE-1)){//超出接受缓冲区,丢掉此包数据,复位重新接收
- rx1_ct=0;
- }else if(rx1_ct>0){
- if(uart_data==UART_END){//包尾
- RxBuffer1[rx1_ct++]=uart_data;
- rx1_len=rx1_ct;
- rx1_ct=0;
- sutMsg.Uart1Recv=1;
- }else if(uart_data==UART_TRANSFORM){//转义字符,忽略
- }else{
- if(last_data==UART_TRANSFORM){
- if(0x01==uart_data)RxBuffer1[rx1_ct++]=UART_TRANSFORM;
- else if(0x02==uart_data)RxBuffer1[rx1_ct++]=UART_HEAD;
- else if(0x03==uart_data)RxBuffer1[rx1_ct++]=UART_END;
- }else{
- RxBuffer1[rx1_ct++]=uart_data;
- }
- }
- }
- last_data=uart_data;
- USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
- }
- }
- */
- /*********************************************************************************************
- *UART1RxTxISRHandler
- *串口1中断处理函数
- **********************************************************************************************/
- void UART1RxTxISRHandler(void)
- {
- __IO unsigned char uart_data;
- __IO static unsigned char uart_last_data=0;
- char *p;
- unsigned char i;
- uint32_t temp = 0;
- //----接收中断--------
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- uart_data = USART_ReceiveData(USART1);
- USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
-
- if(g_ucUARTSel==COM_SEL_MODEM){//Modem Trace
- USART_SendData(USART2, uart_data);
- }else if(g_ucUARTSel==COM_SEL_GPS){//GPS Trace
- USART_SendData(USART3, uart_data);
- }
- }
- //----接收空闲中断----
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET){
- temp = USART1->SR;//先读SR,再读DR
- temp = USART1->DR;//方可清USART_IT_IDLE标志
- temp = UART1_RX_BUFFER_SIZE - DMA1_Channel5->CNDTR;
- DMA_Cmd(DMA1_Channel5,DISABLE);
- //处理数据
- // if(temp>5 && RxBuffer1[0]=='G' && RxBuffer1[1]=='T' && RxBuffer1[2]=='+'){
- if(temp>3){
- g_usUart1RecvLen=temp;
- }else{
- g_usUart1RecvLen=0;
- DMA1_Channel5->CNDTR = UART1_RX_BUFFER_SIZE;//重设DMA传输数据长度
- DMA_Cmd(DMA1_Channel5,ENABLE);//打开DMA
- }
- }
- // //---异常中断---
- if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
- {
- USART_ReceiveData(USART1); //
- USART_ClearFlag(USART1,USART_FLAG_ORE); //
- }
- }
- /*******************************************************************************
- Uart1Init
- Use for Trace
- *******************************************************************************/
- void Uart1Init()
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
- /*
- * USART1_TX -> PA9 , USART1_RX -> PA10
- */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;// GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate =9600;// 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx;//USART_Mode_Rx |
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ClearFlag(USART1,USART_FLAG_TC);
- USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
- USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //使能USART1的发送DMA请求
- Uart1DMAInit();
-
- Uart1RxEnable(0);//1打开 如果为0 不使能该串口,可能导致串口接收异常而内存溢出,修改为默认不接收RX只发TX,按组合键才进入写号模式
- //FUN2 KEY Free 旋两格进入GT模式 现在0的时候要进入GT
- rx1_ct=0;
- g_usUart1RecvLen=0;
- g_ucUart1Sending=0;
- }
- void Uart1RxEnable(int en)
- {
- static unsigned char sucEn=0;
- USART_InitTypeDef USART_InitStructure;
-
- if(sucEn!=en)sucEn=en;
- else return;
-
- if(en){
- USART_InitStructure.USART_BaudRate =9600;// 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能USART1的接收DMA请求ENABLE
- DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
- }else{
- USART_InitStructure.USART_BaudRate =9600;// 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx;//
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
- //DMA_Cmd(DMA1_Channel5, DISABLE); //使能通道
- }
- }
- void Uart1RxEnable2(int en)
- {
- USART_InitTypeDef USART_InitStructure;
- if(en){
- USART_InitStructure.USART_BaudRate =115200;// 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能USART1的接收DMA请求ENABLE
- DMA_Cmd(DMA1_Channel5, ENABLE); //使能通道
- }else{
- USART_InitStructure.USART_BaudRate =115200;// 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx;//
- USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE); //使能USART1的接收DMA请求ENABLE
- //DMA_Cmd(DMA1_Channel5, DISABLE); //使能通道
- }
- }
- /***************************************************************************
- Uart2Init
- Use for MainBoard
- ****************************************************************************/
- void Uart2Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /*使能串口2使用的GPIO时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- /*使能串口2时钟*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
-
- /*串口2 RX管脚配置*/
- /* Configure USART2 Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*串口2 TX管脚配置*/
- /* Configure USART2 Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStructure);
- USART_ClearFlag(USART2,USART_FLAG_TC);
- USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
- USART_ITConfig(USART2, USART_IT_PE, ENABLE);
- USART_ITConfig(USART2, USART_IT_ERR, ENABLE);
- USART_Cmd(USART2, ENABLE);
- Uart2DMAInit();
- #if UART2_TX_USE_DMA==1
- USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //使能USART3 TX 的DMA请求
- #endif
- USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
-
- rx2_ct=0;
- g_usRx2Len=0;
- g_usRx2In=0;
- g_usRx2Out=0;
- g_ucUart2Sending=0;
- memset(RxBuffer2,0,sizeof(RxBuffer2));
- }
- void UART3RxTxISRHandler(void)
- {
- uint16_t i = 0;
- __IO unsigned char uart_data;
- __IO static unsigned char uart_last_data=0;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
- uart_data = USART_ReceiveData(USART3);
- if(g_ucUARTSel==COM_SEL_GPS){
- USART_SendData(USART1, uart_data);
- }else if(0==g_usUart3RecvLen){
- RxBuffer3[rx3_ct]= uart_data;
- if(RxBuffer3[0]!='$')rx3_ct=0;
- else rx3_ct++;
- if(rx3_ct>=(UART3_RX_BUFFER_SIZE-1))rx3_ct=0;
-
- if(uart_last_data==0x0d && uart_data==0x0a){ //收到回车换行符
- if((RxBuffer3[1]=='G' && RxBuffer3[3]=='R' && RxBuffer3[5]=='C')||(RxBuffer3[1]=='G' && RxBuffer3[4]=='G' && RxBuffer3[5]=='A')){//$GPRMC $GNGGA
- //Send message
- // SlwTrace(DEBUG,"GPRMCisg_usUart2RecvLen=rx2_ct?Send message?!\r\n");
- RxBuffer3[rx3_ct-1]=0;
- g_usUart3RecvLen=rx3_ct;
- }else{
- rx3_ct=0;
- }
- }
-
- }else{
- rx3_ct=0;
- }
- USART_ClearITPendingBit(USART3, USART_IT_RXNE); //Clean interrupt flag
- uart_last_data=uart_data;
- }
-
- if(USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET)
- {
- USART_ReceiveData(USART3); //
- USART_ClearFlag(USART3,USART_FLAG_ORE); //
- }
-
- }
- /***************************************************************************
- Uart3Init
- ****************************************************************************/
- void Uart3Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /*使能串口3使用的GPIO时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- /*使能串口3时钟*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
-
- /*串口3 RX管脚配置*/
- /* Configure USART3 Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /*串口3 TX管脚配置*/
- /* Configure USART2 Tx as alternate function push-pull */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_AF_PP;
- // GPIO_Init(GPIOB, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx;
- USART_Init(USART3, &USART_InitStructure);
-
- USART_ClearFlag(USART3,USART_FLAG_TC);
- USART_ITConfig(USART3, USART_IT_TXE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);//USART_IT_IDLE USART_IT_RXNE
- USART_Cmd(USART3, ENABLE);
-
- rx3_ct=0;
- g_usUart3RecvLen=0;
- memset(RxBuffer3, 0, sizeof(RxBuffer3));
- }
- /***********************************************************************************
- *
- ************************************************************************************/
- void SlwTrace(TRACE_TYPE type,char *buf,char needEnd)
- {
- int len;
- #ifndef SLW_DEBUG
- if(type==DEBUG)return;
- #endif
- if(g_ucUARTSel!=COM_SEL_MCU)return;
- #if UART1_TX_USE_DMA==0
- len=strlen(buf);
- if(len>256)len=256;
- Uart1Send(buf,len);
- #else
- len=strlen(buf);
- if(len>UART1_TX_BUFFER_SIZE)len=UART1_TX_BUFFER_SIZE;
- //USART1_Send(buf,len);
- USART1_SendNew(buf,len,needEnd);
- #endif
- }
- /*******************************************************************************
- ComSelect
- 调试口选择
- #define COM_SEL_MCU 0
- #define COM_SEL_GPS 1
- #define COM_SEL_MODEM 2
- *******************************************************************************/
- void ComSelect(unsigned char sel)
- {
- switch(sel)
- {
- case COM_SEL_MCU:
- SlwTrace(INF,"COM<-->MCU!",1);
- os_dly_wait(10);
- g_ucUARTSel=sel;
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
- USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
- break;
- case COM_SEL_GPS:
- SlwTrace(INF,"COM<-->GPS!",1);
- os_dly_wait(10);
- g_ucUARTSel=sel;
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
-
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART3, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART3,USART_DMAReq_Tx,DISABLE);
- USART_DMACmd(USART3,USART_DMAReq_Rx,DISABLE);
- break;
- case COM_SEL_MODEM:
- SlwTrace(INF,"COM<-->Modem!",1);
- os_dly_wait(10);
- g_ucUARTSel=sel;
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
- USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE);
- DMA_Cmd(DMA1_Channel4, DISABLE); //等到需要发送的时候再使能
- DMA_Cmd(DMA1_Channel5, DISABLE); //等到需要发送的时候再使能
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
- USART_DMACmd(USART2,USART_DMAReq_Rx,DISABLE);
- DMA_Cmd(DMA1_Channel6, DISABLE); //等到需要发送的时候再使能
- DMA_Cmd(DMA1_Channel7, DISABLE); //等到需要发送的时候再使能
- break;
- }
- }
- /**************************************************************************
- * USART1_SendOK_IQ
- * 串口1发送数据完毕后,会调用的中断函数,将发送标志置0
- * 此函数需在stm32f10x_it.c 的 DMA1_Channel4_IRQHandler()中被调用
- ***************************************************************************/
- void USART1_SendOK_IQ(void)
- {
- //判断是否为DMA发送完成中断
- if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
- {
- DMA_ClearITPendingBit(DMA1_IT_GL4);
- DMA_ClearFlag(DMA1_FLAG_TC4);
- DMA_Cmd (DMA1_Channel4,DISABLE);
- g_ucUart1Sending = 0; //数据发送完毕
- }
- //printf("USART1_SendOK_IQ\r\n");
- }
- /**************************************************************************
- * USART2_SendOK_IQ
- * 串口3发送数据完毕后,会调用的中断函数,将发送标志置0
- * 此函数需在stm32f10x_it.c 的 DMA1_Channel2_IRQHandler()中被调用
- ***************************************************************************/
- void USART2_SendOK_IQ(void)
- {
- //判断是否为DMA发送完成中断
- if(DMA_GetFlagStatus(DMA1_FLAG_TC7)==SET)
- {
- DMA_ClearITPendingBit(DMA1_IT_GL7);
- DMA_ClearFlag(DMA1_FLAG_TC7);
- DMA_Cmd (DMA1_Channel7,DISABLE);
- g_ucUart2Sending = 0; //数据发送完毕
- }
- //printf("USART3_SendOK_IQ\r\n");
- }
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- /**
- * @brief Retargets the C library printf function to the USART.
- * @param None
- * @retval None
- */
- PUTCHAR_PROTOTYPE
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART */
- USART_SendData(USART1, (uint8_t) ch);
- /* Loop until the end of transmission */
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
- {
-
- }
- return ch;
- }
|