cancel
Showing results for 
Search instead for 
Did you mean: 

How to set LSM6DSM in 3-wire SPI mode with the given C standard MEMS library

Soundhar
Associate II

I have recently brought a STEVAL-STLKT01V1 SensorTile kit in which LSM6DSM, LSM303AGR & LPS22HB are connected in 3 wire SPI on the SPI2 line of STM32L476. I pulled 4 CS lines to high at the beginning of the program and have set the SPI2 to Half duplex master mode with low data transfer rate. even though using the c command  lsm6dsm_spi_mode_set(&dev_ctx, LSM6DSM_AUX_SPI_3_WIRE); I'm getting no response on spi read, so the program is getting struck at lsm6dsm_reset_get(&dev_ctx, &rst).

I was able to use 4 wire SPI with STM32F407 and LSM6DSM, but I still face issues with using 3 Wire mode with STM32F407 and LSM6DSM

i have defined the CS Pin:

#define CS_up_GPIO_Port CS_AG_GPIO_Port

#define CS_up_Pin CS_AG_Pin

my GPIO Configuration:

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOG_CLK_ENABLE();

 HAL_PWREx_EnableVddIO2();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOC_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(BLUE_RST_GPIO_Port, BLUE_RST_Pin, GPIO_PIN_SET);

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOB, CS_AG_Pin|BLUE_CS_Pin|CS_M_Pin, GPIO_PIN_SET);

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(CS_P_GPIO_Port, CS_P_Pin, GPIO_PIN_SET);

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(CS_A_GPIO_Port, CS_A_Pin, GPIO_PIN_SET);

 /*Configure GPIO pin : LED_Pin */

 GPIO_InitStruct.Pin = LED_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

 /*Configure GPIO pin : BLUE_RST_Pin */

 GPIO_InitStruct.Pin = BLUE_RST_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(BLUE_RST_GPIO_Port, &GPIO_InitStruct);

 /*Configure GPIO pins : CS_AG_Pin BLUE_CS_Pin CS_M_Pin */

 GPIO_InitStruct.Pin = CS_AG_Pin|BLUE_CS_Pin|CS_M_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 /*Configure GPIO pin : CS_P_Pin */

 GPIO_InitStruct.Pin = CS_P_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(CS_P_GPIO_Port, &GPIO_InitStruct);

 /*Configure GPIO pin : CS_A_Pin */

 GPIO_InitStruct.Pin = CS_A_Pin;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(CS_A_GPIO_Port, &GPIO_InitStruct);

}

my SPI Mode Configuration:

static void MX_SPI2_Init(void)

{

 /* USER CODE BEGIN SPI2_Init 0 */

 /* USER CODE END SPI2_Init 0 */

 /* USER CODE BEGIN SPI2_Init 1 */

 /* USER CODE END SPI2_Init 1 */

 /* SPI2 parameter configuration*/

 hspi2.Instance = SPI2;

 hspi2.Init.Mode = SPI_MODE_MASTER;

 hspi2.Init.Direction = SPI_DIRECTION_1LINE;

 hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

 hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

 hspi2.Init.NSS = SPI_NSS_SOFT;

 hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

 hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi2.Init.CRCPolynomial = 7;

 hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

 if (HAL_SPI_Init(&hspi2) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN SPI2_Init 2 */

 /* USER CODE END SPI2_Init 2 */

}

Sensor Initialization:

/* 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 sensor boot time */

 HAL_Delay(15);

 lsm6dsm_spi_mode_set(&dev_ctx, LSM6DSM_AUX_SPI_3_WIRE);

 lsm6dsm_auto_increment_set(&dev_ctx, 1);

 /* Restore default configuration */

 lsm6dsm_reset_set(&dev_ctx, PROPERTY_ENABLE);

 do {

  lsm6dsm_reset_get(&dev_ctx, &rst);

 } while (rst);

 /* Enable Block Data Update */

 lsm6dsm_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);

 /* Set Output Data Rate for Acc and Gyro */

 lsm6dsm_xl_data_rate_set(&dev_ctx, LSM6DSM_XL_ODR_12Hz5);

 lsm6dsm_gy_data_rate_set(&dev_ctx, LSM6DSM_GY_ODR_12Hz5);

 /* Set full scale */

 lsm6dsm_xl_full_scale_set(&dev_ctx, LSM6DSM_2g);

 lsm6dsm_gy_full_scale_set(&dev_ctx, LSM6DSM_2000dps);

 /* Configure filtering chain(No aux interface)

  * Accelerometer - analog filter

  */

 lsm6dsm_xl_filter_analog_set(&dev_ctx, LSM6DSM_XL_ANA_BW_400Hz);

 /* Accelerometer - LPF1 path (LPF2 not used) */

 //lsm6dsm_xl_lp1_bandwidth_set(&dev_ctx, LSM6DSM_XL_LP1_ODR_DIV_4);

 /* Accelerometer - LPF1 + LPF2 path */

 lsm6dsm_xl_lp2_bandwidth_set(&dev_ctx, LSM6DSM_XL_LOW_NOISE_LP_ODR_DIV_100);

 /* Accelerometer - High Pass / Slope path */

 //lsm6dsm_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);

 //lsm6dsm_xl_hp_bandwidth_set(&dev_ctx, LSM6DSM_XL_HP_ODR_DIV_100);

 /* Gyroscope - filtering chain */

 lsm6dsm_gy_band_pass_set(&dev_ctx, LSM6DSM_HP_260mHz_LP1_STRONG);

SPI Read & Write:

static int32_t platform_write(void *handle, uint8_t reg,

               uint8_t *bufp,

               uint16_t len)

{

  HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

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

  HAL_SPI_Transmit(handle, bufp, len, 1000);

  HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET);

}

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

              uint16_t len)

{

  reg |= 0x80;

  HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET);

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

  HAL_SPI_Receive(handle, bufp, len, 1000);

  HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET);

}

kindly reply what's the issue with the above-followed procedure

2 REPLIES 2
ABayl.2
Associate II

Hi @Soundhar​ . Can you solved the problem ?

rihu
Associate

Hi,

Can you solved the problem? Because I have know the same problem.