如何使用主应用程序和引导加载程序中的一个功能? (嵌入式) [英] How can I use one function from main application and bootloader? (embedded)

查看:96
本文介绍了如何使用主应用程序和引导加载程序中的一个功能? (嵌入式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要说的是我基于cortex m4开发嵌入式设备的应用程序.

First of all I need to say I develop application for embedded device based on cortex m4.

我具有Bootloader和主应用程序所共有的功能.目前,我为bootloader和app一次编译了两次源文件.但是我的双库dfu空间不足,我只想在ROM中拥有一次这些功能.知道我该如何实现吗?

I have functions that are common for bootloader and main application. For now I compile source files 2 times once for bootloader and app. But I am running short on space for dual bank dfu and I would like to have those functions only once in ROM. Any idea how can I achieve this?

在某些情况下使用函数指针可能会有危险,请检查我的问题-

Using functions pointers maybe danger in some cases, check on my problems - Using pointer functions - 2 seperate applications on 1 device

推荐答案

这只是部分答案,并假定您可以使用相同的地址空间从主代码跳入Bootloader.然后,一种常见的技术是将"bootloader API"作为函数指针表提供.

This is only a partial answer and assumes you can jump into your bootloader from your main code using the same address space. Then, a common technique is to provide your "bootloader API" as a table of function pointers.

例如,您在引导加载程序中具有以下功能:

Say for example you have the following functions in your bootloader:

static int do_something(void)
{
    return 42;
}

static int do_something_else(int arg)
{
    return arg+5;
}

然后,您将在这样的标头中声明您的API:

Then you would declare your API in a header like this:

struct bootloaderApi
{
    int (*do_something)(void);
    int (*do_something_else)(int arg);
};

在自举程序的实现中,您可以在自己的部分中定义此表:

In the implementation of your bootloader, you define this table in its own section:

// this is GCC syntax, use whatever your compiler provides to specify the section
struct bootloaderApi api __attribute__((section("API"))) = {
    do_something,
    do_something_else
};

然后在构建引导加载程序时,请确保将您的部分放置在适当的固定地址上.例如使用GNU链接器,您的链接器脚本中可能会有类似的内容:

Then when building the bootloader, make sure your section is placed at a suitable fixed address. When e.g. using the GNU linker, you could have something like this in your linker script:

SECTIONS {
  // standard sections, e.g.:
  .text : { *(.text) }
  .data : { *(.data) } 
  .bss :  { *(.bss)  *(COMMON) }

  // your API table:
  .API 0x10000 : { *(.API) }
}

这现在假设您的API表将放置在0x10000处.然后,您可以执行以下操作从您的主要代码访问API:

This now assumes your API table will be placed at 0x10000. Then you could do the following to access the API from your main code:

struct bootloaderApi *api = (struct bootloaderApi *)0x10000;

api->do_something();

所有这些只是草图,可以使您了解如何以明智的方式进行此操作.这将在很大程度上取决于您的目标平台和所使用的工具链.

All of this is just a sketch to give you an idea how to do this in a sensible way. It will heavily depend on your target platform and the toolchain you're using.

这篇关于如何使用主应用程序和引导加载程序中的一个功能? (嵌入式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆