查看: 10272|回复: 17

[MPC] MPC5604B的CAN总线通信

[复制链接]
  • TA的每日心情
    开心
    2017-1-12 19:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    3

    主题

    16

    帖子

    0

    注册会员

    Rank: 2

    积分
    111
    最后登录
    2019-2-25
    发表于 2015-9-22 15:38:31 | 显示全部楼层 |阅读模式
    /* main.c: FlexCAN program */
    /* Description: Transmit one message from FlexCAN 0 buf. 0 to FlexCAN C buf. 1
    (从 FlexCAN 0 buf. 0 传递一个消息到FlexCAN C buf. 1)*/  

    /* Rev 0.1 Jan 16, 2006 S.Mihalik, Copyright Freescale, 2006. All Rights Reserved */



    /* Rev 0.2 Jun 6 2006 SM - changed Flexcan A to C & enabled 64 msg buffers */

    /* Rev 0.3 Jun 15 2006 SM - 1. Made globals uninitialized */
    /*                          2. RecieveMsg function:  read CANx_TIMER, removed setting buffer's CODE*/
    /*                          3. added idle loop code for smoother Nexus trace */
    /*                          4. modified for newer Freescale header files (r 16) */

    /* Rev 0.4 Aug 11 2006 SM - Removed redundant CAN_A.MCR init */

    /* Rev 0.5 Jan 31 2007 SM - Removed other redundant CAN_C.MCR init */

    /* Rev 0.6 Mar 08 2007 SM - Corrected init of MBs- cleared 64 MBs, instead of 63 */

    /* Rev 0.7 Jul 20 2007 SM - Changes for MPC5510 */

    /* Rev 0.8 May 15 2008 SM - Changes for new header file symbols */

    /* Rev 0.9 May 22 2009 SM - Changes for MPC56xxB/P/S */

    /* Rev 1.0 Jul 10 2009 SM - Simplified, cleared CAN Msg Buf flag by writing to reg */

    /*   not bit, increased Tx pads slew rate(转换率), changed RxCODE, RxLENGTH, dummy data(虚拟数据) types*/
    /*   & init receiving CAN first to allow CAN bus sync time before receiving 1st msg*/
    /* Rev 1.1 Mar 14 2010 SM - modified initModesAndClock, updated header file */
    /*  NOTE!! structure canbuf_t in header file modified to allow byte addressing*/
    /*Chapter 22*/
    #include "MPC5604B_M27V.h" /* Use proper include file */

    uint32_t  RxCODE;              /* Received message buffer code */
    uint32_t  RxID;                 /* Received message ID */
    uint32_t  RxLENGTH;            /* Recieved message number of data bytes */
    uint8_t   RxDATA[8];            /* Received message data string*/
    uint32_t  RxTIMESTAMP;          /* Received message time */                        

    void initModesAndClks(void)   /*系统初始化*/
    {
      ME.MER.R = 0x0000001D;          /* Enable DRUN, RUN0, SAFE, RESET modes */
                                      /* Initialize PLL before turning it on: (初始化锁相环)*/
    /* Use 1 of the next 2 lines depending on crystal frequency: */
    //锁相环时钟没问题
      CGM.FMPLL_CR.R = 0x02400100;     
      //0x02400100                   /* 8 MHz xtal: Set PLL0 to 64 MHz */  
      /*CGM.FMPLL_CR.R = 0x12400100;*/  /* 40 MHz xtal: Set PLL0 to 64 MHz */   
      ME.RUN[0].R = 0x001F0074;       /* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL */


    //  ME.DRUN.R=0x001F0010;


    // ME.RUNPC[0].R = 0x000000FE;           /* Peri. Cfg. 1 settings: only run in RUN0 mode */
      ME.RUNPC[0].R = 0x00000010;
    /*要什么功能就在PCTL中配置相应的序号*/
    /*选择FlexCAN0和FlexCAN1控制*/
      ME.PCTL[16].R = 0x01;           /* P143   MPC56xxB/P/S FlexCAN0: select ME.RUNPC[1] */       
      //要和RUNPC保持一致
      ME.PCTL[17].R = 0x01;           /* MPC56xxB/S FlexCAN1:  select ME.RUNPC[1] */       
    /*选择SIUL控制*/
      ME.PCTL[68].R = 0x01;           /* MPC56xxB/S SIUL:  select ME.RUNPC[1] */       
    /*此处未进行端口引脚配置*/

    //  ME.PCTL[96].R=0x00;
                                      /* Mode Transition to enter RUN0 mode: */
      ME.MCTL.R = 0x40005AF0;         /* Enter RUN0 Mode & Key */
      ME.MCTL.R = 0x4000A50F;         /* Enter RUN0 Mode & Inverted Key */  
      while (ME.GS.B.S_MTRANS) {}     /* Wait for mode transition to complete */   
                                      /* Note: could wait here using timer and/or I_TC IRQ */
      while(ME.GS.B.S_CURRENTMODE != 4) {} /* Verify RUN0 is the current mode */
    }

    void initPeriClkGen(void)    /*??*/
    //??????????
    {
      CGM.SC_DC[1].R = 0x80;     /* MPC56xxB/S: Enable peri set 2 sysclk divided by 1 */



      CGM.OC_EN.R= 1;
      CGM.OCDS_SC.R= 0x22000000;



    }

    void disableWatchdog(void)
    {
      SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */
      SWT.SR.R = 0x0000d928;
      SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */
    }        

    void initCAN_1 (void)      /*初始化CAN_1*/
    {
      uint8_t   i;

      CAN_1.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs *//*Module Configuration Register----MCR 模式配置寄存器*/
      CAN_1.CR.R = 0x04DB2006;        /* Configure for 8MHz OSC, 100KHz bit time *//*Control Register---CR 控制寄存器*/
      //0x04DB0006
      for (i=0; i<64; i++)
      {
        CAN_1.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */     /*Message Buffer--64个信息缓冲寄存器*/
      }
      /*P433*/




      CAN_1.IMRL.B.BUF04M=1;





      CAN_1.BUF[4].CS.B.IDE = 0;      /* MB 4 will look for a standard ID */       /*IDE=0表示Frame format(帧格式) is standard*/
      CAN_1.BUF[4].ID.B.STD_ID = 555; /* MB 4 will look for ID = 555 */            /*ID(帧标识符)*/
      CAN_1.BUF[4].CS.B.CODE = 4;     /* MB 4 set to RX EMPTY */                   /*CODE=0x0100表示EMPTY: MB is active and empty.*/
    //  CAN_1.RXGMASK.R = 0x1FFFFF10;   /* Global acceptance mask */                 /*Rx Global Mask Register---RXGMASK*/
      SIU.PCR[42].R = 0x0624;         /* MPC56xxB: Config port C10 as CAN1TX, open drain */
      SIU.PCR[35].R = 0x0103;         /* MPC56xxB: Configure port C3 as CAN1RX */      /*CAN_1的收发送*/
      SIU.PSMI[0].R = 0x00;           /* MPC56xxB: Select PCR 35 for CAN1RX Input */   /*P346*//*因为可以作为CAN1Rx的引脚有三个(PC3 PC11 PF15)这里选择PC3以后,要注释 SIU.PSMI[0].R = 0x00;表明选择的是PC3*/
      CAN_1.MCR.R = 0x0000003F;       /* Negate FlexCAN 1 halt state for  64 MB */
    }

    void initCAN_0 (void)     /*初始化CAN_0*/
    {
      uint8_t   i;

      CAN_0.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs */   /*Freeze Mode(It is enabled when the FRZ bit in the MCR is asserted.)*/



      CAN_0.CR.R = 0x04DB0006;        /* Configure for 8MHz OSC, 100KHz bit time */
      /*怎么配置波特率*/


      for (i=0; i<64; i++)
      {
        CAN_0.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */
      }


      CAN_0.IMRL.B.BUF00M=1;
      CAN_0.BUF[0].CS.B.IDE = 0;           /* Use standard ID length */  /*IDE=0表示Frame format(帧格式) is standard*/
      CAN_0.BUF[0].ID.B.STD_ID = 555;      /* Transmit ID is 555 */    /*ID(帧标识符)*/
      CAN_0.BUF[0].CS.B.RTR = 0;           /* Data frame, not remote Tx request frame */  /*RTR=0表示Indicates the current MB has a Data Frame to be transmitted(表明当前信息缓冲寄存器将要发送)*/ /*RTR=Remote Transmission Request(远程发送请求)*/
    //  CAN_0.RXGMASK.R = 0x1FFFFFFF;  


      CAN_0.BUF[0].CS.B.CODE = 8;     /* Message Buffer 0 set to TX INACTIVE */    /*CODE=0x1000表示INACTIVE: MB does not participate in the arbitration process.*/
      SIU.PCR[16].R = 0x0624;         /* MPC56xxB: Config port B0 as CAN0TX, open drain */
      SIU.PCR[17].R = 0x0103;        /* MPC56xxB: Configure port B1 as CAN0RX */    /*CAN_0的收发送*/
      CAN_0.MCR.R = 0x0000003F;       /* Negate FlexCAN 0 halt state for 64 MB */
    }
    /*发送端配置相对简单(或许在信息发送函数中进行了设置),只对TX设置为Inactive,而接收端设置比较复杂,比如帧和MB的设置*/
    void TransmitMsg (void)     /*发送信息函数*/
    {
      uint8_t        i;
                                       /* Assumption:  Message buffer CODE is INACTIVE */
      const uint8_t TxData[] = {"Hello"};  /* Transmit string*/  
      CAN_0.BUF[0].CS.B.IDE = 0;           /* Use standard ID length */  /*IDE=0表示Frame format(帧格式) is standard*/
      CAN_0.BUF[0].ID.B.STD_ID = 555;      /* Transmit ID is 555 */    /*ID(帧标识符)*/
      CAN_0.BUF[0].CS.B.RTR = 0;           /* Data frame, not remote Tx request frame */  /*RTR=0表示Indicates the current MB has a Data Frame to be transmitted(表明当前信息缓冲寄存器将要发送)*/ /*RTR=Remote Transmission Request(远程发送请求)*/
      CAN_0.BUF[0].CS.B.LENGTH = sizeof(TxData) -1 ; /* # bytes to transmit w/o null */
      for (i=0; i<sizeof(TxData); i++)
      {
        CAN_0.BUF[0].DATA.B[i] = TxData[i];      /* Data to be transmitted */  /*DATA表示数据段*/
      }
      CAN_0.BUF[0].CS.B.SRR = 1;           /* Tx frame (not req'd for standard frame)*/  /*SRR=1表示 Recessive value is compulsory for transmission in Extended Format frames(在扩展帧格式下隐形值被强制发送)*/
      CAN_0.BUF[0].CS.B.CODE =0xC;         /* Activate msg. buf. to transmit data frame */ /*RTR=0且CODE=1时Transmit data frame unconditionally once. After transmission, the
    MB automatically returns to the INACTIVE state.(发送结束后,MB自动返回到非活跃状态)*/
    }
    /*发送CAN_0选择缓冲寄存器BUF[0],接收CAN_1选择缓冲寄存器BUF[1]*/
    void RecieveMsg (void)      /*接收信息函数*/
    {  
      uint8_t j;
      uint32_t dummy;

      while (CAN_1.IFRL.B.BUF04I == 0) {}; /* Wait for CAN 1 MB 4 flag */  /*等待标志位*/
      RxCODE   = CAN_1.BUF[4].CS.B.CODE;    /* Read CODE, ID, LENGTH, DATA, TIMESTAMP(读取CAN_1的CODE和ID以及Length,Data,Timestamp等参数) */
      RxID     = CAN_1.BUF[4].ID.B.STD_ID;
      RxLENGTH = CAN_1.BUF[4].CS.B.LENGTH;
      for (j=0; j<RxLENGTH; j++)
      {
        RxDATA[j] = CAN_1.BUF[4].DATA.B[j];
      }
      RxTIMESTAMP = CAN_1.BUF[4].CS.B.TIMESTAMP;
      dummy = CAN_1.TIMER.R;                /* Read TIMER to unlock message buffers */   
      CAN_1.IFRL.R = 0x00000010;            /* Clear CAN 1 MB 4 flag */
    }


    //void delay_ms(int x)
    //{
    //        int a,b;
    //        for(a=x;a>0;a--)
    //                for(b=16000;b>0;b--) ;
    //}



    void main(void)
    {

      volatile uint32_t IdleCtr = 0;


    //  delay_ms(1000);

      initModesAndClks();      /* Initialize mode entries */
      initPeriClkGen();        /* Initialize peripheral clock generation for DSPIs */
      disableWatchdog();       /* Disable watchdog */
      initCAN_1();             /* Initialize FLEXCAN 1 & one of its buffers for receive*/
      initCAN_0();             /* Initialize FlexCAN 0 & one of its buffers for transmit*/
    // TransmitMsg();           /* Transmit one message from a FlexCAN 0 buffer */     
    //  RecieveMsg();            /* Wait for the message to be recieved at FlexCAN 1 */
      while (1)
      {            
           IdleCtr++;    /* Idle loop: increment counter */  
           TransmitMsg();           /* Transmit one message from a FlexCAN 0 buffer */     
           RecieveMsg();            /* Wait for the message to be recieved at FlexCAN 1 */           
      }
    }
    这个是在找的例程的基础上有部分我修改了,在网上看到帖子说配SBC,不明白什么意思,请前辈给予指点

    我知道答案 目前已有17人回答

    CAN模块原理图

    CAN模块原理图
    回复

    使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-9-22 16:59:48 | 显示全部楼层
    CAN是否有连接收发器?通过原理图看是用的CAN转串口的芯片.
    初始化过程中,没有对IO口进行初始化?
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-9-22 17:24:51 | 显示全部楼层
    官网改版以后,好多例程都找不到了。我再找找
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-1-12 19:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    3

    主题

    16

    帖子

    0

    注册会员

    Rank: 2

    积分
    111
    最后登录
    2019-2-25
     楼主| 发表于 2015-9-23 10:14:05 | 显示全部楼层
    安 发表于 2015-9-22 16:59
    CAN是否有连接收发器?通过原理图看是用的CAN转串口的芯片.
    初始化过程中,没有对IO口进行初始化? ...

    P1那两个脚是从单片机引过来的发送接收脚,2551就是CAN收发器,IO的配置是在CAN0和CAN1初始化中的
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-9-23 15:45:24 | 显示全部楼层
    楼主有测试过CAN总线是否有波形吗?
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-9-24 17:36:28 | 显示全部楼层
    楼主看一下这个是否有帮助。
    AN4907SW.zip (1.28 MB, 下载次数: 118)
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-1-12 19:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    3

    主题

    16

    帖子

    0

    注册会员

    Rank: 2

    积分
    111
    最后登录
    2019-2-25
     楼主| 发表于 2015-9-25 09:58:22 | 显示全部楼层
    安 发表于 2015-9-24 17:36
    楼主看一下这个是否有帮助。

    安版主,你好!你发的这个东东,我略显看不懂,
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-1-12 19:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    3

    主题

    16

    帖子

    0

    注册会员

    Rank: 2

    积分
    111
    最后登录
    2019-2-25
     楼主| 发表于 2015-9-25 10:45:14 | 显示全部楼层
    z-bo 发表于 2015-9-25 09:58
    安版主,你好!你发的这个东东,我略显看不懂,

    附一张环回模式下的结构图
    环回模式原理.jpg
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2017-1-12 19:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    3

    主题

    16

    帖子

    0

    注册会员

    Rank: 2

    积分
    111
    最后登录
    2019-2-25
     楼主| 发表于 2015-9-25 10:53:42 | 显示全部楼层
    z-bo 发表于 2015-9-25 10:45
    附一张环回模式下的结构图

    环回模式和静默模式的结构图

    环回模式

    环回模式

    静默模式

    静默模式
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-7-23 21:04
  • 签到天数: 103 天

    连续签到: 1 天

    [LV.6]常住居民II

    228

    主题

    5379

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    16704
    最后登录
    1970-1-1
    发表于 2015-9-25 23:59:31 | 显示全部楼层
    前来学习
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-22 20:54 , Processed in 0.110073 second(s), 31 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表