cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2DTW12 - SPI

DivyaJayan
Associate II

Hi, 

 

I am attempting to obtain readings from the LIS2DTW12 accelerometer on board, utilizing the SPI interface and an STM32F098RCT6 microcontroller.

I’m accessing the ‘who_am_i’ register along with the high and low registers for the x, y, and z axes. Upon debugging, I’ve found that the high and low values for the x, y, and z axes are consistently 0xff, resulting in raw x, y, and z values of 0xffff.

Notably, these values remain unchanged even when the board is in motion.

Please help me.

Here is the code:

 

 

 

 

 

#define LIS2DTW12_WHO_IAM_I_ADDR    0x0F
#define LIS2DTW12_OUT_X_L_ADDR      0x28
#define LIS2DTW12_OUT_X_H_ADDR      0x29
#define LIS2DTW12_OUT_Y_L_ADDR      0x2A
#define LIS2DTW12_OUT_Y_H_ADDR      0x2B
#define LIS2DTW12_OUT_Z_L_ADDR      0x2C
#define LIS2DTW12_OUT_Z_H_ADDR      0x2D

uint8_t LIS2DTW12_ReadReg(uint8_t reg){

	uint8_t Received_data = 0;
	uint8_t address = reg | 0x80;

	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1, &address,1,50);
	HAL_SPI_Receive(&hspi1, &Received_data, 1, 50);
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
	return Received_data;
}
int main(void){
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_SPI1_Init();

  uint8_t who_am_i = LIS2DTW12_ReadReg(LIS2DTW12_WHO_IAM_I_ADDR);
  if(who_am_i != 0x44){}

  uint8_t x_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_X_L_ADDR);
  uint8_t x_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_X_H_ADDR);
  uint8_t y_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Y_L_ADDR);
  uint8_t y_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Y_H_ADDR);
  uint8_t z_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Z_L_ADDR);
  uint8_t z_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Z_H_ADDR);

  uint16_t  x_raw = ((x_high << 8)|x_low);
  uint16_t  y_raw = ((y_high << 8)|y_low);
  uint16_t  z_raw = ((z_high << 8)|z_low);

  while (1)
  {
  }
}
static void MX_SPI1_Init(void){ 
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}

 

 

 

 

 

 

After debugging, values are obtaining as follows:

Screenshot 2024-07-29 121820.png

18 REPLIES 18

I checked and found that MISO and MOSI was interchanged. So I corrected and tried again but still who_am_i register is return the same value 0xff instead of 0x44.

All the other high and low X,Y,Z axes registers are also return the same value as 0xff. Is there any problem with my code.

Please help.

So, again, that suggests that your SPI is not working at all.

  • Are there any other devices on the same SPI bus? If so, are they working OK?
  • Have you used a logic analyser or oscilloscope to verify what's happening on the wires?

 


@DivyaJayan wrote:

Hi Andrew,

Sorry for the low picture resolution. Here is the new one,


Thanks - that's much better!

Please also post one for the STM32 part.

Why do you have a pull-down on CS ?

How is your PA4 pin configured ?

There is no other devices in the same SPI bus.

 

Here is the schematics of STM23 part:Screenshot 2024-07-30 144136.png

 

PA4 is configured as Hardware NSS output signal.

 

Screenshot 2024-07-30 144706.png

 


@DivyaJayan wrote:

PA4 is configured as Hardware NSS output signal.


So that means it is controlled automatically by the SPI hardware - but your code is doing it in software.

Also, you do the transfer in two steps:

	HAL_SPI_Transmit(&hspi1, &address,1,50);
	HAL_SPI_Receive(&hspi1, &Received_data, 1, 50);

With hardware NSS control, that means NSS will be released between the two transactions - so that won't work.

Again, looking on the wires with an oscilloscope or logic analyser would show you what's going on ...

Hi Andrew,

Thank you for your reply.

Already checked the wires with oscilloscope and got correct clock burst.

I configured PA4 pin to disable and tried the code as,

HAL_SPI_TransmitReceive(&hspi1, &address, &Received_data, 1, HAL_MAX_DELAY);

Still again the register who_am_i is return 0xff instead of 0x44.

 If I remove the permanently pull down of CS pin, will there be any change.

Hi Andrew,

 

I tried again and now I am getting different values for who_am_i register which include 0x44 also. But each time when debugging, different values are getting. 

 

Screenshot 2024-07-31 144317.png

Screenshot 2024-07-31 144350.png

Screenshot 2024-07-31 144445.png

These are the values I got for who_am_i and x register.

Please go through it and comment on it. It will be so helpful.

 

Thanking you,

Divya


@DivyaJayan wrote:

 

Already checked the wires with oscilloscope and got correct clock burst.


You could have mentioned that earlier!

So you get a clock - but what about the other lines?

 


@DivyaJayan wrote:

I configured PA4 pin to disable and tried the code as,


Did that make any difference to what you see on the oscilloscope?

So did you re-define address and Received_data appropriately as this is now a 2-byte transaction?

 


@DivyaJayan wrote:

 If I remove the permanently pull down of CS pin, will there be any change.


You have the board and the oscilloscope - try it and see!

Why did you put it there? What purpose do you think it serves?