为什么在下面的代码中使用了CAN_TypeDef类型的指针?

时间:2016-07-12 15:26:30

标签: c protocols can-bus stm32f4discovery

我正在尝试理解代码,我担心的是为什么在下面的代码" CAN_TypeDef"正在使用,我可以简单地使用整数类型的指针,整数指针的不利后果是什么?

以下代码属于stm32f发现板的CAN(控制器区域网络)库代码

文件名中的

can.c

void CAN_setup (uint32_t ctrl)  {
  CAN_TypeDef *pCAN = (ctrl == 1) ? CAN1 : CAN2;
  uint32_t brp;

当我去定义CAN_Typedef时,它会把我扔到这里。

typedef struct
{
  __IO uint32_t              MCR;                 /*!< CAN master control register,         Address offset: 0x00          */
  __IO uint32_t              MSR;                 /*!< CAN master status register,          Address offset: 0x04          */
  __IO uint32_t              TSR;                 /*!< CAN transmit status register,        Address offset: 0x08          */
  __IO uint32_t              RF0R;                /*!< CAN receive FIFO 0 register,         Address offset: 0x0C          */
  __IO uint32_t              RF1R;                /*!< CAN receive FIFO 1 register,         Address offset: 0x10          */
  __IO uint32_t              IER;                 /*!< CAN interrupt enable register,       Address offset: 0x14          */
  __IO uint32_t              ESR;                 /*!< CAN error status register,           Address offset: 0x18          */
  __IO uint32_t              BTR;                 /*!< CAN bit timing register,             Address offset: 0x1C          */
  uint32_t                   RESERVED0[88];       /*!< Reserved, 0x020 - 0x17F                                            */
  CAN_TxMailBox_TypeDef      sTxMailBox[3];       /*!< CAN Tx MailBox,                      Address offset: 0x180 - 0x1AC */
  CAN_FIFOMailBox_TypeDef    sFIFOMailBox[2];     /*!< CAN FIFO MailBox,                    Address offset: 0x1B0 - 0x1CC */
  uint32_t                   RESERVED1[12];       /*!< Reserved, 0x1D0 - 0x1FF                                            */
  __IO uint32_t              FMR;                 /*!< CAN filter master register,          Address offset: 0x200         */
  __IO uint32_t              FM1R;                /*!< CAN filter mode register,            Address offset: 0x204         */
  uint32_t                   RESERVED2;           /*!< Reserved, 0x208                                                    */
  __IO uint32_t              FS1R;                /*!< CAN filter scale register,           Address offset: 0x20C         */
  uint32_t                   RESERVED3;           /*!< Reserved, 0x210                                                    */
  __IO uint32_t              FFA1R;               /*!< CAN filter FIFO assignment register, Address offset: 0x214         */
  uint32_t                   RESERVED4;           /*!< Reserved, 0x218                                                    */
  __IO uint32_t              FA1R;                /*!< CAN filter activation register,      Address offset: 0x21C         */
  uint32_t                   RESERVED5[8];        /*!< Reserved, 0x220-0x23F                                              */ 
  CAN_FilterRegister_TypeDef sFilterRegister[28]; /*!< CAN Filter Register,                 Address offset: 0x240-0x31C   */
} CAN_TypeDef;

我不明白为什么有typedef ..我有CAN的基地址和不同寄存器的偏移量,我没有找到上述代码的任何相关性。

我想尝试这种方式

/ * ========================= CAN BASE ADDRESS =============== * /

#define CAN1_BASE 0x40006800
#define CAN2_BASE  0x40006400

/ =========================================== =============== /

#define CAN1_MCR (CAN1_BASE+ 0x00)
#define CAN2_MCR (CAN2_BASE+ 0x00)  // 0x00 is offset for MCR

#define DEMO(X) (*(unsigned int*)(X)) //将使用它来输入强制转换和deference,访问寄存器。

DEMO(CAN1_MCR) =  (CAN_MCR_INRQ   |   CAN_MCR_NART    ); // CAN_MCR_INRQ and CAN_MCR_NART has hexadecimal vale pointing to specific bit in MCR register

2 个答案:

答案 0 :(得分:0)

关键词是重用。说

#define CAN1_MCR (CAN1_BASE+ 0x00)
#define CAN2_MCR (CAN2_BASE+ 0x00) 

表示您必须两次定义布局(包含所有双重维护问题)。结构方法允许定义一次,并为每个总线使用相同的定义。

答案 1 :(得分:0)

是...您可以使用整数指针并继续使用您使用的方法。 但是你会错过&#34; Code Readability&#34;和#34;结构化代码&#34;。

使用typedef结构,访问寄存器非常容易,代码易于阅读/理解 没别了!!