11 Replies Latest reply on Oct 11, 2012 4:37 AM by gautam.das.g

    SPI Programming in PSoC3


      I wanted to code SPI interface in PSoC but is so far unsucessful. I used Arduino to check if my slave device chips are working. It had a simpler coding such as "data=SPI.transfer(0x00);" to send and receive data at the same time. I had alot of problem understanding how the same function can be implemented in PSoC. Is there any one with a code snippet that can enlighten me on how I can receive and send data on the same Master device in PSoC? (P.S. Example projects are confusing =( ). Thank you!

        • 1. Re: SPI Programming in PSoC3

           There are SPI program in the example programm provided.  See if you can make sense of the prg\ogram.

          • 2. Re: SPI Programming in PSoC3

            This thread may be useful. Download the files submitted and study it.




            Happy coding

            • 3. Re: SPI Programming in PSoC3

              Thanks for the response!

              • 4. Re: SPI Programming in PSoC3

                Hi HappyHour,




                You can refer to the example project which ships along with PSoC Creator. In PSoC Creator Component Catalog, right click on the SPI component, and select the Example project.




                You can modify the project according to your requirement and get it to working.




                Referring to component datasheet will help you understand the role of the APIs and how the component has to be configured to match your needs.









                • 5. Re: SPI Programming in PSoC3

                   I know this was an older thread but for me as starting PSoccer I had the same problems. Here is the code that also might help other newcomers understand how the SPI API works:

                  unsigned char spi(char outdata){      unsigned char result=0;      SPIM_WriteTxData(outdata);      while (SPIM_ReadTxStatus()==SPIM_STS_SPI_DONE);      result=SPIM_ReadRxData();      return result;  }

                  With this code you can send and receive SPI data in the same way as you are used to on other contollers. Apart from this code, you also need to place a component SPIM in which you define the SPI specs (see attachment). Only tested on 8 bits


                  It does not use interrupts and might not be the fastest but it basicly works.



                  • 6. Re: SPI Programming in PSoC3

                    Update - sorry the previous code I write used the wrong while loop and therefor sometimes gave wrong return values, this works better.  

                    • uint8 spi(uint8 outdata){
                    •     uint8 result=0;
                    •     SPIM_ClearRxBuffer();
                    •     SPIM_WriteTxData(outdata);
                    •     while (SPIM_GetRxBufferSize()==0);
                    •     result=(uint8) SPIM_ReadRxData();
                    •     return result;
                    • }


                    • 7. Re: SPI Programming in PSoC3

                      A construct like




                      is always a target for misinterpretation as I have seen in many places (and used as a source for errors myself).


                      While working on a program this can easily get modified to




                      which will really not do what you expect.




                      I like a construct as


                      void Wait(void)


                      {//Don't do anything (yet)






                          while(Condition) Wait();




                      which not only makes clear what is going to happen (no comment needed) but additionally gives me a point to install some background process when needed.




                      Happy coding



                      • 8. Re: SPI Programming in PSoC3

                        Maybe I am missing something Bob, would not be the fiurst time.....




                        I often do following -




                        while ( some condition )  {






                        Inside the loop I do other stuff, not waiting ( implication machine not doing anything )


                        while I am waiting for a condition to fail. Your example also permits this in the wait( )


                        you have called, that could also be doing other stuff. Net is not wasting any MIPs.




                        So what did I miss here in your post ?




                        Regards, Dana.

                        • 9. Re: SPI Programming in PSoC3

                          I just wanted to point to the easi human misinterpretation of small signs like a semi-colon which might be there and might be not. Enhanced by a false indentation errors -like the one in my last post- can lead to heart deseases. The call of Wait() emphasises the programmers intention and is easier to recognize than a semi-colon allone.


                          Using a block-statement with {} and a correct indentation as you showed has the same effect. When performing some actions within the block (so: not ONLY waiting for a condition to become true) the block statement will be more handy.





                          • 10. Re: SPI Programming in PSoC3

                            Correction: not: BECOME TRUE, rather FAILS



                            • 11. Re: SPI Programming in PSoC3

                              Ok, thanks. From now on I will use :


                               while (0==SPIM_GetRxBufferSize()) {/* Do_Nothing*/};


                              Instead of :


                               while (SPIM_GetRxBufferSize()==0) {};


                              Functional the same but more foolproof.