cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2DH12 SPI Read ,The returned data is unchanged; lis2dh12_device_id_get(&dev_ctx, &whoamI) is right, whoamI=0x33

xwang.6
Associate III

in main call

platform_init_h12();

while(1)

{

lis2dh12_read_data_polling();

HAL_Delay(1000);

}

The read_data_polling.c code is as follows:

/* Includes ------------------------------------------------------------------*/

#include <string.h>

#include <stdio.h>

#include "lis2dh12_reg.h"

#include "stm32G0xx_hal.h"

#include "usart.h"

#include "gpio.h"

#include "spi.h"

#include "tim.h"

#include "IMU.h"

/* Private macro -------------------------------------------------------------*/

#define  BOOT_TIME      300 //ms 5

#define CS_up_GPIO_Port GPIOB

#define CS_up_Pin    GPIO_PIN_12

//#define SENSOR_BUS    hspi1

#define SENSOR_BUS    hspi2

 stmdev_ctx_t dev_ctx;

 lis2dh12_reg_t reg;

/* Private variables ---------------------------------------------------------*/

static int16_t data_raw_acceleration[3];

static int16_t data_raw_temperature;

static float acceleration_mg[3];

static float temperature_degC;

static uint8_t whoamI;

static uint8_t tx_buffer[200];

/* Extern variables ----------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/*

 *  WARNING:

 *  Functions declare in this section are defined at the end of this file

 *  and are strictly related to the hardware platform used.

 *

 */

static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,

               uint16_t len);

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,

               uint16_t len);

static void tx_com(uint8_t *tx_buffer, uint16_t len);

static void platform_delay(uint32_t ms);

//static void platform_init(void);

/* Main Example --------------------------------------------------------------*/

void lis2dh12_read_data_polling(void)

{

 /* Read samples in polling mode (no int) */

// while (1) 

{

// lis2dh12_reg_t reg;

/* Read output only if new value available */

lis2dh12_xl_data_ready_get(&dev_ctx, &reg.byte);

if (reg.byte) 

{

/* Read accelerometer data */

memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));

lis2dh12_acceleration_raw_get(&dev_ctx, data_raw_acceleration);

acceleration_mg[0] =

lis2dh12_from_fs2_hr_to_mg(data_raw_acceleration[0]);

acceleration_mg[1] =

lis2dh12_from_fs2_hr_to_mg(data_raw_acceleration[1]);

acceleration_mg[2] =

lis2dh12_from_fs2_hr_to_mg(data_raw_acceleration[2]);

sprintf((char *)tx_buffer,

"Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",

acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);

tx_com(tx_buffer, strlen((char const *)tx_buffer));

}

lis2dh12_temp_data_ready_get(&dev_ctx, &reg.byte);

if (reg.byte) 

{

/* Read temperature data */

memset(&data_raw_temperature, 0x00, sizeof(int16_t));

lis2dh12_temperature_raw_get(&dev_ctx, &data_raw_temperature);

temperature_degC =

lis2dh12_from_lsb_hr_to_celsius(data_raw_temperature);

sprintf((char *)tx_buffer,

"Temperature [degC]:%6.2f\r\n",

temperature_degC);

tx_com(tx_buffer, strlen((char const *)tx_buffer));

}

 }

}

/*

 * @brief Write generic device register (platform dependent)

 *

 * @param handle  customizable argument. In this examples is used in

 *          order to select the correct sensor bus handler.

 * @param reg    register to write

 * @param bufp   pointer to data to write in register reg

 * @param len    number of consecutive register to write

 *

 */

static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,

               uint16_t len)

{

 /* Write multiple command */

 reg |= 0x40;

 HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

// HAL_Delay(1);

 HAL_SPI_Transmit(handle, &reg, 1, 1000);

 HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);

// HAL_Delay(1);

 HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET);

 return 0;

}

/*

 * @brief Read generic device register (platform dependent)

 *

 * @param handle  customizable argument. In this examples is used in

 *          order to select the correct sensor bus handler.

 * @param reg    register to read

 * @param bufp   pointer to buffer that store the data read

 * @param len    number of consecutive register to read

 *

 */

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,

               uint16_t len)

{

 /* Read multiple command */

 reg |= 0x80;

 HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

// HAL_Delay(1);

 HAL_SPI_Transmit(handle, &reg, 1, 1000);

// HAL_Delay(1);

 HAL_SPI_Receive(handle, bufp, len, 1000);

// HAL_Delay(1);

 HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET);

 return 0;

}

/*

 * @brief Send buffer to console (platform dependent)

 *

 * @param tx_buffer   buffer to transmit

 * @param len      number of byte to send

 *

 */

static void tx_com(uint8_t *tx_buffer, uint16_t len)

{

 HAL_UART_Transmit(&huart1, tx_buffer, len, 1000);

}

/*

 * @brief platform specific delay (platform dependent)

 *

 * @param ms    delay in ms

 *

 */

static void platform_delay(uint32_t ms)

{

 HAL_Delay(ms);

// osalThreadDelayMilliseconds(ms);

}

/*

 * @brief platform specific initialization (platform dependent)

 */

 void platform_init_h12(void)

{

/* Initialize mems driver interface */

// stmdev_ctx_t dev_ctx;

dev_ctx.write_reg = platform_write;

dev_ctx.read_reg = platform_read;

dev_ctx.handle = &SENSOR_BUS;

/* Wait boot time and initialize platform specific hardware */

// platform_init();

/* Wait sensor boot time */

platform_delay(BOOT_TIME);

/* Check device ID */

 whoamI = 0;

lis2dh12_device_id_get(&dev_ctx, &whoamI);

if (whoamI != LIS2DH12_ID) 

{

while (1) 

{

/* manage here device not found */

lis2dh12_device_id_get(&dev_ctx, &whoamI);

if (whoamI != LIS2DH12_ID) 

{

printf("LIS2DH12_ID = 0x%X\r\n",whoamI); 

HAL_Delay(300);

}

else

{

printf("LIS2DH12_ID = 0x%X\r\n",whoamI); 

break;

}

}

}

else //0x33

{

printf("LIS2DH12_ID = 0x%X\r\n",whoamI); 

}

/* Enable Block Data Update. */

lis2dh12_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);

/* Set Output Data Rate to 1Hz. */

// lis2dh12_data_rate_set(&dev_ctx, LIS2DH12_ODR_1Hz);

lis2dh12_data_rate_set(&dev_ctx, LIS2DH12_ODR_25Hz);

/* Set full scale to 2g. */

lis2dh12_full_scale_set(&dev_ctx, LIS2DH12_2g);

/* Enable temperature sensor. */

lis2dh12_temperature_meas_set(&dev_ctx, LIS2DH12_TEMP_ENABLE);

/* Set device in continuous mode with 12 bit resol. */

lis2dh12_operating_mode_set(&dev_ctx, LIS2DH12_HR_12bit);

}

0693W00000YAszJQAT.png 

3 REPLIES 3
xwang.6
Associate III

Why hasn't the acceleration_mg changed?

Little_guang
Associate II

Little_guang_0-1691712619898.png

I tried to debug this problem and found a simple solution, which is to manually increment the address by myself, as shown in the picture. Or use a driver that automatically increments the address.

Another solution is to enable register address auto-incrementation. It is enabled by adding value 0x80 to the register address. For example: i2cread(DEVICE_ADDRESS, OUT_X_L | 0x80, buffer, 6); to read 6 register bytes in one go,  starting at address 0x28 (which is OUT_X_L).