Search code examples
memoryflashavrspi

SPI communication receiving 0xFF from slave! What can be the Problem?


I am trying to establish a SPI comunication between an 8-bit ATMEL Microcontroller and the flash memory AT45DB081E. As a first step, I must read the Manufacturur ID and the device ID of the flash through sending the opcode 0x9F and the receiving the desired information. The problem is I get only the responses equal to 0xFF that shows something is not right using SPI. Now, I dig into my code and some more details.

  • The flash spi interface for the desired code suppurts the selected frequenc in MCU!

  • The flash memory is used as slave and supports spi modes 0 and 3. So I decided to just use one of this modes by my master (MCU). I don't know if I shoud do some settings in slave side for selection of spi mode!

  • Slave select pin will be active from high to low connected to PORTB0 of the MCU

  • SCK is on PORTB1, MOSI is on PORTB2, MISO is on PORTB3

This is my code:

#include <avr/io.h>

void spi_init(){
    // Set MOSI, SCK, and SS as output
    DDRB |= (1 << DDB2) | (1 << DDB0) | (1 << DDB1);
    
    PORTB |= (1 << PB0); 
        
    // Enable SPI, set as Master, set clock rate to F_CPU/16 and set the interrupt
    SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
    
}

uint8_t spi_transfer(uint8_t data){
        // Start transmission
        SPDR = data;
        // Wait for transmission complete
        while (!(SPSR & (1 << SPIF))){
        }
        // Return received data
        return SPDR;
}

uint8_t CEval_flash::ReadManfacturer(){
    PORTB &= ~(1 << PB0); // Set SS low
    for(int i=0;i<10;i++){}
    spi_transfer(0x9F);
    volatile uint8_t man_id       =spi_transfer(0xFF);  // 0xFF is sent as dummy to get response
    volatile uint8_t device_id1   =spi_transfer(0xFF);  // (Not returned / Just for Debug)
    volatile uint8_t device_id2   =spi_transfer(0xFF);  // (Not returned / Just for Debug)
    volatile uint8_t ext_d_len    =spi_transfer(0xFF);  // (Not returned / Just for Debug)
    volatile uint8_t ext_d        =spi_transfer(0xFF);  // (Not returned / Just for Debug)
    // Release SS
    PORTB |= (1 << PB0); // Set SS
    return man_id; 
}

int main(){
    spi_init();
    uint8_t cha1 = ReadManfacturer();
}

Now as I told, inside variables man_id, device_id, etc. I get only 0xFF, However based on the memory datasheet, they should be spedific values!


Solution

  • After a lot of tries, finally I found out that the problem was in reset pin of the flash memory. Since it was an active low flash memory, I had to set it during the operations. However I had disconnected it!