AnsweredAssumed Answered

SPI interface K24

Question asked by annamol alex on Oct 19, 2015
Latest reply on Oct 29, 2015 by Hui_Ma

Hi,

 

I am trying to interface the TWRK24F120M with ADXL362 accelerometer. With the sample project available in Kinetis SDK for SPI master and slave, first I tested out the loopback configuration. This worked fine. Then I configured K24 as master and tried interfacing ADXL362 with the SPI0 lines.  The mappings are

PTD0 - SPI0_PCS0 (B63 )

PTD3- SPI0_SIN (B44)

PTD1- SPI0_SCK (B48)

PTD2- SPI0_SOUT (B45)

For ADXL362, for performing a read operation on a register 0x02, the command is 0x0B, followed by register address 0x02, then a dummy byte 0x00. Using the function,

   dspiResult = DSPI_DRV_MasterTransferBlocking(DSPI_MASTER_INSTANCE,

                                                     NULL,

                                                     sendBuffer,

                                                     receiveBuffer,

                                                     sizeof(sendBuffer),//TRANSFER_SIZE

                                                     MASTER_TRANSFER_TIMEOUT);

I tried sending out the command as

  • sendBuffer[]={0x0B,0x02,0x00}
  • sendBuffer[0]=0x0B, sendBuffer[1]=0x02,sendBuffer[2]=0x00
  • sendBuffer[]={0x0B|0x02}

The value 0x0B comes in PUSHR register. But POPR register is either 0x00 or 0xFF.

Similarly, I tried with DSPI_HAL functions for blocking as well as non blocking with dataWord being initialized as uint16_t dataWord ={0x0B|0x02} as they mentioned 16bit data appended by 16 bit command using the   DSPI_HAL_WriteDataMastermode(g_dspiBase[0], &commandConfig, dataWord);  I have attached the code for that.

 

#include <stdio.h>

// SDK Included Files

#include "board.h"

#include "fsl_clock_manager.h"

#include "fsl_os_abstraction.h"

#include "fsl_dspi_master_driver.h"

#include "fsl_debug_console.h"

#include "fsl_dspi_hal.h"

 

 

/*******************************************************************************

* Definitions

******************************************************************************/

#define DSPI_MASTER_INSTANCE        (0)                 /*! User change define to choose DSPI instance */

#define TRANSFER_SIZE               (32)                /*! Transfer size */

#define TRANSFER_BAUDRATE           (500000U)           /*! Transfer baudrate - 500k */

#define MASTER_TRANSFER_TIMEOUT     (5000U)             /*! Transfer timeout of master - 5s */

 

 

/*!******************************************************************************

* Prototypes

******************************************************************************/

 

 

/*******************************************************************************

* Variables

******************************************************************************/

uint8_t receiveBuffer[] = {0};

uint8_t sendBuffer[] = {0xB,0x02,0x00};

uint16_t dataWord = {0x0B|0x02};

 

/*******************************************************************************

* Code

******************************************************************************/

/*!

* @brief DSPI master blocking.

*

* Thid function uses DSPI master to send an array to slave

* and receive the array back from slave,

* thencompare whether the two buffers are the same.

*/

int main(void)

{

    uint8_t loopCount = 1;

    uint32_t i;

    uint32_t calculatedBaudRate;

 

 

    dspi_status_t dspiResult;

    dspi_master_state_t masterState;

    dspi_device_t masterDevice;

    dspi_master_user_config_t masterUserConfig = {

        .isChipSelectContinuous     = false, //false

        .isSckContinuous            = false, //

        .pcsPolarity                = kDspiPcs_ActiveLow,

        .whichCtar                  = kDspiCtar0,

        .whichPcs                   = kDspiPcs0

    };

 

 

    // Init hardware

    hardware_init();

    // Init OSA layer, used in DSPI_DRV_MasterTransferBlocking.

    OSA_Init();

 

 

    // Print a note.

    PRINTF("\r\n DSPI board to board blocking example");

    PRINTF("\r\n This example run on instance 0 ");

    PRINTF("\r\n Be sure DSPI0-DSPI0 are connected \n");

 

 

    // Setup the configuration.

    masterDevice.dataBusConfig.bitsPerFrame = 8;

    masterDevice.dataBusConfig.clkPhase     = kDspiClockPhase_FirstEdge;

    masterDevice.dataBusConfig.clkPolarity  = kDspiClockPolarity_ActiveHigh;

    masterDevice.dataBusConfig.direction    = kDspiMsbFirst;

 

 

    // Initialize master driver.

    dspiResult = DSPI_DRV_MasterInit(DSPI_MASTER_INSTANCE,

                                     &masterState,

                                     &masterUserConfig);

    if (dspiResult != kStatus_DSPI_Success)

    {

        PRINTF("\r\nERROR: Can not initialize master driver \n\r");

        return -1;

    }

 

 

    // Configure baudrate.

    masterDevice.bitsPerSec = TRANSFER_BAUDRATE;

    dspiResult = DSPI_DRV_MasterConfigureBus(DSPI_MASTER_INSTANCE,

                                             &masterDevice,

                                             &calculatedBaudRate);

    if (dspiResult != kStatus_DSPI_Success)

    {

        PRINTF("\r\nERROR: failure in configuration bus\n\r");

        return -1;

    }

    else

    {

        PRINTF("\r\n Transfer at baudrate %lu \r\n", calculatedBaudRate);

    }

   

    dspi_command_config_t commandConfig;

   

    while(1){

    commandConfig.isChipSelectContinuous = true;

    commandConfig.whichCtar = kDspiCtar0;

    commandConfig.whichPcs = kDspiPcs0;

    commandConfig.clearTransferCount = false;

    commandConfig.isEndOfQueue = false;

    DSPI_HAL_WriteDataMastermode(g_dspiBase[0], &commandConfig, dataWord);

    }

 

}

 

But still the result is 0xFF or 0x00 in the POPR regsiter.  Similarly I configured K24 as slave and interfaced with cc2650 from TI. I tried sending numbers like 1,2,3 from CC2650. The data gets received at times properly. But majority it is jumbled. This makes me think something is wrong with the SPI code of K24.

 

Can someone help me out with the register configuration in case I am doing something wrong.? Is there any other API which needs to be invoked for configuring registers for SPI, that is for writing a particular configuration to the ADXL register and reading it back? 

Outcomes