4 Replies Latest reply on Mar 1, 2015 7:43 AM by user_78878863

    Using the SPI component

    user_343349849

       Till now I bitbashed the SPI. I would like to use the component but cannot see how you can read back bytes at same time as sending (in my bit-bashed code I return the received character). Here is my code. Would somone mind advising me how to set this up better. 

         

       

         

      #include <project.h>

         

       

         

      #define CONFIG_REG 0x01

         

       

         

      uint8    RX_Buf[20];

         

      uint8    RX_Bytes;

         

      uint8    TX_Buf[20];

         

       

         

      void SPI_Write(uint8 * buf, uint8 n){

         

          uint8 i;

         

          

         

          for(i=0;i<n;i++){

         

            SPIM_WriteTxData(buf[i]);

         

            while(!(SPIM_ReadStatus() & SPIM_STS_TX_FIFO_EMPTY));

         

          }

         

      }

         

       

         

      uint8 SPI_Read(uint8 adr,uint8 n, uint8 * buf){

         

          uint8 i;

         

          

         

        SPIM_WriteTxData(adr); //Register address

         

      while(!(SPIM_ReadStatus() & SPIM_STS_TX_FIFO_EMPTY)){};

         

          for(i=0;i<n;i++){

         

              SPIM_WriteTxData(0xff); //write dummy byte

         

      while(!(SPIM_ReadStatus() & SPIM_STS_RX_FIFO_NOT_EMPTY)){};

         

            CyDelay(1);

         

       buf[i]= SPIM_ReadRxData();

         

          }

         

      return i;

         

      }

         

       

         

      int main()

         

      {

         

          CyGlobalIntEnable; 

         

          SPIM_Start();

         

          

         

          for(;;)

         

          {

         

            RX_Bytes = SPI_Read(CONFIG_REG,1,RX_Buf);  //read 1 byte from Config Register and place in RX Buffer

         

          

         

            TX_Buf[0] = 'A';

         

            TX_Buf[1] = 'B';

         

            TX_Buf[2] = 'C';

         

            SPI_Write(TX_Buf,3);  //Write 3 bytes of TX buffer to SPI

         

            CyDelay(100);

         

          }

         

      }

        • 1. Re: Using the SPI component
          user_78878863

          Each write to the SPI bus also does a read with the data on MISO. You can retrieve it after the write has finished from the RX buffer.

             

          Whats important: each write also fill the RX buffer, even if the data is not meaningful. So either read the buffer data, or clear the buffer before the actual read starts.

          • 2. Re: Using the SPI component
            user_343349849

             Do you have an example ? I understand the SPI concept but having trouble using the SPI component API in context of a real-wrld program.

               

            My Bit-Bashed code was simple to use blocking code like this...

               

            uint8 Write_SPI(uint8 ch){

               

            // do the write and read slave bits as master bits shifted out

               

            //return value

               

            }

               

            Then to read I would do this

               

            Write_SPI(REGADR);  //register to start read

               

            for(i=0;i<numbytes;i++){

               

              buf[i] = Write_SPI(DUMMY);  //get data from each register

               

            }

               

             

               

            Thanks

            • 3. Re: Using the SPI component
              user_1377889

              The new F-Ram Kit comes with an SPI interface and a component that controls the SPI. Download the kit example-files and study the implementation of the example

                 

               

                 

              Bob

              • 4. Re: Using the SPI component
                user_78878863

                There are also example programs available for all components. Right-click on the SPI Master in the components tree, "example programs".

                   

                In your code, you should check for the "SPI IDLE" status- the TX FIFO is empty as soon as the last byte is placed into the SPI TX register - but then it takes a while to be send. Doing that you can easily create your own, blocking, SPI_Write() and SPI_Read() functions.