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

    SPI Programming in PSoC3

    chua.jun.long

      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
          ki.leung

           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
            ki.leung

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

               

            http://www.cypress.com/?app=forum&id=2232&rID=51593

               

            Happy coding

            • 3. Re: SPI Programming in PSoC3
              chua.jun.long

              Thanks for the response!

              • 4. Re: SPI Programming in PSoC3
                gautam.das.g

                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.

                   

                 

                   

                 

                   

                Regards,

                   

                Gautam

                • 5. Re: SPI Programming in PSoC3
                  user_347933176

                   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.

                     

                  Paddy

                     
                          
                  • 6. Re: SPI Programming in PSoC3
                    user_347933176

                    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;
                    •     
                    • }
                    •    
                       

                    Patrick

                    • 7. Re: SPI Programming in PSoC3
                      user_1377889

                      A construct like

                         

                         while(Condition);

                         

                      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

                         

                          while(condition);
                              DoSomething();

                         

                      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

                         

                      Bob

                      • 8. Re: SPI Programming in PSoC3
                        user_14586677

                        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
                          user_1377889

                          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.

                             

                           

                             

                          Bob

                          • 10. Re: SPI Programming in PSoC3
                            user_1377889

                            Correction: not: BECOME TRUE, rather FAILS

                               

                            Bob

                            • 11. Re: SPI Programming in PSoC3
                              user_347933176

                              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.