在调试的时候使用PE初始化CAN接口代码,后来调试CAN速率自动变化功能,发现无法在重启的情况下自适应变化。
于是跟踪寄存器状态,发现收到上位机变速率数据,进入CAN初始化之后,CTRL0并没有变化,是因为PE的代码,CTRL0的置零放在了CAN CAN_MCR_MDIS_MASK之后、使能之前,造成CTRL0并未被置零,因此造成CAN速率无法改变。
应该是这样的:等到使能之后再置零CTRL0,然后再初始化CTRL0这样就可以随心所欲更改CAN速率了, oh year
/* SIM_SCGC6: FLEXCAN0=1 */
SIM_SCGC6 |= SIM_SCGC6_FLEXCAN0_MASK;//使能CAN0时钟
pinAndInterupt(DeviceDataPrv);
/* CAN0_MCR: MDIS=1 */
CAN0_MCR |= CAN_MCR_MDIS_MASK; /*为了下面的初始化暂时关闭CAN模块 Disable CAN module */
/*Bus Clock selected*/
#if 0 //放在这里因为下面 上面MCR已关闭,在这里设置没有意义,必须在下面MCR中使能CAN后才可设置 CAN_MCR_MDIS_MASK
/* 设置CAN接口的时钟源、接口特性确定发送速率CAN0_CTRL1: PRESDIV=0,RJW=0,PSEG1=0,PSEG2=0,BOFFMSK=0,ERRMSK=0,CLKSRC=1,LPB=0,TWRNMSK=0,RWRNMSK=0,??=0,??=0,SMP=0,BOFFREC=0,TSYN=0,LBUF=0,LOM=0,PROPSEG=0 */
CAN0_CTRL1 = 0;
i = CAN0_CTRL1;
CAN0_CTRL1 = CAN_CTRL1_PRESDIV(0x00) |
CAN_CTRL1_RJW(0x00) |
CAN_CTRL1_PSEG1(0x00) |
CAN_CTRL1_PSEG2(0x00) |
//CAN_CTRL1_CLKSRC_MASK | //0 The CAN engine clock source is the oscillator clock.
CAN_CTRL1_PROPSEG(0x00);
i = CAN0_CTRL1;
#endif
/*使能CAN接口以及接收FIFO CAN0_MCR: MDIS=0,SOFTRST=1 */
CAN0_MCR = (uint32_t)((CAN0_MCR & (uint32_t)~(uint32_t)(
CAN_MCR_MDIS_MASK
)) | (uint32_t)(
CAN_MCR_SRXDIS_MASK | //disalbe self reception
CAN_MCR_SOFTRST_MASK
)); /* Soft Reset */
while (CAN_PDD_GetSoftResetState(CAN0_BASE_PTR) == CAN_PDD_NOT_RESET){} /* Wait for Soft reset Acknowledge */
while (CAN_PDD_GetFreezeAck(CAN0_BASE_PTR) == CAN_PDD_NOT_FREEZE){} /* Wait for entering the freeze mode */
/* CAN0_MCR: WRNEN=1,IRMQ=1,MAXMB&=~0x0E,MAXMB|=1 */
CAN0_MCR = (uint32_t)((CAN0_MCR & (uint32_t)~(uint32_t)(
//CAN_MCR_MAXMB(0x0E)
CAN_MCR_MAXMB(0x0D)
)) | (uint32_t)(
CAN_MCR_WRNEN_MASK |//使能告警中断
CAN_MCR_IRMQ_MASK |//使能接收队列功能
//CAN_MCR_MAXMB(0x01)
CAN_MCR_MAXMB(0x02) |//使能3个BUFFER
CAN_MCR_SRXDIS_MASK //disalbe self reception
)); /* MCR reg. Settings */
#ifndef CAN_TEST
if(CAN_CONFIG1.m_WorkMode == CAN_WORK_MOD_LOOP)
{
CAN0_MCR &= (~(uint32_t)(CAN_MCR_SRXDIS_MASK));//开启接收自己发送数据的功能
}
#endif
/* CAN0_CTRL1: PRESDIV|=0x19,PSEG1|=3,PSEG2|=1,LPB=1,LBUF=1 */
#ifdef CAN_TEST
CAN0_CTRL1 |= CAN_CTRL1_PRESDIV(0x07) |
CAN_CTRL1_PSEG1(0x03) |
CAN_CTRL1_PSEG2(0x01) |
//CAN_CTRL1_LPB_MASK | //close loop mod
CAN_CTRL1_LBUF_MASK; /* Setting CTRL1 register */
#else
changeCanBuat(CAN_CONFIG1.m_BaudRate);
#endif
|