theoryOperation.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <title>Theory of Operation</title>
  7. <title>CMSIS-Driver: Theory of Operation</title>
  8. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  9. <link href="cmsis.css" rel="stylesheet" type="text/css" />
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <script type="text/javascript" src="printComponentTabs.js"></script>
  13. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  14. <script type="text/javascript" src="resize.js"></script>
  15. <script type="text/javascript" src="navtree.js"></script>
  16. <script type="text/javascript">
  17. $(document).ready(initResizable);
  18. $(window).load(resizeHeight);
  19. </script>
  20. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  21. <script type="text/javascript" src="search/search.js"></script>
  22. <script type="text/javascript">
  23. $(document).ready(function() { searchBox.OnSelectItem(0); });
  24. </script>
  25. </head>
  26. <body>
  27. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  28. <div id="titlearea">
  29. <table cellspacing="0" cellpadding="0">
  30. <tbody>
  31. <tr style="height: 46px;">
  32. <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  33. <td style="padding-left: 0.5em;">
  34. <div id="projectname">CMSIS-Driver
  35. &#160;<span id="projectnumber">Version 2.6.0</span>
  36. </div>
  37. <div id="projectbrief">Peripheral Interface for Middleware and Application Code</div>
  38. </td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. </div>
  43. <!-- end header part -->
  44. <div id="CMSISnav" class="tabs1">
  45. <ul class="tablist">
  46. <script type="text/javascript">
  47. <!--
  48. writeComponentTabs.call(this);
  49. //-->
  50. </script>
  51. </ul>
  52. </div>
  53. <!-- Generated by Doxygen 1.8.6 -->
  54. <script type="text/javascript">
  55. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  56. </script>
  57. <div id="navrow1" class="tabs">
  58. <ul class="tablist">
  59. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  60. <li class="current"><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
  61. <li><a href="modules.html"><span>Reference</span></a></li>
  62. <li>
  63. <div id="MSearchBox" class="MSearchBoxInactive">
  64. <span class="left">
  65. <img id="MSearchSelect" src="search/mag_sel.png"
  66. onmouseover="return searchBox.OnSearchSelectShow()"
  67. onmouseout="return searchBox.OnSearchSelectHide()"
  68. alt=""/>
  69. <input type="text" id="MSearchField" value="Search" accesskey="S"
  70. onfocus="searchBox.OnSearchFieldFocus(true)"
  71. onblur="searchBox.OnSearchFieldFocus(false)"
  72. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  73. </span><span class="right">
  74. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
  75. </span>
  76. </div>
  77. </li>
  78. </ul>
  79. </div>
  80. </div><!-- top -->
  81. <div id="side-nav" class="ui-resizable side-nav-resizable">
  82. <div id="nav-tree">
  83. <div id="nav-tree-contents">
  84. <div id="nav-sync" class="sync"></div>
  85. </div>
  86. </div>
  87. <div id="splitbar" style="-moz-user-select:none;"
  88. class="ui-resizable-handle">
  89. </div>
  90. </div>
  91. <script type="text/javascript">
  92. $(document).ready(function(){initNavTree('theoryOperation.html','');});
  93. </script>
  94. <div id="doc-content">
  95. <!-- window showing the filter options -->
  96. <div id="MSearchSelectWindow"
  97. onmouseover="return searchBox.OnSearchSelectShow()"
  98. onmouseout="return searchBox.OnSearchSelectHide()"
  99. onkeydown="return searchBox.OnSearchSelectKey(event)">
  100. <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Pages</a></div>
  101. <!-- iframe showing the search results (closed by default) -->
  102. <div id="MSearchResultsWindow">
  103. <iframe src="javascript:void(0)" frameborder="0"
  104. name="MSearchResults" id="MSearchResults">
  105. </iframe>
  106. </div>
  107. <div class="header">
  108. <div class="headertitle">
  109. <div class="title">Theory of Operation </div> </div>
  110. </div><!--header-->
  111. <div class="contents">
  112. <div class="toc"><h3>Table of Contents</h3>
  113. <ul><li class="level1"><a href="#DriverFunctions">Common Driver Functions</a><ul><li class="level2"><a href="#ProcessorMode">Cortex-M Processor Mode</a></li>
  114. </ul>
  115. </li>
  116. <li class="level1"><a href="#CallSequence">Function Call Sequence</a><ul><li class="level2"><a href="#CS_start">Start Sequence</a></li>
  117. <li class="level2"><a href="#CS_stop">Stop Sequence</a></li>
  118. </ul>
  119. </li>
  120. <li class="level1"><a href="#Share_IO">Shared I/O Pins</a></li>
  121. <li class="level1"><a href="#Data_Xfer_Functions">Data Transfer Functions</a></li>
  122. <li class="level1"><a href="#AccessStruct">Access Struct</a><ul><li class="level2"><a href="#DriverInstances">Driver Instances</a></li>
  123. </ul>
  124. </li>
  125. <li class="level1"><a href="#DriverConfiguration">Driver Configuration</a></li>
  126. <li class="level1"><a href="#CodeExample">Code Example</a></li>
  127. </ul>
  128. </div>
  129. <div class="textblock"><p>This section gives an overview of the general operation of CMSIS-Drivers. It explains the <a class="el" href="theoryOperation.html#DriverFunctions">Common Driver Functions</a> that are common in all CMSIS-Drivers along with the <a class="el" href="theoryOperation.html#CallSequence">Function Call Sequence</a>. The topic <a class="el" href="theoryOperation.html#Data_Xfer_Functions">Data Transfer Functions</a> describes how data read/write operations to the peripheral are implemented.</p>
  130. <p>Each CMSIS-Driver defines an <a class="el" href="theoryOperation.html#AccessStruct">Access Struct</a> for calling the various driver functions and each peripheral (that is accessed via a CMSIS-Driver) has one <a class="el" href="theoryOperation.html#DriverInstances">Driver Instance</a>.</p>
  131. <h1><a class="anchor" id="DriverFunctions"></a>
  132. Common Driver Functions</h1>
  133. <p>Each CMSIS-Driver contains these functions:</p>
  134. <ul>
  135. <li><b>GetVersion:</b> can be called at any time to obtain version information of the driver interface.</li>
  136. <li><b>GetCapabilities:</b> can be called at any time to obtain capabilities of the driver interface.</li>
  137. <li><b>Initialize:</b> must be called before powering the peripheral using <b>PowerControl</b>. This function performs the following:<ul>
  138. <li>allocate I/O resources.<ul>
  139. <li>register an optional <b>SignalEvent</b> callback function.</li>
  140. </ul>
  141. </li>
  142. </ul>
  143. </li>
  144. <li><b>SignalEvent:</b> is an optional callback function that is registered with the <b>Initialize</b> function. This callback function is initiated from interrupt service routines and indicates hardware events or the completion of a data block transfer operation.</li>
  145. <li><b>PowerControl:</b> Controls the power profile of the peripheral and needs to be called after <b>Initialize</b>. Typically, three power options are available:<ul>
  146. <li><code>ARM_POWER_FULL:</code> Peripheral is turned on and fully operational. The driver initializes the peripheral registers, interrupts, and (optionally) DMA.</li>
  147. <li><code>ARM_POWER_LOW:</code> (optional) Peripheral is in low power mode and partially operational; usually, it can detect external events and wake-up.</li>
  148. <li><code>ARM_POWER_OFF:</code> Peripheral is turned off and not operational (pending operations are terminated). This is the state after device reset.</li>
  149. </ul>
  150. </li>
  151. <li><b>Uninitialize:</b> Complementary function to Initialize. Releases the I/O pin resources used by the interface.</li>
  152. <li><b>Control:</b> Several drivers provide a control function to configure communication parameters or execute miscellaneous control functions.</li>
  153. </ul>
  154. <p>The section <a class="el" href="theoryOperation.html#CallSequence">Function Call Sequence</a> contains more information on the operation of each function. Additional functions are specific to each driver interface and are described in the individual sections of each driver.</p>
  155. <h2><a class="anchor" id="ProcessorMode"></a>
  156. Cortex-M Processor Mode</h2>
  157. <p>The CMSIS-Driver functions access peripherals and interrupts and are designed to execute in <b>Privileged</b> mode. When calling CMSIS-Driver functions from RTOS threads, it should be ensure that these threads execute in <b>Privileged</b> mode.</p>
  158. <h1><a class="anchor" id="CallSequence"></a>
  159. Function Call Sequence</h1>
  160. <p>For normal operation of the driver, the API functions <b>GetVersion</b>, <b>GetCapabilities</b>, <b>Initialize</b>, <b>PowerControl</b>, <b>Uninitialize</b> are called in the following order:</p>
  161. <div align="center">
  162. <img src="msc_inline_mscgraph_1.png" alt="msc_inline_mscgraph_1" border="0" usemap="#msc_inline_mscgraph_1.map"/>
  163. <map name="msc_inline_mscgraph_1.map" id="msc_inline_mscgraph_1.map"></map>
  164. </div>
  165. <p>The functions <b>GetVersion</b> and <b>GetCapabilities</b> can be called any time to obtain the required information from the driver. These functions return always the same information.</p>
  166. <h2><a class="anchor" id="CS_start"></a>
  167. Start Sequence</h2>
  168. <p>To start working with a peripheral the functions <b>Initialize</b> and <b>PowerControl</b> need to be called in this order: </p>
  169. <div class="fragment"><div class="line">drv-&gt;Initialize (...); <span class="comment">// Allocate I/O pins</span></div>
  170. <div class="line">drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5abed52b77a9ce4775570e44a842b1295e">ARM_POWER_FULL</a>); <span class="comment">// Power up peripheral, setup IRQ/DMA</span></div>
  171. </div><!-- fragment --><ul>
  172. <li><b>Initialize</b> typically allocates the I/O resources (pins) for the peripheral. The function can be called multiple times; if the I/O resources are already initialized it performs no operation and just returns with <a class="el" href="group__execution__status.html#ga85752c5de59e8adeb001e35ff5be6be7">ARM_DRIVER_OK</a>.</li>
  173. <li><b>PowerControl</b> (<code>ARM_POWER_FULL</code>) sets the peripheral registers including interrupt (NVIC) and optionally DMA. The function can be called multiple times; if the registers are already set it performs no operation and just returns with <a class="el" href="group__execution__status.html#ga85752c5de59e8adeb001e35ff5be6be7">ARM_DRIVER_OK</a>.</li>
  174. </ul>
  175. <h2><a class="anchor" id="CS_stop"></a>
  176. Stop Sequence</h2>
  177. <p>To stop working with a peripheral the functions <b>PowerControl</b> and <b>Uninitialize</b> need to be called in this order: </p>
  178. <div class="fragment"><div class="line">drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5ab6f5becc85ebd51c3dd2524a95d2ca35">ARM_POWER_OFF</a>); <span class="comment">// Terminate any pending transfers, reset IRQ/DMA, power off peripheral</span></div>
  179. <div class="line">drv-&gt;Uninitialize (...); <span class="comment">// Release I/O pins</span></div>
  180. </div><!-- fragment --><p> The functions <b>PowerControl</b> and <b>Uninitialize</b> always execute and can be used to put the peripheral into a <b>Safe State</b>, for example after any data transmission errors. To restart the peripheral in a error condition, you should first execute the <a class="el" href="theoryOperation.html#CS_stop">Stop Sequence</a> and then the <a class="el" href="theoryOperation.html#CS_start">Start Sequence</a>.</p>
  181. <ul>
  182. <li><b>PowerControl</b> (<code>ARM_POWER_OFF</code>) terminates any pending data transfers with the peripheral, disables the peripheral and leaves it in a defined mode (typically the reset state).<ul>
  183. <li>when DMA is used it is disabled (including the interrupts)</li>
  184. <li>peripheral interrupts are disabled on NVIC level</li>
  185. <li>the peripheral is reset using a dedicated reset mechanism (if available) or by clearing the peripheral registers</li>
  186. <li>pending peripheral interrupts are cleared on NVIC level</li>
  187. <li>driver variables are cleared</li>
  188. </ul>
  189. </li>
  190. <li><b>Uninitialize</b> always releases I/O pin resources.</li>
  191. </ul>
  192. <h1><a class="anchor" id="Share_IO"></a>
  193. Shared I/O Pins</h1>
  194. <p>All CMSIS-Driver provide a <a class="el" href="theoryOperation.html#CS_start">Start Sequence</a> and <a class="el" href="theoryOperation.html#CS_stop">Stop Sequence</a>. Therefore two different drivers can share the same I/O pins, for example UART1 and SPI1 can have overlapping I/O pins. In this case the communication channels can be used as shown below:</p>
  195. <div class="fragment"><div class="line">SPI1drv-&gt;Initialize (...); <span class="comment">// Start SPI1</span></div>
  196. <div class="line">SPI1drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5abed52b77a9ce4775570e44a842b1295e">ARM_POWER_FULL</a>);</div>
  197. <div class="line"> ... <span class="comment">// Do operations with SPI1</span></div>
  198. <div class="line">SPI1drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5ab6f5becc85ebd51c3dd2524a95d2ca35">ARM_POWER_OFF</a>); <span class="comment">// Stop SPI1</span></div>
  199. <div class="line">SPI1drv-&gt;Uninitialize ();</div>
  200. <div class="line"> ...</div>
  201. <div class="line">USART1drv-&gt;Initialize (...); <span class="comment">// Start USART1</span></div>
  202. <div class="line">USART1drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5abed52b77a9ce4775570e44a842b1295e">ARM_POWER_FULL</a>);</div>
  203. <div class="line"> ... <span class="comment">// Do operations with USART1</span></div>
  204. <div class="line">USART1drv-&gt;PowerControl (<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5ab6f5becc85ebd51c3dd2524a95d2ca35">ARM_POWER_OFF</a>); <span class="comment">// Stop USART1</span></div>
  205. <div class="line">USART1drv-&gt;Uninitialize ();</div>
  206. </div><!-- fragment --><h1><a class="anchor" id="Data_Xfer_Functions"></a>
  207. Data Transfer Functions</h1>
  208. <p>A CMSIS-Driver implements non-blocking functions to transfer data to a peripheral. This means that the driver configures the read or write access to the peripheral and instantly returns to the calling application. The function names for data transfer end with:</p>
  209. <ul>
  210. <li><b>Send</b> to write data to a peripheral.</li>
  211. <li><b>Receive</b> to read data from a peripheral.</li>
  212. <li><b>Transfer</b> to indicate combined read/write operations to a peripheral.</li>
  213. </ul>
  214. <p>During a data transfer, the application can query the number of transferred data items using functions named <b>Get<em>xxx</em>Count</b>. On completion of a data transfer, the driver calls a callback function with a specific event code.</p>
  215. <p>During the data exchange with the peripheral, the application can decide to:</p>
  216. <ul>
  217. <li>Wait (using an RTOS scheduler) for the callback completion event. The RTOS is controlled by the application code which makes the driver itself RTOS independent.</li>
  218. <li>Use polling functions that return the number of transferred data items to show progress information or partly read or fill data transfer buffers.</li>
  219. <li>Prepare another data transfer buffer for the next data transfer.</li>
  220. </ul>
  221. <p>The following diagram shows the basic communication flow when using the <b>_Send</b> function in an application.</p>
  222. <div class="image">
  223. <img src="Non_blocking_transmit_small.png" alt="Non_blocking_transmit_small.png"/>
  224. <div class="caption">
  225. Non-blocking Send Function</div></div>
  226. <h1><a class="anchor" id="AccessStruct"></a>
  227. Access Struct</h1>
  228. <p>A CMSIS-Driver publishes an <a class="el" href="theoryOperation.html#AccessStruct">Access Struct</a> with the data type name ARM_DRIVER_xxxx that gives to access the driver functions.</p>
  229. <p><b>Code</b> <b>Example:</b> <b>Function</b> <b>Access</b> <b>of</b> <b>the</b> <b>SPI</b> <b>driver</b> </p>
  230. <div class="fragment"><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>_ARM_DRIVER_SPI {</div>
  231. <div class="line"> <a class="code" href="group__common__drv__gr.html#structARM__DRIVER__VERSION">ARM_DRIVER_VERSION</a> (*GetVersion) (void);</div>
  232. <div class="line"> <a class="code" href="group__spi__interface__gr.html#structARM__SPI__CAPABILITIES">ARM_SPI_CAPABILITIES</a> (*GetCapabilities) (void);</div>
  233. <div class="line"> int32_t (*Initialize) (<a class="code" href="group__spi__interface__gr.html#gafde9205364241ee81290adc0481c6640">ARM_SPI_SignalEvent_t</a> cb_event);</div>
  234. <div class="line"> int32_t (*Uninitialize) (void);</div>
  235. <div class="line"> int32_t (*PowerControl) (<a class="code" href="group__common__drv__gr.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5">ARM_POWER_STATE</a> state);</div>
  236. <div class="line"> int32_t (*Send) (<span class="keyword">const</span> <span class="keywordtype">void</span> *data, uint32_t num);</div>
  237. <div class="line"> int32_t (*Receive) ( <span class="keywordtype">void</span> *data, uint32_t num);</div>
  238. <div class="line"> int32_t (*Transfer) (<span class="keyword">const</span> <span class="keywordtype">void</span> *data_out, <span class="keywordtype">void</span> *data_in, uint32_t num);</div>
  239. <div class="line"> uint32_t (*GetDataCount) (void);</div>
  240. <div class="line"> int32_t (*Control) (uint32_t control, uint32_t arg);</div>
  241. <div class="line"> <a class="code" href="group__spi__interface__gr.html#structARM__SPI__STATUS">ARM_SPI_STATUS</a> (*GetStatus) (void);</div>
  242. <div class="line">} <span class="keyword">const</span> <a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a>;</div>
  243. </div><!-- fragment --><h2><a class="anchor" id="DriverInstances"></a>
  244. Driver Instances</h2>
  245. <p>A device may offer several peripherals of the same type. For such devices, the CMSIS-Driver publishes multiple instances of the <a class="el" href="theoryOperation.html#AccessStruct">Access Struct</a>. The name of each driver instance reflects the names of the peripheral available in the device.</p>
  246. <p><b>Code</b> <b>Example:</b> <a class="el" href="theoryOperation.html#AccessStruct">Access Struct</a> <b>for</b> <b>three</b> <b>SPIs</b> <b>in</b> <b>a</b> <b>microcontroller</b> <b>device</b>. </p>
  247. <div class="fragment"><div class="line"><a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a> Driver_SPI1; <span class="comment">// access functions for SPI1 interface</span></div>
  248. <div class="line"><a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a> Driver_SPI2; <span class="comment">// access functions for SPI2 interface</span></div>
  249. <div class="line"><a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a> Driver_SPI3; <span class="comment">// access functions for SPI3 interface</span></div>
  250. </div><!-- fragment --><p>The access functions can be passed to middleware to specify the driver instance that the middleware should use for communication.</p>
  251. <p><b>Example:</b> </p>
  252. <div class="fragment"><div class="line"><span class="keywordtype">void</span> init_middleware (<a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a> *Drv_spi) ...</div>
  253. <div class="line">\\ inside the middleware the SPI driver functions are called with:</div>
  254. <div class="line">\\ Drv_spi-&gt;function (...);</div>
  255. </div><!-- fragment --><div class="fragment"><div class="line">\\ setup middleware</div>
  256. <div class="line">init_middleware (&amp;Driver_SPI1); <span class="comment">// connect middleware to SPI1 interface</span></div>
  257. <div class="line"> :</div>
  258. <div class="line">init_middleware (&amp;Driver_SPI2); <span class="comment">// connect middleware to SPI2 interface</span></div>
  259. </div><!-- fragment --><h1><a class="anchor" id="DriverConfiguration"></a>
  260. Driver Configuration</h1>
  261. <p>For a device family, the drivers may be configurable. The <a class="el" href="referenceImplementation.html">Reference Implementation</a> stores configuration options in a central file with the name <b>RTE_Device.h</b>. However, the configuration of the drivers itself is not part of the CMSIS-Driver specification.</p>
  262. <h1><a class="anchor" id="CodeExample"></a>
  263. Code Example</h1>
  264. <p>The following example code shows the usage of the SPI interface.</p>
  265. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="Driver__SPI_8h.html">Driver_SPI.h</a>&quot;</span></div>
  266. <div class="line"><span class="preprocessor">#include &quot;cmsis_os.h&quot;</span> <span class="comment">// ARM::CMSIS:RTOS:Keil RTX</span></div>
  267. <div class="line"> </div>
  268. <div class="line"> </div>
  269. <div class="line"><span class="keywordtype">void</span> mySPI_Thread(<span class="keywordtype">void</span> <span class="keyword">const</span> *argument);</div>
  270. <div class="line">osThreadId tid_mySPI_Thread;</div>
  271. <div class="line"> </div>
  272. <div class="line"> </div>
  273. <div class="line"><span class="comment">/* SPI Driver */</span></div>
  274. <div class="line"><span class="keyword">extern</span> <a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a> Driver_SPI0;</div>
  275. <div class="line"> </div>
  276. <div class="line"> </div>
  277. <div class="line"><span class="keywordtype">void</span> mySPI_callback(uint32_t event)</div>
  278. <div class="line">{</div>
  279. <div class="line"> <span class="keywordflow">switch</span> (event)</div>
  280. <div class="line"> {</div>
  281. <div class="line"> <span class="keywordflow">case</span> <a class="code" href="group__SPI__events.html#gaabdfc9e17641144cd50d36d15511a1b8">ARM_SPI_EVENT_TRANSFER_COMPLETE</a>:</div>
  282. <div class="line"> <span class="comment">/* Success: Wakeup Thread */</span></div>
  283. <div class="line"> osSignalSet(tid_mySPI_Thread, 0x01);</div>
  284. <div class="line"> <span class="keywordflow">break</span>;</div>
  285. <div class="line"> <span class="keywordflow">case</span> <a class="code" href="group__SPI__events.html#ga8e63d99c80ea56de596a8d0a51fd8244">ARM_SPI_EVENT_DATA_LOST</a>:</div>
  286. <div class="line"> <span class="comment">/* Occurs in slave mode when data is requested/sent by master</span></div>
  287. <div class="line"><span class="comment"> but send/receive/transfer operation has not been started</span></div>
  288. <div class="line"><span class="comment"> and indicates that data is lost. Occurs also in master mode</span></div>
  289. <div class="line"><span class="comment"> when driver cannot transfer data fast enough. */</span></div>
  290. <div class="line"> __breakpoint(0); <span class="comment">/* Error: Call debugger or replace with custom error handling */</span></div>
  291. <div class="line"> <span class="keywordflow">break</span>;</div>
  292. <div class="line"> <span class="keywordflow">case</span> <a class="code" href="group__SPI__events.html#ga7eaa229003689aa18598273490b3e630">ARM_SPI_EVENT_MODE_FAULT</a>:</div>
  293. <div class="line"> <span class="comment">/* Occurs in master mode when Slave Select is deactivated and</span></div>
  294. <div class="line"><span class="comment"> indicates Master Mode Fault. */</span></div>
  295. <div class="line"> __breakpoint(0); <span class="comment">/* Error: Call debugger or replace with custom error handling */</span></div>
  296. <div class="line"> <span class="keywordflow">break</span>;</div>
  297. <div class="line"> }</div>
  298. <div class="line">}</div>
  299. <div class="line"> </div>
  300. <div class="line"><span class="comment">/* Test data buffers */</span></div>
  301. <div class="line"><span class="keyword">const</span> uint8_t testdata_out[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; </div>
  302. <div class="line">uint8_t testdata_in [8];</div>
  303. <div class="line"> </div>
  304. <div class="line"><span class="keywordtype">void</span> mySPI_Thread(<span class="keywordtype">void</span> <span class="keyword">const</span>* arg)</div>
  305. <div class="line">{</div>
  306. <div class="line"> <a class="code" href="group__spi__interface__gr.html#structARM__DRIVER__SPI">ARM_DRIVER_SPI</a>* SPIdrv = &amp;Driver_SPI0;</div>
  307. <div class="line"> osEvent evt;</div>
  308. <div class="line"> </div>
  309. <div class="line"><span class="preprocessor">#ifdef DEBUG</span></div>
  310. <div class="line"><span class="preprocessor"></span> <a class="code" href="group__common__drv__gr.html#structARM__DRIVER__VERSION">ARM_DRIVER_VERSION</a> version;</div>
  311. <div class="line"> <a class="code" href="group__spi__interface__gr.html#structARM__SPI__CAPABILITIES">ARM_SPI_CAPABILITIES</a> drv_capabilities;</div>
  312. <div class="line"> </div>
  313. <div class="line"> version = SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a8834b281da48583845c044a81566c1b3">GetVersion</a>();</div>
  314. <div class="line"> <span class="keywordflow">if</span> (version.<a class="code" href="group__common__drv__gr.html#ad180da20fbde1d3dafc074af87c19540">api</a> &lt; 0x200) <span class="comment">/* requires at minimum API version 2.00 or higher */</span></div>
  315. <div class="line"> { <span class="comment">/* error handling */</span></div>
  316. <div class="line"> <span class="keywordflow">return</span>;</div>
  317. <div class="line"> }</div>
  318. <div class="line"> </div>
  319. <div class="line"> drv_capabilities = SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a065b5fc24d0204692f0f95a44351ac1e">GetCapabilities</a>();</div>
  320. <div class="line"> <span class="keywordflow">if</span> (drv_capabilities.<a class="code" href="group__spi__interface__gr.html#a309619714f0c4febaa497ebdb9b7e3ca">event_mode_fault</a> == 0)</div>
  321. <div class="line"> { <span class="comment">/* error handling */</span></div>
  322. <div class="line"> <span class="keywordflow">return</span>;</div>
  323. <div class="line"> }</div>
  324. <div class="line"><span class="preprocessor">#endif</span></div>
  325. <div class="line"><span class="preprocessor"></span> </div>
  326. <div class="line"> <span class="comment">/* Initialize the SPI driver */</span></div>
  327. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#afac50d0b28860f7b569293e6b713f8a4">Initialize</a>(mySPI_callback);</div>
  328. <div class="line"> <span class="comment">/* Power up the SPI peripheral */</span></div>
  329. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#aba8f1c8019af95ffe19c32403e3240ef">PowerControl</a>(<a class="code" href="Driver__Common_8h.html#ga47d6d7c31f88f3b8ae4aaf9d8444afa5abed52b77a9ce4775570e44a842b1295e">ARM_POWER_FULL</a>);</div>
  330. <div class="line"> <span class="comment">/* Configure the SPI to Master, 8-bit mode @10000 kBits/sec */</span></div>
  331. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__mode__ctrls.html#ga3143ef07c1607b9bc57e29df35cf2fa8">ARM_SPI_MODE_MASTER</a> | <a class="code" href="group__spi__frame__format__ctrls.html#ga7fab572b2fec303e979e47eb2d13ca74">ARM_SPI_CPOL1_CPHA1</a> | <a class="code" href="group__spi__bit__order__ctrls.html#ga98228a708cbab6e214c7ac696f77dab6">ARM_SPI_MSB_LSB</a> | <a class="code" href="group__spi__slave__select__mode__ctrls.html#gab5e319aa3f9d4d8c9ed92f0fe865f624">ARM_SPI_SS_MASTER_SW</a> | <a class="code" href="group__spi__data__bits__ctrls.html#gaf6c099a1d67256a32010120c66c55250">ARM_SPI_DATA_BITS</a>(8), 10000000);</div>
  332. <div class="line"> </div>
  333. <div class="line"> <span class="comment">/* SS line = INACTIVE = HIGH */</span></div>
  334. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__misc__ctrls.html#ga5776272b82decff92da003568540c92f">ARM_SPI_CONTROL_SS</a>, <a class="code" href="Driver__SPI_8h.html#a335b448e07422e9c25616a693ec581cc">ARM_SPI_SS_INACTIVE</a>);</div>
  335. <div class="line"> </div>
  336. <div class="line"> <span class="comment">/* thread loop */</span></div>
  337. <div class="line"> <span class="keywordflow">while</span> (1)</div>
  338. <div class="line"> {</div>
  339. <div class="line"> <span class="comment">/* SS line = ACTIVE = LOW */</span></div>
  340. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__misc__ctrls.html#ga5776272b82decff92da003568540c92f">ARM_SPI_CONTROL_SS</a>, <a class="code" href="Driver__SPI_8h.html#a3f465cdbd1238ddd74f78e14457076c4">ARM_SPI_SS_ACTIVE</a>);</div>
  341. <div class="line"> <span class="comment">/* Transmit some data */</span></div>
  342. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a44eedddf4428cf4b98883b6c27d31922">Send</a>(testdata_out, <span class="keyword">sizeof</span>(testdata_out));</div>
  343. <div class="line"> <span class="comment">/* Wait for completion */</span></div>
  344. <div class="line"> evt = osSignalWait(0x01, 100);</div>
  345. <div class="line"> <span class="keywordflow">if</span> (evt.status == osEventTimeout) {</div>
  346. <div class="line"> __breakpoint(0); <span class="comment">/* Timeout error: Call debugger */</span></div>
  347. <div class="line"> }</div>
  348. <div class="line"> <span class="comment">/* SS line = INACTIVE = HIGH */</span></div>
  349. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__misc__ctrls.html#ga5776272b82decff92da003568540c92f">ARM_SPI_CONTROL_SS</a>, <a class="code" href="Driver__SPI_8h.html#a335b448e07422e9c25616a693ec581cc">ARM_SPI_SS_INACTIVE</a>);</div>
  350. <div class="line"></div>
  351. <div class="line"> <span class="comment">/* SS line = ACTIVE = LOW */</span></div>
  352. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__misc__ctrls.html#ga5776272b82decff92da003568540c92f">ARM_SPI_CONTROL_SS</a>, <a class="code" href="Driver__SPI_8h.html#a3f465cdbd1238ddd74f78e14457076c4">ARM_SPI_SS_ACTIVE</a>);</div>
  353. <div class="line"> <span class="comment">/* Receive 8 bytes of reply */</span></div>
  354. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#adb9224a35fe16c92eb0dd103638e4cf3">Receive</a>(testdata_in, 8);</div>
  355. <div class="line"> evt = osSignalWait(0x01, 100);</div>
  356. <div class="line"> <span class="keywordflow">if</span> (evt.status == osEventTimeout) {</div>
  357. <div class="line"> __breakpoint(0); <span class="comment">/* Timeout error: Call debugger */</span></div>
  358. <div class="line"> }</div>
  359. <div class="line"> <span class="comment">/* SS line = INACTIVE = HIGH */</span></div>
  360. <div class="line"> SPIdrv-&gt;<a class="code" href="group__spi__interface__gr.html#a6e0f47a92f626a971c5197fca6545505">Control</a>(<a class="code" href="group__spi__misc__ctrls.html#ga5776272b82decff92da003568540c92f">ARM_SPI_CONTROL_SS</a>, <a class="code" href="Driver__SPI_8h.html#a335b448e07422e9c25616a693ec581cc">ARM_SPI_SS_INACTIVE</a>);</div>
  361. <div class="line"> }</div>
  362. <div class="line">}</div>
  363. </div><!-- fragment --> </div></div><!-- contents -->
  364. </div><!-- doc-content -->
  365. <!-- start footer part -->
  366. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  367. <ul>
  368. <li class="footer">Generated on Wed Aug 1 2018 17:12:13 for CMSIS-Driver by Arm Ltd. All rights reserved.
  369. <!--
  370. <a href="http://www.doxygen.org/index.html">
  371. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.6
  372. -->
  373. </li>
  374. </ul>
  375. </div>
  376. </body>
  377. </html>