Tart L.
Tart L.

Reputation: 29

How to use W25Q16 FLASH memory in STM32F4 with HAL Driver?

I want to save and restore my integer datas with my FLASH memory in STM32F407 using SPI1. I gave instructions to FLASH like this code.

uint8_t txData[10] = {0xAB, 0x04, 0x06, 0xC7, 0x04, 0x90, 0x00, 0x00, 0x00, 0x00};
uint8_t rxData[10] = {0};

HAL_SPI_Init(&hspi1);
HAL_SPI_Transmit(&hspi1, txData+5, 1, 10000);
HAL_SPI_Transmit(&hspi1, txData+6, 1, 10000);
HAL_SPI_Transmit(&hspi1, txData+7, 1, 10000);
HAL_SPI_Transmit(&hspi1, txData+8, 1, 10000);
HAL_SPI_TransmitReceive(&hspi1, txData+9, rxData, 1, 10000);

But, in rxData[0], it has only FF after HAL_SPI_TransmitReceive(). I want to see my Manufacturer ID.

Thanks to help.

Upvotes: -1

Views: 5940

Answers (1)

Gürtaç Kadem
Gürtaç Kadem

Reputation: 315

You should follow these steps for writing data to W25Q flash modules.

  1. Write enable
  2. Erase chip or address which want to write
  3. Write enable again
  4. Write datas to address

You can use following functions.

void Flash_Erase_Chip(void)
{
    uint8_t Write_Enable = 0x06;
    uint8_t Erase_Chip = 0xC7;

    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);     // CS to low
    HAL_SPI_Transmit(&hspi6,&Write_Enable,1,1000); // Write Enable Command
    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,SET);       // CS to high

    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);     // CS to low
    HAL_SPI_Transmit(&hspi6,&Erase_Chip,1,1000);   // Erase Chip Command
    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,SET);       // CS to high
}

void Flash_Write_Data()
{
    uint8_t Write_Enable = 0x06;
    uint8_t Page_Program = 0x02;
    uint32_t Address = 0x00000000;
    uint8_t txData[10] = {0xAB, 0x04, 0x06, 0xC7, 0x04, 0x90, 0x00, 0x00, 0x00, 0x00};

    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);     // CS to low
    HAL_SPI_Transmit(&hspi6,&Write_Enable,1,1000); // Write Enable Command
    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,SET);       // CS to high

    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);    // CS to low
    HAL_SPI_Transmit(&hspi6,&Page_Program,1,1000);// Page Program Command
    HAL_SPI_Transmit(&hspi6,&Address,4,1000);     // Write Address ( The first address of flash module is 0x00000000 )
    HAL_SPI_Transmit(&hspi6,txData,10,1000);      // Write 10 bytes
    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,SET);      // CS to high
}

void Flash_Read_Data
{
    uint8_t Read_Data = 0x03;
    uint32_t Address = 0x00000000;

    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);   // CS low
    HAL_SPI_Transmit(&hspi6,&Read_Data,1,1000);  // Read Command
    HAL_SPI_Transmit(&hspi6,&Address,4,1000);    // Write Address
    HAL_SPI_Receive(&hspi6,rxData,10,1000);      // Read 10 bytes
    HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,RESET);   // CS high
}

Upvotes: 2

Related Questions