简体   繁体   English

如何在运行时更改嵌入式 c 中 header 文件中定义的值?

[英]How to change the values defined in header file in embedded c during runtime?

I am working on a project using MSP430FR6047 and there is certain header file I need to access and change parameters previously defined.我正在使用 MSP430FR6047 开展一个项目,并且我需要访问和更改先前定义的参数的某些 header 文件。 At the moment I have to flash the MCU with modified header file every time I change the parameter but I was exploring if there is another option to do theses changes without flashing the new code, preferably by UART or some other communication protocol.目前,每次我更改参数时,我都必须使用修改过的 header 文件的 MCU 进行 flash MCU,但我正在探索是否有其他选项可以在不刷新新代码的情况下进行这些更改,最好是通过 UART 或其他一些通信协议。

So my question is how to change these parameters during runtime?所以我的问题是如何在运行时更改这些参数? Does any one know where should I start?有谁知道我应该从哪里开始?

Thanks谢谢

A running program cannot change its source code, supposed that you mean something like #define PARAMETER 23 .正在运行的程序无法更改其源代码,假设您的意思是#define PARAMETER 23 You need variables instead of constants.您需要变量而不是常量。

One primitive solution is this:一个原始的解决方案是:

  1. Invent a global variable per parameter, declare all of them in an extra header file and define all of them in an extra source file for better maintenance.为每个参数创建一个全局变量,在额外的 header 文件中声明所有变量,并在额外的源文件中定义所有变量以便更好地维护。

  2. In the new header file undefine all parameter macros and redefine them to use the variable instead of the literals.在新的 header 文件中取消定义所有参数宏并重新定义它们以使用变量而不是文字。

  3. In the using source files, include the original header file, after that include your new header file.在使用源文件中,包含原始 header 文件,然后包含新的 header 文件。

  4. Initialize the variables initially, and change parameters as you wish during run time.最初初始化变量,并在运行时根据需要更改参数。 (Initialization could be done in the new source file.) (可以在新的源文件中进行初始化。)

This solution avoids heavy editing the using source files and leaves the original header file intact.此解决方案避免大量编辑使用的源文件,并保持原始 header 文件完好无损。

Example:例子:

/* original.h */

#define PARAMETER 23

int f(void); /* returns PARAMETER */
/* new.h */

#if defined(PARAMETER)
    #undef PARAMETER
    #define PARAMETER parameter
#endif

extern int parameter;
/* new.c */

#include "new.h" /* ensures that declarations and definitions match */

int parameter = 23;
/* original.c */

#include "original.h"
#include "new.h"

int f(void) {
    return PARAMETER;
}
/* main.c */

#include <stdio.h>

#include "original.h"
#include "new.h"

int main(void) {
    PARAMETER = 42;
    printf("%d\n", f());
}

If you like to change the original source code, feel free to get rid of all this preprocessor stuff, and directly use variables instead of constants.如果您想更改原始源代码,请随意摆脱所有这些预处理器的东西,并直接使用变量而不是常量。 But then you should re-think your design and provide parameters as arguments to existing or new functions.但是,您应该重新考虑您的设计,并将参数作为 arguments 提供给现有或新功能。 Global variables should be avoided, reasons are left as an exercise to you.应避免使用全局变量,原因留给您练习。

There are 2 cases which change parameter in header file.在 header 文件中更改参数有 2 种情况。 Case 1: Header define default value For example, in header file you have:案例 1:Header 定义默认值 例如,在 header 文件中,您有:

    #define DEFAULT_VALUE      10

then in.c file if it is using like:然后在.c 文件中,如果它使用如下:

    if (a < DEFAULT_VALUE) 
    { /* Do something */ }

If this is the case you could update as following:如果是这种情况,您可以更新如下:

  • Modified the original line:修改了原行:

     if (a < var_DefaultValue) { /* Do something */ }
  • With var_DefaultValue is global variable: var_DefaultValue是全局变量:

     int var_DefaultValue = DEFAULT_VALUE;
  • By default, this will work as original.默认情况下,这将作为原始文件工作。

  • If you want to change value, you could create a thread to receive new value somewhere and then update to var_DefaultValue .如果要更改值,可以创建一个线程以在某处接收新值,然后更新为var_DefaultValue

Case 2: Header file define some precompile tag.案例 2:Header 文件定义了一些预编译标签。 For example:例如:

    #define DEFAULT_FEATURE  1

and in.c file you refer to feature as following:在.c 文件中,您指的功能如下:

    #if DEFAULT_FEATURE
    /* Do Something */
    #endif

For this case, it is impossible to change it by any mean.对于这种情况,无论如何都不可能改变它。

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

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