[STM32F1] 向某个特定的flash地址0x0807F800写入一些数据

[复制链接]
 楼主| 发表于 2025-8-8 12:57 | 显示全部楼层 |阅读模式
我想在STM32Cube IDE中向某个特定的flash地址0x0807F800写入一些数据,我使用了下面提函数进行读写。当我编译代码时,编译成功,但我看到地址0x0807F800是空白的。

nt32_t Flash_Write_Data (uint32_t StartPageAddress, uint32_t *Data, uint16_t numberofwords)

{

static FLASH_EraseInitTypeDef EraseInitStruct;

uint32_t PAGEError;

int sofar=0;

/* Unlock the Flash to enable the flash control register access *************/

  HAL_FLASH_Unlock();

  /* Erase the user Flash area*/

uint32_t StartPage = StartPageAddress;

uint32_t EndPageAdress = StartPageAddress + numberofwords*4;

uint32_t EndPage = EndPageAdress;

  /* Fill EraseInit structure*/

  EraseInitStruct.TypeErase  = FLASH_TYPEERASE_PAGES;

  EraseInitStruct.Page = StartPage;

  EraseInitStruct.NbPages   = ((EndPage - StartPage)/FLASH_PAGE_SIZE) +1;

  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)

  {

   /*Error occurred while page erase.*/

return HAL_FLASH_GetError ();

  }

  /* Program the user Flash area word by word*/

  while (sofar<numberofwords)

  {

   if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, StartPageAddress, Data[sofar]) == HAL_OK)

   {

   StartPageAddress += 4; // use StartPageAddress += 2 for half word and 8 for double word

   sofar++;

   }

   else

   {

    /* Error occurred while writing data in Flash memory*/

   return HAL_FLASH_GetError ();

   }

  }

  /* Lock the Flash to disable the flash control register access (recommended

   to protect the FLASH memory against possible unwanted operation) *********/

  HAL_FLASH_Lock();

  return 0;

}

void Flash_Read_Data (uint32_t StartPageAddress, uint32_t *RxBuf, uint16_t numberofwords)

{

while (1)

{

*RxBuf = *(__IO uint32_t *)StartPageAddress;

StartPageAddress += 4;

RxBuf++;

if (!(numberofwords--)) break;

}

}

uint32_t data[]={0X11111111,0X22222222};

int main(void)

{

Flash_Write_Data ( 0x0807F800 ,data, 2);

Flash_Read_Data( 0x0807F800 , Rx_Data, 3);

}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

32

主题

32

帖子

0

粉丝
快速回复 返回顶部 返回列表