# 實作於 NXP-MPC5748G 車內混合式網路之微控制器閘道<br>Implementation of MCU gateway on NXP-MPC5748G for In-vehicle hybrid networks 指導教授: 張燕光 專題成員: 梁華軒、林群凱 開發工具:S32 Design Studio for Power Architecture® 2017.R1 測試環境: Windows 10 ## 1. 簡介 在現今的社會中,所有的設備都在走入電子化,汽車也不例外。隨著汽車工業的發展,汽車所需要的電子元件越來越多,變速箱、煞車系統…等都需要電子控制器來控制。各式各樣的原件在車子內組成龐大的網路,因此基礎元件以及控制元件的溝通會是一大考驗,如何順利將使用者所想要做的事情傳送到控制元件上,再傳送到正確的基礎單元,進行控制與獲取基礎單元的訊息?在此次的專題中,我們以Windows 10的電腦當作使用者介面以及控制單元,使用者所發送的訊息會傳至gateway進行解析後發送至對應的基礎單元,基礎單元也會做出相應的反應,簡單的模擬出使用者在一台電子汽車上的加減速作業所需要的車載網路環境。 ## 2. 系統架構 ![架構圖](https://i.imgur.com/JoOTFGk.png) 1. 發送加減速資訊。Host PC以非同步收發傳輸器(UART)發送資訊,須通過Gateway (MCU-1) 將傳送資訊轉為CAN communications protocol之格式,並以Standard CAN Message發送至MCU-2。 1. Brake/ Accelerator (MCU-2) 解析或解密收到的CAN Message後,發送解析或解密之結果給Speed Calculator (MCU-3)。 1. Speed Calculator (MCU-3) 收到加減速資訊後,計算結果(當前車速),並將當前車速經過Gateway (MCU-1) 傳回至PC,並且將結果顯示於PC端上的UI,以驗證整體架構之功能正確性。 ![UI介面圖](https://i.imgur.com/SJULgNs.png) 圖形介面當中,上方白色半圓形為儀表板,速度範圍從0到120公里/小時,中間有一根指針會指向目前的速度。下方有兩個踏板,右邊的為油門,在按下後速度會上升每小時10公里,左邊的是煞車,按下後速度會降低每小時5公里。 圖中是三個微控制器以及匯流排,每個微控制器都會將各自的CAN接口連接到同一個匯流排上,並且各自接上電源,擔任 gateway 的微控制器會在用 OpenSDA 與PC 連接。 ## 3. 專題兩大核心須知 * [UART](https://www.circuitbasics.com/basics-uart-communication/) Two wires are needed to transmit data between two UARTs. ![](https://i.imgur.com/ZznTTbW.png) UARTs transmit data asynchronously, which means there is no clock signal to synchronize the output of bits from the transmitting UART to the sampling of bits by the receiving UART. Instead of a clock signal, the transmitting UART adds start and stop bits to the data packet being transferred.<br> UARTs must operate at about the same baud rate (read the incoming bits at a specific frequency). * Advantages - Only uses two wires - No clock signal is necessary - Has a panty bit to allow for error checking - The structure of the data packet can be changed as long as both sides are set up for it - Well documented and widely used method * Disadvantages - The size of the data frame is limited to a maximum of 9 bits - Doesn't support multiple slave or multiple master systems - The baud rate of each UART must be within 10% of each other [If you are interested in how to implement UART example click me](https://hackmd.io/@eddielin0926/IVN-2021-10-13) * CAN * CAN Benefits ![](https://i.imgur.com/BsQTQm7.png) 1. Low-Cost, Lightweight Network CAN provides an inexpensive, durable network that helps multiple CAN devices communicate with one another. An advantage to this is that electronic control units (ECUs) can have a single CAN interface rather than analog and digital inputs to every device in the system. This decreases overall cost and weight in automobiles. 2. Broadcast Communication Each of the devices on the network has a CAN controller chip and is therefore intelligent. All devices on the network see all transmitted messages. Each device can decide if a message is relevant or if it should be filtered. This structure allows modifications to CAN networks with minimal impact. Additional non-transmitting nodes can be added without modification to the network. 3. Priority Every message has a priority, so if two nodes try to send messages simultaneously, the one with the higher priority gets transmitted and the one with the lower priority gets postponed. This arbitration is non-destructive and results in non-interrupted transmission of the highest priority message. This also allows networks to meet deterministic timing constraints. 4. Error Capabilities The CAN specification includes a Cyclic Redundancy Code (CRC) to perform error checking on each frame's contents. Frames with errors are disregarded by all nodes, and an error frame can be transmitted to signal the error to the network. Global and local errors are differentiated by the controller, and if too many errors are detected, individual nodes can stop transmitting errors or disconnect itself from the network completely. ### How CAN Communication Works As stated earlier, CAN is a peer-to-peer network. This means that there is no master that controls when individual nodes have access to read and write data on the CAN bus. When a CAN node is ready to transmit data, it checks to see if the bus is busy and then simply writes a CAN frame onto the network. The CAN frames that are transmitted do not contain addresses of either the transmitting node or any of the intended receiving node(s). Instead, an arbitration ID that is unique throughout the network labels the frame. All nodes on the CAN network receive the CAN frame, and, depending on the arbitration ID of that transmitted frame, each CAN node on the network decides whether to accept the frame. If multiple nodes try to transmit a message onto the CAN bus at the same time, the node with the highest priority (lowest arbitration ID) automatically gets bus access. Lower-priority nodes must wait until the bus becomes available before trying to transmit again. In this way, you can implement CAN networks to ensure deterministic communication among CAN nodes. ![](https://i.imgur.com/jbQS0rO.png) [If you are interested in how to implement CAN example & muiltple build MPC5748G in one computer click me](https://hackmd.io/@eddielin0926/SyirDsQSq) ## 4. 實作 * 使用器材 * 3 個 MPC5748G * 3 條 Micro usb to usb(作為與電腦溝通的媒介:燒入以及UART) * 一台電腦(同時作為燒入工具以及UI呈現) * 足夠多的杜邦線以及麵包板(作為CAN bus的溝通媒介) * 軟體需求 * S32 Design Studio for Power Architecture® 2017.R1 (做為MPC5748G的開發工具) * 程式碼重點解釋 * Gateway ``` c= #define MASTER /* #define SLAVE */ #define TOGGLE_LED_CODE 0xAB /* Definition of the TX and RX message buffers depending on the bus role */ #if defined(MASTER) #define TX_MAILBOX (1UL) #define TX_MSG_ID (1UL) #define RX_MAILBOX (3UL) #define RX_MSG_ID (3UL) .... flexcan_data_info_t dataInfo = //define the message { .data_length = 1U, .msg_id_type = FLEXCAN_MSG_ID_STD,//define the id is standard long .enable_brs = true, .fd_enable = true, .fd_padding = 0U }; ..... void can_receive(void){ /* Define receive buffer */ flexcan_msgbuff_t recvBuff; /* Configure RX message buffer with index RX_MSG_ID and RX_MAILBOX */ FLEXCAN_DRV_ConfigRxMb(INST_CANCOM1, RX_MAILBOX, &dataInfo, RX_MSG_ID); //define the id of the reciever help to know whether message need to be read or not /* Start receiving data in RX_MAILBOX. */ FLEXCAN_DRV_Receive(INST_CANCOM1, RX_MAILBOX, &recvBuff); /* Wait until the previous FlexCAN receive is completed */ while(FLEXCAN_DRV_GetTransferStatus(INST_CANCOM1, RX_MAILBOX) == STATUS_BUSY); //if (recvBuff.data[0] <=200 && recvBuff.data[0]>0) { memset(buff, 0, 20); snprintf(buff,20,"%d\r\n",recvBuff.data[0]); LINFLEXD_UART_DRV_SendDataBlocking(INST_LINFLEXD_UART1, buff, strlen(buff), 300U); /* Insert a small delay to make the blinking visible */ //delay(720000); /* Toggle output value LED1 */ PINS_DRV_TogglePins(PORT, 1 << LED); } } void master_send(uint8_t *ledRequested ){ { /* Configure TX message buffer with index TX_MSG_ID and TX_MAILBOX*/ FLEXCAN_DRV_ConfigTxMb(INST_CANCOM1, TX_MAILBOX, &dataInfo, TX_MSG_ID); /* Execute send non-blocking */ FLEXCAN_DRV_Send(INST_CANCOM1, TX_MAILBOX, &dataInfo, TX_MSG_ID, ledRequested); while (FLEXCAN_DRV_GetTransferStatus(INST_CANCOM1, TX_MAILBOX) == STATUS_BUSY); } } .... int main(void) { ...... while(1) { memset(buff, 0, 20); // UART receiver LINFLEXD_UART_DRV_ReceiveDataBlocking(INST_LINFLEXD_UART1, buff, strlen(buff), 300U); if (buff[0] == '1'){ uint8_t mode=1; master_send(&mode); break; } else if (buff[0] == '2'){ uint8_t mode=2; master_send(&mode); break; } } can_receive(); } ``` * other parts of code is alike please check them by yourself [hear is the link](https://drive.google.com/drive/folders/12iuwljoO6TzN-wGbSR9-G-nKeXQ20Ttq?usp=sharing)