SPI Programming in PSoC3

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

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!

0 Likes
1 Solution
Anonymous
Not applicable

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

View solution in original post

0 Likes
11 Replies
Anonymous
Not applicable

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

0 Likes
Anonymous
Not applicable

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

   

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

   

Happy coding

0 Likes
Anonymous
Not applicable

Thanks for the response!

0 Likes
Anonymous
Not applicable

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

0 Likes
lock attach
Attachments are accessible only for community members.
pacr_284376
Level 5
Level 5
100 replies posted 10 solutions authored 50 sign-ins

 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

   
        
0 Likes
pacr_284376
Level 5
Level 5
100 replies posted 10 solutions authored 50 sign-ins

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Correction: not: BECOME TRUE, rather FAILS

   

Bob

0 Likes
pacr_284376
Level 5
Level 5
100 replies posted 10 solutions authored 50 sign-ins

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. 

0 Likes