Difference between revisions of "Example SPI test"

From wiki.emacinc.com
Jump to: navigation, search
m (Marked buggy. Put in notes regarding what to change.)
 
(34 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{todo|Buggy (2.21.14-17:40->BS+);(2.24.14-13:00->MD-)|Brian Serrano|project=oe 4,md,Buggy,bs}}
+
{{todo|Buggy (2.21.14-17:40->BS+);(2.24.14-13:00->MD-)(2.28.14-17:48->BS+)(03.10.14-13:30->MD-)(08.27.14-16:55->BS+)(09.09.14-17:20->MD-)(09.10.14-17:00->BS+)(09.13.14-21:40->MD+) |Brian Serrano|project=oe 4,md,Complete,bs}}
  
 
This is a guide to the <code>spi_test</code> example project included in the EMAC OE SDK.
 
This is a guide to the <code>spi_test</code> example project included in the EMAC OE SDK.
  
<code>SPI</code> (Serial Peripheral Interface) is a synchronous full duplex serial data communication standard used to interface many types of memory and I/O devices. <code>SPI</code> requires four lines per slave device connection, three of which are shared across the <code>SPI</code> bus. MISO, MOSI and SCLK are the three shared bus lines and chip selects (CSn) are used to determine which device is the target for communication.
+
The <code>SPI</code> protocol works in a master/slave setup. The master is responsible for sending the clock pulses. At each clock pulse, data will be sent and received.  The rising or the falling clock edge will be used to synchronize the transfer depending on the CPOL setting.
  
'''NOTE: This isn't exactly true. SPI only requires 3 lines, not 4. The 4th line is only required if you have more than one device on the SPI bus; otherwise, you can hard-wire the CS of the only device on the SPI bus so that it is always selected.  Additionally, it's not 4 per device from the host processor, because people typically use tricks to reduce the number of lines from the host processor required for the CSn linesFor example, a 1:2 multiplexer can be used to select device A when the input line is low or device B when the input line is high. Similarly, a 2:4 multiplexer can be used to select among 4 devices. With more than 4 devices, an I²C device can often be used which will supply (typically) 8-16 GPIO lines which can be used to feed the chip select lines of the SPI devices while only requiring 2 lines from the host processor.  This means that it's usually possible to get away with using no more than 5 lines in total from the host processor.  Since the number of lines required from the host processor is generally what people are most concerned about, because these are the most difficult to come by, the above paragraph should be reworded to clarify this.  The SPI Programming page has been marked for revision due to this issue as well.'''
+
<code>SPI</code> devices have a slave select pin. Every device will share the <code>MISO</code> (Master Input Slave Output), <code>MOSI</code> (Master Output Slave Input), and <code>Clock</code> pins, but each device will have its own slave select pin (also know as chip select). The slave select pin is used to set one device to be active on the bus while deactivating the rest.  Theoretically, this means a virtually unlimited number of devices can be used on the same <code>SPI</code> bus; in practice, the number is limited by a number of factors, such as required transaction rates, mechanisms (GPIO pins, bus expanders, etc) available for selecting specific devices, and physical routing constraintsThe slave select pin can be active high or active low depending on the device.
 +
 
 +
The <code>SPI</code> protocol defines four signal lines, but only requires three to operate properly. The fourth line is only required if you have more than one device on the SPI bus; otherwise, you can hard-wire the chip select pin of the only device on the <code>SPI</code> bus so that it is always selected.  
  
 
This procedure provides an overview of how to compile and run the <code>spi_test</code> C example project. This is an example test interface for sending a transaction to an EMAC <code>SPI</code> device interface. It is only relevant if the EMAC <code>SPI</code> device interface is enabled for an external <code>SPI</code> device that is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.
 
This procedure provides an overview of how to compile and run the <code>spi_test</code> C example project. This is an example test interface for sending a transaction to an EMAC <code>SPI</code> device interface. It is only relevant if the EMAC <code>SPI</code> device interface is enabled for an external <code>SPI</code> device that is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.
Line 13: Line 15:
 
The <code>spi_test</code> project builds one executable: <code>spi_test</code>.
 
The <code>spi_test</code> project builds one executable: <code>spi_test</code>.
  
== Opening, Building, and Uploading the Project Files ==
+
== Opening, Building and Uploading the Project Files ==
  
For information on opening the project from within Eclipse, please see, [[Importing the EMAC OE SDK Projects with Eclipse]].  Then, follow [[Using the EMAC OE SDK Projects with Eclipse]] for information on how to build, upload, and execute the example.
+
For information on opening the project from within Eclipse, please see [[Importing the EMAC OE SDK Projects with Eclipse]].  Then, follow [[Using the EMAC OE SDK Projects with Eclipse]] for information on how to build, upload and execute the example.
  
'''NOTE: It would be a good idea to point them to the directory which contains this example project in addition to referring them to these generic pages.'''
+
Alternatively, the <code>Makefile</code> can be used with the <code>make</code> command from the commandline to build and upload the example.  For information on this method, please see [[Using EMAC OE SDK Example Projects]].
  
Alternatively, the <code>Makefile</code> can be used with the <code>make</code> command from the command-line to build and upload the example.  For more information on this method, please see, [[Using EMAC OE SDK Example Projects]].
+
====EMAC SDK 5.X====
  
 +
For information on opening the project from within QtCreator, please see [[Getting_Started_With_Qt_Creator#Adding_Source_Files | QtCreator: Adding Source Files]].  Then, follow [[Getting Started With Qt Creator]] for information on how to build, upload and execute the example.
 +
 +
Alternatively, the <code>CMakefile.txt</code> can be used with the <code>cmake</code> command from the commandline to build and upload the example.  For information on this method, please see [[Getting_Started_with_the_EMAC_OE_SDK#Target_Machine_Compiling | Getting Started with the EMAC OE SDK]].
 +
 +
Code can be found at http://git.emacinc.com/OE/example-projects
 
== Usage and Behavior ==
 
== Usage and Behavior ==
 
===Hardware Requirements===
 
===Hardware Requirements===
Line 26: Line 33:
 
The <code>spi_test</code> C example project will run on any EMAC carrier board which has an <code>SPI</code> interface (see also the [http://wikidev.emacinc.com/wiki/EMAC_SPI_Programming EMAC SPI Programming] page).
 
The <code>spi_test</code> C example project will run on any EMAC carrier board which has an <code>SPI</code> interface (see also the [http://wikidev.emacinc.com/wiki/EMAC_SPI_Programming EMAC SPI Programming] page).
  
=== Using spi_test ===
+
=== Using indexed_atod_test ===
 +
 
 +
The <code>indexed_atod_test</code> program is executed from the console. It takes two parameters.
 +
 
 +
root@emac-oe~:$ ./indexed_atod_test device channel
 +
 
 +
:* <code>device</code>: Name of the <code>indexed_atod</code> device node.
 +
:* <code>channel</code>: Number of <code>indexed_atod</code> channels to be displayed.
  
The <code>spi_test</code> program is executed from the console. It takes three parameters.
+
{|style="margin: 0 auto;"
 +
| [[File:Potentiometer3.jpg|left|thumb|300px|alt=A potentiometer circuit.|Figure 1: Potentiometer Circuit]]
 +
| [[File:Hdr8_chan1.jpg|right|thumb|250px|alt=HDR8 Analog I/O|Figure 2: HDR8 ANALOG I/O]]<br clear=all>
 +
|}
  
root@emac-oe~:$ ./spi_test device length mosi
+
This example command was run on an EMAC SoM-150ES carrier board with a SoM-9G20M. This test uses a potentiometer for input to the A/D. You will be communicating with the mcp3208 chip, which is an 8-channel 12-bit A/D converter with SPI Serial Interface. Figure 1 provides a potentiometer test circuit for help on connection to the SoM-150ES carrier board. A circuit like this is recommended for familiarizing oneself with the A/D code before connecting the actual device to be measured by the A/D.
  
:* <code>device</code>: Name of the <code>spi</code> device node.
+
Before running the command in the terminal, you will need to connect the potentiometer to the carrier board as diagrammed. Figure 2 shows where to connect the potentiometer on Header 8 of the SoM-150ES. You will be focusing on CARR_ANL_1 for this example.  
:* <code>length</code>: Length of <code>spi</code> transactions in bytes.
 
:* <code>mosi</code>: Hex value to be transmitted in hexadecimal.
 
  
'''NOTE: In cases like above, be sure to put argument names inside code tags.  I added them in this bullet list for you.  Generally, if a word is typed on the command line or a keyword in output, put it inside code tags to indicate that the exact word, as shown, is important.'''
+
Turn the potentiometer counter-clockwise as far as it goes. You'll see the results when you run the command below. Test results will be displayed in the terminal.
  
This example command was run on an EMAC SoM-150ES carrier board. Test results will be displayed in the terminal. 
 
 
<syntaxhighlight lang=console>
 
<syntaxhighlight lang=console>
root@emac-oe~:$ ./spi_test /dev/mcp3208 1 12
+
root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 4
 +
[0] = 83
 +
[1] = 4094
 +
[2] = 61
 +
[3] = 267
 +
</syntaxhighlight>
  
MOSI MISO
+
The results that are displayed from the terminal show channel 1 analog value at 4094. All the other results are just noise coming from the mcp3208 chip. This noise can be virtually eliminated by grounding the unused A/D inputs.
12  : 00
+
 
 +
For the second example, turn the potentiometer clockwise as far as it goes. You'll see how channel 1 results will change when running the command below. Test results will be displayed in the terminal.
 +
 
 +
<syntaxhighlight lang=console>
 +
root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 4
 +
[0] = 62
 +
[1] = 0
 +
[2] = 10
 +
[3] = 95
 
</syntaxhighlight>
 
</syntaxhighlight>
  
After running the ./spi_test /devmcp3208 1 12 command, the program displays MOSI in hexadecimal and then outputs MISO.
+
Here, the results show channel 1 analog value at zero. Your results may vary, depending on the characteristics of your potentiometer and the ADC noise present.  You can alter the value by turning the potentiometer to different positions.  
  
'''NOTE: A better description of the output is needed, along with more examples of using it.  There should be at least 3-5 examplesAs an example, you can see the way I demonstrated usage of the MySQL commandline [http://liferay.emacinc.com:8080/web/engineering/wiki/-/wiki/Main/Using+the+MySQL+Command+Line on our Liferay site].'''
+
[[File:Hdr8_chan6.jpg|center|thumb|250px|alt=HDR8 Analog I/O|Figure 3: HDR8 ANALOG I/O]]<br clear=all>
 +
 
 +
Before doing example 3, you'll want to put the wiper (B) wire on pin 9 of the SoM-150ES carrier board. Figure 3 shows that pin 9 is CARR_ANL_6. Turn the potentiometer counter-clockwise about half way. You will see how the values on channel 6 will change when running the command below. Test results will be displayed in the terminal.
 +
 
 +
<syntaxhighlight lang=console>
 +
root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 7
 +
[0] = 53
 +
[1] = 0
 +
[2] = 56
 +
[3] = 0
 +
[4] = 29
 +
[5] = 43
 +
[6] = 2044
 +
</syntaxhighlight>
 +
 
 +
The results show the channel 6 analog value at 2044, which is about half way (4095/2). You can change any of the channels value with the potentiometer as long as you put the wiper pin on the channel you want to change.
 +
 
 +
Due to ADC noise, you may see the values fluctuate slightly from one run to the next.  This is normal behavior.  This fluctuation can be more severe with some potentiometers than it is with others; some potentiometers are more prone to picking up noiseThe noise will also be more severe with longer wires, due to transmission line effectsEliminating noise is typically the greatest challenge in high resolution A/D conversion (especially at 18 bit and above), so you should expect to see some noise when performing an informal test such as this.
 +
 
 +
 
 +
==Updates for iPac9x25==
 +
In order to run these examples on the board, either follow the schematic from figure 4 or use part MIKROE-304.
 +
{|style="margin: 0 auto;"
 +
|[[File:Spi Test Schematic.png|thumb|Figure 4: Schematic used to set up spi test for iPac9x25]]
 +
<br clear=all>
 +
|}
  
 
== Summary ==
 
== Summary ==
The <code>spi_test</code> C example project demonstrates how to use the <code>SPI</code> device. <code>SPI</code> enables the serial exchange of data between two devices, one called a master and the other called a slave. It operates in full duplex mode.
+
The <code>indexed_atod_test</code> C example project demonstrates how to use a 12 bit A/D converter chip (mcp3208) with SPI Serial Interface. <code>SPI</code> is simply a way to send data from device to device in a serial fashion (bit by bit). <code>SPI</code> provides good support for communication with peripheral devices that are accessed intermittently.
 
 
'''NOTE: There can be more than one slave per master, so the second sentence needs rewording.  Common uses would be good to include here.  Speeds would be good to note, as well as benefits of SPI over I²C/SMBus.'''
 

Latest revision as of 13:22, 30 November 2022

TODO: {{#todo:Buggy (2.21.14-17:40->BS+);(2.24.14-13:00->MD-)(2.28.14-17:48->BS+)(03.10.14-13:30->MD-)(08.27.14-16:55->BS+)(09.09.14-17:20->MD-)(09.10.14-17:00->BS+)(09.13.14-21:40->MD+) |Brian Serrano|oe 4,md,Complete,bs}}

This is a guide to the spi_test example project included in the EMAC OE SDK.

The SPI protocol works in a master/slave setup. The master is responsible for sending the clock pulses. At each clock pulse, data will be sent and received. The rising or the falling clock edge will be used to synchronize the transfer depending on the CPOL setting.

SPI devices have a slave select pin. Every device will share the MISO (Master Input Slave Output), MOSI (Master Output Slave Input), and Clock pins, but each device will have its own slave select pin (also know as chip select). The slave select pin is used to set one device to be active on the bus while deactivating the rest. Theoretically, this means a virtually unlimited number of devices can be used on the same SPI bus; in practice, the number is limited by a number of factors, such as required transaction rates, mechanisms (GPIO pins, bus expanders, etc) available for selecting specific devices, and physical routing constraints. The slave select pin can be active high or active low depending on the device.

The SPI protocol defines four signal lines, but only requires three to operate properly. The fourth line is only required if you have more than one device on the SPI bus; otherwise, you can hard-wire the chip select pin of the only device on the SPI bus so that it is always selected.

This procedure provides an overview of how to compile and run the spi_test C example project. This is an example test interface for sending a transaction to an EMAC SPI device interface. It is only relevant if the EMAC SPI device interface is enabled for an external SPI device that is connected to the bus. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK.

For more information about the SPI protocol, see the following page: http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

The spi_test project builds one executable: spi_test.

Opening, Building and Uploading the Project Files

For information on opening the project from within Eclipse, please see Importing the EMAC OE SDK Projects with Eclipse. Then, follow Using the EMAC OE SDK Projects with Eclipse for information on how to build, upload and execute the example.

Alternatively, the Makefile can be used with the make command from the commandline to build and upload the example. For information on this method, please see Using EMAC OE SDK Example Projects.

EMAC SDK 5.X

For information on opening the project from within QtCreator, please see QtCreator: Adding Source Files. Then, follow Getting Started With Qt Creator for information on how to build, upload and execute the example.

Alternatively, the CMakefile.txt can be used with the cmake command from the commandline to build and upload the example. For information on this method, please see Getting Started with the EMAC OE SDK.

Code can be found at http://git.emacinc.com/OE/example-projects

Usage and Behavior

Hardware Requirements

The spi_test C example project will run on any EMAC carrier board which has an SPI interface (see also the EMAC SPI Programming page).

Using indexed_atod_test

The indexed_atod_test program is executed from the console. It takes two parameters.

root@emac-oe~:$ ./indexed_atod_test device channel
  • device: Name of the indexed_atod device node.
  • channel: Number of indexed_atod channels to be displayed.
A potentiometer circuit.
Figure 1: Potentiometer Circuit
HDR8 Analog I/O
Figure 2: HDR8 ANALOG I/O

This example command was run on an EMAC SoM-150ES carrier board with a SoM-9G20M. This test uses a potentiometer for input to the A/D. You will be communicating with the mcp3208 chip, which is an 8-channel 12-bit A/D converter with SPI Serial Interface. Figure 1 provides a potentiometer test circuit for help on connection to the SoM-150ES carrier board. A circuit like this is recommended for familiarizing oneself with the A/D code before connecting the actual device to be measured by the A/D.

Before running the command in the terminal, you will need to connect the potentiometer to the carrier board as diagrammed. Figure 2 shows where to connect the potentiometer on Header 8 of the SoM-150ES. You will be focusing on CARR_ANL_1 for this example.

Turn the potentiometer counter-clockwise as far as it goes. You'll see the results when you run the command below. Test results will be displayed in the terminal.

root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 4
[0] = 83
[1] = 4094
[2] = 61
[3] = 267

The results that are displayed from the terminal show channel 1 analog value at 4094. All the other results are just noise coming from the mcp3208 chip. This noise can be virtually eliminated by grounding the unused A/D inputs.

For the second example, turn the potentiometer clockwise as far as it goes. You'll see how channel 1 results will change when running the command below. Test results will be displayed in the terminal.

root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 4
[0] = 62
[1] = 0
[2] = 10
[3] = 95

Here, the results show channel 1 analog value at zero. Your results may vary, depending on the characteristics of your potentiometer and the ADC noise present. You can alter the value by turning the potentiometer to different positions.

HDR8 Analog I/O
Figure 3: HDR8 ANALOG I/O


Before doing example 3, you'll want to put the wiper (B) wire on pin 9 of the SoM-150ES carrier board. Figure 3 shows that pin 9 is CARR_ANL_6. Turn the potentiometer counter-clockwise about half way. You will see how the values on channel 6 will change when running the command below. Test results will be displayed in the terminal.

root@emac-oe~:$ ./indexed_atod_test /dev/mcp3208-gpio 7
[0] = 53
[1] = 0
[2] = 56
[3] = 0
[4] = 29
[5] = 43
[6] = 2044

The results show the channel 6 analog value at 2044, which is about half way (4095/2). You can change any of the channels value with the potentiometer as long as you put the wiper pin on the channel you want to change.

Due to ADC noise, you may see the values fluctuate slightly from one run to the next. This is normal behavior. This fluctuation can be more severe with some potentiometers than it is with others; some potentiometers are more prone to picking up noise. The noise will also be more severe with longer wires, due to transmission line effects. Eliminating noise is typically the greatest challenge in high resolution A/D conversion (especially at 18 bit and above), so you should expect to see some noise when performing an informal test such as this.


Updates for iPac9x25

In order to run these examples on the board, either follow the schematic from figure 4 or use part MIKROE-304.

Figure 4: Schematic used to set up spi test for iPac9x25


Summary

The indexed_atod_test C example project demonstrates how to use a 12 bit A/D converter chip (mcp3208) with SPI Serial Interface. SPI is simply a way to send data from device to device in a serial fashion (bit by bit). SPI provides good support for communication with peripheral devices that are accessed intermittently.