SPI between Master arduino and SLave arduino

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How can I send the string from Slave SPI to Master SPI.

I want to write code in Bare Metal.

The problem which I am facing is I can't make out what will be my blocking function in Master SPI code when receiving data from slave SPI.

As my SPIF bit is cleared in MAster code as soon as I shift out SPDR register from master to receive byte which is coming from the slave.


#define spi_data_reg SPDR
//Naster read byte function
uint8_t MA_SPI0_read_byte()
{
	spi_port &= ~(1<<SS);
	spi_data_reg = 0;
	while(!(SPSR & (1<<SPIF)));                  //Wait until transmission complete
	return(spi_data_reg);                                 //Return received data
   spi_port |= (1<<SS);
}

//Slave send byte function
void SL_SPI0_send_byte(uint8_t data)
{
	spi_data_direc=(1<<MISO);   //MISO as OUTPUT
	spi_data_reg = data;
	while(!(SPSR &(1<<SPIF)));
}

 

 

Kunal Gupta

github.com/gkunalupta

Last Edited: Wed. Dec 2, 2020 - 10:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As always, the key is not to try to do both ends of the link (Master & Slave) at the same time - concentrate on one end at first; get it thoroughly tested, proved, and working.

 

Since Master is by far the commonest use case, I would suggest that you start there - with some well-known, standard, known-working slave

 

EDIT

 

For some good slaves to work with, see: https://www.avrfreaks.net/commen...

 

EDIT 2

 

On concentrating on one end at a time: https://www.avrfreaks.net/commen... - in particular, pay attention to instrumenting your code, and understanding failure cases

 

EDIT 3

 

For application notes, etc, on using your AVR, see its Product Page:

 

https://www.avrfreaks.net/commen...

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Dec 2, 2020 - 10:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You only need one function - one that sends a byte and returns the receive byte. With SPI send and receive are inextricably linked - you send a byte, you get a byte - even if there is nothing on the other end.

 

Also realise that there is nothing to tell the master whether the slave is ready or not. Once the master sends the first byte, the slave only gets interrupted when it arrives, thus the first byte received on the master is wasted.

 

This is the challenge when implementing a slave SPI. This is something I've done recently, but the chip I was using had DMA which made the problem less of an issue.

 

I'd also suggest you get yourself a cheapy logic analyser. It will save your sanity.

 

 

Is Arduino 'Bare Metal'? even if the code you write is exactly the same as in Arduino??

 

Last Edited: Wed. Dec 2, 2020 - 10:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Once the master sends the first byte, the slave only gets interrupted when it arrives, thus the first byte received on the master is wasted.

Some Slaves keep a "status" byte always ready - so the Master will get that in response to its first transmitted byte ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
you send a byte, you get a byte

yes exactly, so now I want is blocking thing like: I want to receive data from slave SPI, Now for My MASTER SPI to receive data, it has to send a byte (SPDR =data) and wait until SPIF is set and then it gets the received data on SPDR. Whether y slave has send data or not , it MAster SPDR gets filled with some data  and my programs move further as it doesn't stick around the Master while loop because my Master SPIF is always set.

So 

Kartman wrote:
This is the challenge when implementing a slave SPI

 

Can u give any insights on how to do that

 

Kunal Gupta

github.com/gkunalupta

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Back to the Product Page - have you read  AN_2585 - AVR151: Setup and use of the SPI on tinyAVR and megaAVR devices ?

 

It covers both Master and Slave.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

If you get yourself a cheapy logic analyser, some careful thinking, coding and testing will give you all the insight you will need. if you toggle port bits that are connected to the logic analyser in tandem with the spi signals, you will be able to se what the slave code is doing in relation to the spi bus activity.