简体   繁体   English

如何在msp430上分配一个变量指针来访问硬件寄存器?

[英]How to assign a variable pointer to access hardware register on msp430?

For context I'm using the mspgcc and the msp430g2553 on the launchpad development board. 对于上下文,我在启动板开发板上使用mspgccmsp430g2553

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. 以上是端口1 gpio引脚的方向寄存器。 P1DIR is a 8bit hardware register. P1DIR是一个8位硬件寄存器。

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. 这不会通过gcc生成任何错误或警告,但它不起作用。 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. 当使用GDB来测试值REG包含值0xFFFF ,而不是0x0022 (的地址P1DIR如在数据表中注明)。

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: 据我所知,链接器使用名为periph.x的文件(位于/usr/local/msp430-uniarch-20110716/msp430/lib/ldscripts/msp430g2553 ),其中包含以下行:

__P1DIR = 0x0022;

Nothing compiler specific at all required, just standard C... 根本不需要任何特定的编译器,只需标准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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM