2 Replies Latest reply on Aug 31, 2020 11:47 AM by ScKi_1600301

    Low-level SPI sequential reads give wrong data

    ScKi_1600301

      I have a simple program for reading the JEDEC information from a serial flash chip using the low-level API.

       

      static void SPI_FlashWriteByte(uint8_t byte)

      {

      SPI_ClearRxFifo();

      SPI_Write(byte);

      while (SPI_GetNumInRxFifo() == 0);

      SPI_ClearRxFifo();

      }

       

      static uint8_t SPI_FlashReadByte()

      {

      SPI_ClearRxFifo();

      SPI_Write(0xFF);

      while (SPI_GetNumInRxFifo() == 0);

      return SPI_Read();

      }

       

      static void SPI_FlashReadJEDEC_Info(uint8_t* mfgID, uint8_t* memoryType, uint8_t* capacityID)

      {

      CS_FlashEnable();

      SPI_FlashWriteByte(0x9F);

      *mfgID = SPI_FlashReadByte();

      *memoryType = SPI_FlashReadByte();

      *capacityID = SPI_FlashReadByte();

      CS_FlashDisable();

      }

       

      int main(void)

      {

           __enable_irq(); /* Enable global interrupts. */

           Cy_SCB_SPI_Init(SPI_HW, &SPI_config, NULL);

           Cy_SCB_SPI_Enable(SPI_HW);

           Trace_Init();

           Trace_Log("\033[2J\033[H"); // Clear screen

           Trace_Log("SPI Test\r\n");

          for(;;)

          {

                uint8_t mfgID, memoryType, capacityID;

                SPI_FlashReadJEDEC_Info(&mfgID, &memoryType, &capacityID);

                Trace_Info("MfgID: 0x%02X, memoryType: 0x%02X, capacityID: 0x%02X", mfgID, memoryType, capacityID);

                CyDelay(100);

          }

      }

       

      Which produces the following signals.

       

      D0 = chip select

      D1 = SCLK

      D2 = MOSI

      D3 = MISO (the decoded data at the top is from MISO)

      screenshot.jpg

       

      The problem is that each SPI_FlashReadByte() call returns the value that was in the FIFO from the SPI_FlashWriteByte() call.

      I've tried using the high-level API with the same results.