Difference between revisions of "Embedded System I2C Tutorial"
From Embedded Systems Learning Academy
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | Socialledge is moving to two portals. | ||
+ | * The Wiki will remain here for general references about the SJ-One board, and to document student reports. | ||
+ | * The bookstack will now be used for SJSU assignments | ||
+ | |||
+ | [http://books.socialledge.com/books/embedded-drivers-real-time-operating-systems/chapter/lesson-i2c This article has been moved here] | ||
+ | |||
+ | <!-- | ||
== Theory of Operation == | == Theory of Operation == | ||
I2C is prounced "eye-squared see". It is also known as "TWI" because of the intial patent issues of this BUS. This is a popular, low throughput (100-1000Khz), half-duplix BUS that only uses two wires regardless of how many devices are on this BUS. Many sensors use this BUS because of its ease of adding to a system. | I2C is prounced "eye-squared see". It is also known as "TWI" because of the intial patent issues of this BUS. This is a popular, low throughput (100-1000Khz), half-duplix BUS that only uses two wires regardless of how many devices are on this BUS. Many sensors use this BUS because of its ease of adding to a system. | ||
Line 112: | Line 119: | ||
Before you jump right into the assignment, do the following: | Before you jump right into the assignment, do the following: | ||
* Read and understand how an I2C master performs slave register read and write operation | * Read and understand how an I2C master performs slave register read and write operation | ||
+ | *: Look at existing code to see how the master operation handles the I2C state machine function | ||
+ | *: This is important so you can understand the existing code base | ||
* Next to each of the master state, determine which slave state is entered when the master enters its state | * Next to each of the master state, determine which slave state is entered when the master enters its state | ||
* Determine how your slave memory or registers will be read or written | * Determine how your slave memory or registers will be read or written | ||
Line 128: | Line 137: | ||
== Assignment == | == Assignment == | ||
+ | |||
+ | === I2C State Machine Assignment === | ||
+ | Design your I2C state machine. Look at the "Master Write" and "Master Read", and do the following: | ||
+ | |||
+ | *Right underneath the Master State, determine which state the slave will enter when the master is in each of the states | ||
+ | *In each slave state, determine the action you will perform for the transaction | ||
+ | *You can design your own state machine, or augment the existing one, whichever method can yield the maximum clarify for your I2C slave state. | ||
+ | |||
+ | === I2C Code Assignment === | ||
+ | ==== Assignment Outline ==== | ||
+ | <b>This is a group of two assignment; submit one copy of code per team and put down the names of your group members as part of the source code you turn in. Only turn in the new code you added, not the entire file.</b> | ||
+ | * The I2C#2 driver is already implemented and used for on-board sensors. | ||
+ | * Study the existing I2C code: i2c_base.cpp. Please ask any questions if you have any, but the driver was implemented using the state machine diagrams given at the below wikipedia page. | ||
+ | * On your master board, you can just use i2c terminal command to write a register to the other board which is acting as a slave. Hence, you only need to write code on one board (I2C slave board). | ||
+ | |||
+ | ==== Guidelines ==== | ||
+ | Design an I2C slave interface, and use one person's board as MASTER and communicate with the second person's SLAVE board. Here are the guidelines: | ||
Extend the I2C base class to also support slave operation. Test your I2C driver by using one board as a master, and another board as a slave. | Extend the I2C base class to also support slave operation. Test your I2C driver by using one board as a master, and another board as a slave. | ||
# Study <B><CODE>i2c_base.cpp</CODE></B>, particularly the following methods: | # Study <B><CODE>i2c_base.cpp</CODE></B>, particularly the following methods: | ||
Line 177: | Line 203: | ||
=== Warning === | === Warning === | ||
Since the I2C state machine function is called from inside an interrupt, you may not be able to to use <B><CODE>printf()</CODE></B>, especially if you are running FreeRTOS. As an alternative, use the debug printf methods from the <B><CODE>printf_lib.h</CODE></B> file. | Since the I2C state machine function is called from inside an interrupt, you may not be able to to use <B><CODE>printf()</CODE></B>, especially if you are running FreeRTOS. As an alternative, use the debug printf methods from the <B><CODE>printf_lib.h</CODE></B> file. | ||
+ | |||
+ | === Hints === | ||
+ | * Start by hooking up master microcontroller running the default, unmodified sample FreeRTOS project | ||
+ | * Familiarize yourself with the master microcontroller I2C commands: "help i2c" | ||
+ | * Initialize the slave microcontroller's slave address such that you will get an interrupt when your address is sent by the master microcontroller | ||
+ | * Add printfs to the I2C state machine code to identify what states you enter when the other master microcontroller is trying to do an I2C transaction | ||
+ | * Follow your diagram and figure out how to make the I2C slave state machine walk through to read and write registers | ||
+ | <BR/> | ||
+ | --> |
Latest revision as of 20:06, 25 January 2019
Socialledge is moving to two portals.
- The Wiki will remain here for general references about the SJ-One board, and to document student reports.
- The bookstack will now be used for SJSU assignments
This article has been moved here