Example Code

I2C and SPI communication on FPGA

Code and Documents

Attachment

Overview

I2C and SPI are widely used communication buses. This example is based on the I2C&SPI API available on VIPM to read the data of 4 different sensors through the both buses.

 

 

Description

The example has been tested on the PXI-7854R board, but I expect it to work on other similar boards (cf. I2C&SPI API device compatibility). The goal is to read the temperature from two temperature sensors using the SPI Bus and to get the data from a Gyroscope and an accelerometer through the I2C bus. The FPGA hosts the circuitry of the generic layer for the I2C and the SPI and every command are sent from the host VI. 

On the breadboard, everything is wired that way: 

 

Diagram of the circuitDiagram of the circuit

Power is supplied from an FPGA pin with 3.3V. The pull-up resistor mustn't be forgotten on each line (Value advised: 4.7k).

 

Note: on the ADXL 345, to activate the I2C: ground the SDO pin.

 

 

Hardware and Software Requirements

 Sensors:

  • LM 74 (temperature sensor) x2
  • ADXL 345 (accelerometer)
  • MPU 6050 (gyroscope)

Hardware:

  • PXI-7854R (FPGA)
  • PXIe-8135 (PXI Controller under Windows 7)
  • SCB-68

Software:

 

Steps to Implement or Execute Code

First of all, build the circuit with the different sensors and wire them to the FPGA through the SCB-68. If you are using the same hardware, I advise you to use the following pins:

SCB-68 Pin

FPGA Pin

Breadboard Pin

67

Connector1/DIO37

SDA

33

Connector1/DIO36

SCL

32

Connector1/DIO24

MISO

65

Connector1/DIO33

SCLK

31

Connector1/DIO32

MOSI

63

Connector1/DIO29

CS0

29

Connector1/DIO28

CS1

57

Connector1/DIO22

3.3V

23

GND

GND

 

Circuit exampleCircuit example

 

Once you are done, open the project.

ProjectProjectYou if you do not use the same device, you will need to reconfigure the FPGA IO: Open the FPGA and the VI main FPGA.vi

Then, modify the IO from the pin you choose on your FPGA.

Configure the SPI pins.Configure the SPI pins.

Configure the I2C pins.Configure the I2C pins.

Note: You can see that I modified the name of the FPGA's Pins to make the program easier to read. 

 

Once you reconfigured your pins, recompile the FPGA. Once you are done, you can run the VI final_main.vi. 

final_main.vifinal_main.vi

Note: to program the FPGA, you can just drag and drop the I2C Engine.vi on your FPGA code. It will make the whole generic layer for you.

Use the I2C Engine.vi to avoid complicated FPGA programming. You can do so for the SPI.Use the I2C Engine.vi to avoid complicated FPGA programming. You can do so for the SPI.

 

Additional Information or References

LM74 :

LM74 Datasheet

 

ADXL 345:

ADXL345 Datasheet

 

MPU 6050:

Register Map

Datasheet

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Tianci
Member
Member
on

HELLO,

I have a question. What does the clock rate mean in SPI WRITE/READ?

Thank you!

 

caillotantoine
NI Employee (retired)
on

Hello Tianci,

The clock rate in the library is the frequency in MHz of the SPI clock line, we often use 1MHz. In my case I bypassed a conversion VI to directly set the clock divider.

 

Best, 

Antoine

 

spalinowy
Member
Member
on

I started the program, but noticed that the values refresh very slowly (every 1 s or so). How can I get the highest possible data reading speed?

Contributors