2020-02-20 20:32发布
holts2 发表于 2014-12-9 16:15 flexcan是FSL的专用CAN ?
richyhuang 发表于 2014-12-9 16:05 接收错误, 以前用microchip,nxp的都有计算器,现在fsl没有了,要靠自己算,我f...... ...
FSL_TICS_Robin 发表于 2014-12-9 16:25 PE就可以实现自动计算,楼主可以尝试配置一个带PE的工程调用CAN的那个LDD试一下。 ...
最多设置5个标签!
应该是吧,以前用的没有flexcan,但是bit timing的计算方式是一样的.
那个是can2.0的标准
PE就可以实现自动计算,楼主可以尝试配置一个带PE的工程调用CAN的那个LDD试一下。
搞定了,根据网友提供的例子,改了一下就可以了.
这里就改了一个定义分频系数的地方就可以了,我现在用的是60M的外设时钟,500K通信
- void CAN1_ORed_Message_buffer_IRQHandler(void)
- {
- printf("CAN1->IFLAG1 = %08x
", CAN1->IFLAG1);
- CAN1->IFLAG1 |= 0xFFFFFFFF;
- }
- void CAN1_Bus_Off_IRQHandler(void)
- {
- printf("CAN1_Bus_Off_IRQHandler
");
- }
- void CAN1_Error_IRQHandler(void)
- {
- printf("CAN1_Error_IRQHandler
");
- }
- void CAN1_Tx_Warning_IRQHandler(void)
- {
- printf("CAN1_Tx_Warning_IRQHandler
");
- }
- void CAN1_Rx_Warning_IRQHandler(void)
- {
- printf("CAN1_Tx_Warning_IRQHandler
");
- }
- void CAN1_Wake_Up_IRQHandler(void)
- {
- printf("CAN1_Tx_Warning_IRQHandler
");
- }
- //tower上面使用can1
- int CANx_init(uint8_t CANChannel,uint32_t baudrateKHz,uint8_t selfLoop,uint8_t idMask)
- {
- int i;
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
-
- //使能FlexCAN外部时钟
- // OSC1->CR |= OSC_CR_ERCLKEN_MASK | OSC_CR_EREFSTEN_MASK;
- SIM->SCGC3 |= SIM_SCGC3_FLEXCAN1_MASK;//使能CAN1的时钟模块
- NVIC_EnableIRQ(CAN1_ORed_Message_buffer_IRQn);
- NVIC_EnableIRQ(CAN1_Bus_Off_IRQn);
- NVIC_EnableIRQ(CAN1_Error_IRQn);
- NVIC_EnableIRQ(CAN1_Tx_Warning_IRQn);
- NVIC_EnableIRQ(CAN1_Rx_Warning_IRQn);
- NVIC_EnableIRQ(CAN1_Wake_Up_IRQn);
- //(p281.pin out)
- if((uint32_t)CANx == (uint32_t)CAN1_BASE)
- {
- PORTC->PCR[16] = PORT_PCR_MUX(2) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK; //上拉
- PORTC->PCR[17] = PORT_PCR_MUX(2) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK; //上拉
- }else if((uint32_t)CANx == (uint32_t)CAN0_BASE)
- {
-
- }else{
- return 2;
- }
- //选择时钟源,外设时钟60MHz,振荡器时钟:50MHz
- CANx->CTRL1 |= CAN_CTRL1_CLKSRC_MASK;//选择内部时钟
- CANx->MCR |= CAN_MCR_FRZ_MASK; //使能冻结模式
- CANx->MCR &= ~CAN_MCR_MDIS_MASK;//使能CAN模块
- //确认已退出冻结模式
- while ((CAN_MCR_LPMACK_MASK & CANx->MCR));
- //软件复位
- CANx->MCR ^= CAN_MCR_SOFTRST_MASK;
- //等待复位完成
- while (CAN_MCR_SOFTRST_MASK & CANx->MCR);
- //等待进入冻结模式
- while (!(CAN_MCR_FRZACK_MASK & CANx->MCR));
- //将16个邮箱缓冲区内容清0
- for (i=0;i<16;i++)
- {
- CANx->MB[i].CS = 0x00000000;
- CANx->MB[i].ID = 0x00000000;
- CANx->MB[i].WORD0 = 0x00000000;
- CANx->MB[i].WORD1 = 0x00000000;
- }
- //接收邮箱过滤IDE比较,RTR不比较
- CANx->CTRL2 &= ~CAN_CTRL2_EACEN_MASK;
- //远程请求帧产生
- CANx->CTRL2 &= ~CAN_CTRL2_RRS_MASK;
- //邮箱首先从接收FIFO队列匹配然后再在邮箱中匹配
- CANx->CTRL2 &= ~CAN_CTRL2_MRP_MASK;
- //使用一个32位过滤器
- CANx->MCR |= (CANx->MCR & ~CAN_MCR_IDAM_MASK) | CAN_MCR_IDAM(0);
- //设置波特率
- if (SetCANBand(CANChannel,baudrateKHz) == 1)//若设置错误
- return 1;
- //模式选择:回环模式或正常模式
- if (1==selfLoop)
- CANx->CTRL1 |= CAN_CTRL1_LPB_MASK; //使用回环模式
- // CANx->CTRL1 |= CAN_CTRL1_LOM_MASK; //使用监听模式
- //初始化掩码寄存器
- if (1==idMask)//屏蔽ID
- {
- CANx->RXMGMASK = 0x1FFFFFFF;
- CANx->RX14MASK = 0x1FFFFFFF;
- CANx->RX15MASK = 0x1FFFFFFF;
- }
- else//不屏蔽ID
- {
- CANx->RXMGMASK = 0x0;
- CANx->RX14MASK = 0x0;
- CANx->RX15MASK = 0x0;
- }
- //如果单独掩码功能使能,为每个队列初始化单独的掩码寄存器
- if (CANx->MCR & CAN_MCR_IRMQ_MASK)
- for (i = 0; i < NUMBER_OF_MB ; i++)
- CANx->RXIMR[i] = 0x1FFFFFFFL;
- //只有在冻结模式下才能配置,配置完推出冻结模式
- CANx->MCR &= ~(CAN_MCR_HALT_MASK);
- //等待直到退出冻结模式
- while ( CANx->MCR & CAN_MCR_FRZACK_MASK);
- //等到不在冻结模式,休眠模式或者停止模式
- while ((CANx->MCR & CAN_MCR_NOTRDY_MASK));
-
- CANEnableRXBuff(1,//uint8_t CANChannel,
- 0,//uint16_t iMB,
- 1//uint32_t id)
- );
- CAN1->IMASK1 |= CAN_IMASK1_BUFLM(0x1ul);
- return 0;
- }
- //============================================================================
- //函数名称:CANSendData
- //函数返回:0:成功,1:失败
- //参数说明:
- // CANChannel:模块号
- // iMB:缓冲区号
- // id: ID号
- // length:数据长度
- // Data[8]:发送数据缓冲区
- //功能概要:发送数据
- //============================================================================
- int CANSendData(uint8_t CANChannel,uint16_t iMB, uint32_t id,uint8_t length,uint8_t Data[])
- {
- int16_t i,wno,bno;
- uint32_t word[2] = {0};
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- //缓冲区和数据长度设置错误
- if (iMB >= NUMBER_OF_MB || length >8)
- return 1; //超出范围
- //转换8个字节转换成32位的word存储
- wno = (length-1)>>2;//是否超过4字节
- bno = (length-1)%4; //
- if (wno > 0) //长度大于4(即发送数据超过4字节)
- {
- word[0] = (
- (Data[0]<<24)
- | (Data[1]<<16)
- | (Data[2]<< 8)
- | Data[3]
- );
- }
- for (i=0;i<=bno;i++)
- word[wno] |= Data[(wno<<2)+i] << (24-(i<<3));
- ///////////////////////////////////////////////////////
- // ID 格式
- // B31 30 29 28 27 26 ... 11 10 9 8 7 6 5 4 3 2 1 0
- // | | | |
- // | | |------------------------------------|
- // | | |--> 29 位 ID
- // | |
- // | |--> RTR 1: 远程帧, 0: 数据帧
- // |
- // |-------> IDE 1 : 扩展ID, 0: 标准ID
- ///////////////////////////////////////////////////////
- //通过id判断帧类型——扩展帧
- wno = (id & CAN_MSG_IDE_MASK)>>CAN_MSG_IDE_BIT_NO; //IDE
- //通过id判断帧类型——远程帧
- bno = (id & CAN_MSG_TYPE_MASK)>>CAN_MSG_TYPE_BIT_NO;//RTR
- //获得ID位数
- i = wno? 0: FLEXCAN_MB_ID_STD_BIT_NO;
- //以下四步骤为发送过程
- CANx->MB[iMB].CS = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE)//缓冲区写激活代码
- | (wno<<FLEXCAN_MB_CS_IDE_BIT_NO)//缓冲区写IDE位
- | (bno<<FLEXCAN_MB_CS_RTR_BIT_NO)//缓冲区写RTR位
- | FLEXCAN_MB_CS_LENGTH(length); //缓冲区写数据长度
- //缓冲区写ID
- CANx->MB[iMB].ID = (1 << FLEXCAN_MB_ID_PRIO_BIT_NO)
- | ((id & ~(CAN_MSG_IDE_MASK|CAN_MSG_TYPE_MASK))<<i);
- //缓冲区写内容
- CANx->MB[iMB].WORD0 = word[0];
- CANx->MB[iMB].WORD1 = word[1];
- //延迟
- for (i = 0;i < 100;i++);
- //通过制定的发送代码开始发送
- CANx->MB[iMB].CS = (CANx->MB[iMB].CS & ~(FLEXCAN_MB_CS_CODE_MASK))
- | FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_ONCE)//写激活代码
- | FLEXCAN_MB_CS_LENGTH(length);//缓冲区写数据长度
- //限时等待发送完成(如果使用中断则限时等待语句可删除)
- i=0;
- while (!(CANx->IFLAG1 & (1<<iMB)))
- {
- if ((i++)>0x1000)
- return 1;
- }
- //清报文缓冲区中断标志
- CANx->IFLAG1 = (1<<iMB);
- return 0;
- }
- //============================================================================
- //函数名称:CANEnableRXBuff
- //函数返回:无
- //参数说明: CANChannel:CAN模块号
- // iMB:缓冲区号
- // id:id号
- //功能概要:使能接收缓冲区
- //============================================================================
- void CANEnableRXBuff(uint8_t CANChannel,uint16_t iMB, uint32_t id)
- {
- uint32_t id2;
- uint32_t cs = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_EMPTY);
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- //将MB配置为非激活状态
- CANx->MB[iMB].CS = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_RX_INACTIVE);
- //取出29位单独的ID
- id2 = id & ~(CAN_MSG_IDE_MASK | CAN_MSG_TYPE_MASK);
- if (id & CAN_MSG_IDE_MASK)//扩展帧
- {
- CANx->MB[iMB].ID = id2;
- cs |= FLEXCAN_MB_CS_IDE;//置位IDE位
- }
- else//标准帧
- {
- CANx->MB[iMB].ID = id2<<FLEXCAN_MB_ID_STD_BIT_NO;
- }
- //激活接收缓冲区,code写0100
- CANx->MB[iMB].CS = cs;
- }
- //============================================================================
- //函数名称:CANRecData
- //函数返回:0:成功,1:失败
- //参数说明:CANChannel:CAN模块号
- // iMB:缓冲区号
- // id: ID号
- // lenght:数据长度
- // Data[8]: 和接收数据缓冲区
- //功能概要:接收数据
- //============================================================================
- uint8_t CANRecData(uint8_t CANChannel,uint16_t iMB, uint32_t *id,uint8_t *Datalenght,uint8_t Data[])
- {
- int8_t i,wno,bno;
- uint16_t code;
- uint8_t *pMBData;
- int16_t length;
- int8_t format;
- uint8_t *pBytes = Data;
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- // 锁定MB
- code = FLEXCAN_get_code(CANx->MB[iMB].CS);
- if (code != 0x02)//缓冲区没有接收到数据,返回错误
- {
- *Datalenght = 0;
- return 1;
- }
- length = FLEXCAN_get_length(CANx->MB[iMB].CS);
- if (length <1)//接收到的数据长度小于1,返回错误
- {
- *Datalenght = 0;
- return 1;
- }
- //判断是标准帧还是扩展帧
- format = (CANx->MB[iMB].CS & FLEXCAN_MB_CS_IDE)? 1:0;
- *id = (CANx->MB[iMB].ID & FLEXCAN_MB_ID_EXT_MASK);
- if (!format)
- {
- *id >>= FLEXCAN_MB_ID_STD_BIT_NO; // 获得标准帧号
- }
- else
- {
- *id |= CAN_MSG_IDE_MASK; //标记扩展的ID
- }
- format = (CANx->MB[iMB].CS & FLEXCAN_MB_CS_RTR)? 1:0;
- if (format)
- {
- *id |= CAN_MSG_TYPE_MASK; //标记为远程帧类型
- }
- //读取报文数据
- wno = (length-1)>>2;
- bno = length-1;
- if (wno>0)
- {
- (*(uint32_t*)pBytes) = CANx->MB[iMB].WORD0;
- swap_4bytes(pBytes);
- bno -= 4;
- pMBData = (uint8_t*)&CANx->MB[iMB].WORD1+3;
- }
- else
- {
- pMBData = (uint8_t*)&CANx->MB[iMB].WORD0+3;
- }
- for (i=0; i <= bno; i++)
- pBytes[i+(wno<<2)] = *pMBData--;
- *Datalenght = length;
- return 0;
- }
- //============================================================================
- //函数名称:EnableCANInterrupt
- //函数返回:无
- //参数说明:
- // CANChannel:CAN模块号
- // iMB:缓冲区号
- //功能概要:使能缓冲区接收和发送中断
- //============================================================================
- void EnableCANInterrupt(uint8_t CANChannel,uint16_t iMB)
- {
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- CANx->IMASK1 = (1<<iMB);
- }
- //============================================================================
- //函数名称:DisableCANInterrupt
- //函数返回:无
- //参数说明:
- // CANChannel:CAN模块号
- // iMB:缓冲区号
- //功能概要:禁止缓冲区接收和发送中断
- //============================================================================
- void DisableCANInterrupt(uint8_t CANChannel,uint16_t iMB)
- {
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- CANx->IMASK1 &= ~CAN_IMASK1_BUFLM(iMB);
- }
- //============================================================================
- //函数名称:CANClearFlag
- //函数返回:无
- //参数说明:
- // CANChannel:CAN模块号
- // iMB:缓冲区号
- //功能概要:清缓冲区中断标志
- //============================================================================
- void CANClearFlag(uint8_t CANChannel,uint16_t iMB)
- {
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- CANx->IFLAG1 = (1<<iMB);
- }
- //============================================================================
- //函数名称:CANGetFlag
- //函数返回:无
- //参数说明:
- // CANChannel:CAN模块号
- // iMB:缓冲区号
- //功能概要:获得缓冲区中断标志
- //============================================================================
- uint32_t CANGetFlag(uint8_t CANChannel,uint16_t iMB)
- {
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- return (CANx->IFLAG1 & (1<<iMB));
- }
- //===========================内部函数=========================================
- //============================================================================
- //函数名称:SetCANBand
- //函数返回:0:配置成功 1:配置成功
- //参数说明:无
- //功能概要:设置CAN的波特率
- //============================================================================
- int SetCANBand(uint8_t CANChannel,uint32_t baudrateKHz)
- {
- CAN_Type *CANx;
- if (CANChannel == 0)
- CANx = CAN0;
- else if (CANChannel == 1)
- CANx = CAN1;
- switch (baudrateKHz)
- {
- case (33): // 33.33K
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/120= 400k sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4,PRESDIV = 120
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(119));
- }
- else
- {
- // 12M/20= 600k sclock, 18Tq
- // PROPSEG = 1, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 4, PSEG1 = 8, PSEG2 = 8,PRESDIV = 20
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(0)
- | CAN_CTRL1_PROPSEG(3)
- | CAN_CTRL1_PSEG1(7)
- | CAN_CTRL1_PSEG2(7)
- | CAN_CTRL1_PRESDIV(19));
- }
- break;
- case (83): // 83.33K
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/48= 1M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4,PRESDIV = 48
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(47));
- }
- else
- {
- // 12M/12= 1M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4,PRESDIV = 12
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(11));
- }
- break;
- case (50):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/80= 0.6M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 40
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(1)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(79));
- }
- else
- {
- // 12M/20= 0.6M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 20
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(19));
- }
- break;
- case (100):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/40= 1.2M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 40
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(39));
- }
- else
- {
- // 12M/10= 1.2M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 10
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(9));
- }
- break;
- case (125):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/32= 1.5M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 32
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(31));
- }
- else
- {
- // 12M/8= 1.5M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 3, PSEG1 = 4, PSEG2 = 4, PRESDIV = 8
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(2)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(7));
- }
- break;
- case (250):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/16= 3M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 16
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(1)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(15));
- }
- else
- {
- // 12M/4= 3M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 4
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(1)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(3));
- }
- break;
- case (500):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 60M/10=6M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 6
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(1)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(9));
- }
- else
- {
- // 12M/2=6M sclock, 12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 2, PSEG1 = 4, PSEG2 = 4, PRESDIV = 2
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(1)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(1));
- }
- break;
- case (1000):
- if (CANx->CTRL1 & CAN_CTRL1_CLKSRC_MASK)
- {
- // 48M/6=8M sclock
- // PROPSEG = 4, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 1, PSEG1 = 1, PSEG2 = 2, PRESCALER = 6
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(3)
- | CAN_CTRL1_RJW(0)
- | CAN_CTRL1_PSEG1(0)
- | CAN_CTRL1_PSEG2(1)
- | CAN_CTRL1_PRESDIV(5));
- }
- else
- {
- // 12M/1=12M sclock,12Tq
- // PROPSEG = 3, LOM = 0x0, LBUF = 0x0, TSYNC = 0x0, SAMP = 1
- // RJW = 4, PSEG1 = 4, PSEG2 = 4, PRESCALER = 1
- CANx->CTRL1 = (0 | CAN_CTRL1_PROPSEG(2)
- | CAN_CTRL1_RJW(3)
- | CAN_CTRL1_PSEG1(3)
- | CAN_CTRL1_PSEG2(3)
- | CAN_CTRL1_PRESDIV(0));
- }
- break;
- default:
- return 1;
- }
- return 0;
- }
复制代码一周热门 更多>