DSP 外设寄存器的定义(以TMS320F28035例程代码为例)——老干妈笔记

传统方法:

#define CPUTIMER0 (volatile unsigned long*) 0x0c00

缺点: 不便对寄存器的值进行位操作;
         无法单独查看寄存器的某一位。 

DSP外设寄存器定义:

  • 第一步:采用结构体、共用体、位域结构定义寄存器变量;
  • 第二步:通过 pragma 伪指令将寄存器变量分配到数据段;
  • 第三步:通过 MEMORY 伪指令指示寄存器的实际硬件空间;
  • 第四步:通过 SECTION 伪指令将寄存器数据段分配到实际硬件空间。

举例:【以 PCLKCR1 外设时钟控制寄存器1 为例(TMS320F2803x)】

DSP 外设寄存器的定义(以TMS320F28035例程代码为例)——老干妈笔记_第1张图片 PCLKCR1 外设时钟控制寄存器1【TMS320F2803x】

第一步:定义寄存器变量

  • 1、位域结构体定义外设时钟控制寄存器1 PCLKCR1
struct PCLKCR1_BITS  {      // bits  description
    Uint16 EPWM1ENCLK:1;    // 0     Enable SYSCLKOUT to EPWM1
    Uint16 EPWM2ENCLK:1;    // 1     Enable SYSCLKOUT to EPWM2
    Uint16 EPWM3ENCLK:1;    // 2     Enable SYSCLKOUT to EPWM3
    Uint16 EPWM4ENCLK:1;    // 3     Enable SYSCLKOUT to EPWM4
    Uint16 EPWM5ENCLK:1;    // 4     Enable SYSCLKOUT to EPWM5
    Uint16 EPWM6ENCLK:1;    // 5     Enable SYSCLKOUT to EPWM6
    Uint16 EPWM7ENCLK:1;    // 6     Enable SYSCLKOUT to EPWM7
    Uint16 rsvd1:1;         // 7     reserved
    Uint16 ECAP1ENCLK:1;    // 8     Enable SYSCLKOUT to ECAP1
    Uint16 rsvd2:5;         // 13:9  reserved
    Uint16 EQEP1ENCLK:1;    // 14    Enable SYSCLKOUT to EQEP1
    Uint16 rsvd3:1;         // 15    reserved
};
  • 2、共用体定义外设时钟控制寄存器1 PCLKCR1
union PCLKCR1_REG {
    Uint16              all;
    struct PCLKCR1_BITS bit;
};
  • 3、结构体定义系统控制结构体,包含系统控制所有寄存器
struct SYS_CTRL_REGS {
    union   XCLK_REG        XCLK;        // 0: XCLKOUT Control
    union   PLLSTS_REG      PLLSTS;      // 1: PLL Status Register
    union   CLKCTL_REG      CLKCTL;      // 2: Clock Control Register
    Uint16                  PLLLOCKPRD;  // 3: PLL Lock Period Register
    union   INTOSC1TRIM_REG INTOSC1TRIM; // 4: Internal Oscillator 1 Trim
    Uint16                  rsvd1;       // 5: reserved
    union   INTOSC2TRIM_REG INTOSC2TRIM; // 6: Internal Oscillator 2 Trim
    Uint16                  rsvd2[2];    // 8-7
    
    //
    // 9: Peripheral clock control register 2
    //
    union   PCLKCR2_REG     PCLKCR2;     
    
    Uint16                  rsvd3;       // 10
    
    //
    // 11: Low-speed peripheral clock pre-scaler
    //
    union   LOSPCP_REG      LOSPCP;      
    
    //
    // 12: Peripheral clock control register
    //
    union   PCLKCR0_REG     PCLKCR0;     
    
    //
    // 13: Peripheral clock control register
    //
    union   PCLKCR1_REG     PCLKCR1;     
    
    //
    // 14: Low-power mode control register 0
    //
    union   LPMCR0_REG      LPMCR0;      
    
    Uint16                  rsvd4;       // 15: reserved
    union   PCLKCR3_REG     PCLKCR3;     // 16: Peripheral clock register
    union   PLLCR_REG       PLLCR;       // 17: PLL control register
    
    //
    // No bit definitions are defined for SCSR because
    // a read-modify-write instruction can clear the WDOVERRIDE bit
    //
    Uint16                  SCSR;      //18: System control and status register
    
    Uint16                  WDCNTR;    //19: WD counter register
    Uint16                  rsvd5;     //20
    Uint16                  WDKEY;     //21: WD reset key register
    Uint16                  rsvd6[3];  //22-24
    
    //
    // No bit definitions are defined for WDCR because
    // the proper value must be written to the WDCHK field
    // whenever writing to this register.
    //
    Uint16                  WDCR;        // 25: WD timer control register
    
    Uint16                  rsvd7[6];    // 26-30
};
  • 4、定义系统控制结构体变量
extern volatile struct SYS_CTRL_REGS SysCtrlRegs;

第二步: 将寄存器变量分配到数据段

代码位于:DSP2803x_GlobalVariableDefs.c

#ifdef __cplusplus
#pragma DATA_SECTION("SysCtrlRegsFile")
#else
#pragma DATA_SECTION(SysCtrlRegs,"SysCtrlRegsFile");
#endif
volatile struct SYS_CTRL_REGS SysCtrlRegs;

第三步: 指示寄存器的实际硬件空间

代码位于:DSP2803x_Headers_nonBIOS.cmd

MEMORY
{

 PAGE 1:    /* Data Memory */

   SYSTEM      : origin = 0x007010, length = 0x000020     /* System control registers */

}

 

DSP 外设寄存器的定义(以TMS320F28035例程代码为例)——老干妈笔记_第2张图片 表源:TMS320F2803x Piccolo™ Microcontrollers.pdf

第四步: 将寄存器数据段分配到实际硬件空间

代码位于:DSP2803x_Headers_nonBIOS.cmd

SECTIONS
{
/*** Peripheral Frame 2 Register Structures ***/
   SysCtrlRegsFile   : > SYSTEM,      PAGE = 1
}

 

你可能感兴趣的:(DSP,F28035笔记,dsp)