AnsweredAssumed Answered

LSM330TR: Reading Accelerometer Output

Question asked by Gursoy.Ali_Ergin on Oct 27, 2016
Hi,

I am trying to read output registers of LSM330. Unfortunately, the outputs does not change even if I shake the board.

Here is my setup;
SDO_A & SDO_G pins connected to 3.3VDC (through microcontroller)
CS_G & CS_A pins are connected to 3.3VDC (through microcontroller)

I2C is enabled to communicate IC and the read address is 0x3B.

I am able to read WHO_AM_I register for both accelerometer and gyro.

Initialazation of LSM330 is;

int32_t memsTaskInit(void)
{
     HAL_StatusTypeDef rc;
     uint16_t writeTimeOut = 0;
     uint8_t data = 0;
     uint8_t read_byte = 0;

     /* select gyroscope and accelerometer communication channel
      * 0 --> SPI
      * 1 --> I2C
      */
     HAL_GPIO_WritePin(ACC_CS_GPIO_Port, ACC_CS_Pin, GPIO_PIN_SET);

     /* select device address pins for accelerometer and gyroscope
      * if you choose reset state for these pins, read the comments of slave addresses at mems_task.h
      * comment out the relevant define according to the pin states
      */
     HAL_GPIO_WritePin(ACC_SEL_A_GPIO_Port, ACC_SEL_A_Pin, GPIO_PIN_SET);
     HAL_GPIO_WritePin(ACC_SEL_G_GPIO_Port, ACC_SEL_G_Pin, GPIO_PIN_SET);
     HAL_GPIO_WritePin(ACC_DEN_GPIO_Port, ACC_DEN_Pin, GPIO_PIN_SET);

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG4_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }

     data |= 0xD8;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     do
     {
          rc = HAL_I2C_Mem_Write(&hi2c_mems, ACCELERATION_SENSOR_SA_WRITE, MEMS_CTRL_REG4_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
          writeTimeOut++;
     }while (rc != HAL_OK);

     read_byte = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG4_A, 1, &read_byte, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }
     if (read_byte != data)
     {
          return VHTS_RC_NOT_OK;
     }

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG5_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }

     data |= 0x67;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     do
     {
          rc = HAL_I2C_Mem_Write(&hi2c_mems, ACCELERATION_SENSOR_SA_WRITE, MEMS_CTRL_REG5_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
          writeTimeOut++;
     }while (rc != HAL_OK);

     read_byte = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG5_A, 1, &read_byte, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }
     if (read_byte != data)
     {
          return VHTS_RC_NOT_OK;
     }

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG6_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }

     data |= 0x20;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     do
     {
          rc = HAL_I2C_Mem_Write(&hi2c_mems, ACCELERATION_SENSOR_SA_WRITE, MEMS_CTRL_REG6_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
          writeTimeOut++;
     }while (rc != HAL_OK);

     read_byte = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG6_A, 1, &read_byte, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }
     if (read_byte != data)
     {
          return VHTS_RC_NOT_OK;
     }

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG7_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }

     data |= 0x70;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     do
     {
          rc = HAL_I2C_Mem_Write(&hi2c_mems, ACCELERATION_SENSOR_SA_WRITE, MEMS_CTRL_REG7_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
          writeTimeOut++;
     }while (rc != HAL_OK);

     read_byte = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_CTRL_REG7_A, 1, &read_byte, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }
     if (read_byte != data)
     {
          return VHTS_RC_NOT_OK;
     }

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_FIFO_CTRL_REG_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }

     data |= 0x3F;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     do
     {
          rc = HAL_I2C_Mem_Write(&hi2c_mems, ACCELERATION_SENSOR_SA_WRITE, MEMS_FIFO_CTRL_REG_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
          writeTimeOut++;
     }while (rc != HAL_OK);

     read_byte = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_FIFO_CTRL_REG_A, 1, &read_byte, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
     {
          return VHTS_RC_NOT_OK;
     }
     if (read_byte != data)
     {
          return VHTS_RC_NOT_OK;
     }

     return VHTS_RC_OK;
}

Here is the code to read the output register:

static int32_t memsReadAccelerationOutput(MEMS_AccelerationOutput_type* outputs)
{
     DebugTrace(5, "memsReadAccelerationOutput\r");

     uint8_t acceleration_arr[6];
     uint8_t data = 0;
     char_t temp[12];

     /* make sure arguments are not null
      */
     if (outputs == NULL)
     {
          return VHTS_RC_NULL_ARG;
     }

     data = 0;
     while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
     HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_WHO_AM_I_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT);
     if (data != 0x40)
     {
          return VHTS_RC_NOT_OK;
     }

     data = 0;
     if (HAL_GPIO_ReadPin(ACC_INT1_A_GPIO_Port, ACC_INT1_A_Pin) == GPIO_PIN_SET)
     {
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_X_L_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_X_L_A not read\r");

               return VHTS_RC_NOT_OK;
          }
          outputs->out_x_l_a = data;

          data = 0;
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_X_H_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_X_H_A not read\r");

               return VHTS_RC_NOT_OK;
          }
          outputs->out_x_h_a = data;

          data = 0;
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_Y_L_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_Y_L_A not read\r");

               return VHTS_RC_NOT_OK;
          }
          outputs->out_y_l_a = data;

          data = 0;
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_Y_H_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_Y_H_A not read\r");


               return VHTS_RC_NOT_OK;
          }
          outputs->out_y_h_a = data;

          data = 0;
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_Z_L_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_Z_L_A not read\r");

               return VHTS_RC_NOT_OK;
          }
          outputs->out_z_l_a = data;

          data = 0;
          while (HAL_I2C_GetState(&hi2c_mems) != HAL_I2C_STATE_READY);
          if (HAL_I2C_Mem_Read(&hi2c_mems, ACCELERATION_SENSOR_SA_READ, MEMS_OUT_Z_H_A, 1, &data, 1, MEMS_I2C_READ_WRITE_TIMEOUT) != HAL_OK)
          {
               DebugTrace(5, "MEMS_OUT_Z_H_A not read\r");

               return VHTS_RC_NOT_OK;
          }
          outputs->out_z_h_a = data;
     }
     else
     {
          DebugTrace(5, "OVERRUN\r");

          return VHTS_RC_NOT_OK;
     }

     /* put the outputs in array
      */
     acceleration_arr[0] = outputs->out_x_h_a;
     acceleration_arr[1] = outputs->out_x_l_a;
     acceleration_arr[2] = outputs->out_y_h_a;
     acceleration_arr[3] = outputs->out_y_l_a;
     acceleration_arr[4] = outputs->out_z_h_a;
     acceleration_arr[5] = outputs->out_z_l_a;
     DebugTrace(5, "Buffer: ");
     DebugTraceArray(5,acceleration_arr,6);
     DebugTrace(5, "\r");

     return VHTS_RC_OK;
}


Could you please lead me to right path?

Regards

Outcomes