cancel
Showing results for 
Search instead for 
Did you mean: 

SPI receive the same data every time (it's the register who_am_i)

TVela.1
Associate III

Hi!

I'm using the LaunchPad CC2642R1/CC2652R1 with the accelerometer LIS2DH and i tested the "who am i" (0x33) register to validate the protocol but when i tried read another register i had the same response that "who am i" (0x33) register. Could you help me with this problem or give me some ideas to solve it?

How can i set up the accelerometer? I can't read any register other than who am i (this register is shows in each reading).

0693W00000Y8yEeQAJ.pngThank you in advance.

Best regards,

Tania

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

> CS to "Low" "0"

Hard wired low? Think (don't have your hardware) you need to toggle it for each new SPI transfer. Otherwise you end up in the "Multiple bytes SPI read protocol", see Figure 8 of the data sheet. And, since MS bit is not set, you are reading the same reg over and over again.

hth

KnarfB

View solution in original post

9 REPLIES 9
Peter BENSCH
ST Employee

Welcome, @TVela.1​, to the community!

First of all, I would like to point out that with the LIS2DH you are working with a sensor that is already on NRND (Not Recommended for New Designs) and therefore should not be used for new developments.

Maybe the Github repository will help you, where you can still find generic C drivers as well as some examples for the LIS2DH?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
KnarfB
Principal III

Where is your code?

hth

KnarfB

TVela.1
Associate III

Hi KnarfB,

This is my code:

//Register

#define WHO_AM_I_ACELEROMETER (0x0F)

#define WHO_AM_I_R (0x33)

#define Read (0x80)

#define Write (0x00)

#define CTRL_REG1 (0x20)

********************************************************************

SPI_Params_init(&spiParams); // Initialize SPI parameters

  spiParams.transferMode = SPI_MODE_BLOCKING;

  spiParams.mode = SPI_MASTER;

  spiParams.bitRate = 100000;

  spiParams.dataSize = 8;

  spiParams.frameFormat = SPI_POL0_PHA0;

  spi = SPI_open(CONFIG_SPI, &spiParams);

  if (spi == NULL) {

    while (1); // SPI_open() failed

  }

//transmitBuffer

  transmitBuffer[0] = ((Read) | WHO_AM_I_ACELEROMETER);

  transmitBuffer[1] = (0x00);

  //receiveBuffer

  receiveBuffer[0] = (0x00);

  receiveBuffer[1] = (0x00);

  //transaction

  spiTransaction.count = 2;

  spiTransaction.txBuf = (void *)transmitBuffer;

  spiTransaction.rxBuf = (void *)receiveBuffer;

  transferOK = SPI_transfer(spi, &spiTransaction);

  if (!transferOK) {

  // Error in SPI or transfer already in progress.

  while(1);

  }

//======================Validation of address================================

  if(receiveBuffer[1] == WHO_AM_I_R){

  //transmitBuffer

  transmitBuffer[0] = (CTRL_REG1 | (Write) );

  transmitBuffer[1] = 0x97;

  //receiveBuffer

  receiveBuffer[0] = (0x00);

  receiveBuffer[1] = (0x00);

  //transaction

  spiTransaction.count = 2;

  spiTransaction.txBuf = (void *)transmitBuffer;

  spiTransaction.rxBuf = (void *)receiveBuffer;

  transferOK = SPI_transfer(spi, &spiTransaction);

  if (!transferOK) {

  while(1);

  }

//========Read register CTRL_REG1=========

  //transmitBuffer

  transmitBuffer[0] = (CTRL_REG1 | (Read));

  transmitBuffer[1] = (0x00);

  //receiveBuffer

  receiveBuffer[0] = (0x00);

  receiveBuffer[1] = (0x00);

  //transaction

  spiTransaction.count = 2;

  spiTransaction.txBuf = (void *)transmitBuffer;

  spiTransaction.rxBuf = (void *)receiveBuffer;

  transferOK = SPI_transfer(spi, &spiTransaction);

  if (!transferOK) {

    while(1);

  }

}

Best regards,

Tania

Is CS asserted as shown in the data sheet?

In the above debug window, transmitBuffer[0] shows 0xA3 which is different from what I would expect from the code?

Hook up a cheap logic analyzer and watch the waveforms if you can.

hth

KnarfB

TVela.1
Associate III

Yes, i'm using cs as "0" and 0xA3 is the register that i'm reading (0x23) with the bit to read (0x80, this is part of the protocol).

My issue is with receiveBuffer. I'm receiving the same data every time "who am i" register.

Regards,

Tania

TVela.1
Associate III

I'm using the STEVAL-MKI135V1 board with just the next connections:

  • MOSI to SDA
  • MISO to SDO
  • CLK to CLK
  • CS to "Low" "0"
  • ADC1, ADC2 and ADC3 to GND
  • VDD to 3.3V
  • VDD_IO to 3.3V

I got the following response when I was trying to read the CTRL_REG1 in first place (it's the same case with others register).

Step 1: Read "CTRL_REG1"

0693W00000Y94SIQAZ.pngIt only receives 0x33 from the "who am i" register when it is the first register read but when I try another register later the register "who am i" the following responses were obtained:

Step 1: Read "WHO_AM_I"

0693W00000Y94UnQAJ.pngStep 2: Read "CTRL_REG1"

0693W00000Y94V2QAJ.png 

I understand, could you confirm if the connection is correct? I'm using another IDE.

  • MOSI to SDA
  • MISO to SDO
  • CLK to CLK
  • CS to "Low" "0"
  • ADC1, ADC2 and ADC3 to GND
  • VDD to 3.3V
  • VDD_IO to 3.3V
KnarfB
Principal III

> CS to "Low" "0"

Hard wired low? Think (don't have your hardware) you need to toggle it for each new SPI transfer. Otherwise you end up in the "Multiple bytes SPI read protocol", see Figure 8 of the data sheet. And, since MS bit is not set, you are reading the same reg over and over again.

hth

KnarfB

TVela.1
Associate III

Hi KnarfB,

Sorry for the delay. Toggle it was the solution.

Thank you for your help.