'STM32 I2C HAL FXOS8700 communication problem
I have a STM32F103RE MCU that is connected to a fxos8700cq sensor through I2C2 port. I've set up the peripherals using stm32cubemx and I'm trying to simply test the communication with the sensor. But as I debug the code, it seems that data transfer is not happening correctly.
These are the i2c settings:
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 100000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
and here is the main code:
uint8_t send_buffer[1];
uint8_t receive_buffer[1];
uint8_t FXOS_ADDR = (FXOS8700_DEVICE_ADDR_SA_11 << 1);
send_buffer[0] = FXOS8700_WHO_AM_I;
receive_buffer[0] = 0;
// chip select for fxos
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_Delay(500);
ret = HAL_I2C_Master_Transmit(&hi2c2, FXOS_ADDR, send_buffer, 1, 100);
//ret = HAL_I2C_Master_Transmit_IT(&hi2c2, FXOS_ADDR, send_buffer, 1);
HAL_Delay(2);
ret = HAL_I2C_Master_Receive(&hi2c2, FXOS_ADDR, receive_buffer, 1, 100);
//ret = HAL_I2C_Master_Receive_IT(&hi2c2, FXOS_ADDR, receive_buffer, 1);
if (receive_buffer[0] == FXOS8700_WHO_AM_I_PROD_VALUE)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
but after I debug the code, and watch the i2c registers after executing the HAL_I2C_Master_Transmit(&hi2c2, FXOS_ADDR, send_buffer, 1, 100); function I get these values. That means the TXE bit in SR1 register is not set.
I also tried using the interrupt mode but that's not working either. the values of i2c registers after the ret = HAL_I2C_Master_Transmit_IT(&hi2c2, FXOS_ADDR, send_buffer, 1); line are like this.
In all cases the functions return HAL_OK and there is no error.
I've tested the sensor and the board communication using mbed and it works fine. So the hardware has no problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
