SPI with FRDM K22F

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

SPI with FRDM K22F

2,858 Views
coopertrooper
Contributor III

Hello everyone,

 

I am trying to get 2 EV boards to communicate with each other via SPI. The problem I am having is that there is really no communication happening between the boards. The signals come out fine between the Slave and Master, but when I check the receive buffer, and it is always the same value (0xff).

 

I looked at the signals in a scope, and I did notice something weird. In the slave, I am sending 0x55, which should look like 0..001010101, however In the scope I see 0...001010000. The image is attached. The Yellow line is the Master Clock, and the blue line is the Slave SDOUT.

 

If anyone could help me out sort my problem I would greatly appreciate it. Been battling to get this SPI ready for 2 days now.

 

Here is the code for the Slave:

void slave(){ uint8_t SPI_instance = 1;     // uint8_t spi_baseAddr = g_spiBaseAddr[SPI_instance];      dspi_slave_state_t mstate;      uint32_t  calculatedBaudRate;      dspi_status_t statRet;      uint8_t sendBuff[8] = {0xFF};       // save data sent to i2c slave      uint8_t receiveBuff[8] = {0xFF};    // save data received from i2c slave      uint8_t numBytes = 8;      uint32_t  instance  =  1;      uint32_t  bitCount  =  16;      int timeOut = 1000; //in uS (total 100mS)      //local variables-----------       //  declare  which  module  instance  you  want  to  use      dspi_slave_state_t  dspiSlaveState;      //  update  configs      dspi_slave_user_config_t  slaveUserConfig;          slaveUserConfig.dataConfig.clkPhase  = kDspiClockPhase_FirstEdge;          slaveUserConfig.dataConfig.clkPolarity  = kDspiClockPolarity_ActiveHigh;          slaveUserConfig.dataConfig.bitsPerFrame  =  bitCount;          slaveUserConfig.dummyPattern  =  DSPI_DEFAULT_DUMMY_PATTERN;       //initialize the operating system     OSA_Init();      printf("\r\nStarting the SPI Test...");              //PTD4 -> spi1-CS0              //PTD5 -> spi1-SCK              //PTD6 -> spi1-SOUT              //PTD7 -> SPI1-SIN              configure_spi_pins(1);      //  init  the  slave  (interrupt  driven)     DSPI_DRV_SlaveInit(instance,  &dspiSlaveState,  &slaveUserConfig);      //  Perform  the  transfer     sendBuff[0] = 0x55;     //  Blocking  call  example     statRet  =  DSPI_DRV_SlaveTransfer(instance,  //  number  of  SPI  peripheral                                         sendBuff,      //  pointer  to  transmit  buffer,  can  be  NULL                                         receiveBuff,  //  pointer  to  receive  buffer,  can  be  NULL                                         numBytes      //  size  of  receive  and  receive  data                                         ); }

 

And here is the code for the master:

void startSPITest(){      //local variables-----------      uint8_t SPI_instance = 1;     // uint8_t spi_baseAddr = g_spiBaseAddr[SPI_instance];      dspi_master_state_t mstate;      uint32_t  calculatedBaudRate;      dspi_status_t statRet;      uint8_t sendBuff[8] = {0xFF};       // save data sent to i2c slave      uint8_t receiveBuff[8] = {0xFF};    // save data received from i2c slave      uint8_t numBytes = 8;      uint32_t framesComplete = 0;      int timeOut = 100; //in uS (total 100mS)       //initialize the operating system     OSA_Init();      //  configure  the  members  of  the  user  config  //     dspi_master_user_config_t  userConfig;         userConfig.isChipSelectContinuous = false;         userConfig.isSckContinuous = false;         userConfig.pcsPolarity = kDspiPcs_ActiveLow;         userConfig.whichCtar = kDspiCtar0;         userConfig.whichPcs = kDspiPcs0;       //configure the type of device the spi will be communicating with      dspi_device_t spiDevice;          spiDevice.dataBusConfig.bitsPerFrame = 16;          spiDevice.dataBusConfig.clkPhase = kDspiClockPhase_FirstEdge;          spiDevice.dataBusConfig.clkPolarity = kDspiClockPolarity_ActiveHigh;          spiDevice.dataBusConfig.direction = kDspiMsbFirst;          spiDevice.bitsPerSec = 50000;            printf("\r\nStarting the SPI Test...");          //PTD4 -> spi1-CS0          //PTD5 -> spi1-SCK          //PTD6 -> spi1-SOUT          //PTD7 -> SPI1-SIN          configure_spi_pins(1);      #if  FSL_FEATURE_SPI_16BIT_TRANSFERS     userConfig.bitCount=  kSpi8BitMode;    //  set  only  if  SPI  module  supports  bit  count  feature     #endif     //init the spi module     DSPI_DRV_MasterInit(SPI_instance,  &mstate,  &userConfig);     //SPI_DRV_MasterInit(SPI_instance,&mstate);     DSPI_DRV_MasterConfigureBus(SPI_instance,  &spiDevice,  &calculatedBaudRate);     //SPI_DRV_MasterConfigureBus(SPI_instance,  &userConfig,  &calculatedBaudRate);      //  Perform  the  transfer     sendBuff[0] = 10;       //make a transfer using the SPI module. Only transfers 1 data word of up to 16 bits.     statRet = DSPI_DRV_MasterTransfer(SPI_instance,NULL,                             sendBuff,receiveBuff,                             numBytes); //    statRet = DSPI_DRV_MasterTransferBlocking(SPI_instance,NULL, //                                sendBuff,receiveBuff, //                                numBytes,1000);      statRet = DSPI_DRV_MasterGetTransferStatus(SPI_instance, &framesComplete);     int transfStart = 0;     while(statRet!= kStatus_DSPI_Success ){         if (transfStart == 0){printf("\r\nTransfer started...");}         transfStart = 1;         statRet = DSPI_DRV_MasterGetTransferStatus(SPI_instance, &framesComplete);      }      //de- initialize the SPI module     DSPI_DRV_MasterDeinit(SPI_instance);       if (statRet != kStatus_DSPI_Success)      {          printf("\r\nSPI communication failed, error code: %d", statRet);      }      printf("\r\n... SPI Test Concluded.");   }
Labels (1)
3 Replies

1,133 Views
Rick_Li
NXP Employee
NXP Employee

Hi Christian Caicedo,

I have only one twr-k22F board and one FRDM-K22 board on my side.

So, I've created a SPI Master project for TWR-K22F and a SPI Slave project for FRDM-K22.

The master SPI will sent strings in main loop while slave SPI will receive the string.

These projects work on my side.

could you please try it on your side?

(in these two projects, I use different SPI pins than that of yours, you can modify it by just change the SPI module settings in Processor Expert SPI module, I'm using KDS on my side)

1,133 Views
josephchamdani
Contributor II

Hi Yong, I'd llike to try your FRDM-K22F SPI slave code to resolve my SPI CS stuck issue.  But while trying in KDS to import as archive file, it says the frdmk22spiSlave.zip is not a valid Zip file.  Please help.  Thanks.

0 Kudos
Reply

1,133 Views
coopertrooper
Contributor III

Hello Yong,

Thank you for getting back to me. I will get back to this particular issue in a couple of days.