|
发表于 2018-9-15 00:51:22
|
显示全部楼层
硬件IIC dma程序不知道为什么,读出来都是0! 不用DMA可以正常使用
cubemx 生成的
#include "main.h"
#include "stm32h7xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_tx;
DMA_HandleTypeDef hdma_i2c1_rx;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
#define EEPROM_ADDRESS 0xA0 /* Address A0 == MB1020 A-01 - Address A6 == MB1020 A-02 */
#define EEPROM_PAGESIZE 4 /* RF EEPROM ANT7-M24LR used */
#define RXBUFFERSIZE 1
uint8_t usart_aRxBuffer[RXBUFFERSIZE];
uint8_t I2c_Buf_Write[256]={0};
uint8_t I2c_Buf_Read[256]={0};
#define I2C_XFER_TIMEOUT_MAX 300
#define EEPROM_MAX_TRIALS 300
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t i;
/* USER CODE END 1 */
/* Enable I-Cache-------------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache-------------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("待写入的数据:\n");
for ( i=0; i<256; i++ ) //填充缓冲
{
I2c_Buf_Read=0; // 清空接收缓冲区
I2c_Buf_Write = i; // 为发送缓冲区填充数据
printf("%4d ", I2c_Buf_Write);
if(i%16 == 15)
printf("\n");
}
uint16_t Memory_Address;
int32_t Remaining_Bytes;
Remaining_Bytes = 256;
Memory_Address = 0;
while (Remaining_Bytes > 0)
{
if(HAL_I2C_Mem_Write_DMA(&hi2c1 , (uint16_t)EEPROM_ADDRESS, Memory_Address, I2C_MEMADD_SIZE_16BIT, (uint8_t*)(I2c_Buf_Write + Memory_Address),EEPROM_PAGESIZE)!= HAL_OK)
{
printf("写入错误\n");
}
/* Check if the EEPROM is ready for a new operation */
while (HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_ADDRESS, EEPROM_MAX_TRIALS, I2C_XFER_TIMEOUT_MAX) != HAL_OK);
Remaining_Bytes -= EEPROM_PAGESIZE;
Memory_Address += EEPROM_PAGESIZE;
}
printf("写入成功,现在读出的数据:\n");
if (HAL_I2C_Mem_Read_DMA(&hi2c1, (uint16_t)EEPROM_ADDRESS, 0, I2C_MEMADD_SIZE_16BIT, (uint8_t *)I2c_Buf_Read, 256) != HAL_OK)
{
printf("接收错误\n");
}
for( i=0; i<256; i++ )
{
if(i%16 == 15)
printf("\n");
printf("%d ",I2c_Buf_Read);
}
printf("EEPROM(AT24C256) IIC_DMA 读写测试成功\n\r");
|
-
|