[STM32L5] HAL I2C 函数调用时,静态常量变量被分配到 RAM 的问题

[复制链接]
522|4
SocialButterfly 发表于 2025-11-15 11:36 | 显示全部楼层 |阅读模式
我的项目中有一个 4096 字节的数据数组,需要通过 I2C 传递给另一个元件。
这个数组是常量,因此我按以下方式声明,以便将其存储在flash中而非 RAM 中:
static const uint8_t myArray[4096] = {
0x80,0x40,0x00,0x11,
...
当我将该变量的指针传递给 HAL I2C 函数后,它突然被分配到了 RAM 中,编译器还报出了 RAM 溢出错误:
HAL_I2C_Slave_Sequential_Transmit_IT(&h_i2c, (uint8_t *)myArray, 4096, I2C_FIRST_AND_LAST_FRAME);

/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-10.macos64_1.0.0.11181127/tools/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: my-proj.elf section `._user_heap_stack' will not fit in region `RAM'
/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-10.macos64_1.0.0.11181127/tools/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: region `RAM' overflowed by 16 bytes
collect2: error: ld returned 1 exit status
make: *** [makefile:87: stereo-slo-051.elf] Error 1
"make -j11 all" terminated with exit code 2. Build might be incomplete.
这该如何解决?

stormwind123 发表于 2025-11-17 19:23 | 显示全部楼层
低优化级别下,编译器可能无法智能识别 const 数据的持久性,默认将其放入 RAM 以确保安全访问。
classroom 发表于 2025-11-17 20:23 | 显示全部楼层
非阻塞传输需要缓冲区在中断上下文中持续有效。若 HAL 内部实现未显式处理 const 数据,编译器可能认为 Flash 中的常量无法满足动态访问需求,从而强制复制到 RAM。
cr315 发表于 2025-11-17 21:24 | 显示全部楼层
可能需要强制将数组存储在 Flash 中
duo点 发表于 2025-11-17 21:24 | 显示全部楼层
分块传输避免大缓冲区
您需要登录后才可以回帖 登录 | 注册

本版积分规则

70

主题

70

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部