3 Replies Latest reply on Oct 29, 2018 10:29 PM by anks

    Implementing a SPIM interface to an SD Card

    user_286189714

      I have Segger's emFile product running with FreeRTOS on a PSoC 5LP using a dedicate SPI master. I haven't implemented locks because the hardware is not shared and so per Segger locks are not needed.

       

      Hardware layer really only requires two calls be implemented which I have done:

       

      static int _HW_Read(U8 Unit, U8 * pData, int NumBytes)
      {

          SD_Card_ClearRxBuffer();
         
          do
          {
              SD_Card_WriteTxData(0xff);              // Write dummy data to clock the SPI slave
             
              while (SD_Card_GetRxBufferSize() == 0); // Wait until receive FIFO is not empty
             
              *pData++ = SD_Card_ReadRxData();          // Read data from buffer
          }
          while (--NumBytes);
       
          return 0;
      }


      static int _HW_Write(U8 Unit, const U8 * pData, int NumBytes) {
          FS_USE_PARA(Unit);

          int i;
         
          for (i = 0; i < NumBytes; i++)
          {
              {
                  SD_Card_WriteTxData((uint8)pData[i]);
              }
          }

          CyDelay(1);
        return 0;
      }

       

      This works OK (but slow) if I include the 1ms delay in the write call. If I remove the delay I get read and/or write error depending upon what commend is executed next. I have tried nearly every type of status check in the write, TX_FIFO_DONE (also or'd with SPI_IDLE), SPI_DONE and just about anything I can think of. BTW - the SPIM is configured to 400KHz internal clock with just the 4 byte FIFO. I have also tried larger read and write buffers to no avail.

       

      Any help, even guesses would be appreciated.