我正在研究 xMega AVR 微 Controller ,但在将硬件寄存器作为指针传递时遇到了问题。很确定这是指针魔术,但经过漫长的一天后无法理解哪里出了问题。
PORTA
定义(在 GCC-AVR 工具链中)如下:
#define PORTA (*(PORT_t *) 0x0600)
这个有效:
int main(void)
{
// This will set GPIO high
PORTA.DIRSET = 1 << 1;
PORTA.OUTSET = 1 << 1;
while(1) {};
}
这行不通:
void gpio_output_set(PORT_t * port, unsigned char pin)
{
// This will *NOT* set GPIO high
port->DIRSET = 1 << pin;
port->OUTSET = 1 << pin;
}
int main(void)
{
gpio_output_set(&PORTA, 1);
while(1) {};
}
为什么?
最佳答案
我认为它应该有效。
本质上是一样的
PORT_t* pPort = &*(PORT_t*)0x0600;
pPort->DIRSET = 0x01;
那么为什么这会成为一个问题呢? Atmel 软件框架库也使用它:
void PORT_ConfigureInterrupt0(PORT_t* port, PORT_INT0LVL_t intLevel, uint8_t pinMask);
PORT_ConfigureInterrupt0(&PORTC, PORT_INT0LVL_MED_gc, 0x01);
关于c++ - 传递硬件寄存器指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38592506/