dog 发表于 2022-7-25 14:44:48

请教两个STM32G0的问题,64位Flash操作,字节对齐问题

用STM32G0系列已有一段时间了,有一些疑问,请教大家:

1. STM32G0系列的内部Flash读写是支持64位的,大家用的是32位还是64位?哪一种速度快?

2. STM32G0系列在字节未对齐的情况下,强制转换会出错,进入HardFault,之前习惯用的 a=*(u32*)addr这类操作,我都改用了memcpy(&a,addr,size);
谁能解释一下原因?


工程师030 发表于 2022-7-25 15:02:00

1、要速度,这芯片有快速编程模式,一次256字节
2、哪个芯片都得字节对齐吧?存什么样的数据,就得按照大小对齐

dog 发表于 2022-7-25 15:07:21

工程师030 发表于 2022-7-25 15:02
1、要速度,这芯片有快速编程模式,一次256字节
2、哪个芯片都得字节对齐吧?存什么样的数据,就得按照大小 ...
(引用自2楼)

1. 256字节是什么概念?请细说一下吧
2. 如果你用过,你会发现区别的。

442502587 发表于 2022-7-25 15:09:08

必须 64位编程,32位就HardFault

dog 发表于 2022-7-25 15:23:34

442502587 发表于 2022-7-25 15:09
必须 64位编程,32位就HardFault
(引用自4楼)

没有的事,可以32位。

工程师030 发表于 2022-7-25 15:24:32

dog 发表于 2022-7-25 15:07
1. 256字节是什么概念?请细说一下吧
2. 如果你用过,你会发现区别的。
(引用自3楼)

你看下参考手册里flash章节就知道了,这个芯片一次好像是只能双字编程,快速编程的代码我打包发你了,这个c文件得编译到ram中才行,放在flash里面运行不了

442502587 发表于 2022-7-25 15:25:15

dog 发表于 2022-7-25 15:23
没有的事,可以32位。
(引用自5楼)

当我没讲,随便你

dog 发表于 2022-7-25 15:31:27

442502587 发表于 2022-7-25 15:25
当我没讲,随便你
(引用自7楼)



不是想和你抬杠,是怕误导大家。
请纠正一下

dog 发表于 2022-7-25 15:32:11

工程师030 发表于 2022-7-25 15:24
你看下参考手册里flash章节就知道了,这个芯片一次好像是只能双字编程,快速编程的代码我打包发你了,这 ...
(引用自6楼)

谢谢,我学习一下

dog 发表于 2022-7-25 15:34:32

也分享一下我写的Flash操作函数。

/***************************************************************************************************************
* @brief: Write u32 Data Array to Flash Address, the Addres must be 64-bits alignmented
* @param addr: Target Flash Address to be Store the Data.
* @param dat: Source u32 Data Array Address
* @param size: u32 Count of Data Array to be write.
* @return: true means Success; false means timeout.
**************************************************************************************************************/
bol FlashWrite32(u32 * addr, u32 * dat, u32 size)
{      
    MUTEX_LOCK();

    // Wait Flash Not Busy
    if(!FlashWaitNotBusy(1000)){
      MUTEX_UNLOCK();
      return false;
    }

    // Enable Program
    SET_BIT(FLASH->CR, FLASH_CR_PG);

    while(size--){
      *(__IO uint32_t*)(addr++) = *dat ++;
    }
   
    // Disable Program
    CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

    MUTEX_UNLOCK();
    return true;
}

/***************************************************************************************************************
* @brief: Write u64 Data Array to Flash Address, the Addres must be 64-bits alignmented
* @param addr: Target Flash Address to be Store the Data.
* @param dat: Source u32 Data Array Address
* @param size: u32 Count of Data Array to be write.
* @return: true means Success; false means timeout.
**************************************************************************************************************/
bol FlashWrite64(u64 * addr, u64 * dat, u32 size)
{
    MUTEX_LOCK();

    // Wait Flash Not Busy
    if(!FlashWaitNotBusy(1000)){
      MUTEX_UNLOCK();
      return false;
    }

    // Enable Program
    SET_BIT(FLASH->CR, FLASH_CR_PG);

    while(size--){
      *(__IO u64*)(addr++) = *dat ++;
    }

    // Disable Program
    CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

    MUTEX_UNLOCK();
    return true;
}

SUPER_CRJ 发表于 2022-7-25 17:06:32

dog 发表于 2022-7-25 15:31
不是想和你抬杠,是怕误导大家。
请纠正一下
(引用自8楼)

你这是读,他的意思是写:
手册表明写Flash必须每次写64bit。

The Flash memory is programmed 72 bits at a time (64 bits + 8 bits ECC).
Programming in a previously programmed address is not allowed except if the data to write
is full zero, and any attempt sets PROGERR flag in the Flash status register (FLASH_SR).
It is only possible to program double word (2 x 32-bit data).
• Any attempt to write byte or half-word sets SIZERR flag in the FLASH_SR register.
• Any attempt to write a double word which is not aligned with a double word address
sets PGAERR flag in the FLASH_SR register.

dukelec 发表于 2022-7-25 23:58:12

你的第二個問題,為了兼容性,一般不建議強制轉換,而是用宏轉換,開銷比 memcpy 小。

更多細節可參見嵌入式聖經 linux kernel:
https://www.kernel.org/doc/html/latest/core-api/unaligned-memory-access.html

這是我從 linux kernel 裏面借鑒的代碼,用於 mcu,位於文件末尾:
https://github.com/dukelec/cdnet/blob/master/utils/cd_utils.h

dog 发表于 2022-7-26 16:20:57

SUPER_CRJ 发表于 2022-7-25 17:06
你这是读,他的意思是写:
手册表明写Flash必须每次写64bit。

(引用自11楼)

我写的32位函数自己用过啊,而且上面坛友的也是32位的,
还有下面这个是HAL库里的:

static __RAM_FUNC void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
{
uint8_t index = 0;
uint32_t dest = Address;
uint32_t src = DataAddress;
uint32_t primask_bit;

/* Set FSTPG bit */
SET_BIT(FLASH->CR, FLASH_CR_FSTPG);

/* Enter critical section: row programming should not be longer than 7 ms */
primask_bit = __get_PRIMASK();
__disable_irq();

/* Fast Program : 64 words */
while (index < 64U)
{
    *(uint32_t *)dest = *(uint32_t *)src;
    src += 4U;
    dest += 4U;
    index++;
}

/* wait for BSY1 in order to be sure that flash operation is ended befoire
   allowing prefetch in flash. Timeout does not return status, as it will
   be anyway done later */

#if defined(FLASH_DBANK_SUPPORT)
while ((FLASH->SR & (FLASH_SR_BSY1 | FLASH_SR_BSY2)) != 0x00U)
#else
while ((FLASH->SR & FLASH_SR_BSY1) != 0x00U)
#endif /* FLASH_DBANK_SUPPORT */
{
}

/* Exit critical section: restore previous priority mask */
__set_PRIMASK(primask_bit);
}

dog 发表于 2022-7-26 16:28:44

dukelec 发表于 2022-7-25 23:58
你的第二個問題,為了兼容性,一般不建議強制轉換,而是用宏轉換,開銷比 memcpy 小。

更多細節可參見嵌入 ...
(引用自12楼)

谢谢大神指点,学习与膜拜

qinxg 发表于 2022-7-27 08:59:06

1. 32bits
2. 除了PC, 其他32bits CPU一般都会hardfaul.指针转换时脑里始终挂根悬

tomzbj 发表于 2022-7-27 11:32:12

2这个, M0/M0+内核不支持非对齐访问, M3/M4是可以的. memcpy是万能做法.

dog 发表于 2022-7-27 15:57:48

u32 CalcXor32(u32 *buf, u32 count)
{
    u32 xor = 0;

    while(count--){
      xor ^= get_unaligned32((u8*)buf);// 这个是学习了楼上dukelec的宏或内联的方法。
      // xor ^= (*buf);//这种貌似很常规的操作,都会出错。
      buf++;
    }

    return xor;
}

dog 发表于 2022-7-27 15:58:47

tomzbj 发表于 2022-7-27 11:32
2这个, M0/M0+内核不支持非对齐访问, M3/M4是可以的. memcpy是万能做法.
(引用自16楼)

是的,M0使用M3/M4的移植代码时要小心。

MasterPhi 发表于 2022-7-28 13:49:46

dog 发表于 2022-7-26 16:20
我写的32位函数自己用过啊,而且上面坛友的也是32位的,
还有下面这个是HAL库里的:


(引用自13楼)

你试试用你的32位写函数写奇数的数据

dog 发表于 2022-7-29 09:03:10

MasterPhi 发表于 2022-7-28 13:49
你试试用你的32位写函数写奇数的数据
(引用自19楼)

不懂,能不能说清楚一点?

liao-ljj 发表于 2022-7-30 21:37:50

前一段时间调试,发现不能放到Main中写Flash,会有异常中断.....到现在没时间搞了1
页: [1]
查看完整版本: 请教两个STM32G0的问题,64位Flash操作,字节对齐问题