|
stm32f4硬件i2c+DMA读mpu6050单步调试能过,运行卡住
以下是卡住代码部分,希望有经验的朋友指导指导!
u32 I2C_DMA_Read( u8 SlaveAddr, u8 ReadAddr, u8* NumByte ,u8* ReadBuf)
{
I2C_ReadPtr = NumByte;
I2C_TimeCnt = I2C_TIME;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_GenerateSTART(I2C1, ENABLE);
I2C_TimeCnt = I2C_TIME;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_Send7bitAddress(I2C1, SlaveAddr, I2C_Direction_Transmitter);
I2C_TimeCnt = I2C_TIME;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))//卡住在此处!单步进入函数又可以通过!
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_SendData(I2C1, ReadAddr);
I2C_TimeCnt = I2C_TIME;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF) == RESET)
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_GenerateSTART(I2C1, ENABLE);
I2C_TimeCnt = I2C_TIME;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_Send7bitAddress(I2C1, SlaveAddr, I2C_Direction_Receiver);
if((u16)(*NumByte) < 2) {
I2C_TimeCnt = I2C_TIME;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR) == RESET)
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_AcknowledgeConfig(I2C1, DISABLE);
(void)I2C1->SR2;
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_TimeCnt = I2C_TIME;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
*ReadBuf = I2C_ReceiveData(I2C1);
(u16)(*NumByte)--;
I2C_TimeCnt = I2C_TIME;
while(I2C1->CR1 & I2C_CR1_STOP)
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
else {
I2C_TimeCnt = I2C_TIME;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
DMA_InitStruct.DMA_Channel = DMA_Channel_1;
DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)I2C1_DR_Address;
DMA_InitStruct.DMA_Memory0BaseAddr = (u32)ReadBuf;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = (u32)(*NumByte);
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream0, &DMA_InitStruct);
I2C_DMALastTransferCmd(I2C1, ENABLE);
DMA_Cmd(DMA1_Stream0, ENABLE);
}
I2C_TimeCnt = I2C_TIME;
while(*NumByte > 0)
if((I2C_TimeCnt--) == 0) return I2C_TimeOut();
return SUCCESS;
}
卡住部分后面的各个while进入后单步都能过,一点run就会卡住,难道是硬件i2c+DMA太快?MPU6050响应不了这么快? |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|