Am I just being dim? [struct access to registers]

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In another thread DP was saying how he'd been using an MCU where the registers/bits are accessible as if they were structure members. This is also how the Xmega does things (though it's to group registers in "blocks" not bits in registers).

To do this for mega168 I came up with:

#include 

#define IN 	0
#define OUT	1

typedef struct {
  uint8_t pinb0:1;
  uint8_t pinb1:1;
  uint8_t pinb2:1;
  uint8_t pinb3:1;
  uint8_t pinb4:1;
  uint8_t pinb5:1;
  uint8_t pinb6:1;
  uint8_t pinb7:1;
} pinb_t;

typedef struct {
  uint8_t ddb0:1;
  uint8_t ddb1:1;
  uint8_t ddb2:1;
  uint8_t ddb3:1;
  uint8_t ddb4:1;
  uint8_t ddb5:1;
  uint8_t ddb6:1;
  uint8_t ddb7:1;
} ddrb_t;

typedef struct {
  uint8_t pb0:1;
  uint8_t pb1:1;
  uint8_t pb2:1;
  uint8_t pb3:1;
  uint8_t pb4:1;
  uint8_t pb5:1;
  uint8_t pb6:1;
  uint8_t pb7:1;
} portb_t;

typedef struct {
  pinb_t  pinb;
  ddrb_t  ddrb;
  portb_t portb;
} io_t;

io_t io __attribute__((section(".io")));

int main(void) {

	io.ddrb.ddb3	= OUT;
	io.portb.pb3	= 1;	
}

which could be extended to more than 3 registers. The linker sets the address of ".io" to be 0x800025 (PINB on 168) and the code works.

But how can I do this without the "io." prefix? I know I can do it by having a separate named section for each register and a --section-start for each of those sections. But that would lead to 60..100 or more --section-starts given to the linker.

What am I missing? There must be an easier way to do this?

I just want:

int main(void) {

	ddrb.ddb3	= OUT;
	portb.pb3	= 1;	
}

PS I guess a linker script listing all the register bases for 60+ named sections is the way?

Last Edited: Wed. Sep 8, 2010 - 08:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not the usual casting?

#define portb (*((volatile portb_t*)&PORTB))

portb.pb3 = 1;

Stealing Proteus doesn't make you an engineer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I knew I was being dim - I was thinking that any such access would involve pointer dereference -> rather than member access.

Obviously time for bed here! :oops:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

a->b is just a shortcut for (*a).b

Stealing Proteus doesn't make you an engineer.