STM32F4中W25Q16 FLASH存储器如何使用HAL驱动?

问题描述 投票:0回答:2

我想使用 SPI1 在 STM32F407 中使用闪存保存和恢复我的整数数据。我像这段代码给了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]
中,它在
FF
之后只有
HAL_SPI_TransmitReceive()
。我想查看我的制造商 ID。

感谢帮助。

hal stm32f4
2个回答
2
投票

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

  1. 写使能
  2. 擦除要写入的芯片或地址
  3. 再次写使能
  4. 写数据到地址

您可以使用以下功能。

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
}

0
投票

他的回答不起作用,完全没用......

© www.soinside.com 2019 - 2024. All rights reserved.