简体   繁体   English

“函数指针模式”下 function 的返回值

[英]Return value from function in "function pointer mode"

First, sorry for my English.首先,对不起我的英语。 I'm brazilian still trying to learn (english,and programming).我是巴西人,仍在努力学习(英语和编程)。

I'm trying to create a firmware application (STM32 uc), that access other firmware (my address was divided in BL + FWA + FWB), but I have one problem and can't find the reason.我正在尝试创建一个固件应用程序 (STM32 uc),它可以访问其他固件(我的地址分为 BL + FWA + FWB),但我有一个问题,找不到原因。 FWB needs to access a function on FWA, to use less flash area. FWB需要在FWA上访问一个function,少用flash区域。 I can access the function itself, execute any operations inside it, but when return is reached, it gives-me an HardFault.我可以访问 function 本身,在其中执行任何操作,但是当返回时,它会给我一个 HardFault。 I'm using scatter file to use memory shared on both FWA and B.我正在使用分散文件来使用在 FWA 和 B 上共享的 memory。

  LR_IROM1 0x08004000 0x00004000  {    ; load region size_region
  ER_IROM1 0x08004000 0x00004000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 UNINIT 0x00001000  {  ; No init data
   *(.noinit)
  }
  RW_IRAM2 0x20001000 0x00004000  {
   .ANY (+RW +ZI) //RW data
  }
}

and my funcition is defined as我的功能定义为

uint getInt(void) __attribute__((section(".ARM.__at_0x8006000")));//My fw was at 0x4000, total size is 0x4000,
                                                                  //so space is not the problem.
uint getInt(void){
    return 42;
}

On my application (running on FWA for testing itself, but no result), I'm trying to call it this way:在我的应用程序上(在 FWA 上运行以测试自身,但没有结果),我试图这样称呼它:

uint (*fun_ptr)(void) = (uint(*)(void))0x8006000;
uint _get_int= fun_ptr();
uint result = _get_int();

Edit: When using编辑:使用时

IntFunc p =             getInt;
uint ccccc = p();
uint aaaa = getInt();

all options work correcty, the problem is when using address.所有选项都正常工作,问题是使用地址时。

As I mentioned, I can enter the function "getInt()", I can execute any code instruction inside it, but when "return 42" is reached, I have a HardFault.正如我提到的,我可以输入 function“getInt()”,我可以执行其中的任何代码指令,但是当到达“return 42”时,我遇到了 HardFault。

I find another way to do this, using structs, working, but I think that is very important to understand this problem, understand where is the error, this helps to don't make mistakes again.我找到了另一种方法来做到这一点,使用结构,工作,但我认为理解这个问题非常重要,理解错误在哪里,这有助于不再犯错误。

EDIT 2: I want to understand the problem, not just simply get an solution.编辑 2:我想了解问题,而不仅仅是简单地获得解决方案。 This method works to me: main.h file这种方法对我有用:main.h 文件

typedef struct bootloaderApi bootloaderApi;
typedef   unsigned int (*do_something_t)(void);
struct bootloaderApi{
    do_something_t do_something;
};

and Main.c:和 Main.c:

const struct bootloaderApi api __attribute__((section(".ARM.__at_0x8005000")))
= {.do_something = &getInt}; //address 0x5000 to test.

and using it as并将其用作

struct bootloaderApi *apit = (struct bootloaderApi *)0x8005000;
uint pa = api.do_something();

it works correctly, pa returns me the uint 42.它工作正常,pa 返回 uint 42。

Thanks.谢谢。

Try:尝试:

uint (*fun_ptr)(void) = (uint(*)(void))0x8006001;

because all function pointers on this architecture must be odd.因为这个架构上的所有 function 指针都必须是奇数。

The address is the real address of the first instruction (which is always even) plus one.地址是第一条指令(总是偶数)的实际地址加一。 The last bit indicates that the instruction uses the "thumb" instruction set, not the "arm" instruction set which is not available on this device.最后一位表示该指令使用“thumb”指令集,而不是“arm”指令集,后者在该设备上不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM