Difference between revisions of "S15: Protocol Interface: I2C - CAN Bridge"
Proj user13 (talk | contribs) (→References Used) |
Proj user13 (talk | contribs) |
||
(25 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | [[File:S15_244_Grp1_project.jpg|right|thumb|400px|Project Setup]]<br> | ||
== Serial Protocol Convertor == | == Serial Protocol Convertor == | ||
+ | <table> | ||
+ | <tr> | ||
+ | <td valign="top" align="justify" width=600px> | ||
+ | == Abstract == | ||
+ | This project is aimed at developing a device that can act as an interface between I2C bus and CAN bus. This device "Protocol Interface: I2C - CAN Birdge" can receive the data over CAN bus and can give it to a device connected on an I2C bus. The data is stored using a mailbox concept meaning message with different CAN ID is stored in different mailboxes. The mailboxes can be configured for a specific CAN ID, CAN ID with Mask Bits and different sizes. | ||
− | |||
− | |||
− | |||
− | |||
== Objectives & Introduction == | == Objectives & Introduction == | ||
− | < | + | In industry generally a system requires micro-controllers with multiple inbuilt CAN peripherals. But not all micro-controllers come with multiple inbuilt CAN peripheral. Considering the economical factors of the system such micro-controllers increases the overall cost. The PI2C244 provides a solution to this problem.<br><br> |
− | + | The PI2C244 device will help to emulate a CAN controller on the I2C device. It is an interface which will send CAN message to I2C device with help of buffering mechanism and vice-versa. It should address issues related to speed synchronization and bi-directional communication, which are necessary to make this device useful. Since the I2C protocol consists of a master - slave approach, it is not possible to control the I2C master by I2C slave. In this project, these conditions are handled using hardware interrupts. Also speed synchronization is a crucial aspect, as these two protocols can have different data speeds. The concept of Queues and Mailboxes is used to handle the speed limitations. FreeRTOS is used for designing the software of this system. | |
− | + | </td> | |
− | + | <td valign="top" align="center"> | |
− | </ | + | [[File:S15_244_Grp1_block_diagram_overall.jpeg|right|thumb|400px|Figure 1. Block Diagram]] |
+ | </td> | ||
+ | </tr> | ||
+ | </table> | ||
== Team Members & Responsibilities == | == Team Members & Responsibilities == | ||
Line 58: | Line 63: | ||
| align="center"|03/08/2015 | | align="center"|03/08/2015 | ||
| align="center"|03/14/2015 | | align="center"|03/14/2015 | ||
− | |Develop driver for interrupt based I2C acting as a Slave Device | + | | Develop driver for interrupt based I2C acting as a Slave Device |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|03/14/2015 | | align="center"|03/14/2015 | ||
Line 66: | Line 71: | ||
| align="center"|03/15/2015 | | align="center"|03/15/2015 | ||
| align="center"|03/21/2015 | | align="center"|03/21/2015 | ||
− | |Exploring CAN API and building CAN bus for communication | + | | Exploring CAN API and building CAN bus for communication |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|03/21/2015 | | align="center"|03/21/2015 | ||
Line 74: | Line 79: | ||
| align="center"|03/22/2015 | | align="center"|03/22/2015 | ||
| align="center"|03/28/2015 | | align="center"|03/28/2015 | ||
− | |Study and Design mailbox system for CAN messages | + | | Study and Design mailbox system for CAN messages |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|03/28/2015 | | align="center"|03/28/2015 | ||
Line 82: | Line 87: | ||
| align="center"|03/29/2015 | | align="center"|03/29/2015 | ||
| align="center"|04/04/2015 | | align="center"|04/04/2015 | ||
− | |Develop program for configuring the Protocol Interface by I2C Master | + | | Develop program for configuring the Protocol Interface by I2C Master |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|04/04/2015 | | align="center"|04/04/2015 | ||
Line 90: | Line 95: | ||
| align="center"|04/05/2015 | | align="center"|04/05/2015 | ||
| align="center"|04/11/2015 | | align="center"|04/11/2015 | ||
− | |Create task for storing CAN messages in mailboxes | + | | Create task for storing CAN messages in mailboxes |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|04/11/2015 | | align="center"|04/11/2015 | ||
Line 98: | Line 103: | ||
| align="center"|04/12/2015 | | align="center"|04/12/2015 | ||
| align="center"|04/18/2015 | | align="center"|04/18/2015 | ||
− | |Create task for giving interrupt to I2C Master | + | | Create task for giving interrupt to I2C Master |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|04/18/2015 | | align="center"|04/18/2015 | ||
Line 106: | Line 111: | ||
| align="center"|04/19/2015 | | align="center"|04/19/2015 | ||
| align="center"|04/25/2015 | | align="center"|04/25/2015 | ||
− | |Create task for sending message on CAN | + | | Create task for sending message on CAN received from I2C Master |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|04/25/2015 | | align="center"|04/25/2015 | ||
Line 114: | Line 119: | ||
| align="center"|04/26/2015 | | align="center"|04/26/2015 | ||
| align="center"|05/02/2015 | | align="center"|05/02/2015 | ||
− | |Design of Status Register for Protocol Interface | + | | Design of Status Register for Protocol Interface |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|05/02/2015 | | align="center"|05/02/2015 | ||
Line 122: | Line 127: | ||
| align="center"|05/03/2015 | | align="center"|05/03/2015 | ||
| align="center"|05/09/2015 | | align="center"|05/09/2015 | ||
− | |Finalizing appropriate addresses for all registers | + | | Finalizing appropriate addresses for all registers |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|05/09/2015 | | align="center"|05/09/2015 | ||
Line 130: | Line 135: | ||
| align="center"|05/10/2015 | | align="center"|05/10/2015 | ||
| align="center"|05/16/2015 | | align="center"|05/16/2015 | ||
− | |Testing of Protocol Interface for various conditions | + | | Testing of Protocol Interface for various conditions |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|05/12/2015 | | align="center"|05/12/2015 | ||
Line 138: | Line 143: | ||
| align="center"|05/17/2015 | | align="center"|05/17/2015 | ||
| align="center"|05/23/2015 | | align="center"|05/23/2015 | ||
− | |Adding a feature of Bit-Masked CAN IDs | + | | Adding a feature of Bit-Masked CAN IDs |
| align="center"|Completed | | align="center"|Completed | ||
| align="center"|05/22/2015 | | align="center"|05/22/2015 | ||
Line 186: | Line 191: | ||
|- | |- | ||
|} | |} | ||
− | |||
− | |||
− | |||
== Hardware Design == | == Hardware Design == | ||
Line 197: | Line 199: | ||
<center> | <center> | ||
− | [[File: | + | [[File:S15_244_Grp1_block_diagram_detailed.jpeg|center|700px|thumb|Figure 2. Detailed Block Diagram]] |
</center> | </center> | ||
Line 287: | Line 289: | ||
== Software Design == | == Software Design == | ||
+ | |||
+ | === Memory Map === | ||
+ | <table> | ||
+ | <tr> | ||
+ | <td valign="top" align="justify" width=400px> | ||
+ | The configuration registers and the memory map of the mailboxes can be seen from the diagram. Each mailbox has a dedicated configuration register that can be written by the I2C master. Each register contains the mailbox size and the requested CAN ID. The master can also mask some bits of the CAN ID to receive some range of CAN messages from the CAN bus. This concept is similar to the hardware filter. Instead of giving the range, mask bits are given. If the CAN ID is not masked, then the Mask bit (MSB) is 0, which states the mailbox is configured for a single CAN ID. The MASK field is considered only when the Mask bit is set to 1. Each configuration register is accessed by its addressed shown in the diagram. | ||
+ | </td> | ||
+ | <td align="center"> | ||
+ | [[File:S15_244_Grp1_Memory_map.jpg|right|thumb|600px|Figure 3. Memory Map]] | ||
+ | </td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | |||
+ | |||
<table> | <table> | ||
<tr> | <tr> | ||
<td valign="top" align="justify" width=400px> | <td valign="top" align="justify" width=400px> | ||
=== Configure Mailbox === | === Configure Mailbox === | ||
− | The flowchart for configuring mailboxes is as shown in | + | The flowchart for configuring mailboxes is as shown in Figure4. The algorithm is as follows: |
# Start | # Start | ||
# Receive data over I2C bus from I2C Master | # Receive data over I2C bus from I2C Master | ||
Line 303: | Line 319: | ||
=== CAN to Mailbox === | === CAN to Mailbox === | ||
− | The flowchart for storing data received over CAN to mailbox is as shown in | + | The flowchart for storing data received over CAN to mailbox is as shown in Figure5. The algorithm is as follows: |
# Start | # Start | ||
# Wait for data to be received over CAN bus using CAN_rx() with a timeout of PortMAXDELAY | # Wait for data to be received over CAN bus using CAN_rx() with a timeout of PortMAXDELAY | ||
Line 313: | Line 329: | ||
</td> | </td> | ||
<td valign="top" align="center"> | <td valign="top" align="center"> | ||
− | [[File:S15_244_Grp1_config_mailbox.jpeg|right|thumb|275px|Figure | + | [[File:S15_244_Grp1_config_mailbox.jpeg|right|thumb|275px|Figure 4. Flowchart on Configuring Mailbox]] |
</td> | </td> | ||
<td valign="top" align="center"> | <td valign="top" align="center"> | ||
− | [[File:S15_244_Grp1_can_to_mailbox.jpeg|right|thumb|350px|Figure | + | [[File:S15_244_Grp1_can_to_mailbox.jpeg|right|thumb|350px|Figure 5. Flowchart on Receiving Data from CAN bus and Storing into Mailboxes]] |
</td> | </td> | ||
</tr> | </tr> | ||
Line 325: | Line 341: | ||
<td valign="top" align="justify" width=400px> | <td valign="top" align="justify" width=400px> | ||
=== Read Status Register === | === Read Status Register === | ||
− | The flowchart for reading Status Register of Protocol Interface is as shown in | + | The flowchart for reading Status Register of Protocol Interface is as shown in Figure6. The algorithm is as follows: |
# Start | # Start | ||
# Receive data over I2C bus from I2C Master | # Receive data over I2C bus from I2C Master | ||
Line 334: | Line 350: | ||
=== Read Mailbox Data === | === Read Mailbox Data === | ||
− | The flowchart for reading mailbox data is as shown in | + | The flowchart for reading mailbox data is as shown in Figure7. The algorithm is as follows: |
# Start | # Start | ||
# Receive data over I2C bus from I2C Master and set it as Requested Mailbox | # Receive data over I2C bus from I2C Master and set it as Requested Mailbox | ||
Line 342: | Line 358: | ||
</td> | </td> | ||
<td valign="top" align="center"> | <td valign="top" align="center"> | ||
− | [[File:S15_244_Grp1_status_register.jpeg|right|thumb|250px|Figure | + | [[File:S15_244_Grp1_status_register.jpeg|right|thumb|250px|Figure 6. Flowchart on Reading Status Register]] |
</td> | </td> | ||
<td valign="top" align="center"> | <td valign="top" align="center"> | ||
− | [[File:S15_244_Grp1_read_data.jpeg|right|thumb|250px|Figure | + | [[File:S15_244_Grp1_read_data.jpeg|right|thumb|250px|Figure 7. Flowchart on Reading Mailbox Data]] |
</td> | </td> | ||
</tr> | </tr> | ||
Line 354: | Line 370: | ||
<td valign="top" align="justify" width=400px> | <td valign="top" align="justify" width=400px> | ||
=== Send Data over CAN === | === Send Data over CAN === | ||
− | The flowchart for sending data from I2C Master to CAN is as shown in | + | The flowchart for sending data from I2C Master to CAN is as shown in Figure 8. The algorithm is as follows: |
# Start | # Start | ||
# Receive data over I2C bus from I2C Master | # Receive data over I2C bus from I2C Master | ||
Line 364: | Line 380: | ||
</td> | </td> | ||
<td align="center"> | <td align="center"> | ||
− | [[File:S15_244_Grp1_send_can.jpeg|right|thumb|250px|Figure | + | [[File:S15_244_Grp1_send_can.jpeg|right|thumb|250px|Figure 8. Flowchart on Sending Data over CAN Bus]] |
+ | </td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | |||
+ | === I2C Slave Device Implementation === | ||
+ | <table> | ||
+ | <tr> | ||
+ | <td valign="top" align="justify" width=400px> | ||
+ | * The I2C slave device has three tasks running on FreeRTOS that can be seen in the diagram. The CAN transmit task transmits the data received by the I2C task from the I2C master controller. This data is updated in the buffer shared by both the tasks. The synchronization of these tasks is achieved using a Binary Semaphore. As soon as the data is available from the I2C master, the I2C tasks wakes up the CAN transmit task. Otherwise, this task waits forever (PortMAXDELAY) for the semaphore. | ||
+ | * The CAN receive tasks receives the message from the CAN bus. It uses the software filter to filter out messages depending on the configuration of the mailboxes. It also gives the interrupt to the I2C master if any of the mailbox exceeds the number of frames stored by the configured size. | ||
+ | * The I2C task handles the messages from the I2C master. These messages include configuration command, read mailbox command, read status command and write CAN data command. Depending on the each command it updates the mailbox configuration and the CAN transmit buffer. Also, it will provide the status register and the mailbox to the I2C master when asked. | ||
+ | </td> | ||
+ | <td align="center"> | ||
+ | [[File:S15_244_Grp1_tasks.jpg|right|thumb|600px|Figure 9. Tasks running on I2C Slave Device]] | ||
</td> | </td> | ||
</tr> | </tr> | ||
Line 374: | Line 404: | ||
* During boot-up process of Protocol Interface, I2C and CAN drivers are initialized.<br> | * During boot-up process of Protocol Interface, I2C and CAN drivers are initialized.<br> | ||
* The device is ready to accept messages from I2C Master to be sent over the CAN bus. The data frame is as follows: | * The device is ready to accept messages from I2C Master to be sent over the CAN bus. The data frame is as follows: | ||
− | [[File: | + | [[File:S15_244_Grp1_I2C_to_Device_CAN_sending.jpeg|center|1000px|thumb|Figure 10. Data Frame sent by I2C Master to Protocol Interface for sending data over CAN Bus]] |
* The I2C Master write data to Configuration Registers of respective Mailbox. The data frame is as follows: | * The I2C Master write data to Configuration Registers of respective Mailbox. The data frame is as follows: | ||
− | [[File:S15_244_Grp1_I2C_to_Device_config.jpeg|center|1000px|thumb|Figure | + | [[File:S15_244_Grp1_I2C_to_Device_config.jpeg|center|1000px|thumb|Figure 11. Data Frame sent by I2C Master to configure mailboxes of Protocol Interface]] |
* According to the Configuration Registers, the mailboxes are configured for specific CAN_id, size and optional Mask_id.<br> | * According to the Configuration Registers, the mailboxes are configured for specific CAN_id, size and optional Mask_id.<br> | ||
* The device will continuously keep on accepting messages from CAN bus. For every message received, the software will check whether the received CAN_id matches with the configured mailbox CAN_id. If the id matches, then the Data Frame i.e. CAN_id and 8-bytes of Data is stored in the particular mailbox. | * The device will continuously keep on accepting messages from CAN bus. For every message received, the software will check whether the received CAN_id matches with the configured mailbox CAN_id. If the id matches, then the Data Frame i.e. CAN_id and 8-bytes of Data is stored in the particular mailbox. | ||
Line 382: | Line 412: | ||
* Once the message is saved in the mailbox, it will check whether the number of frames stored in a mailbox exceeds the configured size for that mailbox. If it exceeds, then device will give an external GPIO interrupt to I2C Master.<br> | * Once the message is saved in the mailbox, it will check whether the number of frames stored in a mailbox exceeds the configured size for that mailbox. If it exceeds, then device will give an external GPIO interrupt to I2C Master.<br> | ||
* On receiving an interrupt, I2C Master will initiate read transaction to read the Status Register. The data frame is as follows: | * On receiving an interrupt, I2C Master will initiate read transaction to read the Status Register. The data frame is as follows: | ||
− | [[File:S15_244_Grp1_status_register_i2c.jpeg|center|1000px|thumb|Figure | + | [[File:S15_244_Grp1_status_register_i2c.jpeg|center|1000px|thumb|Figure 12. Data Frame for sending Status Register from Protocol Interface to I2C Master]] |
* Looking at the bits set in the Status Register, I2C Master will decide to read the data from any one mailbox at a time. | * Looking at the bits set in the Status Register, I2C Master will decide to read the data from any one mailbox at a time. | ||
* I2C Master will initiate read transaction for one mailbox and device will send the data frames of requested mailbox. The data frame is as follows: | * I2C Master will initiate read transaction for one mailbox and device will send the data frames of requested mailbox. The data frame is as follows: | ||
− | [[File:S15_244_Grp1_read_data_i2c.jpeg|center|1000px|thumb|Figure | + | [[File:S15_244_Grp1_read_data_i2c.jpeg|center|1000px|thumb|Figure 13. Data Frame for sending CAN data stored in mailboxes to I2C Master]] |
</p> | </p> | ||
+ | <br> | ||
+ | <br> | ||
+ | === Software Flow === | ||
+ | <p valign="top" align="justify" width=400px> | ||
+ | The software flow is discussed below: | ||
+ | * CAN controller of the PI2C244 accepts all the CAN messages | ||
+ | * Configure the interface for the specific message ID | ||
+ | * After configuration, the CAN messages with configured message ID are stored in the respective mailbox | ||
+ | * Once the data filled in mailbox reaches Low Watermark the interface will send a trigger to the I2C master device | ||
+ | * A master device has to read the read the status register to identify which mailbox is full | ||
+ | * A master device will send initiate a read command, the first byte sent by master device is considered as mailbox number. The interface will then send the data from the requested mailbox | ||
+ | * It can send CAN message frames at any time. To send a CAN frame, initiate a write command and write first byte as 0xFF, following the 11 bit CAN message ID and 8-byte data | ||
+ | </p> | ||
+ | [[File:S15_244_Grp1_sequence_diagram.jpeg|center|800px|thumb|Figure 14. Software Flow]] | ||
== Testing & Technical Challenges == | == Testing & Technical Challenges == | ||
− | + | <p valign="top" align="justify"> | |
− | + | Initial testing included testing of I2C and CAN protocols on the SJOne board. Dummy messages were used to test the communication. Since the software design includes use of semaphores for task synchronization and communication, several test cases were developed to test the design and the program flow. Every time I2C master sends a command, the I2C slave device is supposed to handle the request independently. Few of the commands have same register address, but repeated start conditions. By modifying and designing the I2C slave state diagram, it was achieved. Once the I2C slave was successfully able to differentiate all the commands, the actual sensor data was sent to test the communication. The debugging was done by forming different test benches for I2C slave device. All the CAN controllers were programmed to send 16 different types of CAN messages each with different CAN ID’s. The masking of the CAN ID’s was tested using these different CAN messages present on the CAN bus. | |
+ | </p> | ||
− | + | === Issues Faced === | |
+ | ===== I2C Slave to I2C Master Communication ===== | ||
+ | <p valign="top" align="justify"> | ||
+ | Once I2C Master configures the mailbox for a specific size, it will keep a track of the number of data frames present in a particular mailbox. But, once the number of frames exceeds the configured size, the Protocol Interface being an I2C Slave, cannot initiate a transaction and ask I2C Master to read the data. So, whenever the number of data frames for any mailbox exceeds the configured size, it will send an interrupt to I2C Master informing that at least one of the mailbox is exceeding the number of data frames. In response to this interrupt, I2C Master can initiate a read transaction and read the data from the mailbox. | ||
+ | </p> | ||
+ | <br> | ||
+ | ===== Which Mailbox is FULL? ===== | ||
+ | <p valign="top" align="justify"> | ||
+ | The size configured for each mailbox can be different. The device will send an interrupt whenever the number of messages in any mailbox exceeds the configured size. But, by getting an interrupt, I2C Master does not know that which mailbox is full. So it needs to initiate a read transaction for maximum number of data bytes. But, in doing that, the device (acting as an I2C Slave) can not send NACK to I2C Master saying that the data is over. | ||
+ | So we introduced a concept of a Status Register. Status Register clearly defines that which mailbox is full. Adding this concept also gives an added feature of priority. When I2C Master reads the Status Register, it gets information about all the mailboxes that are full. So, depending on the priority, an I2C Master can decide on which mailbox's data to be read first. | ||
+ | </p> | ||
+ | <br> | ||
+ | ===== Multiple Interrupts for Same Mailbox ===== | ||
+ | <p valign="top" align="justify"> | ||
+ | We had faced a situation where the I2C slave device was giving multiple interrupts for the same mailbox. This issue was solved by restricting the interrupt send function by reading the status bit before sending an interrupt. | ||
+ | </p> | ||
− | === | + | == Conclusion == |
− | + | <p valign="top" align="justify" width=400px> | |
+ | We have successfully implemented the initial version of protocol interface for I2C-CAN communication protocol. The communication is done using the Mailbox concepts. This interface supports configuration of mailboxes for different size (maximum 16 CAN frames) for specific CAN IDs. It also supports the range of CAN IDs using the Mask bits specified during the configuration. The interface allows the I2C master to read the status of all the mailboxes using Status Register. | ||
+ | We have learned CAN communication protocol and I2C communication protocol in detail. We developed an I2C slave device by modifying the slave state diagram depending on the requirement of this project. This project can be extended to support 29-bit CAN IDs and also variable size CAN messages. It will definitely help designers to use with the controllers that does not support CAN interface. We have also provided a board support package that consists of the drivers required for the I2C master controller to use the interface. | ||
+ | </p> | ||
− | == | + | == Datasheet == |
− | + | [https://www.dropbox.com/s/6xx8aetvnf6120s/PI2C244.pdf?dl=0 PI2C244 Protocol Interface: I2C - CAN Bridge Datasheet] | |
== Project Video == | == Project Video == | ||
− | + | [https://youtu.be/dQ6YanIKLP8 Protocol Interface: I2C - CAN Bridge Demo] | |
== Project Source Code == | == Project Source Code == | ||
− | * [https:// | + | * [https://github.com/sanghvimails/Protocol_Interface_I2C_Can_Bridge.git Github Source Code Link] |
== References == | == References == | ||
− | |||
− | |||
− | |||
− | |||
# [http://www.nxp.com/documents/user_manual/UM10360.pdf LPC_USER_MANUAL] | # [http://www.nxp.com/documents/user_manual/UM10360.pdf LPC_USER_MANUAL] | ||
# [http://www.parallax.com/product/28015 Ultrasonic Sensor] | # [http://www.parallax.com/product/28015 Ultrasonic Sensor] | ||
Line 416: | Line 475: | ||
# [http://www.linear.com/product/LT1083 Linear Voltage Regulator] | # [http://www.linear.com/product/LT1083 Linear Voltage Regulator] | ||
# [http://www.socialledge.com/sjsu/index.php?title=Realtime_OS_on_Embedded_Systems Socialledge Embedded Systems Wiki] | # [http://www.socialledge.com/sjsu/index.php?title=Realtime_OS_on_Embedded_Systems Socialledge Embedded Systems Wiki] | ||
− | # Preetpal Kang, Lecture notes of CMPE | + | # Preetpal Kang, Lecture notes of CMPE 244, Computer Engineering, Charles W. Davidson College of Engineering, San Jose State University, Jan-May 2015. |
# en.wikipedia.org/ | # en.wikipedia.org/ |
Latest revision as of 05:31, 25 May 2015
Contents
- 1 Serial Protocol Convertor
- 2 Abstract
- 3 Objectives & Introduction
- 4 Team Members & Responsibilities
- 5 Schedule
- 6 Parts List & Cost
- 7 Hardware Design
- 8 Hardware Interface
- 9 Software Design
- 10 Implementation
- 11 Testing & Technical Challenges
- 12 Conclusion
- 13 Datasheet
- 14 Project Video
- 15 Project Source Code
- 16 References
Serial Protocol Convertor
AbstractThis project is aimed at developing a device that can act as an interface between I2C bus and CAN bus. This device "Protocol Interface: I2C - CAN Birdge" can receive the data over CAN bus and can give it to a device connected on an I2C bus. The data is stored using a mailbox concept meaning message with different CAN ID is stored in different mailboxes. The mailboxes can be configured for a specific CAN ID, CAN ID with Mask Bits and different sizes.
Objectives & IntroductionIn industry generally a system requires micro-controllers with multiple inbuilt CAN peripherals. But not all micro-controllers come with multiple inbuilt CAN peripheral. Considering the economical factors of the system such micro-controllers increases the overall cost. The PI2C244 provides a solution to this problem. |
Team Members & Responsibilities
S.R. | Team Member's Name | Tasks |
---|---|---|
1 | Ishan Bhavsar |
|
2 | Tej Kogekar | |
3 | Rutwik Kulkarni |
|
4 | Mitesh Sanghvi |
Schedule
S.R. | Start Date | End Date | Task | Status | Actual Completion Date |
---|---|---|---|---|---|
1 | 03/08/2015 | 03/14/2015 | Develop driver for interrupt based I2C acting as a Slave Device | Completed | 03/14/2015 |
2 | 03/15/2015 | 03/21/2015 | Exploring CAN API and building CAN bus for communication | Completed | 03/21/2015 |
3 | 03/22/2015 | 03/28/2015 | Study and Design mailbox system for CAN messages | Completed | 03/28/2015 |
4 | 03/29/2015 | 04/04/2015 | Develop program for configuring the Protocol Interface by I2C Master | Completed | 04/04/2015 |
5 | 04/05/2015 | 04/11/2015 | Create task for storing CAN messages in mailboxes | Completed | 04/11/2015 |
6 | 04/12/2015 | 04/18/2015 | Create task for giving interrupt to I2C Master | Completed | 04/18/2015 |
7 | 04/19/2015 | 04/25/2015 | Create task for sending message on CAN received from I2C Master | Completed | 04/25/2015 |
8 | 04/26/2015 | 05/02/2015 | Design of Status Register for Protocol Interface | Completed | 05/02/2015 |
9 | 05/03/2015 | 05/09/2015 | Finalizing appropriate addresses for all registers | Completed | 05/09/2015 |
10 | 05/10/2015 | 05/16/2015 | Testing of Protocol Interface for various conditions | Completed | 05/12/2015 |
11 | 05/17/2015 | 05/23/2015 | Adding a feature of Bit-Masked CAN IDs | Completed | 05/22/2015 |
Parts List & Cost
S.R. | Description | Manufacturer | Part Number | Qty | Cost |
---|---|---|---|---|---|
1 | SJOne Board | - | - | 1 | $80.00 |
2 | Ultrasonic Sensor (Optional) | Parallax | 28015 | 1 | $23.00 |
3 | CAN Transceiver (Free Samples) | Microchip | MCP2551 | 1 | $0.00 |
4 | Linear Voltage Regulator (Free Sample) | LT | LT1083-5 | 1 | $0.00 |
Total | $103.00 |
Hardware Design
This project is used as an interface for an I2C controller to communicate over the CAN bus. The I2C controller should act as an I2C master and the Protocol Interface device will act as an I2C slave device. Since the communication from the I2C slave to the I2C master is not possible, external GPIO interrupts are used. The Protocol Interface has an inbuilt CAN peripheral to communicate over the CAN bus. The I2C master is supposed to configure the mailboxes of the interface for the particular CAN ID’s. These mailboxes are used to store the CAN messages received by the Protocol Interface over the CAN bus. Whenever a message is received by the CAN peripheral, it is stored in the respective mailbox. Once the number of Data Frames stored in any mailbox reached the limit (configured by I2C Master), an interrupt is given. The Protocol Interface uses SJOne development board for the communication that supports both I2C and CAN protocols. The write and read address of the Protocol Interface is 0x50 and 0x51 respectively, which are used by the I2C master for the communication.
Hardware Interface
I2C Bus
The following shows the I2C bus protocol:
- Data transfer is initiated by master device only, when the bus is not busy.
- Data is clocked in; when the clock line (SCL) is HIGH, the state data line (SDA) determines value of the incoming bit. The START or STOP condition are given by changing state of SDA line when SCL line is high.
The states of the diagram are described in detail below:
- IDLE state: both data and clock lines remain HIGH.
- START condition: a HIGH to LOW transition of the SDA line when the SCL line is HIGH. All transactions must be preceded by a START condition.
- STOP condition: a LOW to HIGH transition of the SDA line while the SCL is HIGH. All transactions must be terminated by a STOP condition.
- Data transfer: the data transfer is preceded by a START condition. The state of data line during HIGH period of the SCL represents data bits. The state of data line is changed during the LOW period of the SCL. The data transfer is terminated with a STOP condition. The master device controls the data flow.
- Acknowledgement: After reception of each data byte, the receiver sends the acknowledgement bit. During master write mode, the ACK bit sent by slave signifies that it can accommodate more data. If the slave sends NACK then it cannot accommodate any more data. To acknowledge the slave has to pull down the SDA line during the acknowledge clock pulse during the HIGH period of SCL.
CAN Bus
Controller Area Network (CAN) is a communication protocol which is developed by Bosch. It can be summarized as follows:
- The physical layer of the protocol uses twisted pair wire for transmission and reception of differential signal.
- A deterministic bus which uses non destructive bit wise arbitration.
- It uses small messages (around 8 bytes) protected by checksum for data integrity.
- Instead of explicit addressing, it uses 11 bit or 29 bit identifier which controls its priority on the bus.
- A sophisticated error handling mechanism, where error in message can be handled by retransmission of message.
- Effective mechanism to isolate faults and removing faulty nodes from the bus.
- Because of reliability, noise immunity it is extensively used in automotive industry for effective and efficient communication between different types of controllers.
- The speeds supported by CAN bus are 100K, 250K, 500K, 1MBits per second.
CAN Transceiver
The microprocessor’s inbuilt CAN peripheral cannot directly communicate over the CAN bus. It needs a CAN transceiver to be connected between peripheral and bus. The main two task of CAN transceiver are:
- Receiving: It adapts signal levels from the bus, to levels that the CAN Controller expects. It generally comes with a protective circuitry that protects the processor’s internal CAN peripheral.
- Sending: It converts the transmit-bit signal received from the microcontroller’s internal CAN peripheral into a signal that needs to be sent onto the CAN bus.
- Supports 1Mbps speed of operation.
- Sutaible for 12V and 24V systems.
- Externally-controlled slope for reduced RFI systems.
- Power on reset and voltage brown out protection.
- Low current standby operation.
- Protection against short circuit protection in case of positive or negative battery voltage. It also has thermal shutdown protection, protection against high voltage transients.
Software Design
Memory Map
The configuration registers and the memory map of the mailboxes can be seen from the diagram. Each mailbox has a dedicated configuration register that can be written by the I2C master. Each register contains the mailbox size and the requested CAN ID. The master can also mask some bits of the CAN ID to receive some range of CAN messages from the CAN bus. This concept is similar to the hardware filter. Instead of giving the range, mask bits are given. If the CAN ID is not masked, then the Mask bit (MSB) is 0, which states the mailbox is configured for a single CAN ID. The MASK field is considered only when the Mask bit is set to 1. Each configuration register is accessed by its addressed shown in the diagram. |
Configure MailboxThe flowchart for configuring mailboxes is as shown in Figure4. The algorithm is as follows:
CAN to MailboxThe flowchart for storing data received over CAN to mailbox is as shown in Figure5. The algorithm is as follows:
|
Read Status RegisterThe flowchart for reading Status Register of Protocol Interface is as shown in Figure6. The algorithm is as follows:
Read Mailbox DataThe flowchart for reading mailbox data is as shown in Figure7. The algorithm is as follows:
|
Send Data over CANThe flowchart for sending data from I2C Master to CAN is as shown in Figure 8. The algorithm is as follows:
|
I2C Slave Device Implementation
|
Implementation
The steps involved in implementation of this project are as follows:
- During boot-up process of Protocol Interface, I2C and CAN drivers are initialized.
- The device is ready to accept messages from I2C Master to be sent over the CAN bus. The data frame is as follows:
- The I2C Master write data to Configuration Registers of respective Mailbox. The data frame is as follows:
- According to the Configuration Registers, the mailboxes are configured for specific CAN_id, size and optional Mask_id.
- The device will continuously keep on accepting messages from CAN bus. For every message received, the software will check whether the received CAN_id matches with the configured mailbox CAN_id. If the id matches, then the Data Frame i.e. CAN_id and 8-bytes of Data is stored in the particular mailbox.
- For all the received messages, if a particular mailbox is configured with Mask_id, then it will also check the received CAN_id to match with the masked CAN_id.
- Once the message is saved in the mailbox, it will check whether the number of frames stored in a mailbox exceeds the configured size for that mailbox. If it exceeds, then device will give an external GPIO interrupt to I2C Master.
- On receiving an interrupt, I2C Master will initiate read transaction to read the Status Register. The data frame is as follows:
- Looking at the bits set in the Status Register, I2C Master will decide to read the data from any one mailbox at a time.
- I2C Master will initiate read transaction for one mailbox and device will send the data frames of requested mailbox. The data frame is as follows:
Software Flow
The software flow is discussed below:
- CAN controller of the PI2C244 accepts all the CAN messages
- Configure the interface for the specific message ID
- After configuration, the CAN messages with configured message ID are stored in the respective mailbox
- Once the data filled in mailbox reaches Low Watermark the interface will send a trigger to the I2C master device
- A master device has to read the read the status register to identify which mailbox is full
- A master device will send initiate a read command, the first byte sent by master device is considered as mailbox number. The interface will then send the data from the requested mailbox
- It can send CAN message frames at any time. To send a CAN frame, initiate a write command and write first byte as 0xFF, following the 11 bit CAN message ID and 8-byte data
Testing & Technical Challenges
Initial testing included testing of I2C and CAN protocols on the SJOne board. Dummy messages were used to test the communication. Since the software design includes use of semaphores for task synchronization and communication, several test cases were developed to test the design and the program flow. Every time I2C master sends a command, the I2C slave device is supposed to handle the request independently. Few of the commands have same register address, but repeated start conditions. By modifying and designing the I2C slave state diagram, it was achieved. Once the I2C slave was successfully able to differentiate all the commands, the actual sensor data was sent to test the communication. The debugging was done by forming different test benches for I2C slave device. All the CAN controllers were programmed to send 16 different types of CAN messages each with different CAN ID’s. The masking of the CAN ID’s was tested using these different CAN messages present on the CAN bus.
Issues Faced
I2C Slave to I2C Master Communication
Once I2C Master configures the mailbox for a specific size, it will keep a track of the number of data frames present in a particular mailbox. But, once the number of frames exceeds the configured size, the Protocol Interface being an I2C Slave, cannot initiate a transaction and ask I2C Master to read the data. So, whenever the number of data frames for any mailbox exceeds the configured size, it will send an interrupt to I2C Master informing that at least one of the mailbox is exceeding the number of data frames. In response to this interrupt, I2C Master can initiate a read transaction and read the data from the mailbox.
Which Mailbox is FULL?
The size configured for each mailbox can be different. The device will send an interrupt whenever the number of messages in any mailbox exceeds the configured size. But, by getting an interrupt, I2C Master does not know that which mailbox is full. So it needs to initiate a read transaction for maximum number of data bytes. But, in doing that, the device (acting as an I2C Slave) can not send NACK to I2C Master saying that the data is over. So we introduced a concept of a Status Register. Status Register clearly defines that which mailbox is full. Adding this concept also gives an added feature of priority. When I2C Master reads the Status Register, it gets information about all the mailboxes that are full. So, depending on the priority, an I2C Master can decide on which mailbox's data to be read first.
Multiple Interrupts for Same Mailbox
We had faced a situation where the I2C slave device was giving multiple interrupts for the same mailbox. This issue was solved by restricting the interrupt send function by reading the status bit before sending an interrupt.
Conclusion
We have successfully implemented the initial version of protocol interface for I2C-CAN communication protocol. The communication is done using the Mailbox concepts. This interface supports configuration of mailboxes for different size (maximum 16 CAN frames) for specific CAN IDs. It also supports the range of CAN IDs using the Mask bits specified during the configuration. The interface allows the I2C master to read the status of all the mailboxes using Status Register. We have learned CAN communication protocol and I2C communication protocol in detail. We developed an I2C slave device by modifying the slave state diagram depending on the requirement of this project. This project can be extended to support 29-bit CAN IDs and also variable size CAN messages. It will definitely help designers to use with the controllers that does not support CAN interface. We have also provided a board support package that consists of the drivers required for the I2C master controller to use the interface.
Datasheet
PI2C244 Protocol Interface: I2C - CAN Bridge Datasheet
Project Video
Protocol Interface: I2C - CAN Bridge Demo
Project Source Code
References
- LPC_USER_MANUAL
- Ultrasonic Sensor
- CAN Transceiver
- Linear Voltage Regulator
- Socialledge Embedded Systems Wiki
- Preetpal Kang, Lecture notes of CMPE 244, Computer Engineering, Charles W. Davidson College of Engineering, San Jose State University, Jan-May 2015.
- en.wikipedia.org/