0

I am working on a project using MSP430FR6047 and there is certain header file I need to access and change parameters previously defined. 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.

So my question is how to change these parameters during runtime? Does any one know where should I start?

Thanks

Antony
  • 13
  • 6
  • Definitely you need some storage for **variables** assuming you are not going to re-flash the main ROM each time you change the **variable**. – 0andriy Aug 03 '22 at 21:06
  • Header files are just source code, they could contain anything, althey they are usually contain declarative code only. You need to include an example in code of what you mean by a "parameter" since the nature of that definition will determine the most appropriate answer without guesses or assumptions. Also what are the constraints? Can you modify "_certain header file_" or is it to remain unmodified (perhaps vendor supplied or a third-party library for example). – Clifford Aug 04 '22 at 06:06

2 Answers2

0

A running program cannot change its source code, supposed that you mean something like #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.

  2. In the new header file undefine all parameter macros and redefine them to use the variable instead of the literals.

  3. In the using source files, include the original header file, after that include your new header file.

  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.

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. Global variables should be avoided, reasons are left as an exercise to you.

the busybee
  • 10,755
  • 3
  • 13
  • 30
0

There are 2 cases which change parameter in header file. Case 1: Header define default value For example, in header file you have:

    #define DEFAULT_VALUE      10

then in .c file if it is using like:

    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:

      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.

Case 2: Header file define some precompile tag. For example:

    #define DEFAULT_FEATURE  1

and in .c file you refer to feature as following:

    #if DEFAULT_FEATURE
    /* Do Something */
    #endif

For this case, it is impossible to change it by any mean.

ThongDT
  • 162
  • 7