SPI interface K24

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI interface K24

1,192 Views
annamol
Contributor IV

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? 

Labels (1)
0 Kudos
5 Replies

563 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

I checked ADXL362 accelerometer datasheet, if you want read related register value, it need to read POPR register when dummy read byte was sent.

2015-10-21_14-49-47.png

I also checked TWR-K24F120M board schematics, the PTD[0~3] as SPI0 interface without any hardware conflict.

Wish it helps.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

563 Views
annamol
Contributor IV

Hi,

Is the API I am using the correct one??

sendBuffer[]={0x0B,0x02,0x00}

   dspiResult = DSPI_DRV_MasterTransferBlocking(DSPI_MASTER_INSTANCE,

                                                     NULL,

                                                     sendBuffer,

                                                     receiveBuffer,

                                                     sizeof(sendBuffer),//TRANSFER_SIZE

                                                     MASTER_TRANSFER_TIMEOUT);

I expected receiveBuffer[] to be filled and POPR to have the value 0xAD. MASTER_TRANSFER_TIMEOUT is having the value 5000U

0 Kudos

563 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Alex,

I think you are using the correct DSPI API function.

While, I am checking if there are some SPI configuration affect the communication.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

563 Views
annamol
Contributor IV

Hi,

I tried probing the SPI_CS,SPI_CLK ,MISO and MOSI lines. The CS line shows spikes/glitches in between even if it is configured to stay continuous and not. The changes made in the code are not getting reftected. Is there anything which needs to be done to the CS pin ??

First CS was mapped to B63 and then changed to B48.  The CS becomes stable if the wire is pressed firmly. But the issue is a bit better when CS is mapped to 46 when compared with B63. Is there anything which needs to be done in hardware?

0 Kudos

563 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

It looks like the CS signal was interfered by external noise.

It need make the CS signal connection cable as short as it could be.

And it can use a 0.1uF capacitor connects with related CS pin.

Wish it helps.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos