This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/CC2640R2F: SPI interfacing for ADXL343

Part Number: CC2640R2F
Other Parts Discussed in Thread: CC2640

Tool/software: Code Composer Studio

Dear staff, 

I'm trying to interface to the ADXL343 using SPI functions from SPI.h library.

This is my function to read one byte ID:

void readOneByte(SPI_Transaction *transaction, SPI_Handle *handle, uint8_t address)
{
bool transferOK;
memset((void *) masterRxBuffer, 0, 2); //trial to receive 2 bytes
masterTxBuffer[0] = ADXL343_SPI_READ|(address&0x3F); //Read command

transaction->count = 2; 
transaction->txBuf = (void *) masterTxBuffer;
transaction->rxBuf = (void *) masterRxBuffer;

/* Toggle user LED, indicating a SPI transfer is in progress */
GPIO_toggle(Board_GPIO_LED1);
/* Perform SPI transfer */
transferOK = SPI_transfer(*handle, transaction);
if (transferOK) {
  Display_printf(display, 0, 0, "Master received: %x", masterRxBuffer[1]);
}
else {
   Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
}

}

For setup the spi thread:

void *masterThread(void *arg0)
{
SPI_Handle masterSpi;
SPI_Params spiParams;
SPI_Transaction transaction;
SPI_Transaction *ptr_transaction = &transaction;
SPI_Handle *ptr_handle = &masterSpi;
uint32_t i;

uint8_t data;
uint16_t x,y,z; //data of accelerator

SPI_Params_init(&spiParams);
spiParams.frameFormat = SPI_POL1_PHA1; //Clock standby at high; sampled data at 2nd transition clock
spiParams.bitRate = 100000;
masterSpi = SPI_open(Board_SPI_MASTER, &spiParams);
if (masterSpi == NULL) {
     Display_printf(display, 0, 0, "Error initializing master SPI\n");
      while (1);
}
else {
Display_printf(display, 0, 0, "Master SPI initialized\n");
}

/* READ ID DEVICE OF ADXL343*/
readOneByte(ptr_transaction, ptr_handle,ADXL343_DEVID_REG);

SPI_close(masterSpi);

Display_printf(display, 0, 0, "\nDone");

return (NULL);
}

As I read the SPI protocol of TI have some different to Analog Device in config Frame Format. Specifically, TI implys clock edge to sampling data SPH = 0 meaning first clock transition and SPH = 1 for  second clock transition while Analog Device imply SPH = 1 for sampling at rising edge clock and vice versa. 

when I run debug, I just the right ID of IC at the second Rx buffer. It is the reason why in my code I need to output data at masterRxBuffer[1]. 

My question is whether or not any way to modify SPI_trasfer function to improve this problem?

Thank you.

 

  • Long,

    Could you rephrase this part of your explanation --> "I just the right ID of IC"? I'm not sure what you mean.

    Thanks,

    Seong 

  • Dear Seong,

    My mean is I read ID of ADXL343. Sorry for my typo.  Below is the result of memory when I run the debugger.


    The second byte is 0xCA in case of debug mode but when I run board freely I got the right ID of the IC: 0xE5 as the Image below

    I guess there are some things wrong with synchronous clocking. 

    I hope this helps you clearer.

  • Long,

    Comparing how the frame formats are defined by ADI and the CC2640 Technical Reference Manual (see section 20.4.4.2), you should try using SPO = 1 and SPH = 0. 

    More information on the CC2640's SPI driver API can be found here.

    Hope this helps,

    Seong

  • Seong Kim said:
    20.4.4.2

    Actually I have changed all Dataframe Format SP0 = 0/1; SPH = 0/1 but just SP0 = 1 and SPH = 1 is most suitable work.

    In case SP0 = 1 and SPH = 0; MCU can read ID at first byte is 0xE5; but for other registers, it went something wrong. 

    As you can see from my pictures when I read for data register, it goes intermittently reading, x,y,z valid on Odd times read and invalid after that. 


  • Dear Seong Kim, 

    Now I understand the reason why I must read data from the second byte and later. Just because the TI's SPI sampling data when I transmit a command so if we send write or read command spi, it always takes first garbage bytes. 

    Now I change to spi.dataSize to 16 rather 8 and the process now not intermittently reading data x, y, z as my capture picture shown. However, for read one value from the register, it keeps going something weird. 

    as You can see, for the first read ID it true for ID = 0xE5;

    For second  to senventh command it goes wrong value but  it is ok for next.  

  • Long,

    The way this slave device most likely works is that you need a 2 byte transfer to read a 1 byte ID. This is because the receiving device needs to know what opcode to work with before it can respond with the data you request. If you have any further questions regarding the slave device, please contact ADI's customer support.  

    BR,

    Seong