Connecting CY8CKIT-042 and MCP2515

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.
AbV_4665246
Level 1
Level 1

Hi all,

I am trying to connect MCP2515 CAN controller with my CY8CKIT-042 for logging data from the vehicle.

I am trying to make it work in loopback mode. But the sensor is giving some garbage values to the both receiver buffers of MCP2515.

I am attaching my code along with this and someone please help on this to find out the result.

0 Likes
1 Solution
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi AbV_4665246​,

1. The SPI protocol is implemented such that when the master is sending data in the MOSI line, it simultaneously receives data in the MISO line. Calling the API SPI_MASTER_WriteTxData doesn't guarantee that the transaction is complete, you need to wait until the a complete transaction is sent out before reading the RX FIFO. Otherwise it will read 0 only.

2. If you want to receive x no. bytes from slave, master has to send x no. of bytes. The slave can't send 2 bytes when the master sends only one byte. The master has to send clock for the slave to send data.  If you have nothing to send, simply send 0xFF to receive a byte from slave.

Do the following:

SPI_MASTER_WriteTxData(data);

// Check if no if bytes received is 1, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 1)

{};

HiAddr = (unsigned short) SPI_MASTER_ReadRxData();

SPI_MASTER_WriteTxData(0xFF);

// Check if no if bytes received is 1, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 1)

{};

      LoAddr = (unsigned short) SPI_MASTER_ReadRxData();

Another easier option for your use case if the following:

uint8_t data_packet[2] = {READ_RX_BUF_0_ID, 0xFF};

SPI_MASTER_PutArray(data_packet, sizeof(data_packet));

// Check if no if bytes received is 2, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 2)

{};

HiAddr = (unsigned short) SPI_MASTER_ReadRxData(); // It is first in first out (FIFO)

LoAddr = (unsigned short) SPI_MASTER_ReadRxData();

Simply calling SPI_MASTER_ReadRxData without any data in FIFO will give 0 only.

Please refer code example https://www.cypress.com/documentation/code-examples/ce224339-psoc-4-spi-master  for more details.

I see that you are using a udb based spi component. It is better to you the dedicated SPI block (SCB mode) available in Psoc 4 device. You will the relevant API set there also. Please use SCB component for SPI.

pastedImage_6.png

If you want to send more no of bytes without CS line going high between bytes, use SCB_SpiUartPutArray() and choose continuous mode in the configuration.

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

6 Replies
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi AbV_4665246​,

1. The SPI protocol is implemented such that when the master is sending data in the MOSI line, it simultaneously receives data in the MISO line. Calling the API SPI_MASTER_WriteTxData doesn't guarantee that the transaction is complete, you need to wait until the a complete transaction is sent out before reading the RX FIFO. Otherwise it will read 0 only.

2. If you want to receive x no. bytes from slave, master has to send x no. of bytes. The slave can't send 2 bytes when the master sends only one byte. The master has to send clock for the slave to send data.  If you have nothing to send, simply send 0xFF to receive a byte from slave.

Do the following:

SPI_MASTER_WriteTxData(data);

// Check if no if bytes received is 1, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 1)

{};

HiAddr = (unsigned short) SPI_MASTER_ReadRxData();

SPI_MASTER_WriteTxData(0xFF);

// Check if no if bytes received is 1, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 1)

{};

      LoAddr = (unsigned short) SPI_MASTER_ReadRxData();

Another easier option for your use case if the following:

uint8_t data_packet[2] = {READ_RX_BUF_0_ID, 0xFF};

SPI_MASTER_PutArray(data_packet, sizeof(data_packet));

// Check if no if bytes received is 2, this marks the end of transmission,

while(SPI_MASTER_GetRxBufferSize != 2)

{};

HiAddr = (unsigned short) SPI_MASTER_ReadRxData(); // It is first in first out (FIFO)

LoAddr = (unsigned short) SPI_MASTER_ReadRxData();

Simply calling SPI_MASTER_ReadRxData without any data in FIFO will give 0 only.

Please refer code example https://www.cypress.com/documentation/code-examples/ce224339-psoc-4-spi-master  for more details.

I see that you are using a udb based spi component. It is better to you the dedicated SPI block (SCB mode) available in Psoc 4 device. You will the relevant API set there also. Please use SCB component for SPI.

pastedImage_6.png

If you want to send more no of bytes without CS line going high between bytes, use SCB_SpiUartPutArray() and choose continuous mode in the configuration.

Regards,

Bragadeesh

Regards,
Bragadeesh

Dear BragadeeshV_41​,

Good Morning!!!

I am trying the solution that you suggested. But I am facing with the warning -

comparison between pointer and integer

for the codeline -

while(SPI_MASTER_GetRxBufferSize != 2) 

    {};

Eventhough in the datasheet is is showing SPI_MASTER_GetRxBufferSize should returns -

Returns - uint8: Integer count of the number of bytes/words in the Rx buffer

I like to know why this warning is coming continuously and I am not able to see any data in my Teraterm terminal. Is this because of this mentioned warning?

Kindly help me in this.

Thank you

0 Likes

Dear BragadeeshV_41​,

Continuation to above comment -

I was trying with UDB SPI block.

0 Likes

Dear BragadeeshV_41​,

Kindly ignore the the below comments on the Warning message I was getting. It was because of small syntax issue. I solved it.

I tried to run the code with your second suggestion using SCB block and data packet. But I observed that the controller in getting stuck inside the while loop and not coming outside.

Could you help me on this?

Thank you

0 Likes

Dear BragadeeshV_41​,

I am attaching a screenshot about what I am getting in debugging mode supporting to the above comment.

Here the RxBuffersize is coming as 8. that was one of the reason for hanging in the while loop. When I changed to 8 in while condition it came out giving 0xFF as result.

I think this observation can help you to understand my condition now.

Thank you

SPI.JPG

0 Likes

Hi AbV_4665246​,

Can you probe the data lines and check the waveform? Please share the same here for finding the issue.

Did you see the code example shared in the previous response?

Regards,

Bragadeesh

Regards,
Bragadeesh
0 Likes