Lecture 2-AutoNet - Prof - Animesh Giri
Lecture 2-AutoNet - Prof - Animesh Giri
Networking
BITS Prof.Animesh Giri
Pilani
Pilani Campus
Automotive Networking-
AELZG534
Lecture No. 2
BITS Pilani, Pilani Campus
Automotive
Networks
• Fundamentals of CAN communication
• Protocol Layers
• Bus Access- Arbitration
• Priority Assignments and Frame formats
Hardware Implementation
• Data Transfer Sequence
3
BITS Pilani, Pilani Campus
CAN-B – Low
speed
Low-speed CAN (CAN-B)CAN-B is defined in ISO Standard 11898-
3 and operates at a bit rate of 5 to 125 kBit/s. For many
applications in the comfort/convenience and body area, this
speed is sufficient to meet the real-time requirements
demanded in this area.
Examples of such applications are:
Control of the air-conditioning
system
Seat adjustment
Power-window unit
Sliding-sunroof control
Mirror adjuster
Lighting system
Control of the navigation 4
BITS Pilani, Pilani Campus
CAN
Network
5
BITS Pilani, Pilani Campus
CAN-
C
CAN-C is defined in ISO Standard 11898-2 and operates at
bit rates of 125 kBit/s to1 MBit/s. The data transfer is
therefore able to meet the real-time requirements of the
drivetrain.
6
BITS Pilani, Pilani Campus
CAN
Node
7
BITS Pilani, Pilani Campus
CAN
Transmitter
• A network node - comprises the microcontroller for the
application software, the CAN controller and the CAN
transceiver (bus driver).
• The CAN controller is responsible for the transmit and
receive modes.
• CAN controller generates the bit stream for data
communication from the binary data to be transmitted
and forwards it to the transceiver on the TxD line.
• CAN Transceiver amplifies the signals, generates the
voltage level required for differential data transfer and
transmits the processed bit stream serially on the bus
line (CAN_H and CAN_L)
8
BITS Pilani, Pilani Campus
CAN Voltage
level
a) Low speed CAN B:
Recessive state (logic 1) : CAN_L at 5V, CAN_H at 0V
Dominant state (logic 0) : CAN_L at 1.4V, CAN_H at 3.6V
9
BITS Pilani, Pilani Campus
CAN Bus Lengths
and Termination
1 MBits/s for 40 m (specified)
500 kBits/s up to 100 m (recommendation)
250 kBits/s up to 250 m
125 kBits/s up to 500m
40 kBits/s up to 1,000 m
10
BITS Pilani, Pilani Campus
Protocol
Layers
Application layer: Represents the information in the
form of data structures used by the application. These
sets of data which are to be transmitted are forwarded to
the object layer for this purpose.
Object layer: Manage the messages, Decide which
message should be sent at which time. Perform
message filtering for incoming messages
Transport layer: Furnishes object layer with received
messages, Processes messages prepared by the object
layer for sending in such a way that the physical layer is
able to transmit this information. Transport layer is
responsible for such functions as arbitration or fault
detection and signaling.
Physical layer
The physical layer is the lowest level in the transport
stack. It consists of the physical components of the
network, such as the wiring and the voltages used to
send the information.
11
BITS Pilani, Pilani Campus
Content Based
Addressing
CAN does not address the individual network nodes but the
messages that have been sent.
Each message has a unique marker, or
identifier ,
identifier classifies the content of the
message (e.g. engine speed or power-
window position)
14
BITS Pilani, Pilani Campus
CAN Message Frame -
Data
15
BITS Pilani, Pilani Campus
Data Frame
Format
Start of frame: Bus is recessive in idle state. Start of frame is a dominant bit
Arbitration field:
CAN 2.0 A -11-bit identifier,control bit, the RTR bit (Remote Transmission
Request-dominant bit).
CAN 2.0 B -11-bit identifier, SRR bit (Substitute Remote Request) and the IDE
(Identifier Extension Bit), Second 18-bit identifier follows, RTR bit completes
the arbitration field.
SRR and IDE bits are sent recessively so that CAN 2.0 A frame always
takes priority over a CAN 2.0 B frame if it has the same 11-bit identifier.
Control field
CAN 2.0 A frame: IDE bit (Identifier Extension Bit), which is always sent as
dominant here, followed by a reserved bit for future extensions, which is sent
recessively. The remaining four bits in this field define the number of data bytes
in the next data field.
CAN 2.0 B frame is practically identical. However, since the IDE bit already
belongs to the arbitration field, a further reserved bit for future extensions takes
its place and this reserved bit is sent recessively.
BITS Pilani, Pilani Campus
Data
Frame..
Data field: Actual message information comprised of between 0 and 8 bytes. A
data field in which the length of the data is expressed in 0 results in the
shortest possible data frame with a length of 44 or 64 bits. A frame like this
can be used to synchronize distributed processes. A number of signals can
be transmitted in a single message (e.g. engine temperature and engine
speed).
CRC field: The CRC field (Cyclic Redundancy Checksum)contains a 15-bit
checksum (frame check word) across the preceding frame from the start bit
to the final bit of the data field. The 16th bit (CRC delimiter) in this field is
recessive and closes the checksum. The checksum is a means of detecting
possible transmission interference
ACK field: ACK (acknowledgment) is set by a different node that is able to
acknowledge receipt of the frame directly after the data field. This field
comprises the ACK slot and the recessive ACK delimiter. The ACK slot is also
transmitted recessively by the sender and over-written as dominant by a
receiver upon the message being correctly received. Correct receipt is
confirmed. Signals to the sender that transmission was a success.
17
BITS Pilani, Pilani Campus
Data
frame..
End of frame: End of frame marks the end of the message and comprises
seven recessive bits. With seven bits of the same value in succession, the
stuffing rule is deliberately broken
Interframe space: Interframe space comprises a succession of three recessive
bits which serve to separate successive messages. After a total of 10 of
these recessive bits, the stations are permitted to begin transmitting within
the network again. Until then, the bus remains in idle state. Only data and
remote frames need to include the interframe space. Error and overload
frames can be sent immediately after the last frame. This enables
immediate signaling of errors and problems. The transmitter will usually
initiate a data transmission by sending a data frame. It is also possible for a
receiving station to call in data from a sending station by depositing a
remote frame. The network node that is able to deliver the information
requested makes the information available in response to this query.
18
BITS Pilani, Pilani Campus
Error
Detection
Control mechanisms for detecting errors are integrated in the CAN protocol.
Cyclic redundancy check: For every transmitted message, the transmitter
calculates a check sequence from the start-of-frame, arbitration, control and
data field. A 15-bit checksum is derived from the bit sequence by means of
a generator polynomial. The checksum is then used to detect errors in the
data transfer. With CRC generation, a defined generator polynomial is used
to carry out polynomial division across a given frame range. The remainder
forms the checksum. Once the CRC field has been received, the receiver is
able to check that the frame has been transferred correctly by carrying out
its own polynomial division of the received frame range using the generator
polynomial and checking whether the received checksum matches the
calculated remainder.
Frame check:The frame check involves all bus subscribers, senders as well as
receivers, checking the sent/received data frame for compliance with the
predefined frame structure. The CAN protocol contains a number of fixed-
format fields (start and end-offrame, delimiter) which are checked by all
stations.
19
BITS Pilani, Pilani Campus
Error
Detection..
ACK check: With the ACK check, a receiver acknowledges that the frame has
been received correctly by sending a dominant bit in the ACK slot. The
sender of the frame can then verify whether a message was transferred
correctly. An absence of this bit indicates that a transmission error has been
detected.
Monitoring
The sender of a frame continuously monitors the bus level. It is able to detect a
transmission error by comparing the sent bit and the sampled bit.
Bit stuffing: Data frame or remote frame - A maximum of five successive bits
of equal state may be sent between the start of frame and the end of the
CRC field. As soon as five identical bit states have been transmitted in
succession, the sender includes a bit (stuffing)with the opposite state. The
receiving station clears all of these inserted bits after the message has been
received (destuffing). This measure permits detection of line faults, e.g.
short-circuit or burst interference.
20
BITS Pilani, Pilani Campus
Error
Detection
•If a CAN controller detects a fault, format error, it interrupts the current
transmission by sending an error frame of six successive dominant bits.
• This breaks the stuffing rule that prohibits this type of bit sequence.
•If the sender detects that its message has been interrupted by an error frame,
it stops transmitting and makes another attempt at a later time.
•This effect prevents other stations from accepting the erroneous message and
thereby ensures consistency of data across the entire system.
Fault confinement
Defective stations could weigh heavily on bus traffic if they were to frequently
send erroneous messages or interrupt the transmission of correct messages by
repeatedly sending an error frame.
The CAN protocol localizes station failures by means of statistical error
analysis. A station recognizes the probability of its own malfunction by how
often it aborts messages before other stations send an error frame. The
protocol’s first measure in this case is to prevent a station such as this from
continuing to abort transmissions. In an emergency, the station shuts down
automatically.
21
BITS Pilani, Pilani Campus
Bit
Time..
SYNCHRONIZATION SEGMENT (SYNC_SEG): This part of the bit time is
used to synchronize the various nodes on the bus. An edge is expected to lie
within this segment.
PROPAGATION TIME SEGMENT (PROP_SEG): This part of the bit time is
used to compensate for the physical delay times within the network. It is twice
the sum of the signal’s propagation time on the bus line, the input
comparator delay, and the output driver delay.
PHASE BUFFER SEGMENT1 (PHASE_SEG1)
PHASE BUFFER SEGMENT2
(PHASE_SEG2)
These PHASE-BUFFER-SEGMENTS are used to compensate for edge phase
errors. These segments can be lengthened or shortened by resynchronization.
SAMPLE POINT
The SAMPLE POINT is the point of time at which the bus level is read and
interpreted as the value of that respective bit. It’s location is at the end of
PHASE_SEG1. The time segments for the two bit rates of the CAN FD protocol
are defined by two sets of configuration registers.
22
BITS Pilani, Pilani Campus
Case Study – MCP2515 CAN
Controller
23
BITS Pilani, Pilani Campus
Bit
Time
Time Quantum (TQ): Each segment that makes up a bit time are made up of
integer units called Time Quanta (TQ).
Time Quantum is based on the oscillator period (tOSC).
TQ equals twice the oscillator period.
The TQ length equals one TQ Clock period (tBRPCLK), which is programmable
using a programmable prescaler named the Baud Rate Prescaler (BRP).
SyncSeg = 1TQ
PropSeg is programmable from 1 - 8TQ
PS1 is programmable from 1 - 8TQ and PS2 is programmable from 2 - 8TQ
24
BITS Pilani, Pilani Campus
Bit
Time..
25
BITS Pilani, Pilani Campus
Bit
Time..
Bit Timing Control Registers:
•The CAN Bit Timing Control (CNF) registers are the three registers that
configure the CAN bit time.
•By adjusting the length of the TQ (tTQ) and the number of TQs in each
segment, both the nominal bit time and the sample point can easily be
configured as desired.
The are several requirements for programming the CAN bit timing
segments.
1. PropSeg + PS1 ≥ PS2
2. PropSeg + PS1 ≥ tPROP
3. PS2 > SJW
26
BITS Pilani, Pilani Campus
Bit Time Control
Registers
28
BITS Pilani, Pilani Campus
Sample
Calculations
800KBps
29
BITS Pilani, Pilani Campus
Questions
30
BITS Pilani, Pilani Campus
Arduino CAN
Interface
https://circuitdigest.com/microcontroller-projects/arduino-can-tutorial-interfacing-mcp2515-can-
bus-module-with-arduino
32
BITS Pilani, Pilani Campus
Components
Used
33
BITS Pilani, Pilani Campus
Connectivit
y
CAN Transmitter Receiver
connections Connections
Between CAN
modules
34
BITS Pilani, Pilani Campus
CAN Transmit
Code
CAN Transmitter Code (Arduino Nano):
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#include <DHT.h> //Library for using DHT sensor
#define DHTPIN A0
#define DHTTYPE DHT11
struct can_frame canMsg;
MCP2515 mcp2515(10);
DHT dht(DHTPIN, DHTTYPE); //initilize object dht for class DHT with DHT pin with STM32 and DHT type as DHT11
void setup()
{
while (!Serial);
Serial.begin(9600);
SPI.begin(); //Begins SPI communication
dht.begin(); //Begins to read temperature & humidity sensor value
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
}
void loop()
{
int h = dht.readHumidity(); //Gets Humidity value
int t = dht.readTemperature(); //Gets Temperature value
canMsg.can_id = 0x036; //CAN id as 0x036
canMsg.can_dlc = 8; //CAN data length as 8
canMsg.data[0] = h; //Update humidity value
canMsg.data[1] = t; in [0]
canMsg.data[2] = 0x00; //Update temperature
canMsg.data[3] = 0x00; value in [1]
canMsg.data[4] = 0x00; //Rest all with 0
canMsg.data[5] = 0x00;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;
mcp2515.sendMessage(&canMsg); //Sends the CAN
message delay(1000);
}
35
BITS Pilani, Pilani Campus
CAN Receiver
Code
CAN Receiver Code (Arduino UNO):
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#include <LiquidCrystal.h> //Library for using LCD display
const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Define LCD display pins RS,E,D4,D5,D6,D7
struct can_frame canMsg;
MCP2515 mcp2515(10); // SPI CS Pin 10
void setup()
{ lcd.begin(16,2); //Sets LCD as 16x2 type
lcd.setCursor(0,0); //Display Welcome Message
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("CAN ARDUINO");
delay(3000);
lcd.clear();
SPI.begin();
//Begins SPI
communication
baudrate mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 36
mcp2515.setNormalMode(); //Sets CAN at normal mode BITS Pilani, Pilani Campus
Receiver code
continued..
void loop()
{
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
// To receive data (Poll Read)
{
int x = canMsg.data[0];
int y =
canMsg.data[1];
lcd.setCursor(0,0); //Display Temp & Humidity value received at 16x2 LCD
lcd.print("Humidity : ");
lcd.print(x);
lcd.setCursor(0,1);
lcd.print("Temp : ");
lcd.print(y);
delay(1000);
lcd.clear();
}
}
37
BITS Pilani, Pilani Campus