STM32G031G8U6单片机内部FLASH读写简单学习

控制/MCU

1878人已加入

描述

此篇限定研究范围为FLASH区域的访问规则。

以下结论均为实测所得:

一、读

1.单字节读例程,单字节读可以从任意地址开始读

uint8_t STMFLASH_ReadByte(uint32_t faddr)
{
  return *(volatile uint8_t*)faddr; 
}

2.半字(2个字节)读例程, 半字读只能从偶数地址开始读 ,从奇数地址读的话单片机就死机了

uint16_t STMFLASH_ReadHalfWord(uint32_t faddr)
{
  return *(volatile uint16_t*)faddr; 
}

3.字(4个字节)读例程, 按字读只能从4的整数倍地址开始读 ,否则单片机死机

uint32_t STMFLASH_ReadWord(uint32_t faddr)
{
  return *(volatile uint32_t*)faddr; 
}

二、写

1.单次写入字节数:

我看G0x1的技术参考手册上说一次写入是72bit(64bit加ecc),我实际测试是单次写入只能是32bit,以实际测试为准

(1)单字节写,写不了,单片机死机

我在HAL库里面把库函数FLASH_Program_DoubleWord改成如下进行验证的

``

static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)

{

/* Set PG bit */

SET_BIT(FLASH->CR, FLASH_CR_PG);

*(uint8_t *)Address = (uint8_t)(Data&0xff);__ISB();

*((uint8_t *)Address+1) = (uint8_t)((Data>>8U)&0xff);__ISB();

*((uint8_t *)Address+2) = (uint8_t)((Data>>16U)&0xff);__ISB();

*((uint8_t *)Address+3) = (uint8_t)((Data>>24U)&0xff);__ISB();

*((uint8_t *)Address+4) = (uint8_t)((Data>>32U)&0xff);__ISB();

*((uint8_t *)Address+5) = (uint8_t)((Data>>40U)&0xff);__ISB();

*((uint8_t *)Address+6) = (uint8_t)((Data>>48U)&0xff);__ISB();

*((uint8_t *)Address+7) = (uint8_t)((Data>>56U)&0xff);

}

``

(2)**半字写,写不了,单片机死机

我在HAL库里面把库函数FLASH_Program_DoubleWord改成如下进行验证的

static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
  /* Set PG bit */
  SET_BIT(FLASH->CR, FLASH_CR_PG);


  *(uint16_t *)Address = (uint16_t)(Data&0xffff);__ISB();
  *((uint16_t *)Address+2) = (uint16_t)((Data>>16U)&0xffff);__ISB();
  *((uint16_t *)Address+4) = (uint16_t)((Data>>32U)&0xffff);__ISB();
  *((uint16_t *)Address+6) = (uint16_t)((Data>>48U)&0xffff);

}

(3)按字写(4个字节,32bit),可以,HAL库函数FLASH_Program_DoubleWord里面就是按字写的

static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
  /* Set PG bit */
  SET_BIT(FLASH->CR, FLASH_CR_PG);


  /* Program first word */
  *(uint32_t *)Address = (uint32_t)Data;


  /* Barrier to ensure programming is performed in 2 steps, in right order
    (independently of compiler optimization behavior) */
  __ISB();


  /* Program second word */
  *(uint32_t *)(Address + 4U) = (uint32_t)(Data >> 32U);

}

(4) 按64bit写, 写不了,单片机死机

我在HAL库里面把库函数FLASH_Program_DoubleWord改成如下进行验证的

static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
  /* Set PG bit */
  SET_BIT(FLASH->CR, FLASH_CR_PG);
  
  *(uint64_t *)Address = (uint64_t)Data;
  }

2.写入的首地址规则

经过实际验证得出:**写入首地址必须为8的整数倍,不然就会发生错误:如果为4的整数倍但不是8的整数倍,单片机不会死机,但是写入的值全部变成0x3F,其他既不是4的整数倍也不是8的整数倍的地址,单片机会死机。

按以下代码改变写入首地址即可进行验证

STMFLASH_Write(FLASH_CALIBRATION_ADDR+8,String,sizeof(String));
HAL_Delay(10);
STMFLASH_Read(FLASH_CALIBRATION_ADDR,buffer,sizeof(String)/4+1);
HAL_Delay(10);
Uart_Send_Data(buffer, sizeof(String));
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分