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)));
  • 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. Dec 2, 2020 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, 2021 at 16:51


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.