0
\$\begingroup\$

I am trying to store data in SPI FLASH MEMORY of WINBOND W25Q80DV. I am able to read status registers and manufacturer ID successfully but can't able to write data into it. The procedure I followed:

  1. Set WRITE DATA ENABLE
  2. Check if is enabled and for busy state, if enabled and not busy then.
  3. Set block erase.
  4. set write enable again.
  5. Send page program with address (0x000000), then 10 bytes of data (0xAA).

when I tried to read data from same location it displays (0xFF) all the time.

Here the code I am using.

void Init_SPI(void)
{
    GPIO_InitTypeDef    GPIO_InitStructure;                                     // GPIO Setup object
    // enable clock for CLK & MISO & MOSI GPIO (CLK: GPIO_PB_13 / MISO: GPIO_PB_14 / MOSI: GPIO_PB_15)
    __HAL_RCC_SPI1_FORCE_RESET( );
  __HAL_RCC_SPI1_RELEASE_RESET( );

  __HAL_RCC_SPI1_CLK_ENABLE( );

    GPIO_InitStructure.Mode  = GPIO_MODE_AF_PP;                             // Set GPIO Alternate function Mode
    GPIO_InitStructure.Pin   = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;                    // GPIO Pin Number
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;                            // Set the speed for the selected pins
    GPIO_InitStructure.Pull  = GPIO_PULLUP;                             // GPIO Pull-up/pull-down operation
    GPIO_InitStructure.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

    // configure I2C peripherical
    SPI1_Init.Instance = SPI1;
  SPI1_Init.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  SPI1_Init.Init.CLKPhase = SPI_PHASE_1EDGE;
  SPI1_Init.Init.CLKPolarity = SPI_POLARITY_LOW;
  SPI1_Init.Init.DataSize = SPI_DATASIZE_8BIT;
  SPI1_Init.Init.Direction = SPI_DIRECTION_2LINES;
    SPI1_Init.Init.FirstBit = SPI_FIRSTBIT_MSB;
  SPI1_Init.Init.Mode = SPI_MODE_MASTER;
  SPI1_Init.Init.NSS = SPI_NSS_HARD_OUTPUT;
    SPI1_Init.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    // enable I2C peripheral
  HAL_SPI_Init(&SPI1_Init);
  __HAL_SPI_ENABLE(&SPI1_Init);                     // Enable I2C

}
void SPI_Write_Enable()
{
    uint8_t tx_data[4];
    tx_data[0] = 0x06;
    GpioWrite(&flash_en,0);
    HAL_SPI_Transmit(&SPI1_Init,tx_data,1,1000);
    GpioWrite(&flash_en,1);
    GpioWrite(&flash_en,0);
    tx_data[0] = 0x05;
    HAL_SPI_Transmit(&SPI1_Init,tx_data,1,1000);
    HAL_SPI_Receive(&SPI1_Init,tx_data,1,1000);
    GpioWrite(&flash_en,1);
    if((tx_data[0] >> 1 & 0x01) != 0x01)
    {
        UartPrint("error\r\n");
    }
    GpioWrite(&flash_en,0);
    tx_data[0] = 0x01;
    tx_data[1] = 0x02;
    tx_data[2] = 0x40;
    HAL_SPI_Transmit(&SPI1_Init,tx_data,3,1000);
    GpioWrite(&flash_en,1);

}
void SPI_Write_Data()
{
    uint8_t tx_data[20],cmd[2];

    tx_data[0] = 0x52;
    tx_data[1] = 0x00;
    tx_data[2] = 0x00;
    tx_data[3] = 0x00;
    GpioWrite(&flash_en,0);
    HAL_SPI_Transmit(&SPI1_Init,tx_data,14,1000);
    GpioWrite(&flash_en,1);

    Retry1:
    cmd[0] = 0x06;
    GpioWrite(&flash_en,0);
    while(HAL_SPI_Transmit(&SPI1_Init,cmd,1,1000)!= HAL_OK);
    GpioWrite(&flash_en,1);
    DelayMs(100);
    Retry:
    GpioWrite(&flash_en,0);
    tx_data[0] = 0x05;
    while(HAL_SPI_Transmit(&SPI1_Init,tx_data,1,1000)!=HAL_OK);
    while(HAL_SPI_Receive(&SPI1_Init,tx_data,1,1000)!=HAL_OK);
    GpioWrite(&flash_en,1);
    if((tx_data[0] >> 1 & 0x01) != 0x01)
    {
        UartPrint("error_wel_not enable\r\n");
        DelayMs(1000);
        goto Retry1;
    }
    else
    {
        DelayMs(1000);
        UartPrint("wel enabled\r\n");
    }
    if((tx_data[0] & 0x01) == 0x01)
    {
        UartPrint("BUSY\r\n");
        goto Retry;
    }
    cmd[0] = 0x06;
    GpioWrite(&flash_en,0);
    while(HAL_SPI_Transmit(&SPI1_Init,cmd,1,1000)!= HAL_OK);
    GpioWrite(&flash_en,1);
    GpioWrite(&flash_en,0);
    tx_data[0] = 0x02;
    tx_data[1] = 0x00;
    tx_data[2] = 0x00;
    tx_data[3] = 0x0A;
    tx_data[4] = 0xAA;
    tx_data[5] = 0xAA;
    tx_data[6] = 0xAA;
    tx_data[7] = 0xAA;
    tx_data[8] = 0xAA;
    tx_data[9] = 0xAA;
    tx_data[10] = 0xAA;
    tx_data[11] = 0xAA;
    tx_data[12] = 0xAA;
    tx_data[13] = 0xAA;
    if(HAL_SPI_Transmit(&SPI1_Init,tx_data,14,5000) != HAL_OK)
    {
        UartPrint("write error\r\n");
    }
    GpioWrite(&flash_en,1);
}

void SPI_Read_Data(uint8_t *data)
{
    uint8_t tx_data[20],cmd[2];
    cmd[0] = 0x04;
    GpioWrite(&flash_en,0);
    while(HAL_SPI_Transmit(&SPI1_Init,cmd,1,1000)!= HAL_OK);
    GpioWrite(&flash_en,1);
    GpioWrite(&flash_en,0);
    tx_data[0] = 0x05;
    while(HAL_SPI_Transmit(&SPI1_Init,tx_data,1,1000)!=HAL_OK);
    while(HAL_SPI_Receive(&SPI1_Init,tx_data,1,1000)!=HAL_OK);
    GpioWrite(&flash_en,1);
    if((tx_data[0] >> 1 & 0x01) == 0x01)
    {
        UartPrint(" wel enable\r\n");
    }
    if((tx_data[0] & 0x01) == 0x01)
    {
        UartPrint("BUSY\r\n");
    }
    tx_data[0] = 0x03;
    tx_data[1] = 0x00;
    tx_data[2] = 0x00;
    tx_data[3] = 0x0A;

    GpioWrite(&flash_en,0);
    HAL_SPI_Transmit(&SPI1_Init,tx_data,4,1000);
    if(HAL_SPI_Receive(&SPI1_Init,data,10,1000)!= HAL_OK)
    {
        UartPrint("error_read\r\n");
    }
    GpioWrite(&flash_en,1);
}
////////////////////////////////////////////////////////////////////////////////////

This is just a sample code that I had written for testing. There is while loop which periodically call SPI_Write_Data() and SPI_Read_Data() function. Rest SPI_Init() and SPI_Write_Enable() is one time called in main loop.

\$\endgroup\$
  • \$\begingroup\$ Error resolved, I am writing wrong value to the status registers. \$\endgroup\$ – Vaibhav May 23 '19 at 8:15
  • 1
    \$\begingroup\$ Then write that as an answer (you can write answers to your own questions) with the fixed code (only the relevant part, not everything). No one is going to learn much as it stands right now. \$\endgroup\$ – Arsenal May 23 '19 at 8:51

Your Answer

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

Browse other questions tagged or ask your own question.