I am having a very hard time figuring out how to solve the following problem. I am on an embedded system with very little memory and want to minimize memory usage. Pointers
Usual way is to declare a struct, for example :
struct RegsAtAddrA
{
unsigned int array1[10];
char val1;
// etc
};
then to access it :
volatile RegsAtAddrA *pRegsA = (volatile RegsAtAddrA *) 0x40004000;
pRegsA->val1= 'a';
//etc
EDIT: I just realized that I haven't answered the question. So, here it is :
#include <iostream>
unsigned long a=1;
unsigned long b=2;
volatile unsigned long *port_base_array[] = {
&a,
&b,
//etc
};
int main()
{
std::cout<<"a="<<*port_base_array[0]<<std::endl;
std::cout<<"b="<<*port_base_array[1]<<std::endl;
}
I don't know why @Etienne deleted his answer, but it contained the essential information: The address is cast to volatile unsigned long *. That's what you need an array of.
typedef volatile unsigned long* reg_addr;
reg_addr registers[] = {
&GPIO_PORTA_BASE,
&GPIO_PORTB_BASE,
// ...
};
We need to take the address again (&GPIO_PORTA_BASE), since the macro automatically dereferences them. Access as:
*registers[i] &= your_value;
What I think you're trying to do is something like this:
volatile unsigned long * gpio_porta = &GPIO_PORTA_BASE;
If you're using C++, you could also do the following:
volatile unsigned long & reg_foo = (&GPIO_PORTA_BASE)[3];
volatile unsigned long & reg_foo = gpio_porta[3];
And use it as:
reg_foo &= 0x1;
However, most times I would expect a base address register to actually be stored as a pointer, rather than as the dereference of the pointer. Because of that, I would probably want your macros to be defined as:
#define GPIO_PORTA_BASE ((volatile unsigned long *) 0x40004000)
#define GPIO_PORTB_BASE ((volatile unsigned long *) 0x40005000)
And then you could simply access them as
GPIO_PORTA_BASE[3] &= 0x1
If I'm getting you right, this should be enough:
volatile unsigned long* GPIO_PORTA = (volatile unsigned long*) 0x40004000;
You could use that as
volatile unsigned long regxx = GPIO_PORTA[0x17];
// even
GPIO_PORTA[10] &= 0xF000;