Reputation: 143
I have a list of #define that describes a set of addresses:
#define DMA1_Stream0 ((DMA_Stream_TypeDef *) DMA1_Stream0_BASE)
#define DMA1_Stream1 ((DMA_Stream_TypeDef *) DMA1_Stream1_BASE)
#define DMA1_Stream2 ((DMA_Stream_TypeDef *) DMA1_Stream2_BASE)
#define DMA1_Stream3 ((DMA_Stream_TypeDef *) DMA1_Stream3_BASE)
(this is from a CMSIS header) and a function that should use one of them depending on the given parameters like:
void initDMAStream(uint8_t controller, uint8_t stream)
{
DMA[controller]_Stream[stream]->CR = 0xdadadada;
// etc...
}
I tried using a macro like
#define DMA_STREAM(c, s) DMA ## c ## _Stream ## s
but it's not working because it will replace c
and s
with the names of the function parameters, not with their values.
Is there some way to do this in C?
Upvotes: 0
Views: 77
Reputation: 8677
It looks like you are trying to mix run-time and compile-time evaluation in an unholy way. In particular, you are trying to use run-time information at compile time.
You can make this work by throwing the compile-time constants into an array, and indexing into it at run-time. For example, set up the array as
DMA_Stream_TypeDef * DMAstreams[][4] = {
{DMA0_Stream0, DMA0_Stream1, DMA0_Stream2, DMA0_Stream3},
{DMA1_Stream0, DMA1_Stream1, DMA1_Stream2, DMA1_Stream3},
// ...
};
and access it with
DMAstreams[controller][stream]
at run-time.
Upvotes: 1