CAN总线(控制器局域网总线)
理论知识
CAN总线是由BOSCH公司开发的一种简洁易用、传输速度快、易扩展、可靠性高的串行通信总线
CAN总线特征
- 两根通信线(CAN_H、CAN_L),线路少,无需共地
- 差分信号通信(相对的是单端信号),抗干扰能力强(意思是当有干扰时,两根线同时产生波动,但两根线的差值不变)
- 高速CAN(ISO11898):125k~1Mbps,<40m
- 低速CAN(ISO11519):10k~12kbps,<1km
- 异步,无需时钟线,通信速率由设备各自约定
- 半双工,可挂载多设备,多设备同时发送数据时通过仲裁判断先后顺序
- 11位/29位报文ID,用于区分消息功能,同时决定优先级
- 可配置1~8字节的有效载荷
- 可实现广播式和请求式两种传输方式
- 应答、CRC校验、位填充、位同步、错误处理等特性
名称 | 引脚 | 双工 | 时钟 | 电平 | 设备 | 应用场景 |
UART | TX、RX | 全双工 | 异步 | 单端 | 点对点 | 两个设备互相通信 |
I2C | SCL、SDA | 半双工 | 同步 | 单端 | 多设备 | 单主控外挂多个模块 |
SPI | SCK、MOSI、MISO、SS | 全双 | 同步 | 单端 | 多设备 | 单模块外挂多个模块(高速,可达几Mbps) |
CAN | CAN_H、CAN_L | 半双工 | 异步 | 差分 | 多设备 | 多个主控互相通信 |
CAN硬件电路
- 每个设备通过CAN收发器挂载在CAN总线网络上
- 高速CAN使用闭环网络,CAN_H和CAN_L两端添加120欧的终端电阻(第一个作用是防止回波反射,第二个作用是没有设备操作时,将两根差分线的电压收紧)
- 低速CAN使用开环网络,CAN_H和CAN_L其中一端添加2.2千欧的终端网络(有回波反射作用)
CAN电平标准
- CAN总线采用差分信号,即两线电压差(Vcan_h-Vcan_l)传输数据位
- 高速CAN规定:
电压差为0V时表示逻辑1(隐性电平)
电压差为2V时表示逻辑0(显性电平)
- 低速CAN规定:
电压差为-1.5V时表示逻辑1(隐性电平)
电压差为3V时表示逻辑0(显性电平)
CAN总线帧格式
帧类型 | 用途 |
数据帧 | 发送设备主动发送数据(广播式) |
遥控帧 | 接收设备主动请求数据(请求式) |
错误帧 | 某个设备检测出错误时向其他设备通知错误 |
过载帧 | 接收设备通知其尚未做好接收准备 |
帧间隔 | 用于将数据帧及遥控帧与前面的帧分离开 |
数据帧
总线空闲状态-->帧起始SOF(发送隐性电平1)-->仲裁段(发送11位报文ID以及RTR【隐性电平1表示数据帧,显性电平0表示遥控帧】)-->控制段(IDE:ID扩展标志位:用于区分标准格式还是扩展格式【标准格式发显性电平0,扩展格式发隐性电平1】)、r0(保留位,必须为显性电平0)、4位DLC(用于指定1~8位有效载荷)-->数据段(要发送的数据,大小与前面的DLC对应)-->CRC段(校验算法:CRC及CRC界定符)-->ACK段(收发应答:ACK槽及ACK界定符)-->帧结束(EOF)
扩展格式:SRR替代RTR,协议升级时留下的无意义位
应答的过程是发送方与接收方操纵总线的过程,发送方发完数据后释放总线,等待读取总线信息,如果读到总线被拉紧,就说明有其他设备接收到数据,并且,CAN设备是发送方发送一位,接收方接收一位的过程,两个界定符的作用是给发送方和接收方留出操纵总线的时间。
遥控帧
遥控帧无数据段,RTR为隐性电平1,其他部分与数据帧相同
CAN的遥控帧(Remote Frame)的主要作用是请求其他节点发送具有特定ID的数据帧。具体来说,当一个节点需要从另一个节点获取数据时,它可以发送一个遥控帧,而不是直接发送数据。这个遥控帧包含了请求数据的ID,但没有包含数据本身。接收到遥控帧的节点如果拥有与遥控帧ID相匹配的数据帧,就会响应并发送相应的数据帧。这种方式允许节点仅请求需要的数据,而不是不断发送可能不需要的数据,从而提高了网络的效率和减少了不必要的数据传输。
位填充
位填充规则:发送方每发送5个相同电平后,自动追加一个相反电平的填充位接收方检测到填充位时,会自动移除填充位,恢复原始数据。
位填充作用:
- 增加波形的定时信息,利于接收方执行“再同步”,防止波形长时间无变化,导致接收方不能精确掌握数据采样时机
- 将正常数据流与“错误帧”和“过载帧”区分开,标志“错误帧”和“过载帧”的特异性(错误帧和过载帧回发送6个相同电平)
- 保持CAN总线早发送正常数据流时的活跃状态,防止被误认为总线空闲
位时序
为了灵活调整每个采样点的位置,使采样点对齐数据中心附近,CAN总线对每一个数据位的时长进行了更细的划分,分为同步段(SS)、传播时间段(PTS)、相对缓冲段1(PBS1)和相位缓冲段2(PBS2),每个段又由若干个最小时间单位(Tq)构成
硬同步
- 每个设备都有一个位时序计时周期,当每个设备(发送方)率先发送报文,其他所有设备(接收方)收到SOF的下降沿时,接收方会将自己的位时序计时周期拨到SS段的位置,与发送方的位时序计时周期保持同步
- 硬同步只在帧的第一个下降沿(SOF下降沿)有效
- 经过硬同步后,若发送方和接收方的时钟没有误差,则后续所有数据位的采样点必须都会对齐数据位中心附近
再同步
- 若发送方或接收方的时钟有误差,随着误差积累,数据位边沿逐渐偏离SS段,则此时接收方根据再同步补偿宽度值(SJW)通过加长PBS1段,或缩短PBS2段,以调整同步
- 再同步可以发生在第一个下降沿之后的每个数据位跳变边沿
总线仲裁
CAN基本结构
STM32有CAN外设,微控制器做为CAN控制器,其内部结构如上图所示,发送和接收控制器相当于管家,控制CAN的发送和接收,发送邮箱是发送缓冲区,发送接收控制器可以根据先来后到或者ID优先级两种方式进行选择发送报文,通过CAN_TX发送到外面的CAN收发器,CAN收发器将TTL电平转换为差分电平CAN_H和CAN_L,传输到CAN总线上,发送接收控制器从CAN_RX引脚接收CAN总线发来的数据电平,先经过14个过滤器筛选,通过过滤器的报文再存放到接收FIFO(先入先出寄存器,队列)中,等待CPU读取处理。
代码
标准格式、扩展格式、数据帧、遥控帧
MyCAN.h
#ifndef __MYCAN_H
#define __MYCAN_H
#include "stdint.h"void MyCAN_Init(void);
void MyCAN_Transmit(CanTxMsg *TxMessage);
uint8_t MyCAN_ReceiveFlag(void);
void MyCAN_Receive(CanRxMsg *RxMessage);#endif
MyCAN.c
#include "stm32f10x.h" // Device headervoid MyCAN_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_Init(GPIOA,&GPIO_InitStructure);CAN_InitTypeDef CAN_InitStructure;CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//环回模式CAN_InitStructure.CAN_Prescaler = 48; //波特率 = 36M/48/(1+BS1Tq数+BS2Tq数)=125KCAN_InitStructure.CAN_BS1 = CAN_BS1_2tq;CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;CAN_InitStructure.CAN_NART = DISABLE; //自动重传CAN_InitStructure.CAN_TXFP = DISABLE; //发送邮箱优先级,ENABLE先请求先发送,DISABLE,ID号小的先发送CAN_InitStructure.CAN_RFLM = DISABLE; //FIFO锁定,ENABLE,FIFO溢出时,新报文丢弃,DISABLE,最后收到的报文被新报文覆盖CAN_InitStructure.CAN_AWUM = DISABLE; //自动唤醒,ENABLE,自动,DISABLE,手动CAN_InitStructure.CAN_TTCM = DISABLE; //时间触发通信模式CAN_InitStructure.CAN_ABOM = DISABLE; //离线自动恢复,ENABLE自动恢复,DISABLE手动恢复CAN_Init(CAN1,&CAN_InitStructure);CAN_FilterInitTypeDef CAN_FilterInitStructure;CAN_FilterInitStructure.CAN_FilterNumber = 0;CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; //全通模式CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //过滤器位宽:32位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //过滤器模式:屏蔽模式CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);
}void MyCAN_Transmit(CanTxMsg *TxMessage)
{uint8_t TransmitMailbox = CAN_Transmit(CAN1,TxMessage);uint32_t Timeout = 0;while(CAN_TransmitStatus(CAN1,TransmitMailbox)!=CAN_TxStatus_Ok) //超时退出{Timeout ++;if(Timeout > 100000)break;}
}uint8_t MyCAN_ReceiveFlag(void)
{if(CAN_MessagePending(CAN1,CAN_FIFO0)>0) //检查FIFO里是否有报文{return 1;}return 0;
}void MyCAN_Receive(CanRxMsg *RxMessage)
{CAN_Receive(CAN1,CAN_FIFO0,RxMessage);
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "MyCAN.h"
#include "Key.h"CanTxMsg TxMsgArray[] = {
/* StdId ExtId IDE RTR DLC Data[8] */{0x555,0x00000000,CAN_Id_Standard,CAN_RTR_Data,4,{0x11,0x22,0x33,0x44}},{0x000,0x12345678,CAN_Id_Extended,CAN_RTR_Data,4,{0xAA,0xBB,0xCC,0xDD}},{0x666,0x00000000,CAN_Id_Standard,CAN_RTR_Remote,0,{0x00,0x00,0x00,0x00}},{0x000,0x0789ABCD,CAN_Id_Extended,CAN_RTR_Remote,4,{0x00,0x00,0x00,0x00}},
};CanRxMsg RxMsg;u8 pTMsgArray = 0;int main()
{u8 keyNum = 0;OLED_Init();MyCAN_Init();Key_Init();OLED_ShowString(1,1,"Rx :");OLED_ShowString(2,1,"RxID:");OLED_ShowString(3,1,"Leng:");OLED_ShowString(4,1,"Data:");while(1){keyNum = Key_GetNum();if(keyNum==1){ MyCAN_Transmit(&TxMsgArray[pTMsgArray]);pTMsgArray++;pTMsgArray%=sizeof(TxMsgArray)/sizeof(CanTxMsg);}if(MyCAN_ReceiveFlag()){MyCAN_Receive(&RxMsg);if(RxMsg.IDE == CAN_Id_Standard){OLED_ShowString(1,6,"Std");OLED_ShowHexNum(2,6,RxMsg.StdId,8);}else if(RxMsg.IDE == CAN_Id_Extended){OLED_ShowString(1,6,"Ext");OLED_ShowHexNum(2,6,RxMsg.ExtId,8);}if(RxMsg.RTR == CAN_RTR_Data){OLED_ShowString(1,10,"Data ");OLED_ShowHexNum(3,6,RxMsg.DLC,1);OLED_ShowHexNum(4,6, RxMsg.Data[0],2);OLED_ShowHexNum(4,9, RxMsg.Data[1],2);OLED_ShowHexNum(4,12,RxMsg.Data[2],2);OLED_ShowHexNum(4,15,RxMsg.Data[3],2); }else if(RxMsg.RTR == CAN_RTR_Remote){OLED_ShowString(1,10,"Remote");OLED_ShowHexNum(3,6,RxMsg.DLC,1);OLED_ShowHexNum(4,6, 0x00,2);OLED_ShowHexNum(4,9, 0x00,2);OLED_ShowHexNum(4,12,0x00,2);OLED_ShowHexNum(4,15,0x00,2); }}}
}
过滤器的使用
(1)16位列表模式
/*一共有14个过滤器,可配置多个*/CAN_FilterInitStructure.CAN_FilterNumber = 0;
// CAN_FilterInitStructure.CAN_FilterIdHigh = (0x234<<5)|0x10; //RTR在第五位,|0x10就能接收遥控帧CAN_FilterInitStructure.CAN_FilterIdHigh = 0x234<<5;CAN_FilterInitStructure.CAN_FilterIdLow = 0x345<<5;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x567<<5;CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x000<<5; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit; //过滤器位宽:16位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; //过滤器模式:列表CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);CAN_FilterInitStructure.CAN_FilterNumber = 1;CAN_FilterInitStructure.CAN_FilterIdHigh = 0x123<<5;CAN_FilterInitStructure.CAN_FilterIdLow = 0x678<<5;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x000<<5;CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x000<<5; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);
(2)16屏蔽模式
CAN_FilterInitTypeDef CAN_FilterInitStructure;/*一共有14个过滤器,可配置多个*/CAN_FilterInitStructure.CAN_FilterNumber = 0;CAN_FilterInitStructure.CAN_FilterIdHigh = 0x200<<5; //第一组IDCAN_FilterInitStructure.CAN_FilterMaskIdHigh = (0x700<<5)|0x10|0x8; //对应的屏蔽位CAN_FilterInitStructure.CAN_FilterIdLow = 0x320<<5; //第二组IDCAN_FilterInitStructure.CAN_FilterMaskIdLow = (0x7F0<<5)|0x10|0x8; //对应的屏蔽位CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit; //过滤器位宽:16位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //过滤器模式:屏蔽CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);
(3)32位列表模式
/*一共有14个过滤器,可配置多个*/CAN_FilterInitStructure.CAN_FilterNumber = 0;uint32_t ID1 = 0x123<<21;CAN_FilterInitStructure.CAN_FilterIdHigh = ID1>>16;CAN_FilterInitStructure.CAN_FilterIdLow =ID1;uint32_t ID2 = (0x12345678u<<3)|0x4;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ID2>>16;CAN_FilterInitStructure.CAN_FilterMaskIdLow = ID2; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //过滤器位宽:16位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; //过滤器模式:列表CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);
(4)32位屏蔽模式
/*一共有14个过滤器,可配置多个*/CAN_FilterInitStructure.CAN_FilterNumber = 0;uint32_t ID = (0x12345600u<<3)|0x4;CAN_FilterInitStructure.CAN_FilterIdHigh = ID>>16;CAN_FilterInitStructure.CAN_FilterIdLow =ID;uint32_t Mask = (0x1FFFFF00u<<3)|0x4|0x2;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = Mask>>16;CAN_FilterInitStructure.CAN_FilterMaskIdLow = Mask; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //过滤器位宽:16位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //过滤器模式:列表CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);
(5)只接收遥控帧
/*一共有14个过滤器,可配置多个*/CAN_FilterInitStructure.CAN_FilterNumber = 0;uint32_t ID = 0x2;CAN_FilterInitStructure.CAN_FilterIdHigh = ID>>16;CAN_FilterInitStructure.CAN_FilterIdLow =ID;uint32_t Mask = 0x2;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = Mask>>16;CAN_FilterInitStructure.CAN_FilterMaskIdLow = Mask; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //过滤器位宽:16位CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //过滤器模式:列表CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //配置过滤器关联,进FIFO0排队还是进FIFO1排队CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器开关CAN_FilterInit(&CAN_FilterInitStructure);