123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- /*******************************************************************************************
- * 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
- unsigned char g_ucUART1RxMode=0;
- //UART1 Trace
- unsigned char RxBuffer1[UART1_RX_BUFFER_SIZE];
- static unsigned short rx1_ct;
- unsigned short g_usUart1RecvLen;
- unsigned char g_ucUart1Sending;
- //UART2 用于与MODEM通讯
- unsigned char RxBuffer2[UART2_RX_BUFFER_SIZE];
- static unsigned short rx2_ct;
- unsigned short g_usRx2Len;
- unsigned short g_usRx2In;
- unsigned short g_usRx2Out;
- unsigned char g_ucUart2Sending;
- //UART3 用于与GPS
- unsigned char RxBuffer3[UART3_RX_BUFFER_SIZE];
- unsigned short g_usRx3Len;
- unsigned short rx3_ct;
- /*********************************************************************************************
- *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;
- }
- if(g_ucUARTSel==COM_SEL_GPS){
- 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); //
- }
-
- }
- 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_usRx3Len){
- 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'){//$GPRMC
- //Send message
- RxBuffer3[rx3_ct-1]=0;
- g_usRx3Len=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); //
- }
- }
- /**********************************************************************
- +CSQ:1/r/n 最少8字节
- +POC:83000
- +ZIPRECV:
- +ZIPRECVU:
- +ZIPSTATUS:
- ************************************************************************/
- 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字节是否等于"+ZIPRECV",如果等于则特殊处理,如果不等则找到"/r/n"作为结束符
- if(0!=ModemStrCmp(preData,"+TCPRECV"))
- {
- //非"+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);
- //发送消息
- 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{
- sprintf(preData,"AtMsgBuf Over [%u]",i);
- SlwTrace(INF,preData,1);
- return;
- }
- RunMake(THIS_FILE_ID);
- }
- 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,int len)
- {
- int i;
- for(i=0;i<len;i++){
- USART_SendData(USART2,txbuf[i]);
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
- }
- }
- /*********************************************************************************************
- *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);
- }else{
- if(g_usUart1RecvLen==0)
- {
- RxBuffer1[rx1_ct++]=uart_data;
- if(rx1_ct >= UART1_RX_BUFFER_SIZE) rx1_ct=0;
- if(rx1_ct>=2)
- {
- if(uart_last_data=='\r' && uart_data == '\n')
- {
- g_usUart1RecvLen=rx1_ct;
- rx1_ct=0;
- }
- }
- uart_last_data=uart_data;
- }
- }
- }
- //---异常中断---
- if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
- {
- USART_ReceiveData(USART1); //
- USART_ClearFlag(USART1,USART_FLAG_ORE); //
- }
- }
- /*******************************************************************************
- Uart1Init
- Use for Trace
- *******************************************************************************/
- void Uart1Init(void)
- {
- 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 =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_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
-
- rx1_ct=0;
- g_usUart1RecvLen=0;
- g_ucUart1Sending=0;
- g_ucUART1RxMode=0;
- }
- void Uart1RxEnable(int en)
- {
- static unsigned char sucEn=0;
- USART_InitTypeDef USART_InitStructure;
- if(sucEn!=en)sucEn=en;
- else return;
-
- USART_DeInit(USART1);
- if(en){//使能串口1发送与接收,非DMA
- 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);
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
- }else{//使能串口1发送非DMA
- 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|USART_IT_RXNE, DISABLE); //USART_IT_IDLE USART_IT_RXNE
- }
- }
- /***************************************************************************
- Uart2Init
- ****************************************************************************/
- void Uart2Init(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- 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 USART3 Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
- 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_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 SlwTrace(TRACE_TYPE type,char *buf,char needEnd)
- {
- if(type==DEBUG)return;
- if(g_ucUARTSel!=COM_SEL_MCU)return;
- Uart1Send(buf,strlen(buf));
- if(needEnd!=0) Uart1Send("\r\n",2);
- }
- /*******************************************************************************
- 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, ENABLE);
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
- break;
- case COM_SEL_GPS:
- GPSRestart();
- SlwTrace(INF,"COM<-->Modem_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;
- }
- }
- void Uart3Init(void)
- {//GPS
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /*使能串口2使用的GPIO时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- /*使能串口2时钟*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- /*串口2 RX管脚配置*/
- /* Configure USART3 Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- 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_RXNE, ENABLE);
- USART_ITConfig(USART3, USART_IT_IDLE | USART_IT_TXE, DISABLE);
- USART_Cmd(USART3, ENABLE);
-
- memset(RxBuffer3,0,sizeof(RxBuffer3));
- g_usRx3Len=0;
- }
- /**************************************************************************
- * 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
- * 串口2发送数据完毕后,会调用的中断函数,将发送标志置0
- * 此函数需在stm32f10x_it.c 的 DMA1_Channel7_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; //数据发送完毕
- }
- }
- #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;
- }
|