|
问题:
STM32H7的I2C DMA传输,打开MPU就数据不对,关闭MPU就数据是对的
DMA使用的是DMA1_Stream0
代码:
MPU设置
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x30040000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256B;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x30044000;
MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER2;
MPU_InitStruct.BaseAddress = 0x30000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_32KB;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
I2C DMA传输测试代码
memset(&i2cWriteBuf, 8, sizeof(i2cWriteBuf));
memset(&i2cReadBuf, 0, sizeof(i2cWriteBuf));
//status = HAL_I2C_Mem_Write(&hi2c1, 0xA0, 0, I2C_MEMADD_SIZE_16BIT, i2cWriteBuf, I2C_BUF_LEN, 1000);
status = HAL_I2C_Mem_Write_DMA(&hi2c1, 0xA0, 0, I2C_MEMADD_SIZE_16BIT, i2cWriteBuf, I2C_BUF_LEN);
if (HAL_OK != status)
{
printf("error\r\n");
}
//HAL_Delay(100);
while (HAL_I2C_IsDeviceReady(&hi2c1, 0xA0, 32, 300) != HAL_OK);
//status = HAL_I2C_Mem_Read(&hi2c1, 0xA1, 0, I2C_MEMADD_SIZE_16BIT, i2cReadBuf, I2C_BUF_LEN, 1000);
status = HAL_I2C_Mem_Read_DMA(&hi2c1, 0xA1, 0, I2C_MEMADD_SIZE_16BIT, i2cReadBuf, I2C_BUF_LEN);
if (HAL_OK != status)
{
printf("error2\r\n");
}
while (HAL_I2C_IsDeviceReady(&hi2c1, 0xA0, 32, 300) != HAL_OK);
//HAL_Delay(100);
if (0 == memcmp(i2cReadBuf, i2cWriteBuf, sizeof(i2cReadBuf)))
{
printf("data correct\r\n");
}
else
{
printf("data error\r\n");
}
|
|