TM4C123-HWREG()及外设寄存器地址说明

参考文件:

    tiTivaWare_C_Series-2.1.4.178inchw_types.h tiTivaWare_C_Series-2.1.4.178inchw_memmap.h tm4c123gh6pz datasheet

TIVA程序的编写中常看到这样的操作,比如HWREG((UART0_BASE+UART_O_CTL)或者HWREG(0x12345678)这样的形式

HWREG()的意思就是操作硬件寄存器的意思,里面的参数是tm4c芯片的硬件外设寄存器地址,可以在芯片手册memory地址分配找到。HWREG()是在hw_types.h中定义的,对应的还有其他几个函数,原型如下所示:

// Macros for hardware access, both direct and via the bit-band region.

//

//*****************************************************************************

#define HWREG(x) 

        (*((volatile uint32_t *)(x)))

#define HWREGH(x) 

        (*((volatile uint16_t *)(x)))

#define HWREGB(x) 

        (*((volatile uint8_t *)(x)))

#define HWREGBITW(x, b) 

        HWREG(((uint32_t)(x) & 0xF0000000) | 0x02000000 | 

              (((uint32_t)(x) & 0x000FFFFF) << 5) | ((b) << 2))

#define HWREGBITH(x, b) 

        HWREGH(((uint32_t)(x) & 0xF0000000) | 0x02000000 | 

               (((uint32_t)(x) & 0x000FFFFF) << 5) | ((b) << 2))

#define HWREGBITB(x, b) 

        HWREGB(((uint32_t)(x) & 0xF0000000) | 0x02000000 | 

               (((uint32_t)(x) & 0x000FFFFF) << 5) | ((b) << 2))

HWREG的参数是tm4c芯片的硬件外设寄存器地址.

附: tm4c123gh6pz 寄存器地址 手册2.4节 (92页) 本表格写明了每个外设的寄存器起始地址 对应于相应外设的每个寄存器,又有相应的地址偏移及寄存器描述(见下文) The Code, SRAM, and external RAM regions can hold programs. However, it is recommended that programs always use the Code region because the Cortex-M4F has separate buses that can perform instruction fetches and data accesses simultaneously.

在手册中每一个外设章节,均有Register Map 例: 上图所示的寄存器描述中, 每个外设的寄存器都写明了外设基地址和偏移量

以ADCRIS寄存器操作为例,函数表示为:

HWREG(0x40038000+0x00000004)

在 hw_adc. h中,对相应的寄存器地址均进行了宏定义 #define ADC_O_ACTSS 0x00000000 // ADC Active Sample Sequencer #define ADC_O_RIS 0x00000004 // ADC Raw Interrupt Status ……

在hw_memmap.h中,对外设寄存器的基地址进行了宏定义 #define ADC0_BASE 0x40038000 // ADC0 #define ADC1_BASE 0x40039000 // ADC1 ……

所以 HWREG(ADC0_BASE+ADC_O_RIS) 即与上式表示相同的意义。

综上所述:HWREG 就是用来取外设寄存器地址

经验分享 程序员 微信小程序 职场和发展