初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-4-8
|
通过DMA将串口2数据传到内存,并打印出来。我在main函数里动态分配1024byte空间,希望把串口2的数据存到该内存中,实在网页抓取例程基础上改的,一直没有成功,将DMA缓存改为512以下,可以接受一部分数据,高于这个值,就死机。不知为什么?具体该怎么配置?主要代码如下
////************************串口二传送到内存的DMA配置************************************************///
void USART2_DMA_Config(u32 *RxBuff)
{
DMA_InitTypeDef DMA_InitStructure;
/*开启DMA时钟*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/*设置DMA源:串口数据寄存器地址*/
DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) ( & ( USART2->DR ) );
/*内存地址(要传输的变量的指针)*/
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)RxBuff;
/*方向:从外设到内存*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
/*传输大小DMA_BufferSize=SENDBUFF_SIZE*/
DMA_InitStructure.DMA_BufferSize =512;
/*外设地址不增*/
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
/*内存地址自增*/
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
/*外设数据单位*/
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
/*内存数据单位 8bit*/
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
/*DMA模式:不断循环*/
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;//正常缓存模式
/*优先级:中*/
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
/*禁止内存到内存的传输 */
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
/*配置DMA1的6通道*/
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
}
int main(void)
{
u8 t,*print;
u32 *RxBuff=NULL;
RxBuff=(u32 *)malloc(1024*sizeof(char));
/* 初始化 */
WiFi_RST_INIT();
WiFi_USART1_INIT();
WiFi_USART2_INIT();
USART2_DMA_Config(RxBuff);
WiFi_NVIC_INIT(); //初始化WiFi模块使用的接口和外设
SysTick_Init(); //配置 SysTick 为 1ms 中断一次
KEY_Init();
Delay_ms ( 3000 );
PC_Usart ("\r\n 测试开始\r\n"); //打印测试例程提示信息
if(RxBuff==NULL)
{
PC_Usart ( "\r\nmemory allocation failed.goodbye\r\n");
}
ESP8266_fresh ();
while(1)
{
t=KEY_Scan(0); //得到键值
if(t)
{
switch(t)
{
case KEY_DOWN:
Web_To_ESP();
print=(u8*)RxBuff;
PC_Usart ( "%s", print );
PC_Usart ( "\r\nhello3\r\n");
free(RxBuff);
PC_Usart ( "\r\nhello1\r\n");
t=0;
break;
default:
break;
}
}else Delay_ms(10);
}
}
void Web_To_ESP(void)
{
bool state;
do
{
strEsp8266_Fram_Record .InfBit .FramLength = 0; //从新开始接收新的数据包
ESP8266_Usart ( "%s\r\n", "AT+CIPSTART=\"TCP\",\"eat.weixincore.com\",80" );
Delay_ms ( 500); //延时
strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';
PC_Usart ( "%s", strEsp8266_Fram_Record .Data_RX_BUF );
state= ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "OK" )||( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "ALREAY CONNECT" )||
( ! ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "ERROR" ));
}
while(state==false);
// ESP8266_Link_Server ( enumTCP, "eat.weixincore.com", "80", Single_ID ) ; //此处连接服务器3次,需要优化,但是如果用while,又卡死
// ESP8266_Link_Server ( enumTCP, "eat.weixincore.com", "80", Single_ID );
// ESP8266_Link_Server ( enumTCP, "eat.weixincore.com", "80", Single_ID );
ESP8266_Cmd ( "AT+CIPMODE=1", "OK", 0, 500 ); //0,非透传;1,透传
ESP8266_Cmd ( "AT+CIPSEND", "\r\n", ">", 500 );
ESP8266_SendString ( ENABLE, "GET /index.php?s=/Mobile/PrinterTest/index HTTP/1.1\r\n", NULL, Single_ID );
ESP8266_SendString ( ENABLE, "Host: eat.weixincore.com\r\n", NULL, Single_ID );
ESP8266_SendString ( ENABLE, "User-Agent: abc\r\n", NULL, Single_ID );
ESP8266_SendString ( ENABLE, "Connection: close\r\n", NULL, Single_ID );
ESP8266_SendString ( ENABLE, "\r\n", NULL, Single_ID );
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel6, DISABLE ); //关闭USART1 TX DMA1 所指示的通道
DMA_SetCurrDataCounter(DMA1_Channel4,512);//DMA通道的DMA缓存的大小
DMA_Cmd(DMA1_Channel6, ENABLE);
while(1)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC6)!=RESET) //判断通道6传输完成
{
DMA_ClearFlag(DMA1_FLAG_TC6);//清除通道6传输完成标志
break;
}
}
}
|
|