Вопрос

For context I'm using the mspgcc and the msp430g2553 on the launchpad development board.

I want to create a pointer that can point to various hardware registers. The standard header provides access to the registers through statements like this:

P1DIR &= 0x08;

The above is the direction register for port 1 gpio pins. P1DIR is a 8bit hardware register.

My attempts to assign a pointer that can be used to modify this are as follows:

volatile unsigned char *reg;
reg = (unsigned char *) &P1DIR;

This does not generate any errors or warnings by gcc however it does not work. When using the gdb to test for values reg contains the value 0xFFFF instead of 0x0022 (address of P1DIR) as stated in the data sheet.

Here are the lines from the header to help with the problem:

/* External references resolved by a device-specific linker command file */
#define SFR_8BIT(address)   extern volatile unsigned char address
...
SFR_8BIT(P1DIR);                              /* Port 1 Direction */

As far as I could find the linker uses a file called periph.x (located at /usr/local/msp430-uniarch-20110716/msp430/lib/ldscripts/msp430g2553) which contains the line:

__P1DIR = 0x0022;
Это было полезно?

Решение

Nothing compiler specific at all required, just standard C...

#define WDTCTL     (*((volatile unsigned short *)0x0120))
#define P1DIR (*((volatile unsigned char *)0x0022))
...
    WDTCTL = 0x5A80;
...
    P1DIR |= 0x01;
    P1OUT |= 0x01;
    P1DIR &= ~0x02;

Then check your disassembly to make sure it worked.

f8b4:   b2 40 80 5a     mov #23168, &0x0120 ;#0x5a80
...
    f8ca:   d2 d3 22 00     bis.b   #1, &0x0022 ;r3 As==01
    f8ce:   d2 d3 21 00     bis.b   #1, &0x0021 ;r3 As==01
    f8d2:   f2 f0 fd ff     and.b   #-3,    &0x0022 ;#0xfffd
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top