Skip to main content
Visitor II
January 24, 2023
Solved

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

  • January 24, 2023
  • 7 replies
  • 1737 views

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

    This topic has been closed for replies.
    Best answer by KnarfB

    > 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

    7 replies

    Technical Moderator
    January 24, 2023

    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

    TVela.1Author
    Visitor II
    January 25, 2023

    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
    Super User
    January 24, 2023

    Where is your code?

    hth

    KnarfB

    TVela.1Author
    Visitor II
    January 24, 2023

    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

    Super User
    January 24, 2023

    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.1Author
    Visitor II
    January 24, 2023

    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.1Author
    Visitor II
    January 24, 2023

    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 

    KnarfBAnswer
    Super User
    January 25, 2023

    > 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.1Author
    Visitor II
    February 10, 2023

    Hi KnarfB,

    Sorry for the delay. Toggle it was the solution.

    Thank you for your help.