HDL Implementation of A Controller Area Network Node
HDL Implementation of A Controller Area Network Node
12-31-2003
Recommended Citation
Marino, Anthony Richard, "HDL implementation of a controller area network node" (2003). Theses and
Dissertations. 1341.
https://rdw.rowan.edu/etd/1341
This Thesis is brought to you for free and open access by Rowan Digital Works. It has been accepted for inclusion
in Theses and Dissertations by an authorized administrator of Rowan Digital Works. For more information, please
contact LibraryTheses@rowan.edu.
HDL Implementation of a Controller Area Network Node
by
MASTER OF SCIENCE
Rowan University
Glassboro, New Jersey
2003
ii
TABLE OF CONTENTS
Abstract........................................ ....................................................................................... x
Acknowledgements................................................ ............................................................ xi
iii
3.1 CAN M odule Specification......................................................................................... 43
3.2 CAN Protocol Controller ............................................................................................ 44
3.2.1 Bit Tim ing Logic .................................................................................................. 46
3.2.2 Transmit and Receive Registers ................................................ 49
3.2.3 Bit Stream Processor ............................................................................................ 49
3.2.3.1 Frame Generator............................................................................................. 50
3.2.3.2 Bit Stuffing and D e-stuffing........................................................................... 50
3.2.3.3 Cyclic Redundancy Check ............................................................................. 51
3.2.3.4 M essage Filtering.......................................................................................... 52
3.2.4 M essage Buffer ..................................................................................................... 53
3.2.5 Error M anagem ent Logic...................................................................................... 54
3.3 FPGA Im plem entation ........................................ ...................................... ...... 57
3.3.1 FPG A w ith CAN A pplied to Light Control.......................................................... 58
3.3.1.1 CAN D ata Frame Coding............................................................................... 60
3.3.1.2 Transm itting and Receiving a CAN Fram e................................... ............ 63
3.3.1.2.1 Controlling the CAN Functions ............................................................... 64
R eferences......................................................................................................................... 96
iv
B.1: Initialization and Message Transmission C Code... .................................... 190
B.2: Initialization and M essage Reception C Code ..................................................... 193
v
List of Figures
Figure 1.2 - Individual light locations of the New Jersey State Police light
package in the (a) front and (b) rear of the vehicle. 10
Figure 2.9 - Principle of bit stuffing with a stuff width of 5 bits, where S is the
stuff bit. 35
vi
Figure 3.8 - Transistor used as a basic digital switch. 59
Figure 3.9 - Bit-stuffing of a sample CAN frame from SOF to end of CRC
sequence. 62
Figure 3.11 - Process flow of FPGA light control approach. The user depresses
the switch to activate a light pattern. A CAN message is generated and sent to
the receiving FPGA, which filters the message. The microcontroller reads the
data field and controls the lights accordingly. 64
Figure 4.2 - Process flow of simulated FPGA light control approach using self
reception mode. A message is generated and transmitted by the CAN node and
the message is looped directly back to the receive line. 71
Figure 4.8 - Timing diagram of data byte being transferred and then interpreted
to force LED high. 76
Figure 4.9 - Flow diagram for bit inspection of received data byte. 85
Figure 5.1 - Sectionalized design for CAN node locations within the NJSP troop
car. 89
vii
List of Tables
Table 2.1 - Coding of the number of data bytes by the data length code from [8]. 25
Table 3.2 - Transmit and receive buffer layout of the CAN controller [39]. 54
Table 3.3 - Data coding using single bit position per light. 61
Table 4.1 - Layout of receive buffer after reception of message for the first
simulation. 74
Table 4.3 - Bit layout of the clock divider register; CAN address 31. 79
Table 4.4 - Bit layout of the acceptance code register; CAN address 4. 79
Table 4.5 - Bit layout of the acceptance mask register; CAN address 5. 79
Table 4.6 - Bit layout of the bus timing register 0; CAN address 6. 80
Table 4.7 - Bit layout of the bus timing register 1; CAN address 7. 80
Table 4.8 - Bit layout of the output control register 1; CAN address 8. 81
Table 4.9 - Resulting bit layout after the host microcontroller writes the
prepared message to the transmit buffer. 82
Table 4.10 - Bit layout of the command register for transmitting node; CAN
address 1. 82
Table 4.11 - Bit layout of the status register for the transmitting node; CAN
address 2. 83
Table 4.12 - Resulting bit layout of the receive buffer after the CAN controller
receives the CAN message from the transmitting node. 83
viii
Table 4.13 - Bit layout of the status register for the receiving node; CAN
address 2. 84
Table 4.14 - Bit layout of the command register for the receiving node; CAN
address 1. 84
ix
Abstract
vehicles, there is a growing need for centralized wiring and device interconnection
methods. Each new technology, such as GPS navigation units and in-vehicle video
systems, includes a corresponding wiring harness that needs installation. The summation
of all these hardwired technologies leads to large volumes of wire that negatively affect
safety, power demands, and future design opportunities. A new trend among automotive
manufacturers is to design and implement innovative networks that allow for shared
network that will centralize connections involved with the New Jersey State Police's
pursuit light package currently installed in their troop cars. The intent is to show that if
the wiring involved with such a system can be reduced, then other systems can follow
suit. The network used is the controller area network (CAN) that was developed in the
1980s by Bosch. The work presented in this thesis is supported through the joint
sponsorship of the New Jersey State Police (NJSP), New Jersey Department of
results have been obtained using this approach and should improve driver safety and
assist NJSP with technology modernization and management within their fleet of troop
cars.
x
Acknowledgements
advisor, Dr. John Schmalzel. His relentless devotion to see me succeed has not gone
unnoticed and I will always be thankful for that. He has been a mentor and a friend for
the last five years and I look forward to keeping his company in the future.
As for my co-advisor, Dr. Shreekanth Mandayam, I would like to thank him for
his encouragement, knowledge, and helpful feedback. Of course I can't forget to mention
his ability to keep the mood light, even during my most trying times. Also I would like to
thank Dr. Peter Mark Jansson for serving on my committee and providing me with useful
feedback.
Tremendous gratitude to the joint partnership of the New Jersey State Police, New
that has eclipsed any lab experience and can someday prove useful in the real world.
The past five years have been demanding and rewarding, not just for me but for
my family as well. I'd like to thank my family for supporting me in all my endeavors and
allowing me to pursue my goals. Also I'd like to thank my girlfriend, Dana, who has been
strong friendship with during this experience. All of you have made this experience an
unforgettable one.
xi
Chapter 1: Introduction
Over the past ten years, electronic systems have steadily replaced or augmented
concerns that require improved vehicle operating conditions. To help monitor the
conditions of a vehicle there are numerous sensors and electronic control units (ECUs)
that monitor various emissions related parameters. Effective control of vehicle efficiency
requires ECUs to share information. For example, sensors and actuators sense the
operation of specific components (e.g., oxygen sensor) and actuate others (e.g., fuel
injectors) to maintain optimal engine control. In its simplest terms, today's vehicles
contain hundreds of circuits and other electrical entities. In fact, it is estimated that
today's cars can have up to 4 kilometers of wiring as compared to the 45 meters in cars
Aside from the in-vehicle telematics, which is the use of computers and
passenger car, law enforcement vehicles require additional technologies. Needs differ
with each law enforcement agency but usually include radar systems, in-vehicle video
systems, and mobile computing devices. As can be imagined, each additional technology
introduces a new set of wires and harnesses. Eventually the accumulation of these
hardwired devices has implications on vehicle integrity and power demands. With each
50 kg of wiring, fuel consumption is increased by 0.2 liters per 100 km traveled [1].
years ago [2]. Automotive manufacturers have made strides to reduce wiring costs by
creating in-vehicle networks to help centralize device connections and wiring to be able
to transfer data from one electronic module to another over a serial bus.
1
1.1 In-Vehicle Networks
The ability to reduce and centralize wiring among the various electronic systems
enforcement vehicle, with the focus on creating in-vehicle networks that facilitate
information sharing between the distributed systems within the vehicle, and reducing
point-to-point wiring. Much like a local area network (LAN) that connects computers
together, an in-vehicle network connects the electronic systems of a car together. These
networks are mostly based on serial protocols and are designed to replace point-to-point
wiring. The goal of any in-vehicle protocol is to link and share information with the
distributed electronic systems that make up the three networks (Class A, Class B, and
Class C) of a vehicle.
categories for standards and protocols: Class A, Class B, and Class C [3]. These divisions
are based on network speed and function as shown in Table 1.1. As communication
Class Attributes
2
performance increases, it is expected that in-vehicle networks will eclipse the 1 Mb/s
barrier, and thus a Class D will be required. SAE has not yet defined standards for Class
D networks; however, networks which exceed a data rate of 1 Mb/s are often referred to
as Class D networks.
Class A maintains the lowest data rate. For this classification the data rate peaks
as high as 10 kb/s. The low speed protocols in this category typically use generic UARTs
that are not standardized and are often proprietary. In general, Class A devices support
Class B networks support data rates in the range of 10 - 100 kb/s. These networks
typically promote non-real time control and general information sharing between nodes.
The utilization of Class B can eliminate redundant sensors and other systems by
providing a means to transfer data from node to node. SAE J1850 (section 1.2.1.1) is a
typical Class B network used for diagnostic purposes, which can also be used for other
electronics applications.
Class C is the fastest of the three network categories. With performance capability
systems, such as power train control. The predominant Class C protocol is CAN (section
1.2.1.4).
3
1.2.1 Current In-Vehicle Networks
A few of the established and emerging in-vehicle networks are listed here:
There is no single standard or one protocol that represents a solution to connect all the
with different features that are better suited for particular applications. For example, a
low speed network may be best for non-critical applications, whereas a high-speed
network is best for real time, critical applications. These points will be discussed in the
following sections.
Protection Agency (EPA) has evolved into OBD II keeping pace with stricter emissions
regulations. OBD II can identify problem(s) with the emissions control system before
excessive emissions become a problem [4]. Upon identification, the driver is informed by
means of an indicator light on the instrument panel prompting the owner to seek service.
OBD II also stores codes corresponding to the faults found in the emissions system; these
codes can be accessed through a diagnostic port via a standard protocol. Currently the
4
The SAE J1850 standard is a Class B protocol, which is a medium speed network
and is used for general information communications. There are two forms of J1850. One
is a high-speed version that operates at 41.6 kb/s using a two-wire differential approach.
Collision resolution allows multiple nodes to transmit at the same time without
causing a failure. Similar to CAN, messages in the J1850 protocol are prioritized. This is
accomplished by allowing an active symbol to win bus arbitration over a passive symbol.
When a node transmits a passive symbol and in turn receives an active symbol, the
transmitting node realizes that another node with higher priority is transmitting at the
same time. Thus, the passive node loses arbitration. This process is illustrated in Figure
2.2.
The LIN specification [6] is a single wire communication protocol derived from
the ISO-9141 standard for automotive diagnostics. This protocol is based on the common
SCI (UART) interface, which is available as low cost silicon modules on almost all
Unlike the J1850 protocol, access in a LIN network is governed by a master node
and consequently eliminates the need for collision management in slave nodes.
Furthermore, the master-slave configuration also leaves the master node with the
not transmit simultaneously and thus alleviates the need for bus arbitration. The master
sends out a message via the single wire bus and exactly one slave node will be activated
upon the reception and ensuing filtering of this message. Although the master controls
5
communication, data can be sent from slave to slave by a corresponding message ID
The development of the LIN standard was in response to the lack of a low-end,
low-cost multiplexed network that would complement and not replace existing networks.
With maximum transmission speeds of 20 kb/s, LIN is targeted for applications involving
on-off devices, such as car seats, doors, and mirrors. LIN can also connect higher speed
OBD and LIN are characterized by low speed data transfer rates utilized for
simple applications. The motivation for the MOST specification [7] was based on the
requirement for a low-cost, high-speed multimedia network for the proliferation of in-
In contrast to OBD and LIN, which use copper medium, MOST technology uses a
fiber optic transmission medium. The resulting benefits with optical medium are
increased data rates and robustness. MOST can achieve data rates up to 24.8 Mb/s and
master supplies the clock and all other devices synchronize their operation to this clock.
The technology is similar to the public switched telephone network. There are both data
and control channels defined. The control channels are used to set up the data channels
the sender and receiver are to use. Once the connection is established, data can flow
6
continuously with no further processing of packet information required. This is the
transmitted messages via a message identifier. Based on a received message identifier, all
nodes check to see if the message is relevant. Therefore, messages can be accepted by
one, many, or all nodes. The identifier of a message also determines the priority with
regard to bus access. In other words, CAN provides schemes to provide priority-based
Bus access in a CAN network is not controlled by a master but instead every node
can transmit as soon as the bus is idle. In such a competitive use scheme, it is possible
that several nodes will try to occupy the bus concurrently. In other protocols, such a
situation would lead to destroyed messages. The CAN protocol has established an access
method that guarantees that the message with the highest priority wins access and then
Perhaps the two most appealing aspects of the CAN protocol are the data rate of
up to 1 Mb/s and the robustness of the CAN network. There are two data lines defined by
CAN, which transmit the same data. If one line is damaged or grounded, the network is
not hindered but operates under degraded noise immunity. The CAN protocol also has the
ability to limit bus access for defective nodes in the network. These metrics make CAN
useful for control of real-time critical functions. The CAN protocol will be discussed
extensively in Chapter 2.
7
1.3 Potential Law Enforcement Applications
In-vehicle networks are obviously not a new idea; however, they have not been
fully exploited. One opportunity that has not been fully examined is the use of in-vehicle
networks on platforms other than a standard passenger car. As already suggested, a law
enforcement platform presents a suite of technology over and above the ones found in
civilian vehicles. This suite of technology does not replace the factory-installed devices
inside vehicles but instead builds upon them. In turn, this package of technology adds
wiring in excess of standard vehicle wiring. The problems associated with this added
vehicle wiring include reduced layout space, deterioration of serviceability and lower fuel
efficiency, other performance issues, manufacturing difficulties, and the presence of new
The target vehicle environment discussed in this thesis, the New Jersey State
Police (NJSP) troop car, hosts a variety of technologies, which include a radar system, in-
vehicle video system, two-way radio, and a mobile data computer (MDC) system. The
human interface points for these applications are all located in the cockpit of the vehicle
creating problems finding sufficient space for the system control points. The only
practical area of the car that offers sufficient space for secure mounting of these devices
is the trunk of the vehicle. Because of the separation between the interfaces in the cockpit
and the corresponding processors and control units in the trunk, there is an excessive
One example is the pursuit light package used by the NJSP, illustrated in Figure
1.1 and 1.2. This application includes a combination of LED, strobe, and halogen lights
that are controlled by a switching unit inside the cockpit of the car with a remote power
unit in the trunk of the vehicle. Overall, there are 14 lights in different positions. In the
8
front of the car, there is one strobe light in each headlight as well as two LEDs in the
grille. There are also LED lights in the two side view mirrors. In the rear of the vehicle,
there are two strobe lights and two halogen lights in the interior positioned on the rear
Key:
0 Strobe
A LED
Halogen
deck, and four strobe lights in the brake and reverse lights. As indicated, the interfaces in
the cockpit of the car are separated from the control units in the trunk. For the light
system, there is a dedicated cable that connects each light to the control unit in the trunk.
The control unit then communicates with the cockpit user interface via another cable. The
earlier.
9
LED light
/.LED light
(a)
LED lights
(b)
Figure 1.2 - Individual light locations of the New Jersey State Police Light package in
the (a) front and (b) rear of the vehicle.
electromagnetic fields, voltage spikes, and many other potential risks. With this in mind,
there are a number of special design considerations when designing an in-vehicle network
[9,10]. Non-safety related communication networks should meet most or all of the
considerations detailed in Table 1.2. Additional specifications are required for safety
critical communications. Because CAN was initially designed for use in automobiles, the
need for a fast, robust protocol was critical to prove useful in the automotive
10
environment. CAN nodes have the ability to detect errors as well as to confine errors
based on their severity. Fault confinement essentially guarantees bandwidth for critical
system information by limiting bus access by faulty nodes. The strengths of the
Controller Area Network protocol made it the ideal candidate for automotive networks.
isaDo
?' '
esign C
, , -X
F'onsieration
*' b '.. ; ' S' *-
- *- t t t-."t ,- ^ .;. ; . : 7... ¢OSi-L
" ' '«* ' ^
Descrip
'
ton
-' ~' ' '~
'
The communication networks discussed to this point have been introduced for
1.) To integrate subsystems with the goal of creating a more complex, overall
system.
11
environment. CAN nodes have the ability to detect errors as well as to confine errors
based on their severity. Fault confinement essentially guarantees bandwidth for critical
system information by limiting bus access by faulty nodes. The strengths of the
Controller Area Network protocol made it the ideal candidate for automotive networks.
-C- - J
The communication networks discussed to this point have been introduced for
1.) To integrate subsystems with the goal of creating a more complex, overall
system.
11
Alternatively, low bandwidth networks are commonly used for simple comfort
control applications, such as power windows and adjustable seats, where delays on the
order of a second are acceptable. These comfort systems are highly cost sensitive and the
associated networks hosting these applications are constructed to be cost efficient as well.
The target application discussed in this thesis, the NJSP pursuit light package,
does not require a high bandwidth network since it is not a real-time, critical control
demand reliable and responsive networks. Although, the pursuit light package could
operate on a low bandwidth network such as a single wire LIN implementation, greater
flexibility and added functionality can be achieved with a network such as CAN. The
advantages of CAN over J1850 (OBD), LIN and other low bandwidth networks is the
fact that CAN offers the same functionality but at a higher bit rate. Despite the fact that
CAN may be more expensive than these low bandwidth networks, the CAN protocol has
the flexibility to operate as a class A or class B network much like OBD and LIN as well
as a class C network.
a network. An emerging network such as MOST does not have the market popularity of
an established, standardized network such as CAN. With support from many of the
numerous. CAN is currently the most widely used vehicular network, with more than 100
Although CAN is not the cheapest network it is also not the most expensive
network to implement. CAN stand alone controllers cost around $3 and CAN controllers
13
combined with a microcontroller can cost around $7 [11]. The economics of CAN
combined with its availability; flexibility and robust communications profile make it a
wise choice for an in-vehicle network at this time for the New Jersey State Police
Troopcar. However, in-car electronics will continue to progress towards more multimedia
applications and other complex, distributed electronic controls and the need for a better
and faster network may present itself in the future. In such an event, other networks can
established for the control of multimedia applications while the existing CAN network
handles such applications as the comfort electronics, engine management, and other
control applications. The use of bridges and gateway devices will support the
Since the beginning of the 1980s, many automotive manufacturers have become
actively involved in research and development of Class C and Class A networks. The
concerted efforts were focused on the communication between real-time, critical controls
and comfort and convenience controls. Many different protocols were developed during
this time, but it was CAN that became the predominant protocol for automotive
applications. In 1992, CAN was first applied in Mercedes S-class cars for communication
between engine controller, gear box controller, dashboard, and distributed air
Today CAN has been designed into and applied to many areas related to
automotive and industrial controls, such as airplanes, trains, passenger cars, trucks, buses,
14
textile machinery, medical systems, and robots to name a few [13-19]. While the use of
CAN in the automotive industry is already established, CAN is likely poised to make
another major impact on the automotive market with the growing interest in electric and
hybrid vehicles [20]. CAN is an ideal network for communications in an electric vehicle.
However, with the creation of electric vehicles and drive-by-wire applications, there are
demands for CAN updates designed for safety-critical, real-time automotive systems [21-
22].
devices in addition to stock vehicle electronics. Many law enforcement agencies utilize
mobile PCs and other computing devices to retrieve driving records, arrest records, and
other sources of information to aid the officer. However, these computers usually operate
independently from the networks inside the vehicle, thereby missing an opportunity to
increase the functionality of the vehicle. The problem is further compounded by the fact
that different law enforcement agencies use a variety of vehicle makes and model years,
vehicle networks will facilitate future CAN enabled devices to simply plug and play into
an existing CAN network within an automobile equipped with a CAN bus. Furthermore,
simply replacing point-to-point wiring with a robust serial network could reduce
installation costs, minimize wiring weight, and still provide greater functionality.
The objective of the research work presented in this thesis is to develop a CAN
network within a model law enforcement vehicle that allows multiple applications, such
as the pursuit light package, to interface with the network, while adding functionality and
15
reducing wiring. The selected network must demonstrate the ability to decrease the
of the application, and simultaneously offer increased functionality to the entire vehicle
environment.
network for controlling law enforcement applications. The results of the research work
presented show a reduction of point-to-point wiring in the NJSP troop car, while adding
increased functionality to the entire environment and demonstrating the advantages that
can be gained from the inclusion of reconfigurable logic elements. The proposed network
has been evaluated with the use of field programmable gate arrays (FPGA) to
demonstrate the ability to control an application such as the NJSP pursuit light package.
Chapter 2. The approach proposed for implementing CAN as the in-vehicle network is
presented in Chapter 3. Results from the implementation are then given in Chapter 4.
16
Chapter 2: Background
While there are a large variety of protocols suited for automotive implementation,
in general, the protocols share many fundamental principles. The following discussion
highlights the fundamentals of data communications that provide the basis for
In its simplest form, data communication takes place between two devices that are
devices using a shared communications network. This concept is illustrated in Figure 2.1.
......
(a)
(b) ______
Figure 2.1 - Example of data transfer using (a) point-to-point transmission medium and
device interconnections using a (b) bus-organized communications network.
17
2.1 Data Communications Reference Model
There are two key concepts that need to be realized for devices in a network to
communicate between each other properly: protocol and protocol architecture. In general,
a device or node in a network has the capability of both sending and receiving
information. Therefore, it follows that for two such devices to communicate properly
between each other they need to "speak the same language." To ensure this, there are
conventions that must be met by all devices. The conventions, known as a protocol,
1) Syntax
2) Semantics
3) Timing
These three elements govern such things as data format, error handling, sequencing,
control of the appropriate data to be transmitted, the correct form for data transmission,
The primary reference for most protocol specifications is the open systems
(ISO). According to the OSI model, data communication systems can be described as a
1) Application
2) Presentation
3) Session
4) Transport
5) Network
18
6) Data Link
7) Physical
Figure 2.2 illustrates the seven layers of the OSI model and provides a brief definition of
Application
Provides access to the OSI environment for users and provides distributed information services.
Presentation
Provides independence to the application processes from differences in data representation.
Session
Provides the control structure for communication between applications; establishes, manages, and
terminates connections between cooperating applications.
Transport
Provides reliable, transparent transfer of data between end points; provides end-to-end error
recovery and flow control.
Network
Provides upper layers with independence from the data transmission and switching technologies
used to connect systems; responsible for establishing, maintaining, and terminating connections.
Data Link
Provides for the reliable transfer of information across the physical link; sends blocks of data with-
the necessary synchronization, error control, and flow control.
Physical
Concerned with transmission of the bit stream over physical medium; deals with the mechanical,
electrical, functional, and procedural characteristics to access the physical medium.
However, since the model was intended to provide a framework for any type of
communication system, some systems do not use all the layers. In many cases, only three
layers (physical layer, data link layer, and application layer) are relevant. The CAN
protocol implements most of the lower two layers of the OSI model.
19
2.2 CAN Architecture
specification [8] subdivides CAN into layers according to the ISO/OSI reference model:
1) Data Link
2) Physical
3) Application
The following sections outline the basic concepts of these three layers and further discuss
In general, the data link layer has two main functions. The first function is
construction of data frames, which also control error detection code as well as containing
data. The transmitted control information enables the receiver to determine if a frame is
relevant and whether or not there has been an error in transmission. If a frame has been
The second main function controls bus arbitration to resolve conflicts when
multiple nodes access the bus simultaneously. The following sections describe the
principle of bus arbitration, the format of a CAN frame, error detection, and error
confinement.
access with collision avoidance (CSMA/CA). Although several nodes may start
20
2.) To manage the increasing number of electronic components.
components.
To reach these goals numerous networks were introduced with the same driving
motivation but inevitably led to networks with varying characteristics, thus reinforcing
the idea that there is no single network protocol that is available to connect the variety of
the specific application, availability of parts, and economics, as well as meeting the
requirements discussed in the previous section. Table 1.3 demonstrates the selection of
networks discussed in the previous sections. Among these networks there is a range of
functional and economic advantages and disadvantages. Networks of high bandwidth are
12
This results in the node with the lower priority retiring from bus arbitration and
becoming a receiver of the transmitted message. Thus, the node with highest priority
gains access to the bus. Once the bus becomes free, the subordinate node(s) engage
For comparison purposes, the popular protocol Ethernet [26] follows the
monitors the channel to see if any other node is currently transmitting. The channel is
considered free when after 96 bit times there are no messages occupying that channel.
However, if the channel is busy when the node wishes to transmit, the node will continue
monitoring the channel until it is free. While the node is transmitting, the node continues
to monitor the channel for collisions. If a collision occurs during transmission, the
transmission is immediately concluded and a jam signal is sent over the channel. The jam
signal ensures that all other nodes will realize that there has been a collision. Following
collision, the transmitting node endures a waiting time before attempting to re-transmit
1) Data frame
2) Remote frame
3) Error frame
4) Overload frame
22
The CAN specification details two different, compatible frame formats. There is a "base
format" with an 11-bit frame identifier, and an "extended format" with a 29-bit frame
identifier.
A CAN data frame (Figure 2.4) is composed of seven different bit fields: start-of-
(EOF). It is important to note that the data field can be of length zero.
The SOF bit is represented by a single dominant bit and marks the beginning of
data frames and remote frames. A node in a CAN network is only allowed to begin
arbitration if the bus is idle or free. All nodes then synchronize to the leading edge of the
The arbitration field consists of the identifier field and the remote transmission
request bit (RTR), which is transmitted as a dominant (0) bit in a data frame. A frame is
identified and its priority defined by the frame identifier. The length of the frame
identifier in the base format is 11 bits, which means that 2048 (211) different messages are
discernible. The extended format can distinguish 512 million (229) messages. These bits
are transmitted in the order ID [10:0], with the most significant bit being ID10.
The CAN specification defines the control field as consisting of six bits. The first
bit, identifier extension bit (IDE), distinguishes between base and extended formats. For
the base format, the IDE bit is transmitted as a dominant level. The second bit, rO, is
reserved for future expansion of the CAN protocol and until defined, is transmitted
dominantly. The last four bits in the control field, referred to as the data length code,
23
r
r
0
00
-0
1:4
U II
1>
U
4-h
-o
D
00
VI
VS
oc0
00
-o
._
I:
U-o
Cd
w^
(S
o- -
CO-
jj
b- wUD
24
determine the number of bytes transmitted in the succeeding data field. For example, a
value of 0 corresponds to 0 data bytes and a value of 8 corresponds to 8 data bytes. The
straight binary coding of the number of data bytes by the data length code is illustrated in
Table 2.1.
Table 2.1 - Coding of the number of data bytes by the data length code from [8].
Upon the completed transmission of the data field, the cyclic redundancy check
(CRC) field enables a receiving node to verify the integrity of the received data. CRC is
one of the most common and powerful, error-detecting codes. In general, CRC is
described as follows [23] and summarized in Fig. 2.5. Given a k-bit block of bits or
(FCS). The resulting frame, which is k + n bits long, is exactly divisible by some
25
CRC polynomial process:
Define:
T(X) = (k + n) - bit frame
M(X) = k - bit message
P(X) = (n + 1) - bit divisor
F(X)= n-bit FCS
Now divide T by P.
T(X) X"M(X)+ R(X)
P(X) P(X)
T(X) QX + R(X) R(X)
P(X) P(X) P(X)
P(X) P(X) P(X)
26
predetermined number. After receiving the message, a receiver will then divide the
received frame by the predetermined number. If there is no remainder after the division,
arithmetic, polynomials and random logic. When the CRC procedure is represented as a
polynomial, all values of the polynomial are expressed as a dummy variable X with
binary coefficients. The coefficients correspond to the bits in the binary message. Thus,
for a binary message M= 10011, the polynomial form is M(X)= XS+X 4 +X'+l.Figure 2.5
According to the CAN protocol the CRC field consists of a 15-bit check sequence
plus a delimiter bit. The FCS is derived from a cyclic redundancy code, based upon the
Bose-Chaudhuri-Hocquenghem (BCH) code [28], which is best suited for frames that
contain less than 127 bits. To perform the CRC calculation, the frame header and the data
defined generator polynomial. The remainder of the modulo-2 division is the CRC
sequence sent over the bus as part of the frame. A receiving node in the network receives
the frame polynomial, which is also divided by the same generator polynomial. When the
frame is transmitted error-free, the calculated check sequence of the receiver is the same
The coefficients of the frame polynomial are formed by every bit value from the
SOF to the final data bit, extended by 15 least significant coefficients of value 0. This
27
This division can be implemented by means of a random logic circuit, more specifically a
The CRC delimiter bit is followed by the 2-bit acknowledgement field, which
node transmits the two acknowledgement bits high and then awaits reception
received, the receiving node acknowledges the transmitter by writing zeros in the
acknowledgement slot.
bits, referred to as the end-of-frame flag (EOF), combined with the recessive
acknowledgement delimiter bit. Together, these eight bits indicate an error free
transmitted frame.
Any node can initiate transmission of a specific frame from another node by
means of the remote frame. The frame requested by a node is specified by the identifier
transmitted with the remote frame. The difference between the remote frame and the data
frame formats is the RTR bit level. RTR is transmitted at logic high for remote frame; for
the data frame, RTR is low. The difference in transmission levels ensures that when a
remote frame arbitrates simultaneously with a data frame with the same identifier, the
data frame wins arbitration. Another difference between a data frame and a remote frame
is the fact that the data field of a remote frame is always empty. Instead of the data length
code determining the number of data bytes in the data field of the remote frame, it
28
2.2.1.2.3 Error Frame
In CAN, any sequence of more than five consecutive bits of equal value within a
frame is considered an error frame. An error frame consists of two fields, error flag, and
error delimiter. The error frame indicates the detection of any error during transmission
The first field contains the superposition of error flags transmitted by one or
several nodes. Error flags can take on two different forms: active or passive error flags.
The active error flag consists of six consecutive logic 0 bits, whereas the passive error
flag consists of six consecutive logic 1 bits. An active error node detecting an error
condition signals the anomaly with an active flag. The flag's form violates the bit-stuffing
rule and consequently all other nodes detect an error condition and begin sending their
own error flag. Therefore, the sequence of logic 0 bits monitored on the bus results from
the superposition of the different error flags from various nodes. This sequence ranges
receiving nodes when the flag starts within a bit stuffed frame. The result is a bit stuff
error. However, if the flag starts during arbitration or less than 6 bits before the end of the
CRC sequence that ends in recessive bits, an error will not be recognized by the receiver.
A receiver attempting to send a passive error flag cannot overcome any current activity
on the bus. As a result, error passive receivers have to wait for six equal bits after
After transmission of an error flag, each node sends recessive bits and monitors
the bus level until it detects a recessive bit. Once a recessive bit is detected, the node then
29
starts transmitting seven more recessive bits. This error delimiter field determines if the
overload frame is composed of an overload flag field and an overload delimiter. The
overload frame is used to request a delay of the next data or remote frame by the receiver
or to signal certain error conditions related to the intermission field. The intermission
field is a 3-bit field of recessive bits, which represents a minimum distance of three bits
between frames.
The overall high requirements for security and integrity of data transmission call
for an average of less than one error for every thousand messages (10+3). To achieve this
2) Frame check
3) CRC check
4) Acknowledgement check
During the monitoring phase, every node that transmits a signal monitors the
corresponding bus level. If the bus level does not match the transmitted level, a bit error
30
is detected. Bit checking thus proves to be very effective in detecting global errors as well
All frames in the CAN protocol contain fixed bit fields, SOF, RTR, IDE, DLC,
EOF, and delimiters. These fixed fields must be consistent with the protocol; a non-
compliant format field generates a format error and the receiver rejects the frame.
CAN implements the principle of cyclic redundancy checking (CRC) (see section
2.2.1.2.2) to enable a receiving node to verify the integrity of a received frame. If the
CRC sequence received from the transmitting node is not identical to the CRC sequence
The final error handling mechanism reinforces detected errors. Every node that
detects the violation of the bit-stuffing rule or simply detects an error flag automatically
produces an error frame. The resulting error frame generated for any of the above errors
will be sent to all nodes; upon reception all nodes will discard the received bits of the
corrupted message. Once informed of the error, the transmitter of the corrupted message
will retransmit the original message when the bus becomes free.
Although these mechanisms provide for a high degree of error detection, there are
still possibilities for some errors to go undetected. The probability for errors to be
undetectable at receiving nodes according to the CAN specification is less than 4.7 10' 14.
This means that on average, about one corrupted message is expected to go undetected
31
for every 20 trillion transmitted messages. However, further analysis shows that the
residual error for a 10-node network reaches a maximum value of 7.2 10'12 [29].
The physical layer of the CAN protocol defines how the frames or signals defined
by the data link layer are actually transmitted. The physical layer defines bit
representation, bit timing, bit coding and decoding, and the specification of the physical
medium. The common CAN physical layer concept is specified by the ISO 11898
The bit time, tb, in CAN is the time duration of one bit, which is the reciprocal of
the nominal bit rate. This bit time is partitioned into four segments (Fig. 2.6):
synchronization segment, propagation delay segment, phase buffer segment 1, and phase
buffer segment 2. Each segment consists of a specific amount of time quantum (tq). Time
quantum is defined as the product of the value of the baud rate prescaler (BRP), which
can include integer values from 1 to 32, and the reciprocal of the system clock (fys):
32
BRP
t -(2.1)
B
fsys
The synchronization segment is the component of the bit time where signal edges
occur and is used to synchronize nodes on the CAN bus. Physical delay times within the
network are resolved by the propagation delay segment. Since a CAN node is required to
receive data from another node during its own transmission, any node synchronized to the
bus will be out of phase with the transmitting node of the bit stream seen on the bus.
Therefore, the propagation delay segment must be twice as long as the sum of the
Phase buffer segment 1 and 2 are used to compensate for edge phase errors,
segment and the synchronization segment. The buffer segments are separated by the
sample point, which is the time that the bus level is read and interpreted as a bit value.
Depending on the edge phase error, the phase buffer segments can be altered to
effectively change the sample point and thus restore the sampling point relative to the
current bit position. For example, if the transmitter is operating at a lower clock speed
than the receiver, the signal edge will arrive after the synchronization segment. The
receiver compensates by shifting the sampling point. This is done by extending the phase
buffer segment 1 by an amount equal to the error, as illustrated in Figure 2.7. The same
approach applies to the condition where the transmitter is faster than the receiver.
33
Received Bit
Value
Partition before
resynchronization
Partition after Sync Propagation Delay Seg. Phase Buffer Segment Phase Seg.
resynchronization
The alterations made to the phase buffer segments are only temporary. At the next
bit time, the segments will return to their original values as long as there are no phase
In the CAN protocol, thet bits of a frame are physically represented according to
the Non-Return-to-Zero (NRZ) code (Figure 2.8). Since a transmission medium normally
cannot accept transmitted symbols in their "natural" form; symbols are transmitted using
Bit Time
34
line codes instead. Symbols in CAN are transmitted using the baseband line coding
technique NRZ [33]. NRZ defines two bit levels, dominant (0) and recessive (1). The
generated bit level remains constant over the entire bit time interval. This can result in a
relatively long stream of data bits of equal value and thus there may not be any edges
available for synchronization. Furthermore, recall that more than five consecutive bits of
synchronization issues and error frame conflicts, CAN implements a bit stream coding
bit) of complementary value after a prescribed amount of consecutive bits (stuff width) of
the same level (Figure 2.9). On the receiver side, the procedure is reversed and the
inserted bits are removed. CAN uses a stuff width of five bits; this ensures that a
sequence of six bits of equal polarity is a violation of the bit stuffing rule and will be
Before
Stuffing
After
Stuffing
After
De-
Stuffing
Figure 2.9 - Principle of bit stuffing with a stuff width of 5 bits, where S is the stuff bit.
35
interpreted as an error frame. Bit stuffing allows receiving nodes to synchronize on
Bit stuffing can increase the actual number of transmitted bits beyond the size of
the original message, which can result in a transmission delay. Before bit stuffing, the
total number of bits in a data frame including a three-bit intermission field is:
where n is the number of data bytes in the data field. The CRC delimiter,
acknowledgement field, and end of frame are not coded using the bit stuff method, which
leaves 34 of the 47 control bits subject to bit stuffing. In a worst-case scenario, the total
This model can be used to determine how long it will take to send a frame. Let T equal
the amount of time it takes to transmit a bit over a bus operating at its full 1 Mb/s
potential. For this case, T = ltts. Therefore, the worst-case transmission time, C,, of a
frame is:
For a data field consisting of eight data bytes, n = 8, then C = 135#ts. In other words, for
the largest frame it takes 135 bit times to complete the transmission.
implement CAN networks. The advantage of using the two-wire differential approach is
36
that it is robust to certain faults. If the wires are twisted, it is resistant to electromagnetic
interference. Good transmission line practice means that the bus lines must also be
terminated at each end by a matched resistor to minimize signal reflections. If the lines
are not terminated properly, the signal lines may also become disturbed by induced radio
frequency signals.
Node Node
1 2
7T I]
~~~~~Nd
Bus Bus
Termination Termination
Z=-C (2.5)
Where L and C represent the inductance and capacitance per unit length of the electric
line. The typical line impedance of a two-wire line is about 120 l. To avoid signal
reflections it follows that the two-wire line be terminated at each end with a resistor
equivalent to the characteristic impedance, ZL. The signal reflection coefficient F is given
by:
37
r= Z L- Z (2.6)
ZL + Zo
When the load impedance is equal to the characteristic impedance, there is no signal
reflection.
C
p
(2.7)
Pr= r
With c equal to the velocity of light (3 · 108 m/s), / denoting relative permeability, and £r
denoting relative permittivity. It can also be shown that the propagation velocity can be
1
vp L (2.8)
VLC
Now it can easily be seen that as the inductance and capacitance of the medium increase
the propagation velocity decreases. As a result, to obtain low propagation times for a
given bit rate, the required signal velocity must be large. However, the tradeoff is that the
total propagation delay is dependent upon the bus length and the signal propagation
tprop = v p* (2.9)
Therefore it is essential to keep the overall bus length to a minimum to achieve low
propagation delays.
Propagation times are not the only limiting factor for bus length. With longer bus
lengths, the impact of voltage drop over the bus cannot be ignored. The maximum
voltage drop over the bus must be such that the any receiving node in the network can
38
reliably interpret the signal level of the most remote sending node operating at a
minimum signal level. To determine the maximum bus length, the distributed line
R,
RR<<V)
Riot IIR. -(VI-~ ) (2.10)
(2.10)
2.V,
The model demonstrates node 1 supplying a voltage level (Vl) at one end of the bus line
and reception of a voltage level (Vn) by a receiving node, n, at the opposite end of the
bus. The termination resistors are labeled RI and Ritot is the input resistance of the
receiver.
Rx
Rt
Once the maximum line resistance is known, the maximum bus length can be
determined considering the conductor resistance, p, per unit length and the cross section
of the wire.
R-*A
L= (2.11)
P
Typical wire gauges used in CAN are usually in the range of 18-20 AWG, which
have a respective resistance per unit length in the range of about 6-10 ohms per 1000 feet.
39
For simplicity, keeping the length of the bus to a minimum will eliminate many issues
The CAN standard only specifies layers 1 and 2 of the OSI model. Higher layers,
such as the application layer (layer 7), are typically implemented in software. Leaving
not provided by the physical layer is usually required to conduct network management,
node supervision functions, and other functions. This can be done using standardized
higher layer protocols to bridge the gap between the application and the CAN standard.
Because of the widespread usage of CAN and the varying objectives of each
implementation, several higher layer protocols have been standardized. The three
2) CANopen
3) DeviceNet
The development of these higher layer protocols has led to interoperability of devices
Full details of the specifications for the higher layer protocols are beyond the
scope of this treatment; the interested reader is referred to pertinent references [35-37]. In
brief, the functionality provided by all three protocols allows for improved
master node or by some external configuration tool. The parameters set by the
configuration govern the communication behavior of a device. This allocates which data
40
is stuffed where, into which message or address, and when the message is triggered.
Essentially, the higher layer protocols support the intelligence of a given application.
In this chapter, an overview of the CAN protocol has been presented. The
protocol was described in terms of the ISO/OSI layers 1 and 2 and establishes the CAN
message structure, bus arbitration process, error detection, and timing specifications.
With an understanding of the protocol, the following chapter describes the approach for
41
Chapter 3: Approach
components or by creating the node via a hardware description language. For the
purposes of this thesis, the CAN node will be constructed using a hybrid of hardware
components. The provided HDL for the CAN controller is modeled after the Philips
VHDL, an acronym for Very High Speed Integrated Circuit (VHSIC) Hardware
Description Language, and Verilog are two common types of hardware description
language that can be used to model a digital system from the algorithmic level to the gate
level [40]. These languages allow a system to be described hierarchically; that is, the
The HDL design process [41] is illustrated in Figure 3.1. The design process
begins with a specification phase where the functional requirements of the system are
combinations and interactions among simpler components. Once the requirements are
established, individual modules are defined on a system level and their behaviors
modeled. Hardware design flow then describes functions at the logic level and upon
synthesis; Boolean functions of the system are derived and mapped to elements of an
(FPGA).
42
Specification
System Design
Validation
Logic Design
Validation
Circuit Design
Validation
Layout
Validation
The use of an HDL to create a CAN node allows for the integration of the CAN
controller subsystem along with other logic elements in an FPGA. For example, a
reconfigurability. In addition, HDL also provides an easy means for transferring the
illustrated in Figure 3.2. At the lowest level, the CAN bus lines, CAN high and CAN low,
are connected to the module via a CAN transceiver. The transceiver controls both the
transmission and reception of messages to and from the CAN bus. The next level up from
43
the transceiver is a CAN controller. The CAN protocol, which was discussed in Chapter
microcontroller that handles the overall application. Finally, the highest level is the
application level, which can have a variety of inputs and outputs. For this work, the
intended platform for the CAN network is the NJSP pursuit light package. The
application aspect of this platform would be the "intelligence" of the respective functions
of the light package; for instance, turning on the front strobe lights for 30 seconds and
'lIx I Rx
CAN
Transceiver
CAN bus
The approach chosen focuses on the CAN controller design with interfaces to
integrate with an external transceiver and microcontroller. The first course of action for
the HDL design process is to define all the requirements of the CAN controller. It is
44
assumed that the CAN controller will implement all tasks related to layers 1 and 2 of the
CAN specification that were partially described in Chapter 2. Among the tasks included
5.) Synchronization
Overall, the CAN controller should perform all the protocol related functions in a
separate entity apart from a host microcontroller to relieve the microcontroller of time-
critical tasks.
illustrated in Figure 3.3. Each task can therefore be equivalently modeled with a
functional block in the CAN controller. The advantage of designing the system as a
collection of fundamental blocks relieves the designer from the burden of attempting to
create the system in one step. Instead, each block is built individually and then combined
after all blocks have been completed. Furthermore, in keeping with sound software
engineering principles, each block can be validated independently eliminating the need to
transition from design concept to functional code. If the CAN controller core was being
created from scratch, the following sections provide the requirements needed to make the
45
transition to HDL. However, since the CAN core has already been established the
CAN controller
Clock
Reset
I
T
I
Rx
-Rx Register
/WR
-+
Tx itrol
*,
/L - Tx Register DR
XCVR ta
il111 gUClU ,LU1
Ei.
gen./cnellK
The first block that a bus message encounters is the bit timing logic (BTL). This
block continually monitors the bus line input and handles the related bit timing according
to the CAN protocol. When the BTL detects a transition from recessive to dominant, it
synchronizes on that edge and reception of a new frame can begin. However, as was
discussed in Chapter 2, there are propagation delays and phase shifts that affect the
sampling point.
The BTL provides programmable time segments to compensate for delays. Bit
timing configuration is programmed in two separate registers. Referring to Figure 2.6, the
46
sum of the propagation delay segment and phase buffer segment 1 (time segment 1) is
combined with phase buffer segment 2 (time segment 2) in one register. In another
register, the synchronization jump width (SJW), which defines how far a
resynchronization may move the sample point, and BRP are combined. The
II II II
levels. The purpose is to control the distance between the edges and the sample points.
Edges are detected by sampling the bus level in each time quantum and comparing it with
the bus level at the previous sample point. Depending on the location of the edge relative
to the synchronization segment, the phase error, e, will be negative, positive, or zero:
47
There are two types of synchronization that can occur; hard synchronization, and
frame, only resynchronization can occur. After a hard synchronization, the bit time is
restarted with the end of the synchronization segment. The hard synchronization forces
the edge that caused the hard synchronization to be positioned within the synchronization
Bit resynchronization involves shortening or lengthening the bit time such that the
position of the sample point is shifted relative to the edge. When the phase error is
positive, phase buffer segment 1 is expanded (Figure 2.7). If the magnitude of the error is
less than SJW, phase buffer segment 1 is lengthened by the amount of the error; else, it is
lengthened by SJW. Conversely, if the phase error is negative, phase buffer segment 2 is
shortened. If the magnitude of the error is less than SJW, phase buffer segment 2 is
1) Only one synchronization is allowed between sample points within one bit
time.
2) An edge will only be used for synchronization if the value detected at the
previous sample point differs from the signal level immediately after the
signal edge.
These features of the bit timing logic make it possible to place the sample point at the
right position inside of a bit depending on the quality of the signal received from the bus.
48
3.2.2 Transmit and Receive Registers
The transmit and receive registers serialize messages to be sent and parallelize
messages that are received. These registers hold the data stream bits to allow access to the
data by the bit stream processor (BSP), which controls the loading and shifting of the
registers.
The bit stream processor (BSP) performs several protocol tasks. The BSP
translates messages into frames and frames into messages. Messages are stuffed and
destuffed within the BSP and undergo CRC checking while in the BSP. The BSP is also
responsible for performing the process of bus arbitration (Figure 3.4) according to the
reception of
:vel
dominant
Bus level equals
transmitted level
ii
A i .. .. . ., U..t. l AU
ldllollH-L ^V^LUSVC -
All arbitration bits monitored
transmitted
49
protocol. Recall that the protocol describes bus arbitration in a non-destructive, bit-wise
process (section 2.2.1.1). While the bus is idle, the bus is in the recessive state.
Arbitration is initiated with a change from recessive to dominant via the start of frame bit.
Every successive bit transmitted is compared with the bus level as illustrated in the state
diagram in Figure 3.4. If the transmitted level matches the bus level, then the next bit is
transmitted. If the bus level does not match the transmitted level, then the node either
becomes a receiver or there is an error. Once a node has completed the arbitration
process, the rest of the message (including control and data fields) is transmitted.
Depending on the CAN format (standard or extended), the indexing of the data
frame is known. Therefore, as the received message is accepted its corresponding frames
can be determined based on the indexing. For example, the data frame illustrated in
Figure 2.4 shows that the arbitration field consists of the first 12 bits. The first bit in that
field is the SOF and the next 11 bits describe the identifier field. With this format defined
the frame generator simply reads the message stream and upon reception of the SOF
begins to index the message. Therefore, the first bit is stored in a register labeled SOF
and the next 11 bits in a register labeled identifier. The same holds true for the remaining
The method of bit stuffing is applied to the following frame segments: SOF,
arbitration field, control field, and CRC sequence. When transmitting a message stream,
the transmitter inserts a complementary bit after detecting five consecutive bits of
50
identical value. When receiving a message, the stuff bits are removed and the message is
One component of the BSP generates the CRC code to be transmitted within the
data frame, as well as checking the CRC code of incoming messages as described in
demonstrated with pseudo code [8] in Figure 3.5 exhibits how the CRC sequence would
be developed using an HDL. The resulting CRC sequence is stored in the 15-bit register
Repeat
If CRCNEXT then
End
51
3.2.3.4 Message Filtering
broadcasting. All messages transmitted on the bus are accessible to all nodes and received
by the controller. However, each node is only interested in those transmitted messages
intended for it. For this reason, each CAN node implements a message filtering
mechanism in the protocol controller to ensure that the host microcontroller is only
acceptance mask (Figure 3.6). The acceptance mask register contains the eight most
significant bits of the message identifier ID [10:3], which a message must have to pass
the filter. The bits of the acceptance mask can be set to 0, 1, or don't care (X). If a bit in
the mask is set to 0, then the mask will only allow identifier bits of value 0, and if the
mask value is set to 1 then the mask will only allow bit values of 1 to filter through. If the
Received
Identifier
I I I I I 11
Acceptance
Mask
0 0 0 0 0 0 0 0 0 0 0
Filtered 0 0 0 0 0 0 0 0 0 0 1
Messages 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1 1 1
52
mask is set to don't care, then bit values of either 1 or 0 can filter through. Figure 3.6
setting all mask bits to 0, except for ID 3, which is set to a don't care state.
code and acceptance mask, contain ID [10:3]. The acceptance code register contains the
bits of the message identifier, which a message must have. The acceptance mask register
then determines which of those bits are relevant for filtering. Therefore, if both registers
are set to 0, then the range of filtered messages have identifier values from 0 to 7. If both
registers are completely set to don't care, then all messages with identifiers from 0 to
Once the messages are filtered, they can then be sent to the host microcontroller.
The host microcontroller will subsequently interpret the messages and perform the tasks
The message buffer is composed of a transmit and receive buffer. The transmit
buffer is an interface between the host microcontroller and the BSP that is able to store a
complete message that will be transmitted over the network. To fill the buffer the host
microcontroller writes to the buffer the frame information and then the BSP reads the
Conversely the receive buffer stores received messages that have passed through
the message filtering. The buffer represents a FIFO, first in first out, that allows the host
microcontroller to process one message at a time. Once a message is read from the FIFO,
53
The layout of the receive and transmit buffers are detailed in the product
specification for the Philips SJA1000 stand-alone controller [39]. The address allocation
Table 3.2 - Transmit and receive buffer layout of the CAN controller [39].
The BSP continually monitors the data stream for errors as it processes the data
stream. The BSP looks for the five different error mechanisms discussed in section
2.2.1.3. If the BSP detects a violation in any one of the mechanisms, the result is an error
and the BSP notifies the error management logic (EML). In turn, the EML increments
either a receive error counter (REC) or a transmit error counter (TEC) depending upon
54
whether the error occurred during transmission or reception. According to the CAN
protocol, the values of the error counter determines if the controller is an error-active,
The different error states make it possible to implement error confinement, which
ensures that a faulty node will not block communications along the bus. A node in an
error-active state takes part in normal communications and sends an active error flag
when errors are detected. An error-passive node having a relatively high error count can
still take part in communications, but can only send passive error flags if further errors
are detected. The passive error flag does not disturb the remaining bus communications.
Finally, a node in the bus-off state is not allowed to have any communications on the bus.
Reset
I
I TEC >255
either one of the error counters exceeds 127, the node then enters the error-passive state.
55
A node can return to the error-active state if both counters are below 128. Once the
transmit error counter exceeds 255 the node is disconnected from the bus. From this state,
a node can return to the error-active state only when the counters are reset to zero after
the node has detected 128 sequences of 11 consecutive bits of recessive value.
The error counters are incremented and decremented by the following set of rules
1) When a receiver detects an error, the REC is increased by one, except when
the detected error is a bit error during the transmission of an active-error flag
or an overload flag.
2) When a receiver detects a dominant bit as the first bit after sending an error
error, but does not detect a dominant bit while sending its passive error
arbitration, when the stuff bit should have been recessive and was
remains unchanged.
56
6) Any node endures up to seven consecutive dominant bits after transmitting an
between 1 and 127. If the count is zero, it remains unchanged; if it was greater
The rules for fault confinement state that the TEC of a node is increased by eight when a
node detects an error during transmission and is only decreased by one for a successful
transmission. As a result, a CAN network remains operational when less than every 16
frames is flawed.
The CAN protocol and the HDL implementation of the protocol are established
entities. Synthesizing the CAN model in a Field Programmable Gate Array (FPGA) [42]
has also been explored since the inception of CAN. Utilizing CAN as an in-vehicle
network for various automobiles has been achieved and has been discussed in Chapter 1.
However, an aspect that has never been investigated nor fully exploited is the
vehicles. Section 1.4 discussed potential opportunities to apply the knowledge of CAN to
57
a new platform, in particular the pursuit light package of the NJSP Troop car (Figures 1.1
and 1.2).
To demonstrate the ability of CAN to control the NJSP light package, the CAN
controller core needs to be synthesized into a FPGA. An FPGA offers configurable logic
blocks to allow a designer's program to define the function of the FPGA. This results in a
custom logic circuit that can be reconfigured. As mentioned earlier, the automotive
reprogramming the part so that it isn't necessary to supply a new FPGA each time a
modification is made.
Modeling the control of the NJSP light package can be demonstrated without
direct installation of the CAN network into the vehicle. Instead, a proof-of-concept
approach will be explored. To accomplish this goal, the synthesized FPGA with CAN is
evaluated in a lab environment. The assumption that is made is that if the FPGA design
can demonstrate the ability to control a modified light package, it is suitable to control the
The application controls when a given light or group of lights in the package are
to be turned on, and for how long they are to remain on before being turned off. Once
turned off, another light or set of lights can be activated, and the process continues to
create a desired light pattern. For example, in a pursuit condition, the pattern can consist
of the front strobes activated for 10 seconds, followed by the rear strobes for 10 seconds,
58
Commanding a light pattern is initiated by the user using a switch interface. For
this approach, the activation of a switch generates a CAN message. Embedded in that
message are two key components, the intended address, and the data to be transferred to
that address. In the complete light package, each of the 14 lights could be individually
a CAN controlled light can vary, but in the simplest form, a logic level 1 would turn on a
a switch to turn on and off the current through a load. Using the convention that a binary
0 V, the following circuit (Figure 3.8) is an example of using digital signals to use a
Vcc
. Rb . .
vigrn .(-I
transistor as a switch. If Vin is high, then the transistor is driven to saturation and the LED
is in the "on" state. However, if Vi, is low then the LED is in the "off" state.
59
3.3.1.1 CAN Data Frame Coding
The proposed approach begins with defining the message that will be transmitted
when the switch is activated. To begin with, the message that will be generated will be a
standard CAN frame, which has an 11-bit identifier. Since this is a modified and limited
representation of the light package, the choice of address to assign the FPGA is arbitrary.
The data field can be set using the format explained earlier. Logic level 1 will be
interpreted as "on" and logic level 0 will equate to "off." This is a simple format and
ideal for controlling a single light, but with one data byte, or eight bits, then 8 lights can
be controlled using this format. This is accomplished by defining each bit position as a
light and assigning either a one or zero to each position as shown in Table 3.3.
The bit stream produced by the coding scheme is then processed by the host
microcontroller; if a logic level 1 appears in any of the bit positions, the microcontroller
will supply a transistor switch for the appropriate light or lights with a "high" voltage
condition and place the circuit in the "on" state. Conversely, a logic level 0 would place
Before any lights are activated the complete data frame that is to be transmitted
over the CAN bus must be defined. To start, the SOF bit (logic 0) marks the beginning of
the data frame. The 12-bit arbitration field follows the SOF. The 11 most significant bits
in this field represent the identifier ID [10:0], or the address. The choice of address is
arbitrary since this is a limited network representation, but for simulation purposes the
identifier will be set to 741h. The RTR bit is also contained in the arbitration field, and
60
Table 3.3 - Data coding using single bit position per light.
7<
Xt .it6 "Bij5 B'Ji Bit-3 Kbit2 XB..0°
Bit 1 Li.hStab
X X X X X X X 0 Light 1 off
X X X X X X X 1 Light 1 on
X X X X X X 0 X Light 2 off
X X X X X X 1 X Light 2 on
X X X X X 0 X X Light 3 off
X X X X X 1 X X Light 3 on
X X X X 0 X X X Light 4 off
X X X X 1 X X X Light 4 on
X X X 0 X X X X Light 5 off
X X X 1 X X X X Light 5 on
X X 0 X X X X X Light 6 off
X X 1 X X X X X Light 6 on
X 0 X X X X X X Light 7 off
X 1 X X X X X X Light 7 on
0 X X X X X X X Light 8 off
1 X X X X X X X Light 8 on
The control field consists of an additional six bits. The first bit in this field, IDE,
indicates what CAN format is being applied. For standard format, the IDE is transmitted
as 0 along with the rO bit, which is reserved for future expansion. The last four bits of this
field is the data length code which determines the number of data bytes in the succeeding
data field. For one byte of data, the DLC code is 0001 (Table 2.1).
Following the control field is the data field. In this field is the 1 byte of data that
will be coded as demonstrated in Table 3.3. The value of this data byte will be set to 12h.
The 16-bit CRC field is then calculated from the bit stream starting from the SOF to the
final data bit and is appended to the frame after the data field. The CRC computation uses
the method demonstrated in Figure 3.5 and for the values that have been defined is
calculated to be 30BBh. The CRC sequence is followed by the CRC delimiter bit and is
transmitted high. The acknowledgement field follows the CRC delimiter and consists of
61
two bits transmitted high. The data frame is completed with a sequence of seven
recessive bits and the recessive acknowledgement delimiter bit. Table 3.4 illustrates the
Start of Frame 0
Arbitration Field 111010000010
Control Field 000001
Data Field 00010010
CRC Field 011000010111011
Acknowledgement Field 11
End of Frame 11111111
Once constructed, the data frame needs to undergo the bit-stuffing process before
it can be placed onto the bus. In a data frame, the frame segments SOF, arbitration field,
control field, data field, and CRC sequence undergo bit stuffing (Figure 3.9). With a stuff
width of five, a complementary bit is inserted in the bit stream after five consecutive bits
Before
Stuffing - 011101000001000000100010010011000010111011
Stuffing
Stuffed
Bits
After - 01110100000110000010100010010011000010111011
Stuffing
Figure 3.9 - Bit-stuffing of a sample CAN frame from SOF to end of CRC sequence.
62
of identical value. Figure 3.9 illustrates bit stuffing using the values of the bit fields in
Table 3.4. Notice that in this example the frame is extended by two bits.
Combining the synthesized core, switch interface, and light control into a
complete CAN node is accomplished with the use of the Digilent Digilab2 FPGA
development board, which is based on the Xilinx XC2S200 FPGA [43], host
microcontroller, and a CAN transceiver. The application diagram for the CAN node is
detailed in Figure 3.10. Typically, a CAN controller such as the SJA1000 is interfaced
with an 8-bit microcontroller of the 8051 family. However, the interface occurs via a
multiplexed 8-bit data/ address bus and thus the SJA1000 is compatible with all common
PCA82C250
PA0 TXD CANH
PAl
PA2 RXD CANL
PA3
PA4
PA5
PA6 CAN bus
PA7
PCO
PC2
PEO
PE1
VCC
GND
63
8-bit microcontrollers. The basic I/O provided by the microcontroller allows for the
construction of the CAN message via an on board push button. Depressing the push
button will generate a CAN message in accordance to how it has been defined in Table
3.4. After the message is generated, it is sent to the FPGA, which contains the
synthesized CAN controller core. The transmitting FPGA sends the message to a
receiving CAN core via an attached CAN transceiver, the Philips PCA82C251
transceiver [44]. The receiving CAN node filters the message and passes along the data
microcontroller and the corresponding lights are turned on or off. An overall block
Qrll^t^
3 W IL,11
CAN Bus
..0..I. :g:.::-.,.:S'SSES~ r. +---------------
^ ____ _ _~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
T i a1-it I
LA-4igLs
I
0000,g
.4-~
Figure 3.11 - Process flow of FPGA light control approach. The user depresses the
switch to activate a light pattern. A CAN message is generated and sent to the receiving
FPGA, which filters the message. The microcontroller reads the data field and controls
the lights accordingly.
The link between the user interface, the push button or other switch interface, and
the CAN message generated by the process of activating the switch interface is the host
microcontroller. Since the CAN controller core that is being used in this thesis is a stand-
64
alone controller that only implements the CAN protocol, there is a demand for another
controller that manages the functionality that goes beyond the basic protocol processing.
In all CAN controllers there are a series of registers and message buffers that need to be
configured and monitored. These tasks plus management of reading received messages
and supplying messages to be transmitted are performed by the host microcontroller. The
microcontroller used in this thesis is the Rabbit 2000 8-bit microprocessor [45].
data/address bus (AD [7:0]) and additional read/write control signals (ALE,RD, WR, and
CS) as shown in Figure 3.10. Before any communication can be initiated the controller
must go through a series of initialization actions that configure the device for
communications (Figure 3.12). Upon power-on or after a reset, the microcontroller uses
these control signals to perform an initialization that configures the CAN controller for
CAN communication. The initialization process begins with a reset pulse, low logic
applied to the reset pin, enabling the CAN controller to enter the reset mode. With the
reset mode enabled the microcontroller can then configure the necessary registers. The
configuration of these registers defines such aspects as the acceptance code and
acceptance mask, bit rate, sampling point, and other important properties necessary for
CAN communication.
After configuration the CAN controller can then be started. With the start of the
CAN controller, the controller can begin to receive messages (see Figure 3.12) off the
CAN bus and also transmit messages (see Figure 3.13). The flow diagram of message
reception is demonstrated in Figure 3.12. If a message is waiting in the receive buffer the
host microcontroller is notified and is able to read the message. Once the message is read
65
Empty
IF Message(s) present
I
Figure 3.12 - Flow diagram of message reception [46].
No
I YVC
66
the host microcontroller sends a command to the CAN controller to release the receive
buffer and the process of reading incoming messages continues. To transmit a message
(see Figure 3.13), the message is copied from the host microcontroller to the transmit
buffer contained in the CAN controller. The microcontroller initiates a transmit request
command and the message stored in the transmit buffer is sent. The overall general
program flow of the microcontroller tasks are described by Figure 3.14. Message
transmission and message reception are covered under the main process of the
application.
wl
67
For this thesis work the C-language is used to describe the program flow of the
SJA1000. The target controller is the Rabbit 2000 8-bit microprocessor. The program
controller and controls when the CAN controller is to send and receive messages to and
approach for designing and implementing the CAN protocol in a FPGA to control a light
application related to a law enforcement platform has been detailed. This approach is a
proof of concept methodology aimed at demonstrating the ability of a serial network such
as CAN to control a complete light package typical of the NJSP troop car. The following
chapter presents the compilation of results obtained by applying the methods proposed in
this thesis.
68
Chapter 4: Results
OPENCORES.org provided the HDL code for the CAN protocol controller,
which again is modeled after the Philips SJA1000 stand-alone CAN controller [39]. This
code contained the various subsystems of the controller core as described in Chapter 3.
This code was developed under the agreement that to implement the CAN IP core on
silicon a CAN protocol license must be obtained. However, there are no legal restrictions
The code was then synthesized into a Xilinx XC2S200 FPGA [43] using Xilinx
software tools and interfaced with the Rabbit RCM2000 microprocessor [45] and the
Philips PCA82C251 transceiver [44]. Validating that the synthesized FPGA exhibits the
functioning of a CAN communication node was achieved by the approach detailed in the
preceding chapter. The proceeding sections present the results of this process.
4.1 Synthesis
process of translating HDL to a gate level netlist and the code is optimized for the target
device. The first step of synthesis is to compile the HDL source code and check it for
possible syntax errors. If syntax errors do occur they are corrected and the code is
recompiled. Although it may seem like a direct step there are some constructs that are not
easily accepted by the synthesis tool, Xilinx Foundation Series 4.2i. Some of the common
restrictions that occurred when compiling the code were: initialization of signals and
variables, restricted used of wait statement, and assigning delays. After a successful
69
4.2 Simulation
Once synthesis was completed the CAN controller core was simulated. To
simulate the design the CAN controller was subjected to a testbench. A testbench is a top-
level hierarchical model that instantiates a desired model, the device under test, and
drives that model with a set of test vectors and generates the responses of the model. The
h..
PI
For the purposes of this thesis the testbench instantiated the CAN controller core
and then proceeded to construct a CAN message with 1 data byte. The messages were
then transmitted and the CAN controller received these messages and the messages were
then stored to the receive FIFO discussed in section 3.2.4. Using the ModelSim design
Unlike the setup demonstrated in Figure 3.10, where one CAN node
communicates with another CAN node, the configuration modeled under simulation is a
self reception mode. In this configuration, as shown in Figure 4.2, an individual CAN
node transmits a message with the receive line connected to transmit line to allow self
reception of the transmitted message. This simulation will test the operation of the CAN
70
qxvitolll
OWIL UIII
Lights
Figure 4.2 - Process flow of simulated FPGA light control approach using self reception
mode. A message is generated and transmitted by the CAN node and the message is
looped directly back to the receive line.
To begin with, the testbench sent a string of 11 recessive bits, logic level 1,
indicating that the bus was idle. After this sequence the bus is available again and then a
transmission request was initiated. To transmit a message, the transmission request bit is
set in the command register. This is accomplished by writing this bit high into the
SJA1000 datasheet [39]. Using the display command within the testbench, this process
was able to be monitored and a portion of the transcript is shown in Figure 4.3.
After transmission is requested the message is able to be sent. For the simulation
the standard CAN message was addressed to identifier 741h and contained one data byte
of value 12h. The remaining bit fields are defined in Table 3.4. These are the raw bit
streams and must undergo bit stuffing before they are transmitted. In addition to the fields
shown, there is also 3 recessive bits appended to the end of the message. These bits
represent an interframe space and combined with the acknowledgement field and the end
71
W tWVI Set
rt»«f^
t^Wi«!»™f»»t*« »^
i a o^l i" %-s vs M
I *Loafi wk casseian
Lauff wick-cwaro
8ttLafx ta'rtc
Ts* tarJLk
i TO IALat.. 4~~4 Bu is idle
oi
WliI6nG Dintti o 2405 tlow (41458400) Sermn bit [1]
U Epaed peafo lota
o a"*
Task
iadmu .
14626400) Serndin bit [1]
- Tast; ttj .. aas
(147944001 Senda bit [1]
T,Mkl.lk .I t 149624001 Serg bVit 11]
(5130400) Sering bit [1]
-i Tit* at.,L
I(44-%MSfflhrg 1. 4152984001 Seag bit [1]
1(5466400) Senf bat[1]
(«~tscrnlsa»«fciii)
Ttak
l
Ji Ta.k
J _c ..
Tksvdjw.V.| a 156344001 Se.rat bit [1]
a ITM4)
11 .5Sstog.
Snsg tit 111I
11i
# (582400) Senri bit [1]
15970400) Sendin bt [1]
IMDKMOISind pl
t (6138400) Sendi bit [1]
Task -l 167113001 Tx equested
W."7ii s --,VbiIit1
al|1380a51BSnig J*
T ikurite
Task -~ |bt
l I134400
t IA lIt
""" 'I'-" I'"
a
aS .iK datalsesnCAN
eo bbu4
a iStinoal
inbi j
al74'Mo2WlS'«l*ill«|ll
P4s2askI Ill
II all Ra»(s»*itbij
I 20Dai
Seing bHP
i a 4
SSb? ,
s.sk~ pt
«IW03T205tSlxii~g~a11
«ia i7Sabl
lersssksal ]
a(Wni)BS~iibatBl
« 9792a4m
fiin b (
Vl .Ss 101
PM-P;;1UX -r «|S»129Siblf1|
V^*t»n^M^at!S
Employing the display statement again the process of transmitting the CAN
message bit by bit is monitored through ModelSim (Figure 4.4). The reception of the
CAN message by the CAN controller is observed in the associated timing diagram
(Figure 4.5) generated by the testbench. In the diagram it can be seen that as the message
is received the receive signal rx changes logic levels according to the message. The
receive signal starts with a sequence of recessive bits indicating that the bus is idle as
expected. The request for transmission is initiated and the command register at address 1
is written high. At the next transition the start of frame bit indicates the beginning of a
new frame and the message is received. Reception is able to take place because the bus
72
Iti #....
a;r
-
- T. kKWo.
TaiknwndfLli.
* Un632995i2>tliPItj
I7«120gIS«igt
flitM WI
it167aimSeraBhnli
l(7833<1»SM
Swibvtb
ilD
# Slat lecewing daa Irmn CAN bu
#tf27aZO Sendingb
b
# 9200) Sendingbi111 ID1
10 SOF
t [71 632 Sending bit I1
# (7331 200) Sending bi 11
-l Tktr4IkUt HSin2a1WSB<>bripl # (74920) Sending it 11
- TAk
Te de It (7667200 Sending b [11
- T lh.ld , It (7835Oi) Snding bit 101
It1032Wn0SWgtdqi
- T4*lWLbp, i9Q171im6*«btiol # W300 Sending 1*01
-ri Tk jmftr.- # t0171200) Sen(ing b 101
tl7120t1 sbiP
r
To ,.da o.. t t339200) Sending 1101
-J Tilkn. __.
i I1560e5m72aOsmu|0
t2ISerdr bM|0] nt[507200) Sending 1bl01
# 675200 Senng bit11 Stuff
I10352WS
1S. V'WA
t (9011200) Sending bi I01
-JT rdT .inW ..
-J radt an. #t(17200o1 Snding ba 101
TCkeCLb..
I- rl00612)s»b(>*<ffl It (347200) Sendintg 101
LcanLlc-. '111«3nS««l11 t (s95152 Sending bt ol DLC3
|n!32MSm*QgA #I(683200) Sendin bit 101
#[951so
IK9B61200)} S&*,v
S ng bit 1]1
Mill
t*
$ g61 I
#i12tmaoW
#i~2snasasi}" tl \\ # (10 2003 Sending bit 10
I (270720SlXern
Ab1 I (1ai7200)3
tt(losz ds~1Ol
Sending bi 11
(1061200) Sending i 101
n (10B 2B Sending I101
»P»472V1S««ndtbl|11 t#(11027200) Sending bi (0
l13.72M) Stnii1bi t (1119200) Sending bi |01
\\
114 m5oos12 l
>3 }
73
If the received message is accepted by the message filtering scheme then the
message is stored in the receive buffer. The most significant eight bits of the message
identifier (ID 10:3) are stored in the identifier byte 1 at address 20. Identifier byte 2 at
address 21 is determined by the three remaining bits of the identifier (ID 2:0), RTR, and
the DLC. Addresses 22 to 29 in the receive buffer are reserved for the data bytes. Since,
the message in this simulation contains one data byte of value 12h, this value is stored to
address 22. Table 4.1 illustrates what the receive buffer should contain after the
simulation.
Table 4.1 - Layout of receive buffer after reception of message for the first simulation.
Now that the message should be waiting in the receive buffer, the testbench reads
the receive buffer and displays the results (Figure 4.6). As it can be seen in the graphic,
the value read from the register at address 20 was E8h, address 21 was 21h, and the data
74
byte in address 22 was 12h. This is also confirmed in the timing diagram shown in Figure
4.7.
Finally, with the message received and positioned in the receive buffer, the
testbench retrieves the data byte from the buffer. The data byte is then assigned to a
signal defined as lightl, which is a temporary holding variable. With a value of 12h or in
binary form 00010010, it is evident that there are two bit positions containing an "on"
command. However, for the purposes of this simulation it is interpreted that this is a
command to turn on the LED. Therefore, it can be seen in Figure 4.8 that after register 22
is read the data it contains is copied to a holding variable. The interpretation to turn on
the LED is performed and the LED signal is forced "high" or "on."
· c,---
l.ra
[ ·-JJ-1 -·-··--·- ··-
I
-
11122032CO
"Mbill(
I0237l(X11SOk~tAt
I l253I SwhkvbA
IP237l2S.t&Ijll
-j T&Arq~p.
Il 3432tCI $Wn~*VbitM _ _ _ _ _ _ _ __ _ _ _ _ _ _
IVf32ll2W1 S~n~vbO P
IV3132M$bellS l j # (3500
-4 TaI~
rwijpF -F-
T~a*tW 1jl3I5Mft0ib9S W~tAe
I(Q5472MSV P1I MW I -tar120 Ne
(be6~
-, TW lo-Wo
rj Tal fxLrmgwb Itl3993Maaxbvballj/ # (335787 Re dkrg ietet 1211 * 0N21
IrP4Mo51lS.1mtP /IlAr i VrbbI D3-.A6,.- lXM - MADV2
.4 aavlaft-No- IPA21s
2fm j~ont biP 'l u r lo
r I''-'-"'J IYcL -"
11143072 1 qbi~j tt (3W 14 591
0 0)
P V re. 123)1 Oxx
P04TZ3A I'VIb1 #[MG7MIX Readri resgtes 1241 - Oxx
linfO*k
d..d
l se.t&b PI #(33711310j Readr greitei 1281 a Oxx
i153MOMSV ebill tt (33430 RPe ft reastae 1261 - Oxx
Illtfrnn2r]S.,&~r
ibet
lll~r72Wl~w~e~nebC
I123WOO) .dnpbeP12
Rlb
~ ~338U# itI P(3&C~
M
Renag reyktei 1291 Oxx
i384750
RFt& * Data Byte 1221 tJK12
ja*drw-ds
I -4744 mcw
113A747"
It~ Aa&,V"W* il &O
IOX795% eadimMwo1241 -0
1J1037M"Rob*po*
No" :o
IIrM13"A"*VMhprs
I -W01"
CAMTh00tbe,
&M " [2B)
25 -Oa
IB a1to475
R t m ndSam
Re
Itnqm"L4AlNl1
t339199M R t.Ao f.~
CANTeebonch W.OwdIe
%a *cmanmd NWS**V kx a~brcr^"e
SIUM11,
'Keial1 -r- .1
75
the host microcontroller, the embedded program in the microcontroller mainly reads and
writes to the various registers. Operating the CAN controller in basic mode, the
initialization process begins with enabling the reset mode by configuring the control
register (see Table 4.2). The reset mode is entered when the reset request (RR) is set in
the control register. Under the reset mode the registers that receive the configuration
information are accessible. The host microcontroller then configures the clock divider
register, acceptance code and acceptance mask registers, bus timing registers, and finally
L I I I I I I
0 0 0 0 0 0 0 1
The clock divider register is an 8-bit register, the architecture of the clock divider
register is shown in Table 4.3, that determines if the CAN controller will operate in the
BasicCAN mode or the PeliCAN mode, which offers extended features. For this
implementation the controller is operated in the BasicCAN mode and the microcontroller
sets the CAN mode bit to logic 0. The clock divider register also determines if the
controller provides an output clock for the microcontroller and at what frequency. Since
there are two independent clocks in this implementation the CAN controller clock is
deactivated and this is reflected with logic 1 for the clock off bit. The internal CAN input
comparator of the CAN controller is bypassed since the external transceiver is being
78
4.3 Two CAN Node Network Implementation
The simulation results obtained in the previous section demonstrated that the
CAN controller was functional in a self reception mode, which only requires one CAN
node (Figure 4.2). The implementation of more than one node will provide a better
As it was demonstrated in Figure 3.11 the two node network consists of a transmitting
node and a receiving node. It is possible that both nodes can perform the actions of both
transmitting node, which contains the user interface, represents the control point for the
light application. The receiving node represents the CAN nodes that will be attached to
each light in the light package. For the receiving nodes there is no need for a user
interface since they will be remoted in various locations of the car to control the functions
of the light package dependent upon the messages constructed by the transmitting node.
Once simulation is complete and the CAN controller is verified the HDL model
can be implemented and programmed to the FPGA. With the FPGA programmed the data
and control lines of the CAN controller are then connected to the microprocessor and the
transmit and receive lines attached to the CAN transceiver as detailed in Figure 3.10. The
microprocessor is then programmed with its respective control program and CAN
initialization process that configures the CAN controller for communication. Since the
CAN controller is seen as nothing more than a peripheral memory mapped I/O device for
77
the host microcontroller, the embedded program in the microcontroller mainly reads and
writes to the various registers. Operating the CAN controller in basic mode, the
initialization process begins with enabling the reset mode by configuring the control
register (see Table 4.2). The reset mode is entered when the reset request (RR) is set in
the control register. Under the reset mode the registers that receive the configuration
information are accessible. The host microcontroller then configures the clock divider
register, acceptance code and acceptance mask registers, bus timing registers, and finally
0 0 0 0 0 0 0 1
The clock divider register is an 8-bit register, the architecture of the clock divider
register is shown in Table 4.3, that determines if the CAN controller will operate in the
BasicCAN mode or the PeliCAN mode, which offers extended features. For this
implementation the controller is operated in the BasicCAN mode and the microcontroller
sets the CAN mode bit to logic 0. The clock divider register also determines if the
controller provides an output clock for the microcontroller and at what frequency. Since
there are two independent clocks in this implementation the CAN controller clock is
deactivated and this is reflected with logic 1 for the clock off bit. The internal CAN input
comparator of the CAN controller is bypassed since the external transceiver is being
78
Table 4.3 - Bit layout of the clock divider register; CAN address 31.
0 1 0 0 1 0 0 0 0
In the Basic mode the acceptance filter is defined by the acceptance code register
and the acceptance mask register. As described earlier, the acceptance filter provides the
capability of the CAN controller to filter received messages when the identifier bits of the
received message match those of the acceptance filter. The acceptance code was set to
zero and the acceptance mask was set to don't care, which results in an acceptance filter
that allows every identifier to pass through. The resulting layouts of the acceptance code
register and the acceptance mask register are detailed in Tables 4.4 and 4.5.
Table 4.4 - Bit layout of the acceptance code register; CAN address 4.
0 0 0 0 0 0 0 0
Table 4.5 - Bit layout of the acceptance mask register; CAN address 5.
1 1 1 1 1 1 1 1
I If I I I I I
79
Configuration of the bus timing is governed by the bus timing registers, bus
timing register 0 and 1. Bus timing register 0 contains the values of the baud rate
prescaler (BRP) and the synchronization jump width (SJW). The SJA1000 application
note [46] defines values for these parameters for several examples. The application note
assumes that a 24 MHz clock is being used to clock both the CAN controller and the
microprocessor for this implementation. However, the CAN controller and the
microprocessor are being clocked by separate clocks. The clock of the microprocessor for
this implementation is 22 MHz and the CAN controller is clocked by its internal 24 MHz
clock. For a bit rate of 1 Mb/s the BRP and SJW are both set to zero (see Table 4.6). The
contents of the bus timing register 1 include time segment 1 (TSEG1), time segment 2
(TSEG2), and sampling (SAM), which define the length of the bit period, location of the
sample point, and the number of times the bus is sampled. To reach the bit rate of IMb/s
the application note defines TSEG1 to be 08h and TSEG2 to be 10h and the bus is
Table 4.6 - Bit layout of the bus timing register 0; CAN address 6.
0 0 0 0 0 0 0 0
Table 4.7 - Bit layout of the bus timing register 1; CAN address 7.
I I
0 0 0 1 1 0 0 0
I I I _ I I I
80
To complete the initialization process the output control register is configured to
allow normal output mode and the driver characteristics of the transmit lines (TXO and
TX1) are defined. The application diagram detailed in Figure 3.10 shows that only TXO
from the controller is being used. Therefore, TX1 is floating and TXO is configured as
push/pull. This configuration is described by the bit layout described by Table 4.8.
Table 4.8 - Bit layout of the output control register 1; CAN address 8.
0 0 0 1 1 0 1 0
_ i . . . _ ___ I I
deactivation of the reset mode. Following initialization, CAN message transmission and
Before a message can be transmitted across the CAN bus the message must first
be constructed. The host microcontroller prepares the message, only the identifier, RTR,
DLC, and data bytes need to be defined, and then transfers the message into the transmit
buffer (see Table 3.2) of the CAN controller. The CAN controller will process the
message defined by the microcontroller to ensure it conforms to the CAN protocol. The
same identifier that was used for simulation is used again for this implementation with a
data byte of value Olh. The resulting layout of the transmit buffer is detailed in Table 4.9.
81
Table 4.9 - Resulting bit layout after the host microcontroller writes the prepared
message to the transmit buffer.
Once the message is stored into the transmit buffer the message is ready for
transmission. By writing a transmit request (TR) to the command register (see Table
4.10) the CAN controller is alerted that there is a message queued and the controller
subsequently transmits the message. The status of the message transmission can be
checked by monitoring the transmit buffer status (TBS) and transmission complete status
(TCS) flags in the read only status register (see Table 4.11). If the TCS flag is present
then the most recent requested transmission has completed successfully. If the message is
not yet completed then the TCS is low and the TBS is also low indicating that a message
is still waiting in the transmit buffer. Table 4.11 details the content of the status register
Table 4.10 - Bit layout of the command register for transmitting node; CAN address 1.
82
Table 4.11 - Bit layout of the status register for the transmitting node; CAN address 2.
I I I I I
0 0 0 0 1 1 0 0
Following the transmission of the message from the transmitting node the
receiving node obtains the transmitted message and places it directly into the receive
buffer (see Table 3.2). The resulting layout of the receive buffer (see Table 4.12) is
analogous to the transmit buffer, with the only difference being the CAN addresses. The
host microcontroller is made aware of the received message through constant polling of
the status register. In addition to the transmit flags (TBS and TCS) the status register also
contains a receive buffer status (RBS) flag that indicates whether or not the receive FIFO
contains any messages. A logic 1 for the RBS flag reflects that there is one or more
messages waiting in the buffer and a logic 0 reveals that the buffer is empty.
Table 4.12 - Resulting bit layout of the receive buffer after the CAN controller receives
the CAN message from the transmitting node.
83
Table 4.13 - Bit layout of the status register for the receiving node; CAN address 2.
00 0 0 10 0
The host microcontroller performs a loop that polls the status register, continually
checking if the receive buffer status flag indicates that at least one message is present in
the receive buffer. If the RBS is low then the loop continues until the RBS is high, at
which point the host microcontroller reads the first message from the receive buffer.
After retrieving the message from the buffer the microcontroller sends a release receive
buffer (RRB) command to the command register, as illustrated in Table 4.14. The release
command purges the last read message and pushes the next message up to be read by the
microcontroller.
Table 4.14 - Bit layout of the command register for the receiving node; CAN address 1.
X X X 0 0 1 0 0
processes the most recently read message. Using the coding convention identified in
Table 3.3 the embedded data byte in the received message, 00000001, reflects that all
lights are off except for light 1. The microcontroller examines the bit positions of the data
84
byte(s) and determines if a light in the light package should be turned on or off according
to the logic level present at each bit position. The flow diagram for the bit inspection
bit position
.4
Figure 4.9 - Flow diagram for bit inspection of received data byte.
Chapter 4 has presented the results obtained by using the approach outlined in
Chapter 3. The results have detailed the process of configuring CAN for communications
and the process of sending and receiving messages over the CAN bus. With the results
controlling the NJSP light package. Chapter 5 will continue to discuss the
accomplishments of this work and recommendations on how to move this research ahead
in the future.
85
Chapter 5: Conclusions
for new features, which in turn require more communication. In-vehicle telematics have
information systems including global positioning units, vehicle tracking systems, and
and telematics represent new levels of complexity for the car interior. Integration of
added displays, controls, and wiring becomes more challenging and can lead to decreased
a communications backbone providing a conduit for sharing information among all the
electronic systems. Such a backbone will significantly reduce cabling costs and cabling
volume and it will simplify technology installation. The automotive industry realized that
future market success depended on the availability of such a system; since the beginning
of the 1980s, almost every automotive company initiated research and development to
CAN, OBD, MOST, and LIN. Since the early 1990s, CAN has become the protocol of
choice for automotive applications due to its high performance, robust error detection and
This thesis sought to implement the CAN protocol in hardware designed for the
NJSP troop car platform. This work lays the foundation for a highly integrated police car
that provides greater functionality to the user and makes the vehicle more flexible to
86
facilitate additional technology growth in the future. Further benefits include reduced
reduced cabling in a law enforcement application. The proposed network, CAN, offers a
common data bus that would eliminate the need for point-to-point wiring of the current
systems. Furthermore, this network provides the capability to control applications such as
When the user selects a light combination, a CAN message is generated that
contains the address of the target CAN receiver node. The receiver node filters the CAN
frame and decodes the embedded data field, which contains the instruction for turning on
is expected that the CAN nodes would work as presented in this research if installed in
the NJSP vehicle. The design demonstrated the control of a modified light package; the
accomplish more than just control. An entity with CAN capability can report diagnostic
information obtained from other elements on the shared network, thus supplying the user
with a "smart" technology suite. Above and beyond control and diagnostic applications
CAN theoretically has the ability to transfer media across the bus, even though CAN is
87
However, the driving force behind the development of an in-vehicle network for a
law enforcement platform was not motivated by multimedia applications. The original
objective was to determine whether CAN could perform the functions associated with the
light control application and reduce wiring across the vehicle environment. The work
is a suitable technology to carry out the control of the light package. Furthermore, the
inherent two-wire data bus associated with the CAN implementation automatically
The light package discussed in section 1.3 detailed the 14 different light locations
and the fact that in existing cars, each light has a dedicated cable installation from the
trunk of the car to the light position. Considering that the overall length of the Troopcar is
roughly 17.5 feet, it is estimated that there is about 100 feet of cable installed to support
the lights in the current Troopcar. Furthermore, each cable is a two-wire cable. Factoring
this into the initial estimate results in a conservative approximation of over 200 feet of
individual wires installed to operate the light package. In addition to the potential of
adding to fuel consumption, power demands, and decreasing vehicle space, the
compilation long wire causes the NJSP to incur installation costs that could be minimized
of a CAN bus within the vehicle would consist of a twisted-pair of wires running the
length of the car to facilitate data flow. Power would be obtained from nearby sources to
minimize the amount of additional lengths of power wiring. Therefore, the backbone of
the CAN network would consist of a data cable, a twisted-pair of wires, and power
88
wiring. Additionally, each CAN node installed in the car would have to connect to the
CAN bus. In view of the fact that the CAN bus is centralized the wire required to connect
the CAN nodes to the bus is minimum. With an efficient layout that minimizes the
amount of CAN nodes implemented and maximizes the amount of lights controlled it is
possible to reduce the amount of installed wire by at least half. The CAN nodes are
designed to be able to control more than just one light and hence each light in the light
package does not require an individual node. Instead, the car can be viewed as three
sections; front, middle, and rear, with a CAN node controlling each section as
Front
Middle
Key:
* Strobe
A LED
Rear
* Halogen
CAN node
Figure 5.1 - Sectionalized design for CAN node locations within the NJSP troop car.
For a relatively low cost, a CAN implementation of the light package offers
economic advantages over the current installation method. In addition to the impending
savings in installation costs, CAN provides greater flexibility and versatility over point-
89
to-point wiring. Not only does the CAN implementation reduce wiring but it provides a
communication channel that can facilitate other technologies and systems in the future. In
suitable for controlling the NJSP light package. This approach lays the groundwork for
questions remain unanswered, and there are other potential opportunities to explore.
One area of concern is to ensure that the CAN node design is ruggedized
vehicle. The following sections outline future areas of research that extend the foundation
An in-vehicle network should be able to outlast the useful life of the vehicle it is
installed in. Aside from new substitutable technologies becoming available and protocol
modifications to CAN, which are sustainability issues, the network should be able to
endure the rigors of minor accidents and inclement environments. It is more than likely
that law enforcement agencies have life cycle plans that abbreviate the life span of their
vehicles, but nonetheless the CAN network should be durable enough to survive the life
spans encountered.
To achieve this durability, future considerations should focus on the design and
testing of packaging for the CAN nodes. Without proper packaging of a CAN node,
90
whether it is FPGA based, COTS, or custom silicon, the network could be damaged
during an accident or other event, rendering the applications operating on the network
useless.
electromagnetic interference (EMI) from the ignition system to all the in-vehicle
effects on data transmission can cause bit errors, and may cause CAN nodes to become
inactive if they are excessive. In the event that multiple nodes succumb to EMI, the CAN
network can crash and again render applications functioning on the network inoperative.
This is of critical importance in a law enforcement vehicle, where there are numerous
essential applications that must work at a moments notice in the harshest conditions.
When an emergency call is received, the officer must respond. Therefore, the effects of
EMI disturbances must be minimized so that the officer can activate the pursuit lights and
Error mechanisms that were discussed in section 2.2.1.3 were almost a direct
result of trying to adapt a system around the potential of error due to EMI. In addition,
implementing a twisted pair, two-wire bus with differential signaling increases immunity
to EMI and also continues communications even if one of the lines breaks or is short-
circuited. Although, the protocol is standardized and others have tested it to prove that it
provides greater immunity to EMI, this has not been shown in this thesis.
should examine the EMI immunity of the network to other in-vehicle electronics, such as
91
two-way radios, on board computers, etc. In addition to susceptibility to EMI, the amount
Data integrity can also be compromised when the data bus is damaged. Therefore,
an assessment of the two-wire CAN bus with various faults needs to be completed.
A major factor that resulted in the CAN research discussed in this thesis was the
fact that there were numerous in-vehicle electronics in the NJSP troop car that resulted in
excessive wiring but also were incompatible. In its current form the NJSP troop car has
multiple systems that are add-ins and therefore they have not been designed to share
implementing a common network such as CAN that has been proposed throughout this
thesis.
conceptually feasible to bridge two separate systems that were once completely
independent of each other, such as the light package and the mobile computer, so as to
allow sharing information with each other. With CAN compatibility, the light package
can be controlled through the PC and diagnostic information, for instance if a light was
malfunctioning or burned out, can be sent from the lights back to the PC supplying
92
vehicle that provides increased functionality, simultaneously creating an environment that
Concerns involving compatibility between the standard format and extended format are
resolved by the identifier extension (IDE) bit in the arbitration field that distinguishes
between standard and extended CAN frames. No future work involving formatting is
anticipated to be needed.
All three aspects discussed so far, durability, integrity, and compatibility, play a
part in sustainable design. Sustainable design can be defined as a design that provides
economic, social, and environmental benefits in the long term. For an in-vehicle network
an in-vehicle network or not, that can sustain a long lifetime without being replaced.
decreases waste and decreases the amount of capital and resources spent on purchasing
and installing new technology, thus providing economic and environmental benefits.
An obvious advantage that FPGA design affords is the fact that the logic
components can be reconfigured. Therefore, if there were ever any modifications to the
CAN protocol or a need to adjust the current design, the FPGA could be reconfigured
without having to replace it. Nevertheless, there are environmental concerns with any
printed circuit board (PCB) design. From the process of manufacturing to the time the
PCB is discarded there are numerous pollutants and wastes generated. These wastes
93
include but are not limited to CFCs, acid and alkaline solutions, flux residue, scrap board
metal, and solder [47]. However, this is a problem faced by the entire PCB industry and
is being solved collectively. With the use of a FPGA, these effects can be minimized by
Another issue related to sustainability is the aspect of cost. The design should be
at a cost point where it gains acceptance and also provides a profit for the designer. The
dominant choices for implementing CAN are using FPGAs or ASICs. FPGAs allow for
post fabrication modification, whereas custom silicon (ASICs) does not. However, the
use of an ASIC allows for more efficient use of gates. Using only 40k of a 100k-gate
FPGA can be considered inefficient. Instead, an ASIC designer can develop a custom IC
that implements the CAN protocol using just the number of gates needed. This reduces
the footprint size of the design and saves money. If the price per gate is assumed to be the
same for FPGA and ASIC designs, then ASIC designs will prove to be more economical.
However, price per gate is not the same and therefore there are many tradeoffs involved
with FPGA design versus ASIC design. One tradeoff is whether or not it is more
economical to mass produce a design in a FPGA, which can be reconfigured and result in
future savings, or produce the design with an ASIC, which cannot be reconfigured and
will result in further overhead in the future. There is also an issue of fixed costs such as
nonrecurring-engineering (NRE) that results in higher initial design cost for ASIC design
while FPGA design has a low initial design cost. The end result is that there exists a
break-even point between the unit costs for ASIC and FPGA technology that needs to be
94
also must be considered that as fabrication technology continues to advance, the price per
gate will become cheaper and efficiency of gate usage may become an issue of the past.
Future research in the sustainable design of CAN devices should focus on these
environmental and cost issues. For CAN to endure the life span of a vehicle it should be
cost effective to implement, able to adapt to future modifications, and not pose a threat to
the environment during manufacturing, throughout its life and until the time it is disposed
of.
95
References
11. D. Marsh, "CAN Bus Networks Break Into Mainstream Use," EDN Magazine, Aug.
2002.
12. W. Lawrenz, "Worldwide Status of CAN: Present and Future," Proc. of the 2nd
Int. CAN Conf, 1995.
16. T. Moon, "Control in Automotive Body System Networks," Proc. of the 3rd Int.
CAN Conf., 1996.
96
17. G. Bourdon, P. Raux, S. Delaplace, "CAN for Autonomous Mobile Robots,"
Proc. of the 3rd Int. CAN Conf., 1996.
18. H. Zeltwanger, "Designing Devices Using CAN and CANopen Buses for
Networking," Medical ElectronicsManufacturing, 1999.
19. S. Crisp, "CAN in Las Vegas' Monorail," CiA CAN Newsletter, pp. 12-14, Dec. 2002.
20. M. Bailey, "CAN in Electric Vehicles," Proc. of the 3 rd Int. CAN Conf, 1996.
21. L. Fredriksson, "CAN for Critical Embedded Automotive Networks," IEEE Micro,
Vol. 22, No. 4, pp. 28-35, 2002.
30. ISO-CD 11898-1, Road Vehicles - Controller Area Network (CAN) - Part 1: Data
Link Layer and Physical Signaling, 1999.
31. ISO-CD 11898-2, Road Vehicles - Controller Area Network (CAN) - Part 2: High
Speed Medium Access Unit, 1999.
32. F. Hartwich, A. Bassemir, "The Configuration of the CAN Bit Timing," Proc. of the
6t Int. CAN Conf, 1999.
97
34. T. Nolte, H. Hansson, C. Norstrom, S. Punnekkat, "Using Bit Stuffing
Distributions in CAN Analysis," IEEE Real-Time Embedded Systems Workshop,
Dec. 2001.
35. CAN-in-Automation, CAN Application Layer for Industrial Application, Version 1.1,
CiA DS 201-207, 1996.
37. Open DeviceNet Vendor Association Inc., DeviceNet Specifications, Version 2.0,
Vol. I and II, 1998.
98
Appendix A: HDL Code
The following appendix contains all of the HDL code components used to
construct the CAN controller. This code was written by Igor Mohor and provided through
OPENCORES.org. The code was synthesized, simulated, and implemented using Xilinx
The following code is the top layer of the CAN controller that connects the
// can_top.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/l0ps
// synopsys translate_on
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_top
(
'ifdef CANWISHBONE IF
wb_clk_i,
wb_rst_i,
wb_dat_i,
wb_dat_o,
wb_cyc_i,
wb_stb_i,
wbwei,
wbadri,
99
wb_ack_o,
'else
rsti,
alei,
rd_i,
wri,
port_0_io,
'endif
cscani,
clki,
rxi,
tx_o,
irqon,
clkouto
);
parameter Tp = 1;
'ifdef CANWISHBONEIF
input wb_clk_i;
input wb_rsti;
input [7:0] wb_dat_i;
output [7:0] wb_dat_o;
input wb_cyc_i;
input wb_stb_i;
input wb_we_i;
input [7:0] wbadri;
output wb_ack_o;
reg wb_ack_o;
reg cs_syncl;
reg cs_sync2;
reg cs_sync3;
reg cs_ackl;
reg cs_ack2;
reg cs_ack3;
reg cs_sync_rstl;
reg cs_sync_rst2;
'else
input rst_i;
input ale i;
input rdi;
input wr i;
inout [7:0] port_0_io;
input cs_can_i;
input clki;
input rx_i;
output tx_o;
output irq_on;
output clkout_o;
100
reg data out fifoselected;
wire irq_o;
wire [7:0] dataoutfifo;
wire [7:0] data_out_regs;
/* Mode register */
wire reset_mode;
wire listen_only_mode;
wire acceptance_filter_mode;
wire self_test_mode;
/* Command register */
wire release_buffer;
wire tx_request;
wire abort_tx;
wire selfrxrequest;
wire single_shot_transmission;
101
/* This section is for EXTENDED mode */
/* Acceptance code register */
wire [7:0] acceptance_code_l;
wire [7:0] acceptancecode_2;
wire [7:0] acceptance_code_3;
wire cs;
102
wire needtotx;
wire overrun;
wire info_empty;
wire setbuserror_irq;
wire set_arbitration_lost_irq;
wire [4:0] arbitration_lost_capture;
wire node_error_passive;
wire node_error_active;
wire [6:0] rx_messagecounter;
wire tx out;
wire tx_oen;
wire rst;
wire we;
wire [7:0] addr;
wire [7:0] data_in;
reg [7:0] data_out;
.sample_point(sample_point),
.transmitting(transmitting),
.set resetmode(setresetmode),
.node_bus_off(nodebus_off),
.error_status(error_status),
.rx err cnt(rx err cnt),
.txerrcnt(tx_err_cnt),
.transmit_status(transmit_status),
.receive_status(receive_status),
.txsuccessful(txsuccessful),
.needtotx(needtotx),
.overrun(overrun),
info_empty(info_empty),
.set_bus_error_irq(set_bus_error_irq),
.set_arbitration_lost_irq(set_arbitration_lost_irq),
.arbitration_lost_capture(arbitration_lost_capture),
.node_error_passive(node_error_passive),
.node_error_active(node_error_active),
.rx_message_counter(rx_message_counter),
/* Mode register */
.reset_mode(reset_mode),
.listen_only_mode(listen_only_mode),
.acceptance_filter_mode(acceptance_filter_mode),
.self_test_mode(self_test_mode),
103
/* Command register */
.clear_data_overrun(),
.release_buffer(release_buffer),
.abort_tx(abort tx),
.tx_request(tx_request),
.self_rxrequest(self_rxrequest),
.single_shot_transmission(single_shot_transmission),
.read_arbitration_lost_capture_reg(read_arbitration_lost_capturereg),
104
/* Tx data registers. Holding identifier (basic mode), tx frame
information (extended mode) and data */
.tx_data_0(tx_data_0),
.tx_datal(tx_data_1),
.tx_data_2(tx_data_2),
.tx_data_3(tx_data_3),
.txdata 4(txdata_4),
.txdata 5(tx_data_5),
.txdata_6(tx_data_6),
.tx_data_7(tx_data_7),
.tx_data_8(tx_data_8),
.tx_data_9(tx_data_9),
.txdata_10(txdata_10),
.tx data ll(tx data_11),
.tx data 12(tx data_12)
/* End: Tx data registers */
);
/* Mode register */
.resetmode(reset_mode),
canbsp icanbsp
can_bsp i_canbsp
(
105
.clk(clk_i),
.rst(rst),
.addr(addr),
.data_in(data_in),
.data_out(data_out_fifo),
.fifoselected(dataoutfifoselected),
/* Mode register */
.reset_mode(reset_mode),
.listen_only_miste(listen_only_mode),
.acceptance_filter_mode(acceptance_filter_mode),
.self_testmode(selftestmode),
/* Command register */
.release_buffer(release_buffer),
.tx_request(tx_request),
.abort_tx(abort_tx),
.self_rxrequest(self_rx_request),
.single_shot_transmission(single_shot_transmission),
.read_arbitration_lost_capture_reg(read_arbitration_lost_capture_reg),
106
overflow).
.tx_err_cnt({txerr_cnt_dummy, tx_err_cnt[7:0]}), // The MSB is not
displayed. It is just used for easier calculation (no counter
overflow).
.transmit_status(transmit_status),
.receive_status(receive_status),
.tx successful(txsuccessful),
.needtotx(needtotx),
.overrun(overrun),
.info_empty(info_empty),
.set_bus_error_irq(setbuserror irq),
.set_arbitration_lost_irq(set_arbitration_lost_irq),
.arbitration_lost_capture(arbitration_lost_capture),
.node_error_passive(node_errorpassive),
.node_error_active(node_error_active),
.rx_message_counter(rx_message_counter),
/* Tx signal */
.tx(tx_out),
.tx_oen(tx_oen)
107
assign txo = txoen? l'bz : txout;
108
// Generating acknowledge signal
always @ (posedge wb_clk_i)
begin
wb_acko <=#Tp (cs_ack2 & (-cs_ack3));
end
'else
// Latching address
always @ (negedge clk_i or posedge rst)
begin
if (rst)
addrlatched <= 8'h0;
else if (alei)
addr_latched <=#Tp port_0_io;
end
assign cs = ((wri & (-wr_i q)) I (rdi & (-rd_iq))) & cscani;
'endif
endmodule
109
A.2: BTL HDL Code
This code is the bit timing logic (BTL) component of the CAN controller. The
BTL continually monitors the input and output data streams and handle the related bit
// canbtl.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translate_on
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
/* Mode register */
reset_mode,
110
hardsync,
resync,
/* Output from can_bsp module */
rx_idle,
transmitting,
lastbitofinter
);
parameter Tp = 1;
input clk;
input rst;
input rx;
/* Mode register */
input reset_mode;
wire go_sync;
wire go_segl;
111
wire go_seg2;
wire [8:0] preset_cnt;
wire sync_window;
/* Changing states */
assign go_sync = clk_en & (seg2 & (-hard_sync) & (-resync) &
((quant_cnt == time_segment2)));
assign go_segl = clk_en & (sync I hard_sync I (resync & seg2 &
sync_window) I (resync_latched & sync_window));
assign go_seg2 = clk_en & (segl & (-hardsync) & (quantcnt ==
(time segmentl + delay)));
/* Synchronization stage/segment */
always @ (posedge clk or posedge rst)
begin
112
if (rst)
sync <= 0;
else if (go_sync)
sync <=#Tp l'bl;
else if (go_segl)
sync <=#Tp l'bO;
end
/* Seg2 stage/segment */
always @ (posedge clk or posedge rst)
begin
if (rst)
seg2 <= 0;
else if (go_seg2)
seg2 <=#Tp l'bl;
else if (go_sync I go_segl)
seg2 <=#Tp l'bO;
end
/* Quant counter */
always @ (posedge clk or posedge rst)
begin
if (rst)
quant_cnt <= 0;
else if (go_sync | go_segl I go_seg2)
quant_cnt <=#Tp 0;
else if (clk_en)
quant_cnt <=#Tp quant_cnt + l'bl;
end
/* When late edge is detected (in segl stage), stage segl is prolonged.
*/
always ® (posedge clk or posedge rst)
begin
if (rst)
delay <= 0;
else if (clken & resync & segl)
delay <=#Tp (quant_cnt > sync_jump_width)? (sync_jump_width + 1)
(quant_cnt + 1);
else if (gosync I go_segl)
delay <=#Tp 0;
end
113
// If early edge appears within this window (in seg2 stage), phase
error is fully compensated
assign sync_window = ((time_segment2 - quant_cnt) < ( sync_jump_width +
1));
114
end-of-frame and interframe. No resynchronization is needed
meanwhile). */
always 0 (posedge clk or posedge rst)
begin
if (rst)
resync_blocked <=#Tp l'bl;
else if (reset_mode)
resync_blocked <=#Tp l'bl;
else if (hard_sync)
resync_blocked <=#Tp l'bO;
end
endmodule
115
A.3: BSP HDL Code
The following code is the bit stream processor (BSP). The function of the BSP is
to translate messages into frames and frames into messages. Other tasks performed by the
BSP are bit stuffing/ destuffing, CRC checking, and bus arbitration.
// can_bsp.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translate_on
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_bsp
(
clk,
rst,
sample_point,
sampled_bit,
sampled_bit_q,
tx_point,
hardsync,
addr,
data_in,
data_out,
fifoselected,
/* Mode register */
resetmode,
listen_only_mode,
acceptance_filter_mode,
self_test_mode,
/* Command register */
release_buffer,
tx_request,
abort tx,
116
self_rxrequest,
single_shot_transmission,
/* Arbitration Lost Capture Register */
read_arbitrationlost_capture_reg,
rxidle,
transmitting,
last bit of inter,
setresetmode,
nodebusoff,
error_status,
rx_err_cnt,
tx_err_cnt,
transmitstatus,
receivestatus,
txsuccessful,
needtotx,
overrun,
info_empty,
set_bus_error_irq,
set_arbitrationlost_irq,
arbitration_lost_capture,
node_error_passive,
node_error_active,
rx_message_counter,
117
acceptance_mask_l,
acceptance_mask_2,
acceptance_mask_3,
/* End: This section is for EXTENDED mode */
/* Tx signal */
tx,
txoen
parameter Tp = 1;
input clk;
input rst;
input sample_point;
input sampled_bit;
input sampled_bit_q;
input tx_point;
input hard_sync;
input [7:0] addr;
input [7:0] data_in;
output [7:0] data_out;
input fifo_selected;
input reset_mode;
input listen_only_mode;
input acceptance_filter_mode;
input extended_mode;
input self_test_mode;
/* Command register */
input release_buffer;
input tx_request;
input abort_tx;
input self_rx_request;
input single_shot_transmission;
118
/* Error Code Capture Register */
input read_error_code_capture_reg;
output [7:0] error_capture_code;
output rx_idle;
output transmitting;
output lastbitof_inter;
output set_reset_mode;
output node_bus_off;
output error_status;
output [8:0] rx_errcnt;
output [8:0] tx_err_cnt;
output transmit_status;
output receive_status;
output tx_successful;
output needtotx;
output overrun;
output info_empty;
output set_bus_error_irq;
output set_arbitration_lost_irq;
output [4:0] arbitration_lost_capture;
output node_error_passive;
output node_error_active;
output [6:0] rx_message_counter;
119
information (extended mode) and data */
input [7:0] tx_data_0;
input [7:0] tx_data_l;
input [7:0] txdata_2;
input [7:0] txdata_3;
input [7:0] txdata_4;
input [7:0] tx_data_5;
input [7:0] tx_data_6;
input [7:0] txdata_7;
input [7:0] txdata_8;
input [7:0] tx_data_9;
input [7:0] tx_data_10;
input [7:0] tx_data_11;
input [7:0] txdata_12;
/* End: Tx data registers */
/* Tx signal */
output tx;
output tx oen;
reg reset_mode_q;
reg [5:0] bitcnt;
reg rxidle;
reg rx_idl;
reg rxrtrl;
reg rx_ide;
reg rx_id2;
reg rxrtr2;
reg rxrl;
reg rx_rO;
reg rx_dlc;
reg rxdata;
reg rx_crc;
reg rx_crc_lim;
reg rx_ack;
reg rxacklim;
reg rx_eof;
reg rxinter;
reg go_early_tx latched;
reg rtrl;
reg ide;
reg rtr2;
reg [14:0] crc_in;
120
reg bitstuff cnt tx en;
reg crc_enable;
reg [2:0] eof_cnt;
reg [2:0] passive_cnt;
reg transmitting;
reg errorframe;
reg error_frame_q;
reg enableerrorcnt2;
reg [2:0] errorcntl;
reg [2:0] errorcnt2;
reg [2:0] delayeddominant_cnt;
reg enable overloadcnt2;
reg overload_frame;
reg overload_frame_blocked;
reg [2:0] overload_cntl;
reg [2:0] overloadcnt2;
reg tx;
reg crc_err;
reg arbitration_lost;
reg arbitration_lost_q;
reg [4:0] arbitration_lost_capture;
reg arbitration_cnt_en;
reg arbitration_blocked;
reg tx_q;
reg nodeerror_passive;
reg nodebusoff;
reg nodebus_off_q;
reg ack_err_latched;
reg bit_err_latched;
reg stufferrlatched;
121
reg form_err_latched;
reg rule3_excl_l;
reg rule3_excl_2;
reg rule3_exc2;
reg suspend;
reg susp_cnt_en;
reg [2:0] susp_cnt;
reg error_flag_over_blocked;
wire bitdestuff;
wire bitdestuff_tx;
wire rule5;
/* Rx state machine */
wire gorx_idle;
wire go_rx_idl;
wire gorxrtrl;
wire go_rx_ide;
wire gorxid2;
wire go_rx_rtr2;
wire go_rx_rl;
wire go_rx_r0;
wire go_rx_dlc;
wire go_rx_data;
wire go_rx_crc;
wire go_rx_crc_lim;
wire go_rx_ack;
wire go_rx_acklim;
wire gorx_eof;
wire go_overload_frame;
wire go_rx_inter;
wire go_error_frame;
wire go_crc_enable;
wire rst_crc_enable;
wire bitdestuff_set;
wire bitdestuff_reset;
wire go_early_tx;
wire go_tx;
wire error_frameended;
122
wire overload_frame_ended;
wire bit_err;
wire ack_err;
wire stuff_err;
// of intermission, it starts
reading the identifier (and transmitting its own).
wire overloadneeded = 0; // When receiver is busy, it needs
to send overload frame. Only 2 overload frames are allowed to
// be send in a row. This is not
implemented because host can not send an overload request.
wire arbitration_field;
wire rsttxpointer;
wire sendack;
wire biterrexcl;
wire bit errexc2;
wire biterrexc3;
wire bit_err_exc4;
wire bit_err_exc5;
wire error_flag_over;
wire overload_flag_over;
123
assign go_rx_idle = sample_point & sampled_bit &
last_bitofinter | busfree & (-node_bus_off);
assign go_rx_idl = sample_point &
(-sampled_bit) & (rx_idle I lastbitofinter);
assign go_rx_rtrl = (-bit_de_stuff) & sample_point & rx idl &
(bit_cnt == 10);
assign gorxide = (-bit_de_stuff) & sample_point & rxrtrl;
assign go_rx_id2 = (-bit_de_stuff) & sample_point & rx ide &
sampled_bit;
assign go_rx_rtr2 = (-bit_de_stuff) & sample_point & rx id2 &
(bit_cnt == 17);
assign go_rx_rl = (-bit_de_stuff) & sample_point & rx_rtr2;
assign go_rx_rO = (-bit_de_stuff) & sample_point & (rx ide &
(-sampledbit) | rx_rl);
assign gorx_dlc = (-bit_de_stuff) & sample_point & rx_rO;
assign go_rx_data = (-bit_de_stuff) & sample_point & rxdlc &
(bit_cnt == 3) & (sampled_bit | (Idata_len[2:0])) & (-remote_rq);
assign go_rx_crc = (-bit_de_stuff) & sample_point & (rx_dlc &
(bit_cnt == 3) & ((-sampled_bit) & (-(Idata_len[2:0])) I remote_rq)
rxdata &
(bit_cnt == ((limited_data_len<<3) - l'bl)));
assign go_rx_crc_lim = (-bit_de_stuff) & sample_point & rx_crc &
(bit_cnt == 14);
assign go_rx_ack = sample_point & rx_crc_lim;
assign go_rx_ack_lim = sample_point & rx_ack;
assign go_rx_eof = sample_point & rx_ack_lim;
assign go_rx_inter = ((sample_point & rx_eof &
(eof_cnt == 6)) | errorframe_ended | overload_frame_ended) &
(-overload_needed);
assign ack_err = rx_ack & sample_point & sampled_bit & tx_state &
(-selftestmode);
124
assign bit_err = (tx_state | errorframe | overloadframe | rxack) &
sample_point & (tx != sampled_bit) & (-bit_err_excl) & (-bit_err_exc2)
& (-bit_err_exc3) & (-bit_err_exc4) & (-bit_errexc5);
assign bit_err_excl = tx_state & arbitration_field & tx;
assign bit_err_exc2 = rx_ack & tx;
assign bit_err_exc3 = error_frame & node_error_passive & (error_cntl <
7);
assign bit_err_exc4 = (error_frame & (error_cntl == 7) &
(-enable_error_cnt2)) (overload_frame & (overloadcntl == 7) &
(-enable_overload_cnt2));
assign biterr_exc5 = (error_frame & (error_cnt2 == 7))
(overload_frame & (overload_cnt2 == 7));
// Rx idle state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxidle <= l'bO;
else if (resetmode | go_rxidl | error_frame)
rx_idle <=#Tp l'bO;
else if (gorx_idle)
rxidle <=#Tp l'bl;
end
// Rx idl state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxidl <= l'bO;
else if (resetmode | go_rxrtrl | error_frame)
rx_idl <=#Tp l'bO;
else if (go_rx_idl)
rxidl <=#Tp l'bl;
end
// Rx rtrl state
always @ (posedge clk or posedge rst)
begin
if (rst)
rx rtrl <= l'bO;
else if (resetmode | gorxide | error_frame)
rxrtrl <=#Tp l'bO;
else if (go_rx_rtrl)
rxrtrl <=#Tp l'bl;
end
// Rx ide state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxide <= l'bO;
else if (reset mode | gorxrO | go rx id2 | errorframe)
125
rxide <=#Tp l'bO;
else if (go_rxide)
rxide <=#Tp l'bl;
end
// Rx id2 state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxid2 <= l'bO;
else if (reset_mode | go_rx_rtr2 I error_frame)
rxid2 <=#Tp l'bO;
else if (go_rxid2)
rxid2 <=#Tp l'bl;
end
// Rx rtr2 state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxrtr2 <= l'bO;
else if (reset_mode | go_rx_rl | errorframe)
rx_rtr2 <=#Tp l'bO;
else if (gorxrtr2)
rx_rtr2 <=#Tp l'bl;
end
// Rx rO state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxrl <= l'bO;
else if (reset_mode | go_rx_rO | errorframe)
rx_rl <=#Tp l'bO;
else if (go_rxrl)
rxrl <=#Tp l'bl;
end
// Rx rO state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxrO <= l'bO;
else if (reset_mode | go_rx_dlc | error_frame)
rx_r0 <=#Tp l'bO;
else if (go_rx_rO)
rxr0 <=#Tp l'bl;
end
// Rx dlc state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxdlc <= l'bO;
else if (reset_mode I go_rx_data | gorxcrc | error_frame)
rxdlc <=#Tp l'bO;
else if (go_rxdlc)
126
rx_dlc <=#Tp l'bl;
end
// Rx data state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxdata <= l'bO;
else if (resetmode | go_rxcrc | error_frame)
rxdata <=#Tp l'bO;
else if (go_rxdata)
rx_data <=#Tp l'bl;
end
// Rx crc state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxcrc <= l'bO;
else if (resetmode | go_rx_crc_lim errorframe)
rx_crc <=#Tp l'bO;
else if (gorx_crc)
rx_crc <=#Tp l'bl;
end
// Rx ack state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxack <= l'bO;
else if (resetmode | gorxacklim error_frame)
rx_ack <=#Tp l'bO;
else if (gorxack)
rx_ack <=#Tp l'bl;
end
127
// Rx eof state
always @ (posedge clk or posedge rst)
begin
if (rst)
rxeof <= l'bO;
else if (gorxinter | error_frame go_overload_frame)
rx eof <=#Tp l'bO;
else if (go_rx_eof)
rxeof <=#Tp l'bl;
end
// Interframe space
always @ (posedge clk or posedge rst)
begin
if (rst)
rxinter <= l'bO;
else if (resetmode | gorx idle | gorx_idl go_overload_frame
go_error_frame)
rx_inter <=#Tp l'bO;
else if (go_rxinter)
rx_inter <=#Tp l'bl;
end
// ID register
always @ (posedge clk or posedge rst)
begin
if (rst)
id <= 0;
else if (sample_point & (rx_idl | rxid2) & (-bit de stuff))
id <=#Tp {id[27:0], sampled_bit};
end
// rtrl bit
always @ (posedge clk or posedge rst)
begin
if (rst)
rtrl <= 0;
else if (sample_point & rx_rtrl & (-bit_de_stuff))
rtrl <=#Tp sampled_bit;
end
// rtr2 bit
always @ (posedge clk or posedge rst)
begin
if (rst)
rtr2 <= 0;
else if (sample_point & rx_rtr2 & (-bit_de_stuff))
rtr2 <=#Tp sampledbit;
end
// ide bit
always @ (posedge clk or posedge rst)
begin
if (rst)
ide <= 0;
else if (sample_point & rx_ide & (-bitde_stuff))
ide <=#Tp sampled bit;
128
end
// Data length
always @ (posedge clk or posedge rst)
begin
if (rst)
datalen <= 0;
else if (sample_point & rx_dlc & (-bit_de_stuff))
data_len <=#Tp {data_len[2:0], sampled_bit};
end
// Data
always @ (posedge clk or posedge rst)
begin
if (rst)
tmp_data <= 0;
else if (sample_point & rx_data & (-bitde_stuff))
tmp_data <=#Tp {tmp_data[6:0], sampled_bit};
end
// CRC
always @ (posedge clk or posedge rst)
begin
if (rst)
crc_in <= 0;
else if (sample_point & rx_crc & (-bit_de_stuff))
crc_in <=#Tp {crc_in[13:0], sampledbit};
end
// bitcnt
always @ (posedge clk or posedge rst)
129
begin
if (rst)
bitcnt <= 0;
else if (go_rx_idl go_rx_id2 | go_rxdlc | go_rx_data I go_rx_crc
go_rx_ack | go_rx_eof gorx_inter goerrorframe
go_overload_frame)
bit cnt <=#Tp 0;
else if (sample_point & (-bit_de_stuff))
bitcnt <=#Tp bitcnt + l'bl;
end
// eofcnt
always @ (posedge clk or posedge rst)
begin
if (rst)
eofcnt <= 0;
else if (sample_point)
begin
if (reset_mode | go_rx_inter go_error_frame
go_overload_frame)
eof_cnt <=#Tp 0;
else if (rx_eof)
eof_cnt <=#Tp eof_cnt + l'bl;
end
end
130
begin
if (rst)
bitstuffcnt tx en <= l'bO;
else if (bitdestuff_set & transmitting)
bitstuffcnttxen <=#Tp l'bl;
else if (bitde stuffreset)
bit stuffcnttx_en <=#Tp l'bO;
end
// bit stuffcnttx
always @ (posedge clk or posedge rst)
begin
if (rst)
bit stuff cnt tx <= 1;
else if (bit de stuff_reset)
bitstuff_cnttx <=#Tp 1;
else if (tx_point_q & bit_stuff_cnt_en)
begin
if (bitstuffcnt_tx == 5)
bitstuff_cnt_tx <=#Tp 1;
else if (tx == tx_q)
bit_stuff_cnt_tx <=#Tp bit_stuff_cnt_tx + l'bl;
else
bit_stuff_cnt_tx <=#Tp 1;
end
end
// stuff err
assign stuff_err = sample_point & bit_stuff_cnt_en & bitde_stuff &
(sampled_bit == sampled_bit_q);
131
crcerr <=#Tp crcin != calculated_crc;
else if (reset mode errorframeended)
crcerr <=#Tp l'bO;
end
132
begin
if (rst)
rule3_excl 2 <= l'bO;
else if (reset_mode | error_flag_over)
rule3_excl_2 <=#Tp l'bO;
else if (rule3_excl_l)
rule3_excl_2 <=#Tp l'bl;
else if ((error_cntl < 7) & sample_point & (-sampled_bit))
rule3_excl_2 <=#Tp l'bO;
end
133
canacf icanacf
.clk(clk),
.rst(rst),
.id(id),
/* Mode register */
.reset_mode(reset_mode),
.acceptance_filter_mode(acceptance_filter_mode),
.gorxcrc_lim(gorxcrc_lim),
.go_rx_inter(go_rx_inter),
.go_error_frame(go_error_frame),
.dataO(tmp_fifo[0]),
.datal(tmp_fifo[l]),
*rtrl(rtrl),
.rtr2(rtr2),
.ide(ide),
.no_byteO(no_byteO),
.no_bytel(no_bytel),
.id_ok(id_ok)
134
assign err = form_err | stuff_err | bit err ack err
form_err latched stufferr_latched | bit_err_latched
ack_err_latched X crc_err;
135
3'hl : dataforfifo <= id[28:21];
3'h2 : dataforfifo <= id[20:13];
3'h3 : dataforfifo <= id[12:5];
3'h4 : data_for_fifo <= {id[4:0], 3'hO};
default: data_forfifo <= 0;
endcase
end
else // standard format
begin
case (header_cnt) // synthesis parallel_case
3'h0 : data_for_fifo <= {l'bO, rtrl, 2'h0, data_len};
3'hl : dataforfifo <= id[10:3];
3'h2 : data_for_fifo <= {id[2:0], 5'hO};
default: data_for fifo <= 0;
endcase
end
end
else // normal mode
begin
case (header_cnt) // synthesis parallel_case
3'h0 : dataforfifo <= id[10:3];
3'hl : data_for_fifo <= {id[2:0], rtrl, data_len};
default: data_for_fifo <= 0;
endcase
end
end
else
data_for_fifo <= tmp_fifo[data_cnt-header_len];
end
.wr(wr_fifo),
.datain(data forfifo),
.addr(addr),
.dataout(dataout),
.fifoselected(fifoselected),
.resetmode(resetmode),
.release_buffer(release_buffer),
.extendedmode(extendedmode),
.overrun(overrun),
.info_empty(info_empty),
.info_cnt(rx_message_counter)
);
136
errorframe <=#Tp l'bO;
else if (go_error_frame)
errorframe <=#Tp l'bl;
end
137
end
// passive_cnt
always @ (posedge clk or posedge rst)
begin
if (rst)
passive_cnt <= 0;
else if (reset_mode | error_frame_ended I go_error_frame
go_overload_frame)
passive_cnt <=#Tp 0;
else if (sample_point & (passive_cnt < 5))
begin
if (error_frame_q & (-enable_error_cnt2) & (sampled_bit ==
sampled_bit_q))
passive_cnt <=#Tp passive_cnt + l'bl;
else
passivecnt <=#Tp 0;
end
end
138
always @ (posedge clk or posedge rst)
begin
if (rst)
enableoverloadcnt2 <= l'bO;
else if (reset_mode | overload frame_ended | go_error_frame
go_overload_frame)
enable_overload cnt2 <=#Tp l'bO;
else if (overload_frame & (overload_flag_over & sampledbit))
enableoverload cnt2 <=#Tp l'bl;
end
139
tx <=#Tp l'bl;
end
else if (error_frame)
// Transmitting error frame
begin
if (error_cntl < 6)
begin
if (node_error_passive)
tx <=#Tp l'bl;
else
tx <=#Tp l'bO;
end
else
tx <=#Tp l'bl;
end
else
tx <=#Tp l'bl;
end
end
/* Delayed tx point */
always @ (posedge clk)
begin
tx_point_q <=#Tp tx_point;
end
140
assign extendedchain_std = {r_tx_data_0[7:4], 2'hO, r_tx_data_O[1],
r_txdata_2[2:0], r_tx_data_l[7:0], l'bO};
assign extended_chain_ext = {rtx_data_0[7:4], 2'hO, r_tx_data_0[l],
r_txdata_4[4:0], rtxdata_3[7:0], rtxdata_2[7:3], l'bl, l'bl,
r_txdata_2[2:0], r_txdata_l[7:0], l'bO};
assign extended_chain_data = {rtxdata_12, rtxdata_11, rtxdata_10,
r_tx data_9, r tx data_8, r tx data_7, rtxdata_6, rtxdata_5};
141
tx_data_l[3:0] - 1)) I // data
~~( ~ tx_point & rx_crc_lim
) // crc
(go_rxidle
) | // at the end
(reset_mode
) I
(overload_frame
) I
(error_frame
) i
// Tx state
always @ (posedge clk or posedge rst)
begin
if (rst)
142
txstate <= l'bO;
else if (resetmode | gorx_inter | error_frame | arbitrationlost)
txstate <=#Tp l'bO;
else if (go_tx)
txstate <=#Tp l'bl;
end
// Node is a transmitter
always @ (posedge clk or posedge rst)
begin
if (rst)
transmitter <= l'bO;
else if (go_tx)
transmitter <=#Tp l'bl;
else if (reset_mode | gorxinter)
transmitter <=#Tp l'bO;
end
143
if (rst)
susp_cnt <= 0;
else if (reset_mode | (sample_point & (susp_cnt == 7)))
susp_cnt <=#Tp 0;
else if (susp_cnt_en & sample_point)
susp_cnt <=#Tp susp_cnt + l'bl;
end
144
begin
if (rst)
arbitration_lost_capture <= 5'h0;
else if (read_arbitration_lost_capture_reg)
arbitration_lost_capture <=#Tp 5'h0;
else if (sample_point & (-arbitration_blocked) & arbitration_cnt_en &
(-bitdestuff))
arbitration_lost_capture <=#Tp arbitration_lost_capture + l'bl;
end
145
begin
if (set_resetmode)
txerr_cnt <=#Tp 127;
else if ((tx_err_cnt > 0) & (tx_successful | busfree))
tx_err_cnt <=#Tp tx_err_cnt - l'hl;
else if (transmitter)
begin
if ( (sample point & (-sampled bit) & (delayed domi nant cnt
== 7) ) I // 6
(go_error_frame & rule5
) I // 4 (rule 5 is the same as rule 4)
(error_flag_over & (-error_flag_over_blocked) &
(-rule3_ex:cl_2) & (-rule3_exc2) ) // 3
txerrcnt <=#Tp txerrcnt 4h8;
tx_errcnt <=#Tp tx err cnt + 4'h8;
end
end
end
146
busfreecnt <= 0;
else if (resetmode)
busfreecnt <=#Tp 0;
else if (sample_point)
begin
if (sampled_bit & bus_free_cnt_en & (bus_free_cnt < 10))
busfreecnt <=#Tp busfreecnt + l'bl;
else
bus_free_cnt <=#Tp 0;
end
end
147
/* Error code capture register */
always ® (posedge clk or posedge rst)
begin
if (rst)
error_capture_code <= 8'hO;
else if (read_error_code_capture_reg)
error_capture_code <=#Tp 8'hO;
else if (set_bus_error_irq)
error_capture_code <=#Tp {error_capture_code_type[7:6],
error_capture_code_direction, error_capture_code_segment[4:0]};
end
endmodule
148
A.4: CRC HDL Code
The following code is the cyclic reducany check (CRC) module. This code
generates the CRC sequence to be transmitted and checks the CRC sequence of incoming
messages.
// cancrc.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/10ps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
parameter Tp = 1;
input clk;
input data;
input enable;
input initialize;
^
assign crc_next = data crc[14];
assign crc_tmp = {crc[13:0], l'bO};
149
if (crc_next)
crc <= #Tp crc_tmp A 15'h4599;
else
crc <= #Tp crc_tmp;
end
end
endmodule
150
A.5: ACF HDL Code
This code is the acceptance filtering component of the CAN controller. The
acceptance filtering mechanism ensures that the host microcontroller in only informed of
// canacf.v
// synopsys translateoff
//'include "timescale.v"
'timescale Ins/l0ps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module canacf
(
clk,
rst,
id,
/* Mode register */
resetmode,
acceptancefilter_mode,
extended_mode,
acceptance_code_0,
acceptance code 1,
acceptancecode_2,
acceptancecode_3,
acceptance mask 0,
acceptance_mask 1,
acceptance_mask_2,
acceptance_mask_3,
gorx_crc_lim,
gorxinter,
151
go_error_frame,
dataO,
datal,
rtrl,
rtr2,
ide,
nobyte0,
nobytel,
idok
);
parameter Tp = 1;
input clk;
input rst;
input [28:0] id;
input reset_mode;
input acceptancefilter_mode;
input extended_mode;
output idok;
reg idok;
wire match;
wire match sf std;
wire matchsf ext;
wire match df std;
wire matchdf ext;
152
acceptance_mask_0[2] &
(id[6] == acceptance_code_0 [3]
acceptance_mask_0[3] &
(id[7] == acceptance_code_0 [4]
acceptance_mask_0[4] &
(id[8] == acceptance_code_0 [5]
acceptance_mask_0[5] &
(id[9] == acceptance_code_0 [6]
acceptance_mask_0[6] &
(id[10] == acceptance_code_0 [7]
acceptance_mask_0[7]
);
// Working in extended mode. ID match for standard format (11-bit ID).
Using single filter.
assign match_sf_std = ( (id[3] == acceptance_code_0[0] I
acceptance_mask_0[0] &
(id[4] == acceptance_code_0[1]
acceptance_mask_0[1] &
(id[5] == acceptance_code_0[2]
acceptance_mask_0[2] &
(id [6] == acceptance_code_0[3]
acceptance_mask_0[3] &
(id[7] == acceptance_code_0[4]
acceptance_mask_0[4] &
(id[8] == acceptance_code_0[5]
acceptance_mask_0[5] &
(id[9] == acceptance_code_0[6]
acceptance_mask_0[6] &
(id[10] == acceptance_code_0[7]|
acceptance_mask_0[7] &
(rtrl == acceptance_code_l[4]
acceptance_mask_l[4] ) &
(id[0] == acceptance_code_l[5]
acceptance_mask_l[5] ) &
(id[l] == acceptance_code_l[6]
acceptance_maskl[6] ) &
(id[2] == acceptance_code_l[7]
acceptance_mask_l[7] ) &
(dataO[0] == acceptance_code_2[0]
acceptance_mask_2[0] no_byte0) &
(dataO[1] == acceptancecode_2[1]
acceptance_mask_2[1] no_byte0) &
(data [2] == acceptancecode_2[2]
acceptance_mask_2[2] no_byte0) &
(dataO [3] == acceptance_code_2[3]
acceptance_mask_2[3] no_byte0) &
(datao [4] == acceptance_code_2[4]
acceptance_mask_2[4] nobyte0) &
(datao [5] == acceptance_code_2[5]
acceptance_mask_2[5] no_byte0) &
(datao [6] == acceptancecode_2[6]
acceptance_mask_2[6] no_byteO) &
(dataO [7] == acceptance_code_2[7]
acceptance_mask_2[7] no_byte0) &
153
(datal[0] == acceptance_code_3 [0]
acceptance_mask_3[0] no_bytel) &
(datal [1] == acceptance_code_3 [1]
acceptance_mask_3[1] nobytel) &
(datal [2] == acceptancecode_3 [2]
acceptance_mask_3[2] nobytel) &
(datal [3] == acceptancecode_3 [3]
acceptance_mask_3[3] no_bytel) &
(datal [4] == acceptance_code_3 [4]
acceptance_mask_3[4] no_bytel) &
(datal [5] == acceptance_code_3 [5]
acceptance_mask_3[5] nobytel) &
(datal [6] == acceptancecode_3 [6]
acceptance_mask_3[6] no_bytel) &
(datal [7] == acceptance_code_3 [7]
acceptance_mask_3[7] no_bytel)
) ;
(id[13] == acceptance_code_l[0]
acceptance_mask_l[0] &
(id[14] == acceptance_code_l[l]
acceptance_mask_l[l] &
(id [15] == acceptance_code_l[2]
acceptance_mask_l[2] &
(id [16] == acceptance_code_l[3]
acceptance_mask_l[3] &
(id [17] == acceptance_code_l[4]
acceptance_mask_l[4] &
(id[18] == acceptance_code_l[5]
acceptance_mask_l[5] &
(id[19] == acceptance_code_l[6]
acceptance_mask_l[6] &
(id[20] == acceptance_code_l[7]
acceptance_mask_l[7] &
(id[5] == acceptance_code_2[0] I
acceptance_mask_2[0] ) &
154
(id[6] =acceptance_code_2[1]
acceptance_mask_2[1] &
(id[7] == acceptance_code_2[2]
acceptance_mask_2[2] &
(id[8] == acceptance_code_2[3]
acceptance_mask_2[3] &
(id[9] == acceptance_code_2[4]
acceptance_mask_2[4] &
(id[10] == acceptance_code_2[5]
acceptance_mask_2[5] &
(id[11] == acceptance_code_2[6]
acceptance_mask_2[6] &
(id[12] == acceptance_code_2[7]
acceptance_mask_2[7] &
(rtr2 == acceptance_code_3[2]
acceptance_mask_3[2] &
(id[0] == acceptance_code_3[3]
acceptance_mask_3[3] &
(id[l] == acceptance_code_3[4]
acceptance_mask_3[4] &
(id[2] == acceptance_code_3[5]
acceptance_mask_3[5] &
(id[3] == acceptance_code_3[6]
acceptance_mask_3[6] &
(id[4] == acceptance_code_3[7]
acceptance_mask_3[7]
) ;
(rtrl == acceptance_code_l[4]
acceptance_mask_l[4] ) &
(id[0] == acceptance_code_l[5]
acceptance_mask_l[5] ) &
(id[l] == acceptance_code_l[6]
acceptance_mask_l[6] ) &
(id[2] == acceptance_code_l[7]
acceptance_mask_l[7] ) &
155
(dataO[0] == acceptance_code_3[0]
acceptance_mask_3[0] no_byte0) &
(dataO[l] == acceptance_code_3[1]
acceptance_mask_3[1] nobyteO) &
(data0[2] == acceptance_code_3[2]
acceptance_mask_3[2] nobyte0) &
(data0[3] == acceptance_code_3[3]
acceptance_mask_3[3] no_byte0) &
(data0[4] == acceptance_code_l[4]
acceptance_mask_l[4] no_byteO) &
(data0[5] == acceptance_code_l[5]
acceptance_mask_l[5] no_byte0) &
(data0[6] == acceptance_code_l[6]
acceptance_mask_l[6] nobyte0) &
(data0[7] == acceptancecode_l[7]
acceptance_mask_l[7] nobyte) )
(rtrl == acceptance_code_3[4]
acceptance_mask_3[4] ) &
(id[0] == acceptance_code_3[5]
acceptance_mask_3[5] ) &
(id[l] == acceptance_code_3[6]
acceptance_mask_3[6] ) &
(id[2] == acceptance_code_3[7]
acceptance_mask_3[7] ) )
);
// Working in extended mode. ID match for extended format (29-bit ID).
Using double filter.
assign match_df_ext = (((id[21] == acceptance_code_0[0]
acceptance_mask_0[0] ) &
(id[22] == acceptance_code_0[1]
acceptance_mask_0[1] ) &
(id[23] == acceptance_code_0[2]
acceptance_mask_0[2] ) &
(id[24] == acceptance_code_0[3]
acceptance_mask_0[3] ) &
(id[25] == acceptance_code_0[4]
acceptance_mask_0[4] ) &
156
(id[26] == acceptance_code_0[5]
acceptance_mask_0[5] &
(id[27] == acceptance_code_0[6]
acceptance_mask_0[6] &
(id[28] == acceptance_code_0[7]
acceptance_mask_0[7] &
(id[13] == acceptance_code_l[0]
acceptance_mask_l[0] &
(id[14] == acceptance_code_l[1]
acceptance_mask_l[1] &
(id[15] == acceptance_code_l[2]
acceptance_mask_l[2] &
(id[16] == acceptance_code_l[3]
acceptance_mask_l[3] &
(id[17] == acceptance_code_l[4]
acceptance_mask_l[4] &
(id[18] == acceptance_code_l[5]
acceptance_mask_l[5] &
(id[19] == acceptance_code_l[6]
acceptance_mask_l[6] &
(id[20] == acceptance_code_l[7]
acceptance_mask_l[7] )
((id[21] == acceptance_code_2[0]
acceptance_mask_2[0] &
(id[22] == acceptance_code_2[1]
acceptance_mask_2[1] &
(id[23] == acceptance_code_2[2]
acceptance_mask_2[2] &
(id[24] == acceptance_code_2[3]
acceptance_mask_2[3] &
(id[25] == acceptance_code_2[4]
acceptance_mask_2[4] &
(id[26] == acceptance_code_2[5]
acceptance_mask_2[5] &
(id[27] == acceptance_code_2[6]
acceptance_mask_2[6] &
(id[28] == acceptance_code_2[7]
acceptance_mask_2[7] &
(id[13] == acceptance_code_3[0]
acceptance_mask_3[0] &
(id[14] == acceptance_code_3[1]
acceptance_mask_3[1] &
(id[15] == acceptance_code_3[2]
acceptance_mask_3[2] &
(id[16] == acceptance_code_3[3]
acceptance_mask_3[3] &
(id[17] == acceptance_code_3[4]
acceptance_mask_3[4] &
(id[18] == acceptance_code_3[5]
acceptance_mask_3[5] &
(id[19] == acceptance_code_3[6]
acceptance_mask_3[6] &
157
(id[20] == acceptance_code_3[7] |
acceptancemask_3[7] ) )
// ID ok signal generation
always @ (posedge clk or posedge rst)
begin
if (rst)
idok <= 0;
else if (go_rx_crc_lim) // sample_point is already
included in go_rx_crc_lim
begin
if (extended_mode)
begin
if (acceptance_filtermode) // dual filter
begin
if (ide) // extended frame message
id_ok <=#Tp matchdfext;
else // standard frame message
id_ok <=#Tp matchdf_std;
end
else // single filter
begin
if (ide) // extended frame message
idok <=#Tp matchsfext;
else // standard frame message
id_ok <=#Tp match_sf_std;
end
end
else
idok <=#Tp match;
end
else if (reset_mode | go_rx_inter | goerror_frame) //
sample_point is already included in go_rxinter
idok <=#Tp 0;
end
endmodule
158
A.6: Receive FIFO HDL Code
The following code is the receive FIFO of the CAN controller. This is where the
messages are stored until they are read by the host microcontroller.
// can fifo.v
// synopsys translateoff
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPA RAM
parameter Tp = 1;
input clk;
input rst;
input wr;
input [7:0] datain;
input [7:0] addr;
input reset mode;
input releasebuffer;
159
input extended_mode;
input fifo_selected;
'ifdef ACTELAPARAM
'else
'ifdef XILINXRAM
'else
reg [7:0] fifo [0:63];
reg [3:0] lengthfifo[0:63];
reg overruninfo[0:63];
endif
'endif
// length counter
always @ (posedge clk or posedge rst)
begin
if (rst)
lencnt <= 0;
else if (reset_mode | write_length_info)
len_cnt <=#Tp l'bO;
else if (wr & (-fifo_full))
len_cnt <=#Tp lencnt + l'bl;
160
end
// wr_info_pointer
always ® (posedge clk or posedge rst)
begin
if (rst)
wr_info_pointer <= 0;
else if (reset_mode)
wr_info_pointer <=#Tp 0;
else if (write_length_info & (-info_full))
wr_info_pointer <=#Tp wr_info_pointer + l'bl;
end
// rd_info_pointer
always @ (posedge clk or posedge rst)
begin
if (rst)
rd_info_pointer <= 0;
else if (reset_mode)
rd_info_pointer <=#Tp 0;
else if (release_buffer & (-fifo_empty))
rd_info_pointer <=#Tp rd_info_pointer + l'bl;
end
// rd_pointer
always @ (posedge clk or posedge rst)
begin
if (rst)
rd_pointer <= 0;
else if (release_buffer & (-fifo_empty))
rd_pointer <=#Tp rd_pointer + length_info;
else if (resetmode)
rd_pointer <=#Tp 0;
end
// wr_pointer
always @ (posedge clk or posedge rst)
begin
if (rst)
wr_pointer <= 0;
else if (wr & (-fifofull))
wrpointer <=#Tp wr_pointer + l'bl;
else if (resetmode)
wr_pointer <=#Tp 0;
end
// latch overrun
always @ (posedge clk or posedge rst)
begin
if (rst)
latchoverrun <= 0;
else if (reset_mode | write_length_info)
latch_overrun <=#Tp 0;
else if (wr & fifo full)
latch_overrun <=#Tp l'bl;
end
161
// Counting data in fifo
always @ (posedge clk or posedge rst)
begin
if (rst)
fifocnt <= 0;
else if (wr & (-release_buffer) & (-fifo_full))
fifocnt <=#Tp fifo_cnt + l'bl;
else if ((-wr) & release_buffer & (-fifo_empty))
fifo_cnt <=#Tp fifo_cnt - length_info;
else if (wr & release_buffer & (-fifo full) & (-fifo_empty))
fifo_cnt <=#Tp fifo_cnt - length_info + l'bl;
else if (reset_mode)
fifo_cnt <=#Tp 0;
end
// Selecting which address will be used for reading data from rx fifo
always @ (extended_mode or rdpointer or addr)
begin
if (extended_mode) // extended mode
begin
read_address <= rd_pointer + (addr - 8'd16);
end
else // normal mode
begin
read_address <= rd_pointer + (addr - 8'd20);
end
end
162
.WRB (-(wr & (-fifo_full))),
.RDB (-fifo_selected),
.WADDR (wr_pointer),
.RADDR (readaddress)
);
actel_ram_64x4_sync info_fifo
(
.DO (length_info),
.RCLOCK (clk),
.WCLOCK (clk),
.DI (len_cnt),
.PO ), // parity not used
.WRB (-(write_length_info & (-info_full))),
.RDB (l'bO), // always enabled
.WADDR (wr_info_pointer),
.RADDR (rd_info_pointer)
);
actel_ram_64xl_sync overrun_fifo
(
.DO (overrun),
.RCLOCK (clk),
.WCLOCK (clk),
.DI (latch_overrun I (wr & fifofull)),
.PO ), // parity not used
.WRB (-(write_length_info & (-info_full))),
.RDB (l'bO), // always enabled
.WADDR (wr_info_pointer),
.RADDR (rd_info_pointer)
);
'else
'ifdef XILINX RAM
/*
ram_64x8_sync fifo
(
.addra(wr_pointer),
.addrb(read_address),
.clka(clk),
.clkb(clk),
.dina(datain),
.doutb(data_out),
.wea(wr & (-fifo_full))
);
*/
RAMB4_S8_S8 fifo
(
.DOA(),
.DOB(data_out),
.ADDRA({3'hO, wr_pointer)),
.CLKA(clk),
.DIA(data_in),
.ENA(l'bl),
.RSTA(l'bO),
163
.WEA(wr & (-fifo_full)),
.ADDRB({3'hO, read_address}),
.CLKB(clk),
.DIB(8'hO),
.ENB(l'bl),
.RSTB(l'bO),
.WEB(l'bO)
);
ram_64x4_sync info_fifo
.addra(wr_infoointer),
.addra(wr_info_pointer),
.clkaddrb(rd_info_pointer)clk),
.clka(clk),
.clkb(clk),
.dina(len_cnt),
.doutb(length_info),
.wea(write_length_info & (~info_full))
) ;
RAMB4_S4_S4 infofifo
(
.DOA(),
.DOB(length_info),
.ADDRA({4'hO, wr_info_pointer}),
.CLKA(clk),
.DIA(len_cnt),
.ENA(l'bl),
.RSTA(l'bO),
.WEA(write_length_info & (-info_full)),
.ADDRB({4'hO, rd_info_pointer}),
.CLKB(clk),
.DIB(4'hO),
.ENB(l'bl),
.RSTB(l'bO),
.WEB(l'bO)
);
ram_64xl_sync overrun_fifo
addra(wrinfoointer),
.addra(wr_info_pointer),
.addrb(rd_info_pointer),
.clka(clk),
.clkb(clk),
.dina(latch_overrun I (wr & fifofull)),
.doutb(overrun),
.wea(write_length_info & (-info_full))
);*/
RAMB4 S1 Sl overrun fifo
.DOA(),
.DOB(overrun),
.ADDRA((6'hO, wr_info_pointer}),
.CLKA(clk),
164
.DIA(latch_overrun I (wr & fifo_full)),
.ENA(l'bl),
.RSTA(l'bO),
.WEA(write_length_info & (-info_full)),
.ADDRB({6'hO, rd_info_pointer}),
.CLKB(clk),
.DIB(l'hO),
.ENB(l'bl),
.RSTB(l'bO),
.WEB(l'bO)
);
'else
// writing data to fifo
always @ (posedge clk)
begin
if (wr & (-fifo_full))
fifo[wr_pointer] <=#Tp data_in;
end
// writing length_fifo
always @ (posedge clk)
begin
if (write_length_info & (-info_full))
length_fifo[wr_info_pointer] <=#Tp len_cnt;
end
// reading length_fifo
assign length_info = length_fifo[rd_info_pointer];
// overrun info
always @ (posedge clk)
begin
if (write_lengthinfo & (-info_full))
overrun_info[wr_info_pointer] <=#Tp latch_overrun | (wr &
fifofull);
end
// reading overrun
assign overrun = overrun_info[rd_info_pointer];
'endif
endif
endmodule
165
A.7: Register Set HDL Code
The following sections of code define the register set within the CAN controller
// can_registers.v
// synopsys translateoff
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_registers
(
clk,
rst,
cs,
we,
addr,
data_in,
dataout,
irq,
sample_point,
transmitting,
setresetmode,
node_bus_off,
error_status,
rxerr_cnt,
txerrcnt,
transmitstatus,
receive_status,
tx_successful,
needtotx,
overrun,
info_empty,
set_bus_error_irq,
set_arbitration_lost_irq,
arbitration_lost_capture,
node_error_passive,
166
node_error_active,
rx_message_counter,
/* Mode register */
reset_mode,
listen_only_mode,
acceptance_filter_mode,
self test mode,
/* Command register */
clear_data_overrun,
release_buffer,
abort_tx,
tx_request,
self_rxrequest,
single shottransmission,
167
acceptance_code_3,
parameter Tp = 1;
input clk;
input rst;
input cs;
input we;
input [7:0] addr;
input [7:0] data_in;
output irq;
input sample_point;
input transmitting;
input set_reset_mode;
input node_bus_off;
input error_status;
input [7:0] rx_err_cnt;
input [7:0] tx_err_cnt;
input transmit_status;
input receive_status;
input tx_successful;
input need_totx;
input overrun;
input info_empty;
input set_bus_error_irq;
input set_arbitration_lost_irq;
input [4:0] arbitration_lost_capture;
input node_error_passive;
168
input node_error_active;
input [6:0] rx_message_counter;
/* Mode register */
output reset_mode;
output listen_only_mode;
output acceptance_filter_mode;
output self_test_mode;
/* Command register */
output clear_data_overrun;
output release_buffer;
output abort_tx;
output tx_request;
output selfrx_request;
output single_shot_transmission;
169
output [7:0] acceptance_code_2;
output [7:0] acceptance_code_3;
/* Acceptance mask register */
output [7:0] acceptance_mask_l;
output [7:0] acceptance_mask_2;
output [7:0] acceptancemask_3;
reg tx_successful_q;
reg overrun_q;
reg overrun_status;
reg transmissioncomplete;
reg transmit_buffer_status_q;
reg receive_bufferstatus;
reg info_empty_q;
reg error_status_q;
reg node_bus_off_q;
reg node_error_passive_q;
reg transmitbufferstatus;
reg singleshot_transmission;
// Some interrupts exist in basic mode and in extended mode. Since they
are in different registers they need to be multiplexed.
wire data_overrun_irqen;
wire error_warning_irq_en;
wire transmit_irq_en;
wire receive_irq_en;
170
wire read = cs & (-we);
wire read_irq_reg = read & (addr == 8'd3);
assign read_arbitration_lost_capture_reg = read & extended_mode & (addr
== 8'dll);
assign read_error_code_capture_reg = read & extended_mode & (addr ==
8'd12);
171
assign wetxerrcnt = cs & we & (addr == 8'd15) &
reset_mode & extended_mode;
wire we_acceptance_code_l = cs & we & (addr == 8'd17) &
reset_mode & extended_mode;
wire we_acceptance_code_2 = cs & we & (addr == 8'd18) &
reset_mode & extended_mode;
wire we_acceptance_code_3 = cs & we & (addr == 8'd19) &
reset_mode & extendedmode;
wire we_acceptance_mask_l = cs & we & (addr == 8'd21) &
reset_mode & extended_mode;
wire we_acceptance_mask_2 = cs & we & (addr == 8'd22) &
reset_mode & extended_mode;
wire we_acceptance_mask_3 = cs & we & (addr == 8'd23) &
resetmode & extendedmode;
/* End: This section is for EXTENDED mode */
/* Mode register */
wire [0:0] mode;
wire [4:1] modebasic;
wire [3:1] modeext;
wire receiveirqenbasic;
wire transmit_irq_en_basic;
wire errorirqen_basic;
wire overrun_irq_en_basic;
172
.clk(clk),
.rst(rst));
assign reset_mode = mode[0];
assign listen_only_mode = extendedmode & modeext[1];
assign selftest_mode = extendedmode & modeext[2];
assign acceptance_filter_mode = extendedmode & modeext[3];
/* Command register */
wire [4:0] command;
can_register_asyn_syn #(1, l'hO) COMMAND_REGO
.datain(datain[0]),
.dataout(command[0]),
.we(we_command),
.clk(clk),
.rst(rst),
.rst_sync(tx_request & sample_point)
);
173
always @ (posedge clk or posedge rst)
begin
if (rst)
single_shot_transmission <= l'bO;
else if (we_command & data_in[l] & (data_in[l] datain[4]))
single_shot_transmission <=#Tp l'bl;
else if (tx_successful & (-txsuccessful_q))
single_shot_transmission <=#Tp l'bO;
end
/* Status register */
174
if (rst)
receivebufferstatus <= 1'bO;
else if (release_buffer)
receive_buffer_status <=#Tp l'bO;
else if (-info_empty)
receive_buffer_status <=#Tp l'bl;
end
175
assign time_segmentl = bus_timing_l[3:0];
assign time_segment2 = bus_timing_l[6:4];
assign triple_sampling = bus_timing_l[7];
/* End Bus Timing 1 register */
always @ (cd)
begin
case (cd) // synopsys_full_case
synopsys_paralel_case
3'b000 : clkout div <= 0;
176
3'bOO1 : clkoutdiv <= 1;
3'bOO1 : clkoutdiv <= 2;
3'bO11 clkoutdiv <= 3;
3'b1lOO clkoutdiv <= 4;
3'blO1 : clkoutdiv <= 5;
3'bllO : clkoutdiv <= 6;
3'blll : clkout div <= 0;
endcase
end
/a
//always @ (cd or clkout_tmp or clock_off)
always @ (cd or clkout_tmp or clock_off)
begin
if (clock_off)
clkout <=#Tp l'bl;
// else if (&cd)
// clkout <=#Tp clk;
else
clkout <=#Tp clkout_tmp;
end
*/
assign clkout = clock_off ? l'bl : ((&cd)? clk : clkout_tmp);
177
( .data_in(data_in),
.data_out(acceptance_mask_0),
.we(we_acceptance_mask_0),
.clk(clk)
);
/* End: Acceptance mask register */
/* End: This section is for BASIC and EXTENDED mode */
/* Tx data 0 register. */
can_register #(8) TX_DATAREGO
(.data_in(data_in),
.data_out(tx_data_0),
.we(we_tx data 0),
.clk(clk)
/ End: Tx data register.
/* End:
Tx data 1 register. */
/* Tx data 1 register. */
can_register #(8) TX_DATA_REG1
(.data_in(data_in),
.dataout(txdata_l),
.we(we_txdata_l),
.clk(clk)
End: Tx data 1 register.
/* End:
Tx data register.
1 */
/* Tx data 2 register. */
canregister #(8) TXDATAREG2
(.data_in(data_in),
.data_out(tx_data_2),
.we(we_txdata_2),
.clk(clk)
/* Tx data 3 register. */
can_register #(8) TX_DATA_REG3
.datain(data_in),
.data_out(tx_data_3),
.we(we_txdata_3),
.clk(clk)
End: Tx data 3 register.
/* End:
Tx data register.
3 */
/* Tx data 4 register. */
can_register #(8) TX_DATA_REG4
(.data_in(data_in),
.data_out(tx_data_4),
.we(we_txdata_4),
.clk(clk)
/ End: Tx data 4 register.
/* End:
Tx data register.
4 */
/* Tx data 5 register. */
can_register #(8) TX_DATA_REG5
(.data_in(data_in),
.data_out(tx_data_5),
178
.we(wetxdata_5),
.clk(clk));
/* End: Tx data 5 register. */
/* Tx data 6 register. */
can_register #(8) TX_DATA_REG6
(.data_in(data_in),
.dataout(txdata 6),
.we(we_txdata_6),
.clk(clk)
/* End: Tx data 6 register.
/* End:data
Tx register.
6 */
/* Tx data 7 register. */
can_register #(8) TXDATA_REG7
(.data_in(data_in),
.data_out(tx_data_7),
.we(we_txdata_7),
.clk(clk)
);
/* End: Tx data 7 register. */
/* Tx data 8 register. */
can_register #(8) TX_DATA_REG8
(.data_in(data_in),
.data_out(tx_data_8),
.we(wetx data_8),
.clk(clk)
);
/* End: Tx data 8 register. */
/* Tx data 9 register. */
can_register #(8) TX_DATA_REG9
(.data_in(data_in),
.data_out(tx_data_9),
.we(wetxdata_9),
.clk(clk)
End: Tx data 9 register.
/* End:
Tx data register.
9 */
/* Tx data 10 register. */
can_register #(8) TX_DATA_REG10
(.data_in(data_in),
.dataout(txdata_10),
.we(we_txdata_10),
.clk(clk)
);
/* End: Tx data 10 register. */
/* Tx data 11 register. */
canregister #(8) TX_DATA_REG11
(.data_in(data_in),
.data_out(tx_data_11),
.we(we_txdata_11),
.clk(clk)
/ End: Tx data 11 register.
/* End: Tx data 11 register. */
179
/* Tx data 12 register. */
can_register #(8) TX_DATA_REG12
( .data_in(datain),
.data_out(txdata_12),
.we(we_txdata_12),
.clk(clk)
) ;
/* End: Tx data 12 register. */
180
/* Acceptance mask register 3 */
can_register #(8) ACCEPTANCE_MASK_REG3
(.data_in(data_in),
.data_out(acceptance_mask_3),
.we(we_acceptance_mask_3),
.clk(clk)
);
/* End: Acceptance code register */
181
8'd26 data_out_tmp 8'hO;
8'd27 data_outtmp 8'hO;
8'd28 data_out_tmp 8'h0;
8'd29 data_out_tmp {l'bO, rx_message_counter};
8'd31 dataouttmp clock divider;
// Some interrupts exist in basic mode and in extended mode. Since they
are in different registers they need to be multiplexed.
assign data_overrun_irq_en = extended_mode ? data_overrun_irq_en_ext
: overrunirqen_basic;
assign error_warning_irq_en = extended_mode ? error_warning_irq_en_ext
: error_irq_en_basic;
182
assign transmit_irq_en = extended_mode ? transmit_irq_en_ext
: transmit_irq_en_basic;
assign receive_irq_en = extended_mode ? receive_irq_enext
: receive_irq_en_basic;
reg data_overrun_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
data_overrun_irq <= l'bO;
else if (overrun & (-overrun_q) & data_overrun_irq_en)
data_overrun_irq <=#Tp l'bl;
else if (read_irq_reg)
data_overrun_irq <=#Tp l'bO;
end
reg transmit_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
transmit_irq <= l'bO;
else if (transmit_buffer_status & (-transmit_buffer_status_q) &
transmit_irq_en)
transmit_irq <=#Tp l'bl;
else if (read_irq_reg)
transmit_irq <=#Tp l'bO;
end
reg receive_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
receive_irq <= l'bO;
else if (release_buffer)
receive_irq <=#Tp l'bO;
else if ((-info_empty) & (-receive_irq) & receive_irq_en)
receive_irq <=#Tp l'bl;
end
reg error_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
error_irq <= l'bO;
else if (((error_status ^ error_status_q) | (node_bus_off
node_bus_off_q)) & error_warning_irq_en)
error_irq <=#Tp l'bl;
else if (read_irq_reg)
error_irq <=#Tp l'bO;
end
reg bus_error_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
bus_error_irq <= l'bO;
else if (set_bus_error_irq & bus_error_irq_en)
183
bus_error_irq <=#Tp l'bl;
else if (read_irq_reg)
bus_error_irq <=#Tp l'bO;
end
reg arbitration_lost_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
arbitration_lost_irq <= l'bO;
else if (set_arbitration_lost_irq & arbitration_lost_irq_en)
arbitration_lost_irq <=#Tp l'bl;
else if (read_irq_reg)
arbitration_lost_irq <=#Tp l'bO;
end
reg error_passive_irq;
always @ (posedge clk or posedge rst)
begin
if (rst)
error_passive_irq <= l'bO;
else if ((node_error_passive & (-node_error_passive_q)
(-node_error_passive) & node_error_passive_q & node_error_active) &
error_passive_irq_en)
error_passive_irq <=#Tp l'bl;
else if (read_irq_reg)
error_passive_irq <=#Tp l'bO;
end
endmodule
184
// can_register.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translate_on
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_register
( data_in,
dataout,
we,
clk
endmodule
185
// can_register_asyn.v
// synopsys translateoff
//'include "timescale.v"
'timescale ins/l0ps
// synopsys translate_on
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPA_RAM
module can_register_asyn
(datain,
dataout,
we,
clk,
rst
)i
endmodule
186
// can_register_asynsyn.v
// synopsys translateoff
//'include "timescale.v"
'timescale ins/l0ps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_register_asyn_syn
(data_in,
dataout,
we,
clk,
rst,
rst_sync
);
endmodule
187
// can_register_syn.v
// synopsys translate_off
//'include "timescale.v"
'timescale Ins/lOps
// synopsys translateon
//'include "can defines.v"
// Uncomment following line if you want to use CAN in Actel APA devices
(embedded memory used)
//define ACTELAPARAM
module can_register_syn
(data_in,
data_out,
we,
clk,
rst_sync
);
endmodule
188
A.8: Inverse Bit Order HDL Code
The following code simply inverts the bit order of the input data stream.
// canibo.v
// This module only inverts bit order
module canibo
di,
do
endmodule
189
Appendix B: C Code
The following appendix contains all of the C code that describes the processes of
initializing the CAN controller, writing a message into the the transmit buffer, and
// Control register
190
//#define CntrR 0x01 //Enables reset mode
//Clear a register
#define clearbyte 0x00
//Don't care
#define dontcare OxFF
void main(void)
{
int i;
i=0;
while (i<2000) {
//End of Initialization
191
} //end of loop
192
B.2: Initialization and Message Reception C Code
The following code is the initialization and message reception procedure. This
program also turns on an LED if the data byte received is Olh which relates to light 1
turning on.
// Control register
//#define CntrR 0x01 //Enables reset mode
//Clear a register
#define clearbyte 0x00
//Don't care
#define dontcare OxFF
193
void main(void)
{
//End of Initialization
int i;
i=0;
while (i<l)
{
data = ReadExData(0x02);
}
if (message = 0x01) //Turn on LED if data byte is 0x01 (light 1 on)
{
int j; // define an integer j to serve as a loop counter
194
// now write all ones to port A which sets outputs high and LED's
off
195
BitWrPortI(PEDR, &PEDRShadow, Low, ALE); // Set the ALE line low
BitWrPortI(PCDR, &PCDRShadow, Low, RD); // Set the RD line Low
WrPortI( SPCR, &SPCRShadow, 0x80); // setup parallel port A as
an input
Data = RdPortI(PADR); // Read into PortA values at
ADO to AD7
BitWrPortI(PCDR, &PCDRShadow, High, RD); // Set the RD line high
BitWrPortI(PCDR, &PCDRShadow, High, CS); // Set the CS High
return Data;
}
196