在STM32中将变量或代码设定在指定内存地址的方法

各个编译器有自己不同的语法

对于arm_none_eabi_gcc:

// Sampling variables
#define ADC_SAMPLE_MAX_LEN		2000
__attribute__((section(".ram4"))) static volatile int16_t m_curr0_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_curr1_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph1_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph2_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_ph3_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_vzero_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile uint8_t m_status_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_curr_fir_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int16_t m_f_sw_samples[ADC_SAMPLE_MAX_LEN];
__attribute__((section(".ram4"))) static volatile int8_t m_phase_samples[ADC_SAMPLE_MAX_LEN];

在连接脚本中需声明该内存块

MEMORY
{
    flash : org = 0x08000000, len = 16k
    flash2 : org = 0x0800C000, len = 393216 - 16	/* NEW_APP_MAX_SIZE - CRC_INFO */
	crcinfo : org = 0x0805FFF0, len = 8		/* CRC info */
    ram0  : org = 0x20000000, len = 128k    /* SRAM1 + SRAM2 */
    ram1  : org = 0x20000000, len = 112k    /* SRAM1 */
    ram2  : org = 0x2001C000, len = 16k     /* SRAM2 */
    ram3  : org = 0x00000000, len = 0
    ram4  : org = 0x10000000, len = 64k     /* CCM SRAM */
    ram5  : org = 0x40024000, len = 4k      /* BCKP SRAM */
    ram6  : org = 0x00000000, len = 0
    ram7  : org = 0x00000000, len = 0
}

并在连接脚本后面要有添加数据的操作

    .ram4 (NOLOAD) : ALIGN(4)
    {
        . = ALIGN(4);
        *(.ram4)
        *(.ram4.*)
        . = ALIGN(4);
        __ram4_free__ = .;
    } > ram4

针对IAR:


#if defined (CCMRAM)
#if defined (__ICCARM__)
#pragma location = ".ccmram"     //这里是针对IAR的语法
#elif defined (__CC_ARM) || defined(__GNUC__)
__attribute__( ( section ( "ccmram" ) ) )    //这里是针对GCC等编译器的语法
#endif
#endif
/**
  * @brief  It return the Vqd componets fed in input plus the feed forward
  *         action and store the last Vqd values in the internal variable.
  * @param  pHandle Feed forward  strutcture.
  * @param  Vqd Initial value of Vqd to be manipulated by FF .
  * @retval Volt_Components Vqd conditioned values.
  */
Volt_Components FF_VqdConditioning( FF_Handle_t * pHandle, Volt_Components Vqd )
{
  int32_t wtemp;

  pHandle->VqdPIout = Vqd;

  wtemp = ( int32_t )( Vqd.qV_Component1 ) + pHandle->Vqdff.qV_Component1;

  SATURATION_TO_S16( wtemp )

  Vqd.qV_Component1 = ( int16_t )wtemp;

  wtemp = ( int32_t )( Vqd.qV_Component2 ) + pHandle->Vqdff.qV_Component2;

  SATURATION_TO_S16( wtemp )

  Vqd.qV_Component2 = ( int16_t )wtemp;

  return ( Vqd );
}

上面的操作是将函数代码放到指定的存储区内。需要快速执行的代码或数据,可以放在SMT32的CCRAM区里。内核访问该存储区的速度比访问FLASH快很多。

关注公众号《首飞》回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2,机器人学等机器人行业常用技术资料。

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

首飞爱玩机器人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值