如何在HAL驱动程序中使用STM32F4中的W25Q16 FLASH存储器?

问题描述:

我想使用SPI1在STM32F407中用我的FLASH存储器保存和恢复我的整数数据。我给这个代码的FLASH指令。如何在HAL驱动程序中使用STM32F4中的W25Q16 FLASH存储器?

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); 

但是,在rxData[0],它只有FFHAL_SPI_TransmitReceive()。我想看到我的制造商ID。

感谢您的帮助。

您应该按照以下步骤将数据写入W25Q闪存模块。

  1. 写使能想写
  2. 写使能再次
  3. 写DATAS解决

您可以使用下面的功能

  • 擦除芯片或地址。

    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 
    }