在STM32上实现使用DMA+USART3传输数据
生活随笔
收集整理的這篇文章主要介紹了
在STM32上实现使用DMA+USART3传输数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
串口的配置的文檔如下所示
出口3的頭文件
串口3的c文件
#include "delay.h" #include "usart3.h" #include "stdarg.h" #include "stdio.h" #include "string.h" u16 USART3_RX_STA=0; void USART3_IRQHandler(void) {u8 res; if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//?óê?μ?êy?Y{ res =USART_ReceiveData(USART3); if(USART3_RX_STA<USART3_MAX_RECV_LEN) //?1?éò??óê?êy?Y{TIM_SetCounter(TIM4,0);//??êy?÷???? if(USART3_RX_STA==0)TIM4_Set(1); //ê1?ü?¨ê±?÷4μ??D?? USART3_RX_BUF[USART3_RX_STA++]=res; //?????óê?μ?μ??μ }else {USART3_RX_STA|=1<<15; //????±ê???óê?íê3é} } } //3?ê??ˉIO ′??ú2 //pclk1:PCLK1ê±?ó?μ?ê(Mhz) //bound:2¨ì??ê void USART3_Init(u32 bound) { NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOAê±?óRCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);USART_DeInit(USART3); //?′??′??ú2//USART2_TX PA.2GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.2GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?GPIO_Init(GPIOB, &GPIO_InitStructure); //3?ê??ˉPA2//USART2_RX PA.3GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?GPIO_Init(GPIOB, &GPIO_InitStructure); //3?ê??ˉPA3USART_InitStructure.USART_BaudRate = bound;//ò?°?éè???a9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá÷????USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ê?·¢?£ê?USART_Init(USART3, &USART_InitStructure); //3?ê??ˉ′??ú 2//2¨ì??êéè??// USART2->BRR=(pclk1*1000000)/(bound);// 2¨ì??êéè?? //USART2->CR1|=0X200C; //1??í£?1,?TD£?é??.USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); //ê1?ü′??ú2μ?DMA·¢?íUART_DMA_Config(DMA1_Channel2,(u32)&USART3->DR,(u32)USART3_TX_BUF);//DMA1í¨μà7,íaéè?a′??ú2,′?′¢?÷?aUSART2_TX_BUF USART_Cmd(USART3, ENABLE); //ê1?ü′??ú #ifdef USART3_RX_EN //è?1?ê1?üá??óê?//ê1?ü?óê??D??USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//?a???D?? NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//?à??ó??è??3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óó??è??3NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1?üNVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷TIM4_Init(99,7199); //10ms?D??USART3_RX_STA=0; //??á?TIM4_Set(0); //1?±??¨ê±?÷4 #endif } //′??ú2,printf oˉêy //è·±£ò?′?·¢?íêy?Y2?3?1yUSART2_MAX_SEND_LEN×??ú void u3_printf(char* fmt,...) { va_list ap;va_start(ap,fmt);sprintf((char*)USART3_TX_BUF,fmt,ap);va_end(ap);while(DMA_GetCurrDataCounter(DMA1_Channel2)!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,strlen((const char*)USART3_TX_BUF)); //í¨1ydma·¢?í3?è¥ } //?¨ê±?÷4?D??·t??3ìDò void TIM4_IRQHandler(void) { if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//ê??üD??D??{ USART3_RX_STA|=1<<15; //±ê???óê?íê3éTIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //??3yTIMx?üD??D??±ê?? TIM4_Set(0); //1?±?TIM4 } } //éè??TIM4μ??a1? //sta:0£?1?±?;1,?a??; void TIM4_Set(u8 sta) {if(sta){TIM_SetCounter(TIM4,0);//??êy?÷????TIM_Cmd(TIM4, ENABLE); //ê1?üTIMx }else TIM_Cmd(TIM4, DISABLE);//1?±??¨ê±?÷4 } //í¨ó??¨ê±?÷?D??3?ê??ˉ //?aà?ê????????aAPB1μ?2±?£???APB1?a36M //arr£o×??ˉ??×°?μ?£ //psc£oê±?ó?¤·??μêy void TIM4_Init(u16 arr,u16 psc) { NVIC_InitTypeDef NVIC_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ê±?óê1?ü//TIM4ê±?óê1?ü //?¨ê±?÷TIM33?ê??ˉTIM_TimeBaseStructure.TIM_Period = arr; //éè???ú??ò????üD?ê??t×°è????ˉμ?×??ˉ??×°????′??÷?ü?úμ??μ TIM_TimeBaseStructure.TIM_Prescaler =psc; //éè??ó?à′×÷?aTIMxê±?ó?μ?ê3yêyμ??¤·??μ?μTIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éè??ê±?ó·???:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?òé???êy?£ê?TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //?ù?Y???¨μ?2?êy3?ê??ˉTIMxμ?ê±???ùêyμ¥??TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //ê1?ü???¨μ?TIM4?D??,?êDí?üD??D??NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//?à??ó??è??3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×óó??è??3NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1?üNVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷} #endif ///////////////////////////////////////USART2 DMA·¢?í????2?·?////////////////////////////////// //DMA1μ??÷í¨μà???? //?aà?μ?′?ê?D?ê?ê?1ì?¨μ?,?aμ?òa?ù?Y2?í?μ??é??à′DT?? //′ó′?′¢?÷->íaéè?£ê?/8??êy?Y?í?è/′?′¢?÷??á??£ê? //DMA_CHx:DMAí¨μàCHx //cpar:íaéèμ??· //cmar:′?′¢?÷μ??· void UART_DMA_Config(DMA_Channel_TypeDef*DMA_CHx,u32 cpar,u32 cmar) {DMA_InitTypeDef DMA_InitStructure;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //ê1?üDMA′?ê?DMA_DeInit(DMA_CHx); //??DMAμ?í¨μà1??′??÷??éè?aè±ê??μDMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //DMAíaéèADC?ùμ??·DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //DMA?ú′??ùμ??·DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //êy?Y′?ê?·??ò£?′ó?ú′??áè?·¢?íμ?íaéèDMA_InitStructure.DMA_BufferSize = 0; //DMAí¨μàμ?DMA?o′?μ?′óD?DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //íaéèμ??·??′??÷2?±?DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //?ú′?μ??·??′??÷μY??DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //êy?Y?í?è?a8??DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //êy?Y?í?è?a8??DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //1¤×÷?ú?y3£?o′??£ê?DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMAí¨μà xóμóD?Dó??è?? DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMAí¨μàx??óDéè???a?ú′?μ??ú′?′?ê?DMA_Init(DMA_CHx, &DMA_InitStructure); //?ù?YDMA_InitStruct?D???¨μ?2?êy3?ê??ˉDMAμ?í¨μàUSART1_Tx_DMA_Channel?ù±êê?μ???′??÷ } //?a??ò?′?DMA′?ê? void UART_DMA_Enable(DMA_Channel_TypeDef*DMA_CHx,u8 len) {DMA_Cmd(DMA_CHx, DISABLE ); //1?±? ??ê?μ?í¨μà DMA_SetCurrDataCounter(DMA_CHx,len);//DMAí¨μàμ?DMA?o′?μ?′óD? DMA_Cmd(DMA_CHx, ENABLE); //?a??DMA′?ê? } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////gps文件
#include "gps.h" #include "led.h" #include "delay.h" #include "usart3.h" #include "stdio.h" #include "stdarg.h" #include "string.h" #include "math.h" ////////////////////////////////////////////////////////////////////////////////// //±?3ìDò??1??§?°ê1ó?£??′?-×÷??Dí?é£?2?μ?ó?óú???üè?o?ó?í? //ALIENTEK STM32?a·¢°? //ATK-NEO-6M GPS?£?é?y?ˉ′ú?? //?yμ??-×ó@ALIENTEK //??ê???ì3:www.openedv.com //DT??è??ú:2014/3/30 //°?±?£oV2.0 //°?è¨?ùóD£?μá°?±????£ //Copyright(C) 1??YêDD?òíμ?×ó????óD?T1??? 2009-2019 //All rights reserved //******************************************************************************** //V2.0 DT???μ?÷ 20140330 //1,ìí?óUblox_Cfg_Cfg_Saveoˉêy //2,ìí?óUblox_Cfg_Msgoˉêy //3,ìí?óUblox_Cfg_Prtoˉêy. ////////////////////////////////////////////////////////////////////////////////// //′óbufà???μ?μ?μúcx???oo??ù?úμ????? //·μ???μ:0~0XFE,′ú±í?oo??ù?ú????μ???ò?. // 0XFF,′ú±í2?′??úμúcx???oo? u8 NMEA_Comma_Pos(u8 *buf,u8 cx) { u8 *p=buf;while(cx){ if(*buf=='*'||*buf<' '||*buf>'z')return 0XFF;//ó?μ?'*'?ò??·?·¨×?·?,?ò2?′??úμúcx???oo?if(*buf==',')cx--;buf++;}return buf-p; } //m^noˉêy //·μ???μ:m^n′?·?. u32 NMEA_Pow(u8 m,u8 n) {u32 result=1; while(n--)result*=m; return result; } //str×a???aêy×?,ò?','?ò??'*'?áê? //buf:êy×?′?′¢?? //dx:D?êyμ???êy,·μ????μ÷ó?oˉêy //·μ???μ:×a??oóμ?êy?μ int NMEA_Str2num(u8 *buf,u8*dx) {u8 *p=buf;u32 ires=0,fres=0;u8 ilen=0,flen=0,i;u8 mask=0;int res;while(1) //μ?μ???êyoíD?êyμ?3¤?è{if(*p=='-'){mask|=0X02;p++;}//ê??oêyif(*p==','||(*p=='*'))break;//ó?μ??áê?á?if(*p=='.'){mask|=0X01;p++;}//ó?μ?D?êyμ?á?else if(*p>'9'||(*p<'0')) //óD·?·¨×?·?{ ilen=0;flen=0;break;} if(mask&0X01)flen++;else ilen++;p++;}if(mask&0X02)buf++; //è¥μ??oo?for(i=0;i<ilen;i++) //μ?μ???êy2?·?êy?Y{ ires+=NMEA_Pow(10,ilen-1-i)*(buf[i]-'0');}if(flen>5)flen=5; //×??àè?5??D?êy*dx=flen; //D?êyμ???êyfor(i=0;i<flen;i++) //μ?μ?D?êy2?·?êy?Y{ fres+=NMEA_Pow(10,flen-1-i)*(buf[ilen+1+i]-'0');} res=ires*NMEA_Pow(10,flen)+fres;if(mask&0X02)res=-res; return res; } //·???GPGSVD??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void NMEA_GPGSV_Analysis(nmea_msg *gpsx,u8 *buf) {u8 *p,*p1,dx;u8 len,i,j,slx=0;u8 posx; p=buf;p1=(u8*)strstr((const char *)p,"$GPGSV");len=p1[7]-'0'; //μ?μ?GPGSVμ?ì?êyposx=NMEA_Comma_Pos(p1,3); //μ?μ??é???àD?×üêyif(posx!=0XFF)gpsx->svnum=NMEA_Str2num(p1+posx,&dx);for(i=0;i<len;i++){ p1=(u8*)strstr((const char *)p,"$GPGSV"); for(j=0;j<4;j++){ posx=NMEA_Comma_Pos(p1,4+j*4);if(posx!=0XFF)gpsx->slmsg[slx].num=NMEA_Str2num(p1+posx,&dx); //μ?μ??àD?±ào?else break; posx=NMEA_Comma_Pos(p1,5+j*4);if(posx!=0XFF)gpsx->slmsg[slx].eledeg=NMEA_Str2num(p1+posx,&dx);//μ?μ??àD????? else break;posx=NMEA_Comma_Pos(p1,6+j*4);if(posx!=0XFF)gpsx->slmsg[slx].azideg=NMEA_Str2num(p1+posx,&dx);//μ?μ??àD?·?????else break; posx=NMEA_Comma_Pos(p1,7+j*4);if(posx!=0XFF)gpsx->slmsg[slx].sn=NMEA_Str2num(p1+posx,&dx); //μ?μ??àD?D???±èelse break;slx++; } p=p1+1;//?D??μ???ò???GPGSVD??¢} } //·???GPGGAD??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void NMEA_GPGGA_Analysis(nmea_msg *gpsx,u8 *buf) {u8 *p1,dx; u8 posx; p1=(u8*)strstr((const char *)buf,"$GPGGA");posx=NMEA_Comma_Pos(p1,6); //μ?μ?GPS×′ì?if(posx!=0XFF)gpsx->gpssta=NMEA_Str2num(p1+posx,&dx); posx=NMEA_Comma_Pos(p1,7); //μ?μ?ó?óú?¨??μ??àD?êyif(posx!=0XFF)gpsx->posslnum=NMEA_Str2num(p1+posx,&dx); posx=NMEA_Comma_Pos(p1,9); //μ?μ?o£°????èif(posx!=0XFF)gpsx->altitude=NMEA_Str2num(p1+posx,&dx); } //·???GPGSAD??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void NMEA_GPGSA_Analysis(nmea_msg *gpsx,u8 *buf) {u8 *p1,dx; u8 posx; u8 i; p1=(u8*)strstr((const char *)buf,"$GPGSA");posx=NMEA_Comma_Pos(p1,2); //μ?μ??¨??ààDíif(posx!=0XFF)gpsx->fixmode=NMEA_Str2num(p1+posx,&dx); for(i=0;i<12;i++) //μ?μ??¨???àD?±ào?{posx=NMEA_Comma_Pos(p1,3+i); if(posx!=0XFF)gpsx->possl[i]=NMEA_Str2num(p1+posx,&dx);else break; } posx=NMEA_Comma_Pos(p1,15); //μ?μ?PDOP???????èòò×óif(posx!=0XFF)gpsx->pdop=NMEA_Str2num(p1+posx,&dx); posx=NMEA_Comma_Pos(p1,16); //μ?μ?HDOP???????èòò×óif(posx!=0XFF)gpsx->hdop=NMEA_Str2num(p1+posx,&dx); posx=NMEA_Comma_Pos(p1,17); //μ?μ?VDOP???????èòò×óif(posx!=0XFF)gpsx->vdop=NMEA_Str2num(p1+posx,&dx); } //·???GPRMCD??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void NMEA_GPRMC_Analysis(nmea_msg *gpsx,u8 *buf) {u8 *p1,dx; u8 posx; u32 temp; float rs; p1=(u8*)strstr((const char *)buf,"GPRMC");//"$GPRMC",?-3£óD&oíGPRMC·??aμ??é??,1ê???D??GPRMC.posx=NMEA_Comma_Pos(p1,1); //μ?μ?UTCê±??if(posx!=0XFF){temp=NMEA_Str2num(p1+posx,&dx)/NMEA_Pow(10,dx); //μ?μ?UTCê±??,è¥μ?msgpsx->utc.hour=temp/10000;gpsx->utc.min=(temp/100)%100;gpsx->utc.sec=temp%100; } posx=NMEA_Comma_Pos(p1,3); //μ?μ??3?èif(posx!=0XFF){temp=NMEA_Str2num(p1+posx,&dx); gpsx->latitude=temp/NMEA_Pow(10,dx+2); //μ?μ???rs=temp%NMEA_Pow(10,dx+2); //μ?μ?' gpsx->latitude=gpsx->latitude*NMEA_Pow(10,5)+(rs*NMEA_Pow(10,5-dx))/60;//×a???a?? }posx=NMEA_Comma_Pos(p1,4); //???3?1ê?±±?3 if(posx!=0XFF)gpsx->nshemi=*(p1+posx); posx=NMEA_Comma_Pos(p1,5); //μ?μ??-?èif(posx!=0XFF){ temp=NMEA_Str2num(p1+posx,&dx); gpsx->longitude=temp/NMEA_Pow(10,dx+2); //μ?μ???rs=temp%NMEA_Pow(10,dx+2); //μ?μ?' gpsx->longitude=gpsx->longitude*NMEA_Pow(10,5)+(rs*NMEA_Pow(10,5-dx))/60;//×a???a?? }posx=NMEA_Comma_Pos(p1,6); //???-?1ê??÷?-if(posx!=0XFF)gpsx->ewhemi=*(p1+posx); posx=NMEA_Comma_Pos(p1,9); //μ?μ?UTCè??úif(posx!=0XFF){temp=NMEA_Str2num(p1+posx,&dx); //μ?μ?UTCè??úgpsx->utc.date=temp/10000;gpsx->utc.month=(temp/100)%100;gpsx->utc.year=2000+temp%100; } } //·???GPVTGD??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void NMEA_GPVTG_Analysis(nmea_msg *gpsx,u8 *buf) {u8 *p1,dx; u8 posx; p1=(u8*)strstr((const char *)buf,"$GPVTG"); posx=NMEA_Comma_Pos(p1,7); //μ?μ?μ????ù?êif(posx!=0XFF){gpsx->speed=NMEA_Str2num(p1+posx,&dx);if(dx<3)gpsx->speed*=NMEA_Pow(10,3-dx); //è·±£à?′ó1000±?} } //ìáè?NMEA-0183D??¢ //gpsx:nmeaD??¢?á11ì? //buf:?óê?μ?μ?GPSêy?Y?o3???ê×μ??· void GPS_Analysis(nmea_msg *gpsx,u8 *buf) {NMEA_GPGSV_Analysis(gpsx,buf); //GPGSV?a??NMEA_GPGGA_Analysis(gpsx,buf); //GPGGA?a?? NMEA_GPGSA_Analysis(gpsx,buf); //GPGSA?a??NMEA_GPRMC_Analysis(gpsx,buf); //GPRMC?a??NMEA_GPVTG_Analysis(gpsx,buf); //GPVTG?a?? }//GPSD£?éoí???? //buf:êy?Y?o′???ê×μ??· //len:êy?Y3¤?è //cka,ckb:á???D£?é?á1?. void Ublox_CheckSum(u8 *buf,u16 len,u8* cka,u8*ckb) {u16 i;*cka=0;*ckb=0;for(i=0;i<len;i++){*cka=*cka+buf[i];*ckb=*ckb+*cka;} } /////////////////////////////////////////UBLOX ????′ú??///////////////////////////////////// //?ì2éCFG?????′DD?é?? //·μ???μ:0,ACK3é1| // 1,?óê?3?ê±′í?ó // 2,??óD?òμ?í?2?×?·? // 3,?óê?μ?NACKó|′e u8 Ublox_Cfg_Ack_Check(void) { u16 len=0,i;u8 rval=0;while((USART3_RX_STA&0X8000)==0 && len<100)//μè′y?óê?μ?ó|′e {len++;delay_ms(5);} if(len<250) //3?ê±′í?ó.{len=USART3_RX_STA&0X7FFF; //′?′??óê?μ?μ?êy?Y3¤?è for(i=0;i<len;i++)if(USART3_RX_BUF[i]==0XB5)break;//2é?òí?2?×?·? 0XB5if(i==len)rval=2; //??óD?òμ?í?2?×?·?else if(USART3_RX_BUF[i+3]==0X00)rval=3;//?óê?μ?NACKó|′eelse rval=0; //?óê?μ?ACKó|′e}else rval=1; //?óê?3?ê±′í?óUSART3_RX_STA=0; //??3y?óê?return rval; } //????±£′? //??μ±?°????±£′??úía2?EEPROMà??? //·μ???μ:0,?′DD3é1|;1,?′DDê§°ü. u8 Ublox_Cfg_Cfg_Save(void) {u8 i;_ublox_cfg_cfg *cfg_cfg=(_ublox_cfg_cfg *)USART3_TX_BUF;cfg_cfg->header=0X62B5; //cfg headercfg_cfg->id=0X0906; //cfg cfg idcfg_cfg->dlength=13; //êy?Y??3¤?è?a13??×??ú. cfg_cfg->clearmask=0; //??3y?ú???a0cfg_cfg->savemask=0XFFFF; //±£′??ú???a0XFFFFcfg_cfg->loadmask=0; //?ó???ú???a0 cfg_cfg->devicemask=4; //±£′??úEEPROMà??? Ublox_CheckSum((u8*)(&cfg_cfg->id),sizeof(_ublox_cfg_cfg)-4,&cfg_cfg->cka,&cfg_cfg->ckb);while(DMA1_Channel2->CNDTR!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,sizeof(_ublox_cfg_cfg)); //í¨1ydma·¢?í3?è¥for(i=0;i<6;i++)if(Ublox_Cfg_Ack_Check()==0)break; //EEPROMD′è?Dèòa±è????ê±??,?ùò?á?D??D???à′?return i==6?1:0; } //????NMEAê?3?D??¢??ê? //msgid:òa2ù×÷μ?NMEA???¢ì???,??ì???????μ?2?êy±í // 00,GPGGA;01,GPGLL;02,GPGSA; // 03,GPGSV;04,GPRMC;05,GPVTG; // 06,GPGRS;07,GPGST;08,GPZDA; // 09,GPGBS;0A,GPDTM;0D,GPGNS; //uart1set:0,ê?3?1?±?;1,ê?3??a??. //·μ???μ:0,?′DD3é1|;????,?′DDê§°ü. u8 Ublox_Cfg_Msg(u8 msgid,u8 uart1set) {_ublox_cfg_msg *cfg_msg=(_ublox_cfg_msg *)USART3_TX_BUF;cfg_msg->header=0X62B5; //cfg headercfg_msg->id=0X0106; //cfg msg idcfg_msg->dlength=8; //êy?Y??3¤?è?a8??×??ú. cfg_msg->msgclass=0XF0; //NMEA???¢cfg_msg->msgid=msgid; //òa2ù×÷μ?NMEA???¢ì???cfg_msg->iicset=1; //??è??a??cfg_msg->uart1set=uart1set; //?a1?éè??cfg_msg->uart2set=1; //??è??a??cfg_msg->usbset=1; //??è??a??cfg_msg->spiset=1; //??è??a??cfg_msg->ncset=1; //??è??a?? Ublox_CheckSum((u8*)(&cfg_msg->id),sizeof(_ublox_cfg_msg)-4,&cfg_msg->cka,&cfg_msg->ckb);while(DMA1_Channel2->CNDTR!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,sizeof(_ublox_cfg_msg)); //í¨1ydma·¢?í3?è¥return Ublox_Cfg_Ack_Check(); } //????NMEAê?3?D??¢??ê? //baudrate:2¨ì??ê,4800/9600/19200/38400/57600/115200/230400 //·μ???μ:0,?′DD3é1|;????,?′DDê§°ü(?aà?2??á·μ??0á?) u8 Ublox_Cfg_Prt(u32 baudrate) {_ublox_cfg_prt *cfg_prt=(_ublox_cfg_prt *)USART3_TX_BUF;cfg_prt->header=0X62B5; //cfg headercfg_prt->id=0X0006; //cfg prt idcfg_prt->dlength=20; //êy?Y??3¤?è?a20??×??ú. cfg_prt->portid=1; //2ù×÷′??ú1cfg_prt->reserved=0; //±£á?×??ú,éè???a0cfg_prt->txready=0; //TX Readyéè???a0cfg_prt->mode=0X08D0; //8??,1??í£?1??,?TD£?é??cfg_prt->baudrate=baudrate; //2¨ì??êéè??cfg_prt->inprotomask=0X0007;//0+1+2cfg_prt->outprotomask=0X0007;//0+1+2cfg_prt->reserved4=0; //±£á?×??ú,éè???a0cfg_prt->reserved5=0; //±£á?×??ú,éè???a0 Ublox_CheckSum((u8*)(&cfg_prt->id),sizeof(_ublox_cfg_prt)-4,&cfg_prt->cka,&cfg_prt->ckb);while(DMA1_Channel2->CNDTR!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,sizeof(_ublox_cfg_prt)); //í¨1ydma·¢?í3?è¥delay_ms(200); //μè′y·¢?ííê3é USART3_Init( baudrate); //??D?3?ê??ˉ′??ú2 return Ublox_Cfg_Ack_Check();//?aà?2??á·′??0,òò?aUBLOX·¢??à′μ?ó|′e?ú′??ú??D?3?ê??ˉμ?ê±oòò??-±??a?úá?. } //????UBLOX NEO-6μ?ê±?ó??3?ê?3? //interval:??3?????(us) //length:??3??í?è(us) //status:??3?????:1,??μ???óDD§;0,1?±?;-1,μíμ???óDD§. //·μ???μ:0,·¢?í3é1|;????,·¢?íê§°ü. u8 Ublox_Cfg_Tp(u32 interval,u32 length,signed char status) {_ublox_cfg_tp *cfg_tp=(_ublox_cfg_tp *)USART3_TX_BUF;cfg_tp->header=0X62B5; //cfg headercfg_tp->id=0X0706; //cfg tp idcfg_tp->dlength=20; //êy?Y??3¤?è?a20??×??ú.cfg_tp->interval=interval; //??3?????,uscfg_tp->length=length; //??3??í?è,uscfg_tp->status=status; //ê±?ó??3?????cfg_tp->timeref=0; //2???UTC ê±??cfg_tp->flags=0; //flags?a0cfg_tp->reserved=0; //±£á????a0cfg_tp->antdelay=820; //ìì???óê±?a820nscfg_tp->rfdelay=0; //RF?óê±?a0nscfg_tp->userdelay=0; //ó??§?óê±?a0nsUblox_CheckSum((u8*)(&cfg_tp->id),sizeof(_ublox_cfg_tp)-4,&cfg_tp->cka,&cfg_tp->ckb);while(DMA1_Channel2->CNDTR!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,sizeof(_ublox_cfg_tp)); //í¨1ydma·¢?í3?è¥return Ublox_Cfg_Ack_Check(); } //????UBLOX NEO-6μ??üD??ù?ê //measrate:2aá?ê±??????£?μ¥???ams£?×?éù2??üD?óú200ms£¨5Hz£? //reftime:2???ê±??£?0=UTC Time£?1=GPS Time£¨ò?°?éè???a1£? //·μ???μ:0,·¢?í3é1|;????,·¢?íê§°ü. u8 Ublox_Cfg_Rate(u16 measrate,u8 reftime) {_ublox_cfg_rate *cfg_rate=(_ublox_cfg_rate *)USART3_TX_BUF;if(measrate<200)return 1; //D?óú200ms£??±?óí?3?cfg_rate->header=0X62B5; //cfg headercfg_rate->id=0X0806; //cfg rate idcfg_rate->dlength=6; //êy?Y??3¤?è?a6??×??ú.cfg_rate->measrate=measrate;//??3?????,uscfg_rate->navrate=1; //μ?o??ù?꣨?ü?ú£?£?1ì?¨?a1cfg_rate->timeref=reftime; //2???ê±???aGPSê±??Ublox_CheckSum((u8*)(&cfg_rate->id),sizeof(_ublox_cfg_rate)-4,&cfg_rate->cka,&cfg_rate->ckb);while(DMA1_Channel2->CNDTR!=0); //μè′yí¨μà7′?ê?íê3é UART_DMA_Enable(DMA1_Channel2,sizeof(_ublox_cfg_rate));//í¨1ydma·¢?í3?è¥return Ublox_Cfg_Ack_Check(); } #ifndef __GPS_H #define __GPS_H #include "sys.h" ////////////////////////////////////////////////////////////////////////////////// //±?3ìDò??1??§?°ê1ó?£??′?-×÷??Dí?é£?2?μ?ó?óú???üè?o?ó?í? //ALIENTEK STM32?a·¢°? //ATK-NEO-6M GPS?£?é?y?ˉ′ú?? //?yμ??-×ó@ALIENTEK //??ê???ì3:www.openedv.com //DT??è??ú:2014/3/30 //°?±?£oV2.0 //°?è¨?ùóD£?μá°?±????£ //Copyright(C) 1??YêDD?òíμ?×ó????óD?T1??? 2009-2019 //All rights reserved //******************************************************************************** //V2.0 DT???μ?÷ 20140330 //1,ìí?óUblox_Cfg_Cfg_Saveoˉêy //2,ìí?óUblox_Cfg_Msgoˉêy //3,ìí?óUblox_Cfg_Prtoˉêy. ////////////////////////////////////////////////////////////////////////////////// //GPS NMEA-0183D-òé??òa2?êy?á11ì??¨ò? //?àD?D??¢ __packed typedef struct { u8 num; //?àD?±ào?u8 eledeg; //?àD?????u16 azideg; //?àD?·?????u8 sn; //D???±è }nmea_slmsg; //UTCê±??D??¢ __packed typedef struct { u16 year; //?ê·Yu8 month; //??·Yu8 date; //è??úu8 hour; //D?ê±u8 min; //·??óu8 sec; //???ó }nmea_utc_time; //NMEA 0183 D-òé?a??oóêy?Y′?·??á11ì? __packed typedef struct { u8 svnum; //?é???àD?êynmea_slmsg slmsg[12]; //×??à12???àD?nmea_utc_time utc; //UTCê±??u32 latitude; //?3?è ·?à?′ó100000±?,êμ?êòa3yò?100000u8 nshemi; //±±?3/???3,N:±±?3;S:???3 u32 longitude; //?-?è ·?à?′ó100000±?,êμ?êòa3yò?100000u8 ewhemi; //???-/?÷?-,E:???-;W:?÷?-u8 gpssta; //GPS×′ì?:0,?′?¨??;1,·?2?·??¨??;2,2?·??¨??;6,?y?ú1à??. u8 posslnum; //ó?óú?¨??μ??àD?êy,0~12.u8 possl[12]; //ó?óú?¨??μ??àD?±ào?u8 fixmode; //?¨??ààDí:1,??óD?¨??;2,2D?¨??;3,3D?¨??u16 pdop; //???????èòò×ó 0~500,??ó|êμ?ê?μ0~50.0u16 hdop; //???????èòò×ó 0~500,??ó|êμ?ê?μ0~50.0u16 vdop; //′1?±???èòò×ó 0~500,??ó|êμ?ê?μ0~50.0 int altitude; //o£°????è,·?′óá?10±?,êμ?ê3yò?10.μ¥??:0.1m u16 speed; //μ????ù?ê,·?′óá?1000±?,êμ?ê3yò?10.μ¥??:0.0011?à?/D?ê± }nmea_msg; //////////////////////////////////////////////////////////////////////////////////////////////////// //UBLOX NEO-6M ????(??3y,±£′?,?ó??μè)?á11ì? __packed typedef struct { u16 header; //cfg header,1ì?¨?a0X62B5(D????£ê?)u16 id; //CFG CFG ID:0X0906 (D????£ê?)u16 dlength; //êy?Y3¤?è 12/13u32 clearmask; //×ó??óò??3y?ú??(1óDD§)u32 savemask; //×ó??óò±£′??ú??u32 loadmask; //×ó??óò?ó???ú??u8 devicemask; //??±ê?÷?t?????ú?? b0:BK RAM;b1:FLASH;b2,EEPROM;b4,SPI FLASHu8 cka; //D£?éCK_A u8 ckb; //D£?éCK_B }_ublox_cfg_cfg; //UBLOX NEO-6M ???¢éè???á11ì? __packed typedef struct { u16 header; //cfg header,1ì?¨?a0X62B5(D????£ê?)u16 id; //CFG MSG ID:0X0106 (D????£ê?)u16 dlength; //êy?Y3¤?è 8u8 msgclass; //???¢ààDí(F0 ′ú±íNMEA???¢??ê?)u8 msgid; //???¢ ID //00,GPGGA;01,GPGLL;02,GPGSA;//03,GPGSV;04,GPRMC;05,GPVTG;//06,GPGRS;07,GPGST;08,GPZDA;//09,GPGBS;0A,GPDTM;0D,GPGNS;u8 iicset; //IIC??ê?3?éè?? 0,1?±?;1,ê1?ü.u8 uart1set; //UART1ê?3?éè?? 0,1?±?;1,ê1?ü.u8 uart2set; //UART2ê?3?éè?? 0,1?±?;1,ê1?ü.u8 usbset; //USBê?3?éè?? 0,1?±?;1,ê1?ü.u8 spiset; //SPIê?3?éè?? 0,1?±?;1,ê1?ü.u8 ncset; //?′?aê?3?éè?? ??è??a1?′?é.u8 cka; //D£?éCK_A u8 ckb; //D£?éCK_B }_ublox_cfg_msg; //UBLOX NEO-6M UART???úéè???á11ì? __packed typedef struct { u16 header; //cfg header,1ì?¨?a0X62B5(D????£ê?)u16 id; //CFG PRT ID:0X0006 (D????£ê?)u16 dlength; //êy?Y3¤?è 20u8 portid; //???úo?,0=IIC;1=UART1;2=UART2;3=USB;4=SPI;u8 reserved; //±£á?,éè???a0u16 txready; //TX Readyòy??éè??,??è??a0u32 mode; //′??ú1¤×÷?£ê?éè??,????D£?é,í£?1??,×??ú3¤?èμèμ?éè??.u32 baudrate; //2¨ì??êéè??u16 inprotomask; //ê?è?D-òé?¤???á±??? ??è?éè???a0X07 0X00?′?é.u16 outprotomask; //ê?3?D-òé?¤???á±??? ??è?éè???a0X07 0X00?′?é.u16 reserved4; //±£á?,éè???a0u16 reserved5; //±£á?,éè???a0 u8 cka; //D£?éCK_A u8 ckb; //D£?éCK_B }_ublox_cfg_prt; //UBLOX NEO-6M ê±?ó??3??????á11ì? __packed typedef struct { u16 header; //cfg header,1ì?¨?a0X62B5(D????£ê?)u16 id; //CFG TP ID:0X0706 (D????£ê?)u16 dlength; //êy?Y3¤?èu32 interval; //ê±?ó??3?????,μ¥???ausu32 length; //??3??í?è,μ¥???aussigned char status; //ê±?ó??3?????:1,??μ???óDD§;0,1?±?;-1,μíμ???óDD§. u8 timeref; //2???ê±??:0,UTCê±??;1,GPSê±??;2,μ±μ?ê±??.u8 flags; //ê±????3?éè??±ê??u8 reserved; //±£á? signed short antdelay; //ìì???óê±signed short rfdelay; //RF?óê±signed int userdelay; //ó??§?óê± u8 cka; //D£?éCK_A u8 ckb; //D£?éCK_B }_ublox_cfg_tp; //UBLOX NEO-6M ?¢D??ù?ê?????á11ì? __packed typedef struct { u16 header; //cfg header,1ì?¨?a0X62B5(D????£ê?)u16 id; //CFG RATE ID:0X0806 (D????£ê?)u16 dlength; //êy?Y3¤?èu16 measrate; //2aá?ê±??????£?μ¥???ams£?×?éù2??üD?óú200ms£¨5Hz£?u16 navrate; //μ?o??ù?꣨?ü?ú£?£?1ì?¨?a1u16 timeref; //2???ê±??£o0=UTC Time£?1=GPS Time£?u8 cka; //D£?éCK_A u8 ckb; //D£?éCK_B }_ublox_cfg_rate; int NMEA_Str2num(u8 *buf,u8*dx); void GPS_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPGSV_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPGGA_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPGSA_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPGSA_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPRMC_Analysis(nmea_msg *gpsx,u8 *buf); void NMEA_GPVTG_Analysis(nmea_msg *gpsx,u8 *buf); u8 Ublox_Cfg_Cfg_Save(void); u8 Ublox_Cfg_Msg(u8 msgid,u8 uart1set); u8 Ublox_Cfg_Prt(u32 baudrate); u8 Ublox_Cfg_Tp(u32 interval,u32 length,signed char status); u8 Ublox_Cfg_Rate(u16 measrate,u8 reftime); #endif總結(jié)
以上是生活随笔為你收集整理的在STM32上实现使用DMA+USART3传输数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作者:张慧(1984-),女,中国科学院
- 下一篇: STM32时钟树学习笔记