2 Replies Latest reply on May 11, 2017 10:41 PM by user_365962704

    Improve SPI transfers

    user_365962704

      Hi,

         

      I'm trying to find more ways to transfer multiple bytes (arrays) via SPI, for now i'm sending one by one (see the for loop):

         

      void WriteLongRegister(const uint8_t *data, const size_t dataSize)

         

      {

         

      if ( NULL == data ) { return; }

         

      uint8_t i;

         

      SPI_ClearRxBuffer();

         

      SPI_ClearTxBuffer();

         

      SS_Write(0);

         

      for(i = dataSize; i != 0; i--)

         

      {

         

      SPI_WriteTxData(data[dataSize - i]);

         

      }

         

      while(0 == (SPI_ReadTxStatus() & SPI_STS_SPI_IDLE));

         

      SS_Write(1);

         

      }

         

       

         

      So, now i'm thinking about filling the SPI FIFO buffer and waiting for it to get empty to fill it again until transfer the whole array. Here's a ruff idea, haven't tested in real PSoC

         

      static void sendArray(const uint8_t* array, size_t size)
      {
          if( array == NULL ) return;
          
          uint8_t i;
          
          SS_Write(0); // SS pin low
          
          // "array" will be placed on the SPI Tx buffer
          // in blocks, not one by one, so we need to
          // calculate the rest bytes
          // let's say array is 22 bytes long,
          // and SPI_TX_BUFFER_SIZE is 4 bytes
          // We need to send array in 6 blocks
          // 5 blocks of 4 bytes and other block of 2 bytes
          // rest = 2 bytes
          uint8_t rest = size % SPI_TX_BUFFER_SIZE;
          
          // here we send the 5 blocks
          for( i = 0; i <= size; i += SPI_TX_BUFFER_SIZE )
          {
              SPI_PutArray( &array[i], SPI_TX_BUFFER_SIZE );
          }
          
          // Here send the rest of the array
          // i don't know how yet :(
          SPI_PutArray( &array[size - rest], rest );
          
          SS_Write(1); // SS pin idle
      }

         

       

         

      What do you think?

         

      any improvements?

         

      is worth the optimization (controlling or sending data to TFT displays can be improve with this)?

         

      More ways to send "large" arrays via SPI without sending byte by byte?

         

       

         

      Thanks in advance

         

      Carlos

         

      PD: Will attach the project later today.