4

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)));
    }
2
  • At first: spi_port |= (1<<SS); after the return wil not work. You'll never reach the line. Why do you initialize spi_data_reg = 0;? Have you set the master to input like spi_data_direc=(1<<MISO); elsewhere? I did not yet programmed SPI communications with the low level api, so I do not know if one of these could be the problem. But as no one else repsonded ... Just some ideas; better than nothing. – Peter Paul Kiefer Dec 2 '20 at 13:42
  • As far as I understand SPI communication, Slave cannot send data to Master without Master initiating the communication. Typically, Master sends a dummy data so that it can receive a response from Slave. So Master needs to know how many bytes to receive and send dummy data to Slave as many times to get the response. – Kavka Jan 6 at 16:51
1

Feel free to take a look at my YouTube videos. They are not monetized so I'm not self-promoting. You can even skip the videos and checkout my GitHub repo with the Master and Slave sketches. https://youtu.be/A9oSX_dwJKQ https://youtu.be/PSUzhCtNRRQ https://github.com/linhartr22/Arduino_SPI_Master_Slave_Demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.