1 Reply Latest reply on Jun 30, 2019 8:02 AM by MiKi_3184011

    SPI_Read function translation please

    MiKi_3184011

      I don't know why this is frustrating me so much but i'm ridiculously new to PSoC and SPI.  This function works on a previous generation of PSoC i played with but now i would like to use it with the PSoC 6 API.  Any help translating would be appreciated since it seems to be too much for me today......lol

       

      unsigned char SPI_Read(unsigned char* data,

                             unsigned char bytesNumber)

      {

          while(~SPIM_ADC_ReadTxStatus() & SPIM_ADC_STS_SPI_DONE){CyDelayUs(2);}

          SPIM_ADC_ClearRxBuffer();

          for (int i = 0; i < bytesNumber; i++){

              SPIM_ADC_WriteTxData(0x00);

          }

          while(~SPIM_ADC_ReadTxStatus() & SPIM_ADC_STS_SPI_DONE){CyDelayUs(2);}

          if(SPIM_ADC_GetRxBufferSize()){

              for (int i = 0; i < bytesNumber; i++){

                  data[i] = SPIM_ADC_ReadRxData();

              }

          }

          return bytesNumber;

      }

        • 1. Re: SPI_Read function translation please
          MiKi_3184011

          This works.  Any recommendations on optimization or preffered options sincerely appreciated.

           

           

          unsigned char SPI_Read(unsigned char* data, unsigned char bytesNumber)

          {

          Cy_SysLib_DelayUs(20);

              while(~Cy_SCB_SPI_GetTxFifoStatus(KIT_SPI_HW) & Cy_SCB_SPI_IsTxComplete(KIT_SPI_HW)){Cy_SysLib_DelayUs(2);}

              Cy_SCB_SPI_ClearRxFifo(KIT_SPI_HW);

              for (int i = 0; i < bytesNumber; i++){

              Cy_SCB_SPI_Write(KIT_SPI_HW, 0x00);

              }

           

           

              Cy_SysLib_DelayUs(20);

              while(~Cy_SCB_SPI_GetTxFifoStatus(KIT_SPI_HW) & Cy_SCB_SPI_IsTxComplete(KIT_SPI_HW)){Cy_SysLib_DelayUs(2);}

              if(Cy_SCB_SPI_GetNumInRxFifo(KIT_SPI_HW)){

                  for (int i = 0; i < bytesNumber; i++){

                      data[i] = Cy_SCB_SPI_Read(KIT_SPI_HW);

                  }

              }

              return bytesNumber;

          }