# CANBUS 2.0A和2.0B差異及CAN FD (Controller Area Network) ###### tags: `protocols ` <style> .red { color: red; } .blue { color: blue; } .green { color: green; } .yellow { color: yellow; } </style> * CAN bus 是差動雙線通訊(CAN_H / CAN_L),差動訊號範圍約為 ±2V 以上。 * CAN BUS架構基本上都是以 CAN 2.0A and CAN 2.0B為延伸 * 多主多從(multi-master)。 * 優先權由 CAN ID 決定:數值越小優先權越高(因為 0 代表 dominant) * CAN Protocols: CAN 2.0A and CAN 2.0B. * CAN SPEED : 125kbs、250kbs、500kbs、1Mbps(不能向上支援CANFD) * CAN FD : 250kbs、500kbs、1Mbps、2Mbps、5Mbps(可以向下支援CAN BUS) * Error handling:CAN 有強大的錯誤檢測(Stuff Error, CRC Error, ACK Error 等) ![image](https://hackmd.io/_uploads/H1qifC6j0.png) ## CAN 硬體 * CAN BUS 需要 transceiver只負責來接收發。 * CAN BUS protocols也可以透過軟體來分析。 * CAN BUS 需要 pull-up 電阻 120歐姆。 ## CANBUS 2.0A和2.0B差異及CAN FD、CANOPEN * CANBUS協議版本2.0A和2.0B的差異主要在protocols 不一樣。 * CAN-FD則是CANBUS的延伸。 * CANOPEN主要是在OSI中的網路層以上(包括網路層)的協定,上層應用層等。 - CANopen標準包括address、數個小的通訊子協定及由設備子協定所定義的應用層。 ![image](https://hackmd.io/_uploads/rJQaICKexx.png) ![](https://hackmd.io/_uploads/Hyt0gmGUh.png) * CAN FD與CAN的差異在Data field 和 CRC 長度 * CAN FD 是 64byte * CAN bus 2.0 是 8byte * 更詳細資料請繼續閱讀下面 ### CAN BUS 2.0A * 在CAN 2.0A協議中,標準protocols有11位元組。 * CAN 2.0A 使用 11 位protocols,最多允許傳輸 2^11(或 2048)條不同的消息。它具有較低優先級的消息,可以中斷較高優先級的消息。 * 2.0A版本的CAN協議使用基本帧格式(Base Frame Format),支持11位protocols(Standard Identifier)和29位protocols(Extended Identifier)。 ### CAN BUS 2.0B * CAN 2.0B 使用 29 位protocols,最多允許傳輸 2^29(或 536,870,912)不同的消息。它有兩種類型的消息幀:標準的和擴展的。 2.0B 協議還可以檢測正在發送的消息中的錯誤,並具有錯誤檢測和恢復機制。 * 2.0B版本的CAN協議則支持基本帧格式和擴展帧格式(Extended Frame Format),擴展帧格式可以支持更長的識別符(最長可達到32位),並且提供了更強大的錯誤檢測和診斷能力。 * 此外,CAN 2.0B協議支持故障檢測和故障隔離機制,可以在發生錯誤時自動關閉相應的節點,以提高系統的可靠性和安全性。而CAN 2.0A協議則沒有這些功能。 ``` CAN (Controller Area Network) is a communication protocol used in various industries for communication between microcontrollers and devices. There are two versions of CAN protocols: CAN 2.0A and CAN 2.0B. CAN 2.0A uses an 11-bit identifier, which allows for up to 2^11 (or 2048) different messages to be transmitted. It has a lower priority message that can interrupt higher priority messages. The 2.0A protocol can also detect errors in the message being sent. CAN 2.0B uses a 29-bit identifier, which allows for up to 2^29 (or 536,870,912) different messages to be transmitted. It has two types of message frames: standard and extended. The 2.0B protocol can also detect errors in the message being sent and has an error detection and recovery mechanism. Both CAN 2.0A and CAN 2.0B use the same physical layer, which includes the physical connection and voltage levels used for communication. ``` ### CAN OPEN(Controller Area Network, CAN) * CAN BUS 2.0延伸且支援的一種針對在網路封包的定義 * Network management (NMT) protocols. * Service Data Object (SDO) protocol. * Process Data Object (PDO) protocol. * Synchronization Object (SYNC) protocol. * Time Stamp Object (TIME) protocol. * Emergency Object (EMCY) protocol. ### CAN-FD Main features of the CAN-FD protocol are listed below: • Compatibility with the CAN protocol: CAN-FD node is able to send/receive CAN messages according to ISO 11898-1 • Error-checking improvement, based on checksum field up to CRC 21 bits • Prioritization of messages • Guarantee of latency times • Configuration flexibility • Multicast reception with time synchronization • System-wide data consistency up to 64 bytes per message • Multimaster • Error detection and signaling • Distinction between temporary errors and permanent failures of nodes and autonomous switching off of defect nodes ### CAN and CAD-FD差異 * CAN FD與標準CAN結構相似,但有以下主要不同點: * CAN FD:最大數據速率可達 8 Mbps 或更高(取決於網絡層和物理層) * CAN FD:每個數據最多 64(Bytes),提升數據傳輸效率 * CAN FD: 數據階段(Data Phase) 可切換至更高的速率,提高吞吐量 * CAN FD 提供更強的 CRC(Cyclic Redundancy Check),提高數據完整性 * FDF(FD Format)位:用於區分 CAN FD field 與標準 CAN field * BRS(Bit Rate Switching)位:允許數據階段使用更高的傳輸速率 * ESI(Error State Indicator)位:指示節點是否處於錯誤狀態 * 數據長度:支持 0、1、2、3、4、5、6、7、8、12、16、20、24、32、48、64 字節的有效數據 ## CAN sample point 說明 * <span class="red">CAN sample point 百分比最少要 80%達到抗雜訊能力,且Master and Slave Sample point要為相同的80%</span> - 例如: I2C Slave 100k = Master 100K * 實際 Bit Time = SYNC_SEG + PROP_SEG + PHASE_SEG1 + PHASE_SEG2 1. SYNC_SEG (Synchronization Segment, SYNC_SEG) :同步區段,固定 1 time quantum(Tq),用來偵測訊號邊緣,與其他節點同步。 2. PROP_SEG (Propagation Time Segment, PROP_SEG) :傳播延遲補償時間。 3. PHASE_SEG1 (Phase Buffer Segment 1, PHASE_SEG1) :第一階段調整區,用來做同步補償。 4. PHASE_SEG2 (Phase Buffer Segment 2, PHASE_SEG2) :第二階段調整區,用來做同步補償。 5. SJW:同步跳躍寬度,代表允許調整 PHASE_SEG1 或 SEG2 的最大時間,用來處理時鐘差異。 6. Baud Rate Prescaler (BRP / NBRP):將 CAN 時脈除頻,用來產生一個 Tq。 ### Sample point 公式 * <span class="red">CAN_NBTP calculation: - Nominal bit rate prescaler(NBRP) = (fdcan_clk / ((3 + PHASE_SEG1 + PHASE_SEG2) * bit rate)) - Nominal Time segment 1(PHASE_SEG1) = ((PHASE_SEG2 + 1) * sample point / (1 - sample point))</span> ![image](https://hackmd.io/_uploads/H1fC71hMgl.png) ### Bit tim 設計分類 * Bit Time CAN (ISO 11898-1:2015) 以前設計: - BS1 (Bit Segment 1):SYNC_SEG + PHASE_SEG1(含PROP_SEG) - BS2 (Bit Segment 2):PHASE_SEG2 - SJW:同步跳躍寬度NBRP / BRP:除頻器(影響一個 Tq 的長度) * Bit Time Flexible Data-Rate CAN (ISO 11898-1:2015): - BRP(Bit Segment ): PROP_SEG - BS1 (Bit Segment 1):PHASE_SEG1 - BS2 (Bit Segment 2):PHASE_SEG2 - SJW:同步跳躍寬度NBRP / BRP:除頻器(影響一個 Tq 的長度) #### Example 1: REG設計有 SYNC_SEG + PHASE_SEG1(含PROP_SEG) + PHASE_SEG2 算法 ![image](https://hackmd.io/_uploads/SyatR1hMxx.png) ```= Can 配置: 54MHZ 1MHZ 總和54 * T SYNC_SEG = 1 (tQ) * T PHASE_SEG1 = 43 (tQ) * T PHASE_SEG2 = 10 (tQ) 1. 計算整個位時間 T BIT = TSYNC_SEG + TPHASE_SEG1 + TPHASE_SEG2 TBIT = 1 + 43 + 10 = 54tQ 2. 計算採樣點之前的時間總和(分子) Numerator = TSYNC_SEG + TPHASE_SEG1 Numerator = 1 + 43 = 44 tQ 3. 計算採樣點百分比 Sample point = 44/54 * 100% = 0.81 * 100% = 81% ``` #### Example 2: REG設計有 SYNC_SEG + PROP_SEG + PHASE_SEG1 + PHASE_SEG2 算法 ![image](https://hackmd.io/_uploads/Hkhw01nGle.png) ```= Can 配置: 54MHZ 1MHZ 總和54 * T SYNC_SEG = 1 (tQ) * T PROP_SEG = 5 (tQ) * T PHASE_SEG1= 38 (tQ) * T PHASE_SEG2 = 10 (tQ) 1. 計算整個位時間 T BIT = TSYNC_SEG + TPROP_SEG + TPHASE_SEG1 + TPHASE_SEG2 TBIT = 1 + 5 + 38 + 10 = 54tQ 2. 計算採樣點之前的時間總和(分子) Numerator = TSYNC_SEG + TPROP_SEG + TPHASE_SEG1 Numerator = 1 + 5 + 38 = 44 tQ 3. 計算採樣點百分比 Sample point = 44/54 * 100% = 0.81 * 100% = 81% ``` ## CANBUS and CANFD Protocol ### STM32 Protocol ![image](https://hackmd.io/_uploads/BkwfYnbcJg.png) After identifier, CAN 2.0 and CAN-FD have a different action: 1. CAN-FD 訊息可分為三個階段 (1) a first arbitration phase (2) a data phase (3) second arbitration phase The first arbitration phase is a message that contains: • a start of frame (SOF) • an ID number and other bits, that indicate the purpose of the message (supplying or requesting data), and the speed and format configuration (CAN or CAN-FD). The data transmission phase consists on: • the data length code (DLC), that indicates how many data bytes the message contains • the data the user wishes to send • the check cyclic redundancy sequence (CRC) • a dominant bit 3. arbitration phase and ID,指示消息的目的或提供請求數據和速度格式配置(CAN and CANFD)標準偵測是12bit = 11 bit + 1 bit r1; 4. 32bit= 11bit Base ID + 1 bit r1 + 1 bit IDE + 18 bit Extended ID + 1 bit r0, ### CAN 自行定義 ![image](https://hackmd.io/_uploads/r1HuOTn8Jg.png) ### NXP Protocol ![image](https://hackmd.io/_uploads/Bk7QEU7F1g.png) ## CAN ID * CAN上有三個device,為A,B,C。 * A device有接收ID:202,302;發送ID:101,102; * B device有接收ID:101,303;發送ID:201,202; * C device有接收ID:101,102,201;發送ID:302,303; * 如果A device發送了ID為101的Commad,因為B device和C device都有接收為101的ID,那麼B device和C device都可以接收到這條Commad。 * 如果B device發送了ID為202的Commad,因為A device有接收為202的ID,那麼A device可以接收到這條Commad。 * 如果A device發送了ID為102的Commad,因為C device有接收為102的ID,那麼A device可以接收到這條Commad。 ## 使用STM32 ### CAN2.0A與CAN2.0B的Arbitration Field * 一個32bit的Filter,可以看到在CAN2.0B當中STDID的確有10個bit,EXTID有17個bit而剩餘的就是IDE與RTR共 32bit,所以32位元的可以實現對標準與擴展的訊息過濾。 * ![](https://i.imgur.com/YjgkfVp.png) * 兩個16bit的Filter,包含了CAN2.0A當中的11個STDID、1個IDE、1個RTR與3個EXTID,只能實現對標準模式的訊息過濾。 * ![](https://i.imgur.com/x0V4QTl.png) ### Filter模式 * IDMASK : 當設置為1則會去比對所接收到的ID與Register當中的ID相不相符,0則不比對。像下方這張圖來說紅色的部分就是會比對的ID位置必須與暫存器當中相同才可以通過。 * ![](https://i.imgur.com/MBYtfK0.png) * 以32bit來看最多只能寫入一組ID去過濾而已 * ![](https://i.imgur.com/CGgCMBE.png) * 以16bit來看最多可寫入兩組ID,但只能識別標準模式下的ID * ![](https://i.imgur.com/8IMqdvS.png) * IDLIST : 所接收到的訊息當中ID必須與Filter Register當中所有的bit相同才會接收,只要有任何一個不相同則不接收。 ![](https://i.imgur.com/Ee2zUOs.png) * 以32位元來看因為是IDLIST模式,可以設置兩種ID來進行過濾 * ![](https://i.imgur.com/1FkAMWm.png) * 以16位元來看最多可設置四種ID來進行過濾 * ![](https://i.imgur.com/M4Znhkk.png) ## 邏輯分析儀解析解說 ![22F31326](https://hackmd.io/_uploads/BJRiafN9yx.png) * 只要資料有連續5個0或連續5個1 就會塞入一個stuff bit - 如果是5個0 就塞一個 1 , 5個1 就塞一個0 - CAN控制器 會自己移除這個bit,但是邏輯分析儀上面會捕捉到這個bit # 參考 * [CAN—通讯实验](https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/book/CAN.html) * [STM32-12 CANBus概念介紹](https://ithelp.ithome.com.tw/articles/10284348)