#define 语句用于内存地址 [英] #define statement for address of memory

查看:60
本文介绍了#define 语句用于内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个#define 语句有什么作用?它用于定义内存地址.但我不明白 (uint32_t *) 部分

What does this #define statement do? It is used to define an address of memory. But I don't understand the (uint32_t *) part

#define GPxDAT (uint32_t *) 0x6FC0 

推荐答案

通常用于访问映射到地址空间的硬件寄存器,或者一些特定的内存地址

It is usually used to access hardware registers mapped into the address space, or some particular memory addresses

硬件寄存器应该被定义为volatile,因为寄存器可以在没有任何程序活动的情况下改变(因为它们被硬件改变了).

Hardware registers should be defined as volatile as registers can change without any program activity (as they are changed by the hardware).

#define GPIOREGA ((volatile uint32_t *) 0x6FC0) 

然后你可以分配或读取这个内存位置(*GPIOREGA = something; something = *GPIOREGA)

then you can assign or read this memory location (*GPIOREGA = something; something = *GPIOREGA )

有时会以这种方式使用更复杂的数据结构(来自 STM32 标头的示例)

Sometimes much more complex data structures are used this way (example from the STM32 headers)

#define __IO volatile

typedef struct
{
  __IO uint32_t MODER;    /*!< GPIO port mode register,               Address offset: 0x00      */
  __IO uint32_t OTYPER;   /*!< GPIO port output type register,        Address offset: 0x04      */
  __IO uint32_t OSPEEDR;  /*!< GPIO port output speed register,       Address offset: 0x08      */
  __IO uint32_t PUPDR;    /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
  __IO uint32_t IDR;      /*!< GPIO port input data register,         Address offset: 0x10      */
  __IO uint32_t ODR;      /*!< GPIO port output data register,        Address offset: 0x14      */
  __IO uint32_t BSRR;     /*!< GPIO port bit set/reset register,      Address offset: 0x18      */
  __IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address offset: 0x1C      */
  __IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
} GPIO_TypeDef;

#define PERIPH_BASE           0x40000000U /*!< Peripheral base address in the alias region                                */

#define AHB1PERIPH_BASE       (PERIPH_BASE + 0x00020000U)
#define GPIOA_BASE            (AHB1PERIPH_BASE + 0x0000U)

#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)

你可以像任何普通指针一样使用它

And you can use it is as any normal pointer

GPIOA -> MODER |= (1 << 15);

这篇关于#define 语句用于内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆