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.
