gpio宏定义追踪如下:
1、在led.h里定义
//LED端口定义
#define LED0 PAout(8)// PA8
#define LED1 PDout(2)// PD2
2、在sys.h里定义
//All rights reserved
//********************************************************************************
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
3、在stm32f10x.h里
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
#define FLASH_BASE ((uint32_t)0x08000000) /*!< FLASH base address in the alias region */
#define SRAM_BASE ((uint32_t)0x20000000) /*!< SRAM base address in the alias region */
#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */
#define SRAM_BB_BASE ((uint32_t)0x22000000) /*!< SRAM base address in the bit-band region */
#define PERIPH_BB_BASE ((uint32_t)0x42000000) /*!< Peripheral base address in the bit-band region */
#define FSMC_R_BASE ((uint32_t)0xA0000000) /*!< FSMC registers base address */
/*!< Peripheral memory map */
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
存在问题:1.为什么加的数字是12和8,这两个数字意味着什么?
2.这个是位带操作,即#define LED0 PAout(8)// PA8,#define LED1 PDout(2)// PD2 ,PA8和PD2意味着在0X40000000^0X400FFFFC.31这1MB范围里占两个位,而相对应的在等效的别名区即0X42000000.0^0X43FFFFFC.0里占两个32位的寄存器,PD2=0,意味着在等效别名区一个32位的寄存器等于0,那么这个32位寄存器控制IO的机理是什么?
3.若把这段代码应用到STM32F207XXXX上,是否只需设置STM32F207XXXX库函数里stm32f10x.h的响应值,不修改其他文件即可,
尤其是对
#define GPIOA_ODR_Addr (GPIOA_BASE+12)
#define GPIOD_ODR_Addr (GPIOD_BASE+12)#define GPIOA_IDR_Addr (GPIOA_BASE+8)
#define GPIOD_IDR_Addr (GPIOD_BASE+8)
里,12和8的数值不变? |