HAL_I2C_Mem_Write_DMA / HAL_I2C_Mem_Read_DMA what is the problem ?
Hi I'm trying to run I2C in DMA mode with LIS35 (accelerometer). I wrote simple code as below but each time when I try to run or debug it I'm getting back return "LIS35_ERROR;" which means that LIS35_I2C_Init(void) function goes wrong.
Previously (I mean yesterday) I wrote two similarly projects:
First was based on HAL_I2C_Mem_Write / HAL_I2C_Mem_Read functions - and all works properly (return LIS35_OK;)
Second was based on HAL_I2C_Mem_Write_IT / HAL_I2C_Mem_Read_IT functions - and all works properly(return LIS35_OK;)
Environment:
- STM32CubeMX - updated today,
- Board - Nucleo-F103RB
- CubeMX project added as attachment
- I2C configuration al on pictures below
/* USER CODE BEGIN 4 */
char LIS35_I2C_Init(void)
{
uint8_t Sett_Lis35_cr2_boot = LIS35_REG_CR2_BOOT;
uint8_t RegVal, LIS35Settings;
HAL_StatusTypeDef state1, state2, state3;
volatile long int i;
//reset LIS35 settings
if(HAL_I2C_Mem_Write_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR2, 1, &Sett_Lis35_cr2_boot, 1) != HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
//Write settings - activate all axis
LIS35Settings = LIS35_REG_CR1_XEN | LIS35_REG_CR1_YEN | LIS35_REG_CR1_ZEN | LIS35_REG_CR1_ACTIVE;
//WRITE CONFIGURATION TO LIS35 CHIP
if(HAL_I2C_Mem_Write_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR1, 1, &LIS35Settings, 1)!= HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
//Read configuration - if OK, LIS35 is up and running
if(HAL_I2C_Mem_Read_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR1, 1, &RegVal, 1) != HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
if (RegVal == LIS35Settings){
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
return LIS35_OK;
}
return LIS35_ERROR;
}
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c){
static uint8_t cntTx;
cntTx++;
}
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c){
static uint8_t cntRx;
cntRx++;
}
/* USER CODE END 4 */
Thanks in advance for any help :-)
Farnk