SPI receive data from slave trouble

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

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I'm new to SPI and have run into a snag. I have a a PCB with a PSoC 3 trying to communicate to an nRF24L01+. With PsoC Creator, I have added an SPI mater to the project and have a simple loop where, after I do a "SPIM_Start()" I try to do some simple register write/read on the slave. I have a logic analyzer showing me the SPI data that is being transfered. It appears as though my write to say register 0 is working .. I basically do a "SPIM_WrtieTxData(<addr>+32)"  ( register write requires bit 6 to be a "1" to indicate a register write, so register "0" if the value == 32), then I do another SPIM_WrtieTxData(<value>) to write the data to that register. So far so good, and the analyzer shows the xfer. But, when I try to read the register; I send "SPIM_WrtieTxData(<addr>)" (bit 6 is clear for a read, so just the reg addr is needed) then another write ("SPIM_WrtieTxData(0xFF)") with bugus data, which gives the slave the clock cycles it needs to shift the return value out  (verified with analyzer), I then do a "temp = SPIM_ReadRxData()" to read what I would think would be the slave return data but the value returned is NOT what is expected (nor what the analyzer shows). So, I must be missing some simple step before I should read the slave data.

Here is a simple example of my code.. and a snapshot of the analyzer output (attached)

My guess is I'm missing some Tx and/or Rx status checks but I'm not sure what and why?

Any suggestions?

main() {

  uint8 addr, val;

  SPIM_Start();

  for (;;) {

     addr = 32;     // Set bit 6 to write to a register (i.e.  001a aaaa ..  "aaaaa" is the register address). So reg "0" here.

      SPIM_WriteTxData(addr);   // Tx addr to slave device

      SPIM_WriteTxData(0x1E);  // Write 0x1E (to register 0 .. just an xfer test, not tryiing to set anything, yet).

      CyDelayUs(10u);   // Give small delay for the xfer

     addr = 0;                    // Now read regsiter 0. A read operation is just the address (i.e. 000a aaaa)

     SPIM_WriteTxData(addr);     // Tx addr to slave

     SPIM_WriteTxData(0xFF);    // Tx bogus data to give slave the clock cycles it needs to shift out register 0 contents

     CyDelayUs(10u);   // Give small delay for the xfer

     val = SPIM_ReadRxData();     // We should see 0x1E, but we val == 0x00 (analyzer shows 0x1E).

  }

}

0 Likes
1 Solution

Can you please  check this Code Example  named as "SPI_Design" .You will get this Code example by going to this tab as shown in image attached in Creator-

pastedImage_0.png

It may be helpful

View solution in original post

0 Likes
3 Replies
AnkitaS_51
Employee
Employee
100 likes received 50 likes received 25 likes received

Please post the project

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is my "main.c" (attached). If you need to whole project let me know.

There is nothing special about this code, all I'm trying to do is get SPI write/reads to/from an nRF24L01+ to work. Since my original post, I have added a loop that waits for SPIM_ReadRxStatus(). This seems to have fixed the issue I was having and now see a return value when reading SPIM_ReadRxData(). But, if you could quickly look at the code and make suggestions, since I'm still not really sure I'm doing things correctly, I would greatly appreciate it.

0 Likes

Can you please  check this Code Example  named as "SPI_Design" .You will get this Code example by going to this tab as shown in image attached in Creator-

pastedImage_0.png

It may be helpful

0 Likes