284 lines
18 KiB
HTML
284 lines
18 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.10"/>
|
|
<title>VectorNav C Library: spi/main.c</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<script type="text/javascript">
|
|
$(document).ready(function() { init_search(); });
|
|
</script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectlogo"><img alt="Logo" src="proglib.png"/></td>
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">VectorNav C Library
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.10 -->
|
|
<script type="text/javascript">
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search');
|
|
</script>
|
|
<div id="navrow1" class="tabs">
|
|
<ul class="tablist">
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="annotated.html"><span>Data Structures</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li><a href="examples.html"><span>Examples</span></a></li>
|
|
<li>
|
|
<div id="MSearchBox" class="MSearchBoxInactive">
|
|
<span class="left">
|
|
<img id="MSearchSelect" src="search/mag_sel.png"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
alt=""/>
|
|
<input type="text" id="MSearchField" value="Search" accesskey="S"
|
|
onfocus="searchBox.OnSearchFieldFocus(true)"
|
|
onblur="searchBox.OnSearchFieldFocus(false)"
|
|
onkeyup="searchBox.OnSearchFieldChange(event)"/>
|
|
</span><span class="right">
|
|
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
|
|
</span>
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div><!-- top -->
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">spi/main.c</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<p>This example illustrates communicating with a VectorNav sensor using the SPI protocol. Since access to the low-level SPI communication interface is specific to each platform, this example "mocks" the SPI module and serve as a starting point for replacing with the SPI access code specific to your platforms.</p>
|
|
<h1><a class="anchor" id="vsCSpi"></a>
|
|
Visual Studio (Windows)</h1>
|
|
<ol type="1">
|
|
<li>Open the solution file for your specific Visual Studio version located at <code><root>/c/examples/spi/projects/vs20XX/spi.sln</code>.<br />
|
|
<br />
|
|
</li>
|
|
<li>Build the entire solution by going to the menu <code>BUILD -> Build Solution</code>.<br />
|
|
<br />
|
|
</li>
|
|
<li>Right-click the project <code>spi</code> and select <code>Debug -> Start new instance</code>.</li>
|
|
</ol>
|
|
<h1><a class="anchor" id="makeCSpi"></a>
|
|
Make (Linux/Mac OS X)</h1>
|
|
<ol type="1">
|
|
<li>Open a terminal and change to the directory <code><root>/c/examples/spi</code> .<br />
|
|
<br />
|
|
</li>
|
|
<li>To build the example, run the command <code>make</code> .<br />
|
|
<br />
|
|
</li>
|
|
<li>Run the example by executing the command <code>./spi</code> .</li>
|
|
</ol>
|
|
<h1><a class="anchor" id="clionCSpi"></a>
|
|
CLion (Windows/Linux/Mac OS X)</h1>
|
|
<ol type="1">
|
|
<li>Open the project file located at <code><root>/c/examples/spi</code> in CLion.<br />
|
|
<br />
|
|
</li>
|
|
<li>Make sure the <code>spi</code> configuration is active. You can set this by clicking the small drop-down list in the upper-right corner and selecting the option <code>spi</code>.<br />
|
|
<br />
|
|
</li>
|
|
<li>Build the solution by going to the menu <code>Run -> Build</code>.<br />
|
|
<br />
|
|
</li>
|
|
<li>Run the example by going to the menu <code>Run -> Run 'spi'</code>.</li>
|
|
</ol>
|
|
<h1><a class="anchor" id="otherCSpi"></a>
|
|
Other</h1>
|
|
<p>To compile and run for an environment not listed here, you will need to add all of the <code>*.c</code> files in the directory <code><root>/c/src</code> along with the file located at <code><root>/c/examples/spi/main.c</code> to your project for compilation. You will also need to add <code><root>/c/include</code> to your include directories.</p>
|
|
<h1><a class="anchor" id="cSpiSource"></a>
|
|
Source</h1>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <stdio.h></span></div>
|
|
<div class="line"><span class="preprocessor">#include <string.h></span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="comment">/* Include to get access to the VectorNav SPI functions. */</span></div>
|
|
<div class="line"><span class="preprocessor">#include "vn/protocol/spi.h"</span></div>
|
|
<div class="line"><span class="preprocessor">#include "vn/protocol/common.h"</span></div>
|
|
<div class="line"><span class="preprocessor">#include "<a class="code" href="thread_8h.html">vn/xplat/thread.h</a>"</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">int</span> display_error(<span class="keyword">const</span> <span class="keywordtype">char</span>* msg);</div>
|
|
<div class="line"><span class="keywordtype">void</span> mockspi_initialize(<span class="keywordtype">void</span>);</div>
|
|
<div class="line"><span class="keywordtype">void</span> mockspi_writeread(<span class="keyword">const</span> <span class="keywordtype">char</span>* dataOut, <span class="keywordtype">size_t</span> dataOutSize, <span class="keywordtype">char</span>* dataIn);</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="comment">/* This example walks through using the VectorNav C Library to connect to</span></div>
|
|
<div class="line"><span class="comment"> * and interact with a mock VectorNav sensor through the Serial Peripheral</span></div>
|
|
<div class="line"><span class="comment"> * Interaface (SPI). Once you work through and understand the example, you</span></div>
|
|
<div class="line"><span class="comment"> * may want to try replacing the mock functions with ones that interface</span></div>
|
|
<div class="line"><span class="comment"> * with your SPI subsystem. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="keywordtype">char</span> txbuf[0x100];</div>
|
|
<div class="line"> <span class="keywordtype">char</span> rxbuf[0x100];</div>
|
|
<div class="line"> <span class="keywordtype">size_t</span> bufcmdsize;</div>
|
|
<div class="line"> <span class="keywordtype">size_t</span> responseSize;</div>
|
|
<div class="line"> <a name="_a0"></a><a class="code" href="unionvec3f.html">vec3f</a> ypr;</div>
|
|
<div class="line"> <span class="keywordtype">char</span> strConversions[50];</div>
|
|
<div class="line"> <span class="keywordtype">size_t</span> i;</div>
|
|
<div class="line"></div>
|
|
<div class="line"> mockspi_initialize();</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* With SPI 'initialize', let's work through reading the current yaw, pitch,</span></div>
|
|
<div class="line"><span class="comment"> * roll values from the sensor. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* First we must generate the command to query the sensor. */</span></div>
|
|
<div class="line"> bufcmdsize = <span class="keyword">sizeof</span>(txbuf); <span class="comment">/* First set this variable to the size of the buffer. */</span></div>
|
|
<div class="line"> <span class="keywordflow">if</span> (<a name="a1"></a><a class="code" href="group__spi__genread__functions.html#ga9a894bb76035e5e778a4db7688cf60d3">VnSpi_genReadYawPitchRoll</a>(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> &bufcmdsize, <span class="comment">/* Pass in the pointer since the function will set this with the length of the command generate. */</span></div>
|
|
<div class="line"> 0,</div>
|
|
<div class="line"> &responseSize) != E_NONE)</div>
|
|
<div class="line"> <span class="keywordflow">return</span> display_error(<span class="stringliteral">"Error generating read yaw, pitch, roll command.\n"</span>);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Send out the command over SPI. */</span></div>
|
|
<div class="line"> mockspi_writeread(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> responseSize,</div>
|
|
<div class="line"> rxbuf);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Now the sensor will have responded with data on this transaction but</span></div>
|
|
<div class="line"><span class="comment"> * since the sensor only responds on following transaction, we will</span></div>
|
|
<div class="line"><span class="comment"> * disregard this data. These double transactions can be mitigated by only</span></div>
|
|
<div class="line"><span class="comment"> * requesting the same data each time or by staggering the the requested</span></div>
|
|
<div class="line"><span class="comment"> * data in an appropriate order. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Make sure enough time has passed for the sensor to format the previous response. */</span></div>
|
|
<div class="line"> <a name="a2"></a><a class="code" href="thread_8h.html#ae184a96ccf6c94c4b9d7b937655b0c44">VnThread_sleepMs</a>(1); <span class="comment">/* Actual sensor requirement is only 50 us. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Retransmit so the sensor responds with the previous request. */</span></div>
|
|
<div class="line"> mockspi_writeread(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> responseSize,</div>
|
|
<div class="line"> rxbuf);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Now parse the received response. */</span></div>
|
|
<div class="line"> <span class="keywordflow">if</span> (VnSpi_parseYawPitchRoll(</div>
|
|
<div class="line"> rxbuf,</div>
|
|
<div class="line"> &ypr) != E_NONE)</div>
|
|
<div class="line"> <span class="keywordflow">return</span> display_error(<span class="stringliteral">"Error parsing yaw, pitch, roll.\n"</span>);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> str_vec3f(strConversions, ypr);</div>
|
|
<div class="line"> printf(<span class="stringliteral">"Current YPR: %s\n"</span>, strConversions);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* We have now shown how to process one full command transaction which</span></div>
|
|
<div class="line"><span class="comment"> * requires two SPI transactions because the VectorNav sensor requires a</span></div>
|
|
<div class="line"><span class="comment"> * short amount of time to ready the response. Now we can optimize this</span></div>
|
|
<div class="line"><span class="comment"> * transaction squence to utilize this behavior when we are only requesting</span></div>
|
|
<div class="line"><span class="comment"> * the same data each time. This is illustrated in the for loop below. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="keywordflow">for</span> (i = 0; i < 25; i++)</div>
|
|
<div class="line"> {</div>
|
|
<div class="line"> <span class="comment">/* For this loop, we want to display data at ~10 Hz. */</span></div>
|
|
<div class="line"> <a class="code" href="thread_8h.html#ae184a96ccf6c94c4b9d7b937655b0c44">VnThread_sleepMs</a>(100);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Perform a transaction for the same sensor register. */</span></div>
|
|
<div class="line"> mockspi_writeread(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> responseSize,</div>
|
|
<div class="line"> rxbuf);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Now since the previous command was for the same register, we will</span></div>
|
|
<div class="line"><span class="comment"> * have valid data and can print/use the results. */</span></div>
|
|
<div class="line"> <span class="keywordflow">if</span> (VnSpi_parseYawPitchRoll(</div>
|
|
<div class="line"> rxbuf,</div>
|
|
<div class="line"> &ypr) != E_NONE)</div>
|
|
<div class="line"> <span class="keywordflow">return</span> display_error(<span class="stringliteral">"Error parsing yaw, pitch, roll.\n"</span>);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> str_vec3f(strConversions, ypr);</div>
|
|
<div class="line"> printf(<span class="stringliteral">"Current YPR: %s\n"</span>, strConversions);</div>
|
|
<div class="line"> }</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* We illustrate how to write settings to the sensor by changing the</span></div>
|
|
<div class="line"><span class="comment"> * asynchronous data output type. Note that this setting only affects the</span></div>
|
|
<div class="line"><span class="comment"> * output on the UART ports and has no effect on the SPI ports. It is only</span></div>
|
|
<div class="line"><span class="comment"> * used for illustration purposes. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Remember to reset the bufcmdsize variable to let the function know how</span></div>
|
|
<div class="line"><span class="comment"> * large the provided buffer is. */</span></div>
|
|
<div class="line"> bufcmdsize = <span class="keyword">sizeof</span>(txbuf);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="keywordflow">if</span> (<a name="a3"></a><a class="code" href="group__spi__genwrite__functions.html#gae5e4e9b342f5eb6462541b7f5af9523a">VnSpi_genWriteAsyncDataOutputType</a>(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> &bufcmdsize,</div>
|
|
<div class="line"> 0,</div>
|
|
<div class="line"> &responseSize,</div>
|
|
<div class="line"> VNYPR) != E_NONE)</div>
|
|
<div class="line"> <span class="keywordflow">return</span> display_error(<span class="stringliteral">"Error generating write async data output type command.\n"</span>);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> mockspi_writeread(</div>
|
|
<div class="line"> txbuf,</div>
|
|
<div class="line"> responseSize,</div>
|
|
<div class="line"> rxbuf);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">int</span> display_error(<span class="keyword">const</span> <span class="keywordtype">char</span>* msg)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> printf(<span class="stringliteral">"%s\n"</span>, msg);</div>
|
|
<div class="line"> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> mockspi_initialize(<span class="keywordtype">void</span>)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="comment">/* Do nothing since we are faking the SPI interface. */</span></div>
|
|
<div class="line">}</div>
|
|
<div class="line"></div>
|
|
<div class="line"><span class="keywordtype">void</span> mockspi_writeread(<span class="keyword">const</span> <span class="keywordtype">char</span>* dataOut, <span class="keywordtype">size_t</span> dataOutSize, <span class="keywordtype">char</span>* dataIn)</div>
|
|
<div class="line">{</div>
|
|
<div class="line"> <span class="comment">/* This function fakes a SPI subsystem for this example. */</span></div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="keywordtype">char</span> yprResponse[] = { 0x00, 0x01, 0x08, 0x00, 0xd8, 0x9c, 0xd4, 0x42, 0x44, 0xba, 0x9e, 0x40, 0x4e, 0xe4, 0x8b, 0x40 };</div>
|
|
<div class="line"></div>
|
|
<div class="line"> <span class="comment">/* Silence 'unreferenced formal parameters' warning in Visual Studio. */</span></div>
|
|
<div class="line"> (dataOut);</div>
|
|
<div class="line"> (dataOutSize);</div>
|
|
<div class="line"></div>
|
|
<div class="line"> memcpy(dataIn, yprResponse, <span class="keyword">sizeof</span>(yprResponse));</div>
|
|
<div class="line">}</div>
|
|
</div><!-- fragment --> </div><!-- contents -->
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated on Tue Jan 3 2017 18:26:57 for VectorNav C Library by  <a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
|
</a> 1.8.10
|
|
</small></address>
|
|
</body>
|
|
</html>
|