123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- /*******************************************************************************************
- * 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];
- 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++;
- if(uart_data==0x0a)isr_sem_send(&ModemUartSem);
- }
- //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;
- }
- }
- 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中断处理函数
- **********************************************************************************************/
- 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);
- 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);
- }else {
-
- if(g_usUart1RecvLen!=0){//上一包数据未处理完,不处理新来的数据,直接扔掉!
- rx1_ct=0;
- RxBuffer1[0]=0;
- return;
- }
-
- RxBuffer1[rx1_ct++]=uart_data;
- if(uart_last_data==0x0d &&uart_data==0x0a){
- if(RxBuffer1[0]=='>'||RxBuffer1[0]=='A'||RxBuffer1[0]=='G'){
- g_usUart1RecvLen=rx1_ct;
- RxBuffer1[rx1_ct]=0;
- }else{
- rx1_ct=0;
- RxBuffer1[0]=0;
- }
- }
- }
- if(rx1_ct>=(UART1_RX_BUFFER_SIZE-1)){//数据超过缓冲区的数据,过滤不处理
- rx1_ct=0;
- g_usUart1RecvLen=0;
- }
- }
- uart_last_data=uart_data;
- USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志
- // //---异常中断---
- if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
- {
- USART_ReceiveData(USART1); //
- USART_ClearFlag(USART1,USART_FLAG_ORE); //
- }
- }
- /*******************************************************************************
- Uart1Init
- Use for Trace
- *******************************************************************************/
- void Uart1Init()
- {
-
- 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_Rx | USART_Mode_Tx;
- 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
- }
- 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_RXNE, ENABLE); //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_RXNE, DISABLE); //ENABLE
- }
- }
- 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_RXNE, ENABLE); //ENABLE //USART_IT_IDLE USART_IT_RXNE
- }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_RXNE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- }
- }
- /***************************************************************************
- 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);
- 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, DISABLE); //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;
- #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_ITConfig(USART2, USART_IT_RXNE, DISABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //USART_IT_IDLE USART_IT_RXNE
- 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_ITConfig(USART3, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART3, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- 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_ITConfig(USART2, USART_IT_RXNE, ENABLE); //ENABLE DISABLE
- USART_ITConfig(USART2, USART_IT_IDLE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- 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;
- }
|