AnsweredAssumed Answered

spi eeprom reading problem

Question asked by carter.bob on Dec 13, 2012
Latest reply on Mar 21, 2014 by fy.yu

I am writing data into an ST 95M02SPI eeprom memory, from a STM32F051 micro but I am unable to read it back. On the scope I can see the proper data on MISO (just an incrementing count). SCK looks to be fine, (stops after LSB on MISO) and CS goes high just after that. However, I just can't read the returned value into the main app. All I read is 00h

It looks as if MISO data is not being shifted in the shift register or RXFIFO just contains all zeros.Using the SPI_Config function below was the only way I could get the SPI signals to show anything.  If anyone could help, I'd greatly appreciate it. I'm using the SPI2 port on the microcontroller.

void SPI_Config(void)
  SPI_InitTypeDef SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable SPI clock, SPI2 */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);    

  /* SPI pin mappings */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;

  /* SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;    
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* SPI MISO pin configuration */    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;    // This needs to have a PULL-UP
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  // Configure CS pin as output floating
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);    
  /* SPI configuration -------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_Init(SPIx, &SPI_InitStructure);
  SPI_SSOutputCmd(SPIx, ENABLE);
  SPI_Cmd(SPIx, ENABLE);    

uint8_t SPI_Flash_Read(uint8_t ins, uint8_t address)
uint8_t spi_rd_data_local;
    // Send SPI instruction         
    SPI_SendData8(SPIx, ins);    
      while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) != SET);
    // Send SPI address H
    SPI_SendData8(SPIx, 0x00);
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) != SET);    
    // Send SPI address M
    SPI_SendData8(SPIx, 0x00);
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) != SET);    
    // Send SPI address
    SPI_SendData8(SPIx, address);
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) != SET);    
      // Send Dummy Byte for Clocks
    SPI_SendData8(SPIx, 0xFF);
      // Now sit back and wait for the data
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
    spi_rd_data_local = SPI_ReceiveData8(SPIx);                

    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET);
        printf("\n\r Local Read Data Received = %.2X", spi_rd_data_local);
    return (spi_rd_data_local);