concatenate the variable using Macro - c

I have different Address in Macro's. Which I need to pick any of the address depends on my application. Here the Details below.
#define Location1_Subset1_Sub1 0x011F
#define Location1_Subset1_Sub2 0x0150
#define Location1_Subset1_Sub3 0x0170
#define Location1_Subset2_Sub1 0x0190
#define Location1_Subset2_Sub2 0x01AF
#define Location1_Subset2_Sub3 0x01EF
#define Location2_Subset1_Sub1 0x0211
#define Location2_Subset1_Sub2 0x0230
#define Location2_Subset1_Sub3 0x0240
#define Location2_Subset2_Sub1 0x027F
#define Location2_Subset2_Sub2 0x02A0
#define Location2_Subset2_Sub3 0x02EF
The above Macros is for Address.
if(cond)
{
var1 = 1;
if(cond)
{
var2 = 2;
}
if(cond)
{
var3 = 1;
}
}
uint32 = Read_Address = fn(var1, var2, var3);
This is an example of my application. Based on the var1, var2 and var3, macro should pick the respective address. According to example above. It should pick the Address Location1_Subset2_sub1.
I need to define one macro, which will concatenate the variable. I tried with below macro, which is not right.
#define fn(var1,var2,var3) (Location##var1_Subset##var2_sub##var3)
It is concat the string "Locationvar1_Subsetvar2_subvar3". But I want which will concate the value in var's. I Would be thankful, if some one guide me.

Macros and variables live in entirely different worlds: they cannot read the value of variables. Macros are expanded during the preprocessing stage, so your program isn't even compiled yet. They can only do purely textual manipulation of your source code.
Consider storing your constants in a static array:
static const uint32 fn[2][2][3] = {
{
{0x011F, 0x0150, 0x0170},
{0x0190, 0x01AF, 0x01EF}
},
/* ... */
};
Then you can access them directly with var1 to var3 as indices:
uint32 Read_Address = fn[var1 - 1][var2 - 1][var3 - 1];

Use this source code to concat the strings.
#define fn(var1,var2,var3) (Location##var1##_Subset##var2##_sub##var3)
But in your program, you can't do through this way.Becase Macro is processed in pre-compile time,not in running time.

Related

Assigning #define macros to variable

I am trying to change some code but am running into issues. Here are the existing lines:
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000UL)
#define GPIOF_BASE (AHB1PERIPH_BASE + 0x1400UL)
#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)
#define SSD1306_CS_Port GPIOF
I want to make SSD1306_CS_Port a variable and change the value in real time.
Thanks!
I tried:
GPIO_TypeDef *SSD1306_CS_Port = GPIOF;
But got an error about expansion of macro. What am I not getting here?
Your pointer can't have the same name as macro.
GPIO_TypeDef *SSD1306_CS_Port = GPIOF;
^^^^^ It is invalid
The name has to be distinct
GPIO_TypeDef *SSD1306_CS_Port_ptr = GPIOF;
You do not need to declare this pointer (it is even wrong as you add another level of indirection and use SRAM). Simple use:
SSD1306_CS_Port -> ODR |= SSD1306_CS_Pin;
//you need to define SSD1306_CS_Pin as well of course
//same with other GPIO registers

How to shorten these lines using preprocessor?

I have these lines in my code and was thinking that there may be some nice way to use preprocessor to generate these lines (0 through 31).
Mem_type MEM_0[MAX_NUM_MEM];
Mem_type MEM_1[MAX_NUM_MEM];
Mem_type MEM_2[MAX_NUM_MEM];
Mem_type MEM_3[MAX_NUM_MEM];
Mem_type MEM_4[MAX_NUM_MEM];
Mem_type MEM_5[MAX_NUM_MEM];
...
Mem_type MEM_30[MAX_NUM_MEM];
Mem_type MEM_31[MAX_NUM_MEM];
Mem_type *MEM[NUM_BANKS];
MEM[0] = MEM_0;
MEM[1] = MEM_1;
MEM[2] = MEM_2;
MEM[3] = MEM_3;
MEM[4] = MEM_4;
MEM[5] = MEM_5;
...
MEM[30] = MEM_30;
MEM[31] = MEM_31;
For example, something like:
#define Gen(n) MEM[n] = MEM_n
#for (k=0; k<32; k++) Gen(k);
(The reason why I don't do like the below is that I found that my machine has some maximum contiguous array size limit, so I tried to split it into separate arrays so that I can have larger aggregated MEM size.)
Mem_type MEM[NUM_BANKS][MAX_NUM_MEM];
Using boost/preprocessor/repetition/repeat.hpp :
#include <boost/preprocessor/repetition/repeat.hpp>
class Mem_type {};
#define MAX_NUM_MEM 5
#define NUM_BANKS 5
#define MEM_DECL(z, n, text) Mem_type MEM_## n[MAX_NUM_MEM];
#define MEM_MEMB(z, n, text) MEM_## n,
// expands to `Mem_type MEM_?[MAX_NUM_MEM];`
BOOST_PP_REPEAT(NUM_BANKS, MEM_DECL, ())
Mem_type *MEM[NUM_BANKS] = {
// expands to `MEM_?,`
BOOST_PP_REPEAT(NUM_BANKS, MEM_MEMB, ())
};
There are articles on the net about symbolic computations on preprocessing stage, one typical instance would be http://jhnet.co.uk/articles/cpp_magic
If the machinery defined there is too much for you and you don't care much about the prettiness of generated code you could use a cheap alternative, something like (untested):
#define ONE_MEM(i, a) MemType mem_ ## a[MAX_MEM]; mem[i] = mem_ ## a
#define MEM_1(i, a) ONE_MEM(i, a); ONE_MEM(i + 1, a ## 1)
#define MEM_2(i, a) MEM_1(i, a); MEM_1(i + 2, a##2)
#define MEM_4(i, a) MEM_2(i, a); MEM_2(i + 4, a##4)
and so forth, now it's logarithmic in amount of macros defined.
(Haven't tested it, the actual definition might need a concat indirection or two.)
There can be improvements, like declaring a macro parameter to substitute for mem etc.
The stack is very limited and should not be used to allocate huge data structures like this as discussed here. Instead try to allocate your memory using new[]. If you do need multidimensional arrays you can use an array of pointers that point to arrays pointing to your structures as described here.
However as your initial intention was to have a single array, this should do the trick without the need of the preprocessor:
Mem_type* MEM = new Mem_type[MAX_NUM_MEM]; // MAX_NUM_MEM is multiplied by NUM_BANKS here
// do things [...]
delete[] MEM;
MEM = nullptr;
It would be an idea to wrap this up in a class, allocate in the constructor, throw an exception if allocation fails and deallocate in the destructor.
Using dynamic allocation with STL vectors:
#include <vector>
class Mem_type {};
const int MAX_NUM_MEM = 5;
const int NUM_BANKS = 5;
// allocates NUM_BANKS vectors with MAX_NUM_MEM objects of Mem_type
std::vector<std::vector<Mem_type>> MEM(NUM_BANKS, std::vector<Mem_type>(MAX_NUM_MEM));
You can use an X Macro along with token concatenating
#define LIST_OF_MEMS \
X(0) \
X(1) \
X(2) \
X(3) \
X(4) \
X(5) \
... \
X(30) \
X(31) \
Now you can use it every time you want to do anything with all the MEMs. Note that using all caps for a variable name is a bad idea
// declaration
#define X(num) Mem_type mem_##num[MAX_NUM_MEM];
LIST_OF_MEMS
#undef X
// assignment
#define X(num) MEM[num] = mem_##num;
LIST_OF_MEMS
#undef X

Can I define a macro using a macro in C?

I have a set of #defines like these:
#define MODULE1_PINMASK 0x1
#define MODULE2_PINMASK 0x2
#define MODULE3_PINMASK 0x3
where the value of the pinmask depends on the second argument of:
#define MODULE1_PORT_PIN A,1
#define MODULE2_PORT_PIN A,2
#define MODULE3_PORT_PIN A,3
If at any point in future, I make a change, e.g:
#define MODULE1_PORT_PIN A,1 /* changes to #define MODULE1_PORT_PIN A,4 */
I need to also change the pinmask:
#define MODULE1_PINMASK 0x1 /* then becomes #define MODULE1_PINMASK 0x4 */
I'm trying to automate the process by not having to manually change the pinmask. So far I've got these macros to extract the second argument of MODULEX_PORT_PIN (I don't care about the first argument in this case):
#define GET_SECOND(X, Y) Y
#define GET_PIN(PORT_PIN) GET_SECOND(PORT_PIN)
If i use them in functions, I get the correct result, for instance:
uint8_t pinmask=0x0;
switch (GET_PIN(MODULE2_PORT_PIN))
{
case 1:
pinmask = 0x1;
break;
case 2:
pinmask = 0x2;
break;
case 3:
pinmask = 0x3;
break;
default:
break;
}
printf ("%#x", pinmask); /* prints "0x2" */
but I want to keep the pinmasks as #defines. Is there a way to implement a #define GET_PINMASK macro which uses the switch case to define the pinmask? I'm aiming for something like:
#define MODULE1_PINMASK ASSIGN_PINMASK(GET_PIN(MODULE1_PORT_PIN))
which in this case would define MODULE1_PINMASK as 0x1.
EDIT: The second argument in #define MODULE1_PORT_PIN A,1 is an uint8_t and not a hex value and so I can't pass it directly.
I think you may be overthinking the problem. If the second field of each MODULEn_PORT_PIN define is always an integer constant expression, then this should work:
#define MODULE1_PORT_PIN A,1
#define MODULE2_PORT_PIN A,2
#define MODULE3_PORT_PIN A,3
#define GET_SECOND(X, Y) (Y)
#define PIN_TO_MASK(PIN) (1ul << GET_SECOND(PIN))
#define MODULE1_PINMASK PIN_TO_MASK(MODULE1_PORT_PIN)
#define MODULE2_PINMASK PIN_TO_MASK(MODULE2_PORT_PIN)
#define MODULE3_PINMASK PIN_TO_MASK(MODULE3_PORT_PIN)
It is not clear from your question whether the second field can be something other than an integer constant expression. If the second field ever involves an enum constant, then the MODULEn_PINMASK macros can still be used in any context except for #if expressions. If it ever involves a variable, then they can only be used inside the body of a function. (Since this is C and not C++, that's true even if the variable is const.)
There is no way to avoid having to write each #define individually. If that is a problem, you should be thinking about writing a program that generates the list of #defines. Generating source code from a DSL of your own invention, at build time, is an under-valued technique.
Have you considered using x-macros?
You start by creating an abstract #define for the list of entries:
#define CREATE_LIST() \
ENTRY(1, A, 0x1) \
ENTRY(2, A, 0x2) \
ENTRY(3, A, 0x3)
And then invoke the list for different definitions of ENTRY:
// Get the number of entries. Creates something like:
// const uint8_t PIN_COUNT = 0 + 1 + 1 + 1;
#define ENTRY(number, x, y) + 1
const uint8_t PIN_COUNT = \
CREATE_LIST()
;
#undef ENTRY
// Array of first parameters
#define ENTRY(number, x, y) #x ,
const char * Pin_names[PIN_COUNT] =
{
CREATE_LIST()
};
#undef ENTRY
// Array of second parameters
#define ENTRY(number, x, y) y,
const uint8_t Pin_masks[PIN_COUNT] =
{
CREATE_LIST()
};
#undef ENTRY
// Array of module names
#define ENTRY(number, x, y) STRINGIFY(MODULE ## number) ,
const char * Module_names[PIN_COUNT] =
{
CREATE_LIST()
};
#undef ENTRY
The preprocessor will expand this to something like:
const uint8_t PIN_COUNT =
+ 1 + 1 + 1
;
const char * Pin_names[PIN_COUNT] =
{
"A" , "A" , "A" ,
};
const uint8_t Pin_masks[PIN_COUNT] =
{
0x1, 0x2, 0x3,
};
const char * Module_names[PIN_COUNT] =
{
"MODULE1", "MODULE2", "MODULE3"
};
The possibilities are endless. It's less readable, but perhaps slightly more maintainable.

How to separate #defined arguments in C

I'm working on an embedded project and I have all the ports and pins defined like this:
#define SENSOR_1 gpioPortA,15
Is there a way to extract just the integer without creating a new #define?
So far I used a typedef and assigned the pin value from the #define like so:
typedef struct
{
GPIO_Port_TypeDef port;
uint8_t pin;
}sensor_t;
sensor_t sensor1 = {SENSOR_1};
/* Now sensor1.pin is the pin value */
but I want to pass the pin to a switch case and I can only use constant values for that and I'd rather avoid using an if statement.
Is there a way to pass the pin to a switch case without a new #define?
Short answer is no.
You could however use sensor1.pin when needed but not in case of switch statement as switch does not support variables there. In this case use if-else statement.
Or do it like this:
#define SENSOR_1_PIN 10
#define SENSOR_1 my_port,SENSOR_1_PIN
and then in switch you use SENSOR_1_PIN in case part.
switch (condition) {
case SENSOR_1_PIN:
//Do stuff
break;
//....
}
Just to remind again as it was posted in first comment on your question. Doing that is very dangerous.
Most proper way would be to do it like this:
#define SENSOR_1_PORT GPIOB
#define SENSOR_1_PIN 15
//If structure order changes here, you may lead to wrong data interpretation
sensor_t sensor1 = {SENSOR_1_PORT, SENSOR_1_PIN};
If you are C99 compliant, you may do it even more safer like this:
//If structure order changes here, your data are still properly assigned to fields
sensor_t sensor1 = {.port = SENSOR_1_PORT, .pin = SENSOR_1_PIN};
You can define a macro to extract the value from your definition.
#define GET_SECOND(x, y) y
#define PIN_VALUE(x) GET_SECOND(x)
switch (pin) {
case PIN_VALUE(SENSOR_1):
/* ... */
break;
}
PIN_VALUE must allow SENSOR_1 to be expanded via helper macro so that the second part can be extracted.
The proper solution at this point is to re-design the definitions into something that makes more sense, or alternatively create new constants.
As a last resort, if you are stuck with these macros, you can parse them in the following way:
#include <stdio.h>
#include <stdint.h>
typedef int GPIO_Port_TypeDef; // whatever type this happens to be
typedef struct
{
GPIO_Port_TypeDef port;
uint8_t pin;
}sensor_t;
#define GET_FIELD(field,...) (sensor_t){__VA_ARGS__}.field
#define SENSOR_1 gpioPortA,15
int main (void)
{
int gpioPortA = 1;
printf("%d %d", GET_FIELD(port, SENSOR_1), GET_FIELD(pin, SENSOR_1));
}
The type-generic version would be:
#define GET_FIELD(type, field, ...) (type){__VA_ARGS__}.field
...
printf("%d %d", GET_FIELD(sensor_t, port, SENSOR_1), GET_FIELD(sensor_t, pin, SENSOR_1));
This scales variably no matter how many fields there are. This is however not recommended practice. Macros in general, and variadic macros in particular, should be avoided.
As for how to use run-time variables in case - you can't. Use an if-else if statement instead.
What about inserting a define inside a define ? Instead of directly adding 15, you could make a define holding 15 and insert it elsewhere.
E.g:
#define SENSORVAL 15
#define SENSOR_1 gpioPortA,SENSORVAL
typedef struct
{
GPIO_Port_TypeDef port;
uint8_t pin;
}sensor_t;
sensor_t sensor1 = {SENSOR_1};
/* Now sensor1.pin is the pin value */

Best way to define offsets via C preprocessor

I would like to define a macro that will help me to auto generate offsets. Something like this:
#define MEM_OFFSET(name, size) ...
MEM_OFFSET(param1, 1);
MEM_OFFSET(param2, 2);
MEM_OFFSET(param3, 4);
MEM_OFFSET(param4, 1);
should generate the following code:
const int param1_offset = 0;
const int param2_offset = 1;
const int param3_offset = 3;
const int param4_offset = 7;
or
enum {
param1_offset = 0,
param2_offset = 1,
param3_offset = 3,
param4_offset = 7,
}
or even (not possible using C-preprocessor only for sure, but who knows ;)
#define param1_offset 0
#define param2_offset 1
#define param3_offset 3
#define param4_offset 7
Is it possible to do without running external awk/bash/... scripts?
I'm using Keil C51
It seems I've found a solution with enum:
#define MEM_OFFSET(name, size) \
name ## _offset, \
___tmp__ ## name = name ## _offset + size - 1, // allocate right bound offset and introduce a gap to force compiler to use next available offset
enum {
MEM_OFFSET(param1, 1)
MEM_OFFSET(param2, 2)
MEM_OFFSET(param3, 4)
MEM_OFFSET(param4, 1)
};
In the comments to your post you mention that you're managing an EEPROM memory map, so this answer relates to managing memory offsets rather than answering your specific question.
One way to manage EEPROM memory is with the use of a packed struct. ie, one where there is no space between each of the elements. The struct is never instantiated, it is only used for offset calculations.
typedef struct {
uint8_t param1;
#ifdef FEATURE_ENABLED
uint16_t param2;
#endif
uint8_t param3;
} __packed eeprom_memory_layout_t;
You could then use code like the following to determine the offset of each element as needed(untested). This uses the offsetof stddef macro.
uint16_t read_param3(void) {
uint8_t buf;
eeprom_memory_layout_t * ee;
/* eeprom_read(offset, size, buf) */
eeprom_read(offsetof(eeprom_memory_layout_t, param3), sizeof(ee->param3), &buf);
return buf;
}
Note that the struct is never instantiated. Using a struct like this makes it easy to see your memory map at a glance, and macros can easily be used to abstract away the calls to offsetof and sizeof during access.
If you want to create several structures based on some preprocessor declarations, you could do something like:
#define OFFSET_FOREACH(MODIFIER) \
MODIFIER(1) \
MODIFIER(2) \
MODIFIER(3) \
MODIFIER(4)
#define OFFSET_MODIFIER_ENUM(NUM) param##NUM##_offset,
enum
{
OFFSET_FOREACH(OFFSET_MODIFIER_ENUM)
};
The preprocessor would then produce the following code:
enum
{
param1_offset,
param2_offset,
param3_offset,
param4_offset,
}
I'm sure somebody will figure a nice preprocessor trick to compute the offset values with the sum of its predecessors :)
If you are doing this in C code, you have to keep in mind that const int declarations do not declare constants in C. To declare a named constant you have to use either enum or #define.
If you need int constants specifically, then enum will work well, although I the auto-generation part might be tricky in any case. Off the top of my head I can only come up with something as ugly as
#define MEM_OFFSET_BEGIN(name, size)\
enum {\
name##_OFFSET = 0,\
name##_SIZE__ = size,
#define MEM_OFFSET(name, size, prev_name)\
name##_OFFSET = prev_name##_OFFSET + prev_name##_SIZE__,\
name##_SIZE__ = size,
#define MEM_OFFSET_END()\
};
and then
MEM_OFFSET_BEGIN(param1, 1)
MEM_OFFSET(param2, 2, param1)
MEM_OFFSET(param3, 4, param2)
MEM_OFFSET(param4, 1, param3)
MEM_OFFSET_END()
Needless to say, the fact that it requires the next offset declaration to refer to the previous offset declaration by name defeats most of the purpose of this construct.
Try something like:
#define OFFSET(x) offsetof(struct {\
char param1[1], param2[2], param3[4], param4[1];\
},x)
Then you can use OFFSET(param1), etc. and it's even an integer constant expression.

Resources