Creating variables in MCUXpresso IDE

 



Introduction

MCUXpresso is a tool with an interface to build firmware for several micrcocontrollers, for example NPX microcontrollers, on my case, to LPC17xx family. Recently, while developing a feature, I explored the use of variables to my case, where the code should be used for different boards with different configurations but the same logic.

An example would be ports configurations for LPC1756 and LPC1768. They have a different number of ports and also a different ports position, but use the same logic. In this case, the best option would be create variables to add to the code and build with different configurations, which contains different variable values. This way I can easily build firmware without to change the code.

So in this short post I will share about how to use variables and different configurations with the same code to build for different microcontrollers.

Configurations

Like told before, the same code could be used for different boards, and the key to do that and to not create several branches with different values is to use different configurations on IDE. The example below contains a model with how the code could be written in a way that, depending of  LPC_CHIP value, something would be added to the code compilation or not.

#define LPC_1756    0
#define LPC_1768    1

#if LPC_CHIP == LPC_1768
.
.
.
#elseif
.
.
.
#endif

The opposite would be to not use conditions and define the values. If a different board was added would be necessary to create a new branch with the new configuration. If you have a new feature the code should be added in both branches, whats is not the ideal. The configurations use variables, like LPC_CHIP, that will be presented in the next topic.

To create a new configuration you need to go to Project > C/C++ build and select one of the options, like Build Variables. At the top you will see the option Manage Configurations, and in this table you can select to duplicate or to create a new one.


When you select a configuration all the changes that you apply will be saved to this configuration. This is very useful for variables but also for others configs like Tool Settings includes and optimization, for example. In the image below you can see how can you select the option in the several configurations created. When you decide to build you can just select one of the options that all configurations will be applied.


Variables

Variables are created for configurations. Those variables can be used in the code, with conditions, to define for example the board and the ports. In the example below you can see how to define and how to use it. 

Here we a have the variable NumChannel defined for two different configurations and with different values.



After to define you need to add this variables to Preprocessor symbols, to be read by your code. 



The code will read NUM_CLANNELS, that is defined in Prepocessor, not NumChannel. See the example below.

#if NUM_CLANNELS == 64
.
.
.
#elseif
.
.
.
#endif

Conclusion

This way, using variables and configurations your build will became easier, reducing your code and branches.

Comments

Popular Posts