You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1068 lines
118 KiB

<!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"/>
<title>Trinamic TMC26X Stepper Driver for Arduino: TMC26XStepper.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">Trinamic TMC26X Stepper Driver for Arduino
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.6.1 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
<li><a href="globals.html"><span>File&#160;Members</span></a></li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">TMC26XStepper.cpp</div> </div>
</div><!--header-->
<div class="contents">
<a href="_t_m_c26_x_stepper_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> TMC26XStepper.cpp - - TMC26X Stepper library for Wiring/Arduino - Version 0.1</span>
<a name="l00003"></a>00003 <span class="comment"> </span>
<a name="l00004"></a>00004 <span class="comment"> based on the stepper library by Tom Igoe, et. al.</span>
<a name="l00005"></a>00005 <span class="comment"> </span>
<a name="l00006"></a>00006 <span class="comment"> Copyright (c) 2011, Interactive Matter, Marcus Nowotny</span>
<a name="l00007"></a>00007 <span class="comment"> </span>
<a name="l00008"></a>00008 <span class="comment"> Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<a name="l00009"></a>00009 <span class="comment"> of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<a name="l00010"></a>00010 <span class="comment"> in the Software without restriction, including without limitation the rights</span>
<a name="l00011"></a>00011 <span class="comment"> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<a name="l00012"></a>00012 <span class="comment"> copies of the Software, and to permit persons to whom the Software is</span>
<a name="l00013"></a>00013 <span class="comment"> furnished to do so, subject to the following conditions:</span>
<a name="l00014"></a>00014 <span class="comment"> </span>
<a name="l00015"></a>00015 <span class="comment"> The above copyright notice and this permission notice shall be included in</span>
<a name="l00016"></a>00016 <span class="comment"> all copies or substantial portions of the Software.</span>
<a name="l00017"></a>00017 <span class="comment"> </span>
<a name="l00018"></a>00018 <span class="comment"> THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<a name="l00019"></a>00019 <span class="comment"> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<a name="l00020"></a>00020 <span class="comment"> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<a name="l00021"></a>00021 <span class="comment"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<a name="l00022"></a>00022 <span class="comment"> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<a name="l00023"></a>00023 <span class="comment"> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<a name="l00024"></a>00024 <span class="comment"> THE SOFTWARE.</span>
<a name="l00025"></a>00025 <span class="comment"> </span>
<a name="l00026"></a>00026 <span class="comment"> */</span>
<a name="l00027"></a>00027
<a name="l00028"></a>00028 <span class="preprocessor">#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100</span>
<a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor"> #include &lt;Arduino.h&gt;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#else</span>
<a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor"> #include &lt;WProgram.h&gt;</span>
<a name="l00032"></a>00032 <span class="preprocessor">#endif</span>
<a name="l00033"></a>00033 <span class="preprocessor"></span><span class="preprocessor">#include &lt;SPI.h&gt;</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &quot;<a class="code" href="_t_m_c26_x_stepper_8h.html">TMC26XStepper.h</a>&quot;</span>
<a name="l00035"></a>00035
<a name="l00036"></a>00036 <span class="comment">//some default values used in initialization</span>
<a name="l00037"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6560b3471273e99e280ba795e3469ede">00037</a> <span class="preprocessor">#define DEFAULT_MICROSTEPPING_VALUE 32</span>
<a name="l00038"></a>00038 <span class="preprocessor"></span>
<a name="l00039"></a>00039 <span class="comment">//TMC26X register definitions</span>
<a name="l00040"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">00040</a> <span class="preprocessor">#define DRIVER_CONTROL_REGISTER 0x0ul</span>
<a name="l00041"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">00041</a> <span class="preprocessor"></span><span class="preprocessor">#define CHOPPER_CONFIG_REGISTER 0x80000ul</span>
<a name="l00042"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">00042</a> <span class="preprocessor"></span><span class="preprocessor">#define COOL_STEP_REGISTER 0xA0000ul</span>
<a name="l00043"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a478d9bde09a6528eef6af6ffeeb6caba">00043</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD2_LOAD_MEASURE_REGISTER 0xC0000ul</span>
<a name="l00044"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#af35f569d42ea3b1d634901a3b6a908ee">00044</a> <span class="preprocessor"></span><span class="preprocessor">#define DRIVER_CONFIG_REGISTER 0xE0000ul</span>
<a name="l00045"></a>00045 <span class="preprocessor"></span>
<a name="l00046"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a3b02ee1f518b0c90c16488f937abd443">00046</a> <span class="preprocessor">#define REGISTER_BIT_PATTERN 0xFFFFFul</span>
<a name="l00047"></a>00047 <span class="preprocessor"></span>
<a name="l00048"></a>00048 <span class="comment">//definitions for the driver control register</span>
<a name="l00049"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8f5cb0c066109ffb18cefc0e85ee1d1b">00049</a> <span class="preprocessor">#define MICROSTEPPING_PATTERN 0xFul</span>
<a name="l00050"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa64245f223209654c60588e4558e0bab">00050</a> <span class="preprocessor"></span><span class="preprocessor">#define STEP_INTERPOLATION 0x200ul</span>
<a name="l00051"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a770601bf0153e4bc639b9c3005b15af7">00051</a> <span class="preprocessor"></span><span class="preprocessor">#define DOUBLE_EDGE_STEP 0x100ul</span>
<a name="l00052"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">00052</a> <span class="preprocessor"></span><span class="preprocessor">#define VSENSE 0x40ul</span>
<a name="l00053"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a143b7757272f07866d9655bde8303d9a">00053</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_MICROSTEP_POSTION 0x0ul</span>
<a name="l00054"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">00054</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_STALL_GUARD_READING 0x10ul</span>
<a name="l00055"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">00055</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_STALL_GUARD_AND_COOL_STEP 0x20ul</span>
<a name="l00056"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">00056</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_SELECTION_PATTERN 0x30ul</span>
<a name="l00057"></a>00057 <span class="preprocessor"></span>
<a name="l00058"></a>00058 <span class="comment">//definitions for the chopper config register</span>
<a name="l00059"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a57418a67ff726d540b813230bca1d536">00059</a> <span class="preprocessor">#define CHOPPER_MODE_STANDARD 0x0ul</span>
<a name="l00060"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">00060</a> <span class="preprocessor"></span><span class="preprocessor">#define CHOPPER_MODE_T_OFF_FAST_DECAY 0x4000ul</span>
<a name="l00061"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">00061</a> <span class="preprocessor"></span><span class="preprocessor">#define T_OFF_PATTERN 0xful</span>
<a name="l00062"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">00062</a> <span class="preprocessor"></span><span class="preprocessor">#define RANDOM_TOFF_TIME 0x2000ul</span>
<a name="l00063"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">00063</a> <span class="preprocessor"></span><span class="preprocessor">#define BLANK_TIMING_PATTERN 0x18000ul</span>
<a name="l00064"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">00064</a> <span class="preprocessor"></span><span class="preprocessor">#define BLANK_TIMING_SHIFT 15</span>
<a name="l00065"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">00065</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_DECREMENT_PATTERN 0x1800ul</span>
<a name="l00066"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a36e554a87785ce6ba998b79aae9e74e0">00066</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_DECREMENT_SHIFT 11</span>
<a name="l00067"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">00067</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_LOW_VALUE_PATTERN 0x780ul</span>
<a name="l00068"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">00068</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_LOW_SHIFT 7</span>
<a name="l00069"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">00069</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_START_VALUE_PATTERN 0x78ul</span>
<a name="l00070"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">00070</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_START_VALUE_SHIFT 4</span>
<a name="l00071"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">00071</a> <span class="preprocessor"></span><span class="preprocessor">#define T_OFF_TIMING_PATERN 0xFul</span>
<a name="l00072"></a>00072 <span class="preprocessor"></span>
<a name="l00073"></a>00073 <span class="comment">//definitions for cool step register</span>
<a name="l00074"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8a261a77d198b85f6dd8416387b354b3">00074</a> <span class="preprocessor">#define MINIMUM_CURRENT_FOURTH 0x8000ul</span>
<a name="l00075"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbe13a0464355e42fbe786ca5f58ed8d">00075</a> <span class="preprocessor"></span><span class="preprocessor">#define CURRENT_DOWN_STEP_SPEED_PATTERN 0x6000ul</span>
<a name="l00076"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac8f748bf735c447dbed7dd4c7b631a87">00076</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_MAX_PATTERN 0xF00ul</span>
<a name="l00077"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aba6c07e5672e34e618bb3a550ab0d2bc">00077</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_CURRENT_STEP_WIDTH_PATTERN 0x60ul</span>
<a name="l00078"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae1862dfb958c03698b0abd95fda033ea">00078</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_MIN_PATTERN 0xful</span>
<a name="l00079"></a>00079 <span class="preprocessor"></span>
<a name="l00080"></a>00080 <span class="comment">//definitions for stall guard2 current register</span>
<a name="l00081"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">00081</a> <span class="preprocessor">#define STALL_GUARD_FILTER_ENABLED 0x10000ul</span>
<a name="l00082"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae631457932894a974334892704550ecc">00082</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_TRESHHOLD_VALUE_PATTERN 0x17F00ul</span>
<a name="l00083"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">00083</a> <span class="preprocessor"></span><span class="preprocessor">#define CURRENT_SCALING_PATTERN 0x1Ful</span>
<a name="l00084"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">00084</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_CONFIG_PATTERN 0x17F00ul</span>
<a name="l00085"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6a1cb1fd61cf7c570f94376fa11fe55b">00085</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_VALUE_PATTERN 0x7F00ul</span>
<a name="l00086"></a>00086 <span class="preprocessor"></span>
<a name="l00087"></a>00087 <span class="comment">//definitions for the input from the TCM260</span>
<a name="l00088"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">00088</a> <span class="preprocessor">#define STATUS_STALL_GUARD_STATUS 0x1ul</span>
<a name="l00089"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbb23d2f055c9eab55eac29d1a75deb4">00089</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OVER_TEMPERATURE_SHUTDOWN 0x2ul</span>
<a name="l00090"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa317fd77f2f26fdfbfd331e21d9069e8">00090</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OVER_TEMPERATURE_WARNING 0x4ul</span>
<a name="l00091"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8e03041302a092174fa33b3cf837dca2">00091</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_SHORT_TO_GROUND_A 0x8ul</span>
<a name="l00092"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a903c3eba99695a32c6736463dcfd93ae">00092</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_SHORT_TO_GROUND_B 0x10ul</span>
<a name="l00093"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae9cbbe5af7188e6bff8fe412f8e42f59">00093</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OPEN_LOAD_A 0x20ul</span>
<a name="l00094"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab29dc5cd6c6c4e5bf99e71bd563e1be1">00094</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OPEN_LOAD_B 0x40ul</span>
<a name="l00095"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab96ed1635faee6650e9cce73598a2773">00095</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_STAND_STILL 0x80ul</span>
<a name="l00096"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a5c3d76da63f585e37813c32be2e11ab7">00096</a> <span class="preprocessor"></span><span class="preprocessor">#define READOUT_VALUE_PATTERN 0xFFC00ul</span>
<a name="l00097"></a>00097 <span class="preprocessor"></span>
<a name="l00098"></a>00098 <span class="comment">//default values</span>
<a name="l00099"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">00099</a> <span class="preprocessor">#define INITIAL_MICROSTEPPING 0x3ul //32th microstepping</span>
<a name="l00100"></a>00100 <span class="preprocessor"></span>
<a name="l00101"></a>00101 <span class="comment">//debuging output</span>
<a name="l00102"></a>00102 <span class="comment">//#define DEBUG</span>
<a name="l00103"></a>00103
<a name="l00104"></a>00104 <span class="comment">/*</span>
<a name="l00105"></a>00105 <span class="comment"> * Constructor</span>
<a name="l00106"></a>00106 <span class="comment"> * number_of_steps - the steps per rotation</span>
<a name="l00107"></a>00107 <span class="comment"> * cs_pin - the SPI client select pin</span>
<a name="l00108"></a>00108 <span class="comment"> * dir_pin - the pin where the direction pin is connected</span>
<a name="l00109"></a>00109 <span class="comment"> * step_pin - the pin where the step pin is connected</span>
<a name="l00110"></a>00110 <span class="comment"> */</span>
<a name="l00111"></a><a class="code" href="class_t_m_c26_x_stepper.html#a3ef40763b8b8ab2b6ed4978c0647906c">00111</a> <a class="code" href="class_t_m_c26_x_stepper.html#a3ef40763b8b8ab2b6ed4978c0647906c" title="creates a new represenatation of a stepper motor connected to a TMC26X stepper driver">TMC26XStepper::TMC26XStepper</a>(<span class="keywordtype">int</span> number_of_steps, <span class="keywordtype">int</span> cs_pin, <span class="keywordtype">int</span> dir_pin, <span class="keywordtype">int</span> step_pin, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> current, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> resistor)
<a name="l00112"></a>00112 {
<a name="l00113"></a>00113 <span class="comment">//we are not started yet</span>
<a name="l00114"></a>00114 started=<span class="keyword">false</span>;
<a name="l00115"></a>00115 <span class="comment">//by default cool step is not enabled</span>
<a name="l00116"></a>00116 cool_step_enabled=<span class="keyword">false</span>;
<a name="l00117"></a>00117
<a name="l00118"></a>00118 <span class="comment">//save the pins for later use</span>
<a name="l00119"></a>00119 this-&gt;cs_pin=cs_pin;
<a name="l00120"></a>00120 this-&gt;dir_pin=dir_pin;
<a name="l00121"></a>00121 this-&gt;step_pin = step_pin;
<a name="l00122"></a>00122
<a name="l00123"></a>00123 <span class="comment">//store the current sense resistor value for later use</span>
<a name="l00124"></a>00124 this-&gt;resistor = resistor;
<a name="l00125"></a>00125
<a name="l00126"></a>00126 <span class="comment">//initizalize our status values</span>
<a name="l00127"></a>00127 this-&gt;steps_left = 0;
<a name="l00128"></a>00128 this-&gt;direction = 0;
<a name="l00129"></a>00129
<a name="l00130"></a>00130 <span class="comment">//initialize register values</span>
<a name="l00131"></a>00131 driver_control_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">DRIVER_CONTROL_REGISTER</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>;
<a name="l00132"></a>00132 chopper_config_register=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">CHOPPER_CONFIG_REGISTER</a>;
<a name="l00133"></a>00133
<a name="l00134"></a>00134 <span class="comment">//setting the default register values</span>
<a name="l00135"></a>00135 driver_control_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">DRIVER_CONTROL_REGISTER</a>|<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>;
<a name="l00136"></a>00136 microsteps = (1 &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>);
<a name="l00137"></a>00137 chopper_config_register=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">CHOPPER_CONFIG_REGISTER</a>;
<a name="l00138"></a>00138 cool_step_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">COOL_STEP_REGISTER</a>;
<a name="l00139"></a>00139 stall_guard2_current_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a478d9bde09a6528eef6af6ffeeb6caba">STALL_GUARD2_LOAD_MEASURE_REGISTER</a>;
<a name="l00140"></a>00140 driver_configuration_register_value = <a class="code" href="_t_m_c26_x_stepper_8cpp.html#af35f569d42ea3b1d634901a3b6a908ee">DRIVER_CONFIG_REGISTER</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>;
<a name="l00141"></a>00141
<a name="l00142"></a>00142 <span class="comment">//set the current</span>
<a name="l00143"></a>00143 <a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2" title="set the maximum motor current in mA (1000 is 1 Amp) Keep in mind this is the maximum peak Current...">setCurrent</a>(current);
<a name="l00144"></a>00144 <span class="comment">//set to a conservative start value</span>
<a name="l00145"></a>00145 <a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649" title="Sets and configure the classical Constant Off Timer Chopper.">setConstantOffTimeChopper</a>(7, 54, 13,12,1);
<a name="l00146"></a>00146 <span class="comment">//set a nice microstepping value</span>
<a name="l00147"></a>00147 <a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0" title="Set the number of microsteps in 2^i values (rounded) up to 256.">setMicrosteps</a>(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6560b3471273e99e280ba795e3469ede">DEFAULT_MICROSTEPPING_VALUE</a>);
<a name="l00148"></a>00148 <span class="comment">//save the number of steps</span>
<a name="l00149"></a>00149 this-&gt;number_of_steps = number_of_steps;
<a name="l00150"></a>00150 }
<a name="l00151"></a>00151
<a name="l00152"></a>00152
<a name="l00153"></a>00153 <span class="comment">/*</span>
<a name="l00154"></a>00154 <span class="comment"> * start &amp; configure the stepper driver</span>
<a name="l00155"></a>00155 <span class="comment"> * just must be called.</span>
<a name="l00156"></a>00156 <span class="comment"> */</span>
<a name="l00157"></a><a class="code" href="class_t_m_c26_x_stepper.html#aad1ed82b3e05940bde5a6c7ed3d3e8f7">00157</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aad1ed82b3e05940bde5a6c7ed3d3e8f7" title="configures and starts the TMC26X stepper driver. Before you called this function the stepper driver i...">TMC26XStepper::start</a>() {
<a name="l00158"></a>00158
<a name="l00159"></a>00159 <span class="preprocessor">#ifdef DEBUG </span>
<a name="l00160"></a>00160 <span class="preprocessor"></span> Serial.println(<span class="stringliteral">&quot;TMC26X stepper library&quot;</span>);
<a name="l00161"></a>00161 Serial.print(<span class="stringliteral">&quot;CS pin: &quot;</span>);
<a name="l00162"></a>00162 Serial.println(cs_pin);
<a name="l00163"></a>00163 Serial.print(<span class="stringliteral">&quot;DIR pin: &quot;</span>);
<a name="l00164"></a>00164 Serial.println(dir_pin);
<a name="l00165"></a>00165 Serial.print(<span class="stringliteral">&quot;STEP pin: &quot;</span>);
<a name="l00166"></a>00166 Serial.println(step_pin);
<a name="l00167"></a>00167 Serial.print(<span class="stringliteral">&quot;current scaling: &quot;</span>);
<a name="l00168"></a>00168 Serial.println(current_scaling,DEC);
<a name="l00169"></a>00169 <span class="preprocessor">#endif</span>
<a name="l00170"></a>00170 <span class="preprocessor"></span> <span class="comment">//set the pins as output &amp; its initial value</span>
<a name="l00171"></a>00171 pinMode(step_pin, OUTPUT);
<a name="l00172"></a>00172 pinMode(dir_pin, OUTPUT);
<a name="l00173"></a>00173 pinMode(cs_pin, OUTPUT);
<a name="l00174"></a>00174 digitalWrite(step_pin, LOW);
<a name="l00175"></a>00175 digitalWrite(dir_pin, LOW);
<a name="l00176"></a>00176 digitalWrite(cs_pin, HIGH);
<a name="l00177"></a>00177
<a name="l00178"></a>00178 <span class="comment">//configure the SPI interface</span>
<a name="l00179"></a>00179 SPI.setBitOrder(MSBFIRST);
<a name="l00180"></a>00180 SPI.setClockDivider(SPI_CLOCK_DIV8);
<a name="l00181"></a>00181 <span class="comment">//todo this does not work reliably - find a way to foolprof set it (e.g. while communicating</span>
<a name="l00182"></a>00182 <span class="comment">//SPI.setDataMode(SPI_MODE3);</span>
<a name="l00183"></a>00183 SPI.begin();
<a name="l00184"></a>00184
<a name="l00185"></a>00185 <span class="comment">//set the initial values</span>
<a name="l00186"></a>00186 send262(driver_control_register_value);
<a name="l00187"></a>00187 send262(chopper_config_register);
<a name="l00188"></a>00188 send262(cool_step_register_value);
<a name="l00189"></a>00189 send262(stall_guard2_current_register_value);
<a name="l00190"></a>00190 send262(driver_configuration_register_value);
<a name="l00191"></a>00191
<a name="l00192"></a>00192 <span class="comment">//save that we are in running mode</span>
<a name="l00193"></a>00193 started=<span class="keyword">true</span>;
<a name="l00194"></a>00194 }
<a name="l00195"></a>00195
<a name="l00196"></a>00196 <span class="comment">/*</span>
<a name="l00197"></a>00197 <span class="comment"> Mark the driver as unstarted to be able to start it again</span>
<a name="l00198"></a>00198 <span class="comment"> */</span>
<a name="l00199"></a><a class="code" href="class_t_m_c26_x_stepper.html#af968e70a13068f1e71ac0fa6865630c5">00199</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af968e70a13068f1e71ac0fa6865630c5" title="resets the stepper in unconfigured mode.">TMC26XStepper::un_start</a>() {
<a name="l00200"></a>00200 started=<span class="keyword">false</span>;
<a name="l00201"></a>00201 }
<a name="l00202"></a>00202
<a name="l00203"></a>00203
<a name="l00204"></a>00204 <span class="comment">/*</span>
<a name="l00205"></a>00205 <span class="comment"> Sets the speed in revs per minute</span>
<a name="l00206"></a>00206 <span class="comment"></span>
<a name="l00207"></a>00207 <span class="comment">*/</span>
<a name="l00208"></a><a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd">00208</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd" title="Sets the rotation speed in revolutions per minute.">TMC26XStepper::setSpeed</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> whatSpeed)
<a name="l00209"></a>00209 {
<a name="l00210"></a>00210 this-&gt;speed = whatSpeed;
<a name="l00211"></a>00211 this-&gt;step_delay = (60UL * 1000UL * 1000UL) / ((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)this-&gt;number_of_steps * (<span class="keywordtype">unsigned</span> long)whatSpeed * (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)this-&gt;microsteps);
<a name="l00212"></a>00212 <span class="preprocessor">#ifdef DEBUG</span>
<a name="l00213"></a>00213 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Step delay in micros: &quot;</span>);
<a name="l00214"></a>00214 Serial.println(this-&gt;step_delay);
<a name="l00215"></a>00215 <span class="preprocessor">#endif</span>
<a name="l00216"></a>00216 <span class="preprocessor"></span> <span class="comment">//update the next step time</span>
<a name="l00217"></a>00217 this-&gt;next_step_time = this-&gt;last_step_time+this-&gt;step_delay;
<a name="l00218"></a>00218
<a name="l00219"></a>00219 }
<a name="l00220"></a>00220
<a name="l00221"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa564f5cc0218d30ef897c2830c768c29">00221</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa564f5cc0218d30ef897c2830c768c29" title="reads out the currently selected speed in revolutions per minute.">TMC26XStepper::getSpeed</a>(<span class="keywordtype">void</span>) {
<a name="l00222"></a>00222 <span class="keywordflow">return</span> this-&gt;speed;
<a name="l00223"></a>00223 }
<a name="l00224"></a>00224
<a name="l00225"></a>00225 <span class="comment">/*</span>
<a name="l00226"></a>00226 <span class="comment"> Moves the motor steps_to_move steps. If the number is negative, </span>
<a name="l00227"></a>00227 <span class="comment"> the motor moves in the reverse direction.</span>
<a name="l00228"></a>00228 <span class="comment"> */</span>
<a name="l00229"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac073a742496885f1f60751f9fb9c395d">00229</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac073a742496885f1f60751f9fb9c395d" title="Initiate a movement for the given number of steps. Positive numbers move in one, negative numbers in ...">TMC26XStepper::step</a>(<span class="keywordtype">int</span> steps_to_move)
<a name="l00230"></a>00230 {
<a name="l00231"></a>00231 <span class="keywordflow">if</span> (this-&gt;steps_left==0) {
<a name="l00232"></a>00232 this-&gt;steps_left = abs(steps_to_move); <span class="comment">// how many steps to take</span>
<a name="l00233"></a>00233
<a name="l00234"></a>00234 <span class="comment">// determine direction based on whether steps_to_mode is + or -:</span>
<a name="l00235"></a>00235 <span class="keywordflow">if</span> (steps_to_move &gt; 0) {
<a name="l00236"></a>00236 this-&gt;direction = 1;
<a name="l00237"></a>00237 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (steps_to_move &lt; 0) {
<a name="l00238"></a>00238 this-&gt;direction = 0;
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240 <span class="keywordflow">return</span> 0;
<a name="l00241"></a>00241 } <span class="keywordflow">else</span> {
<a name="l00242"></a>00242 <span class="keywordflow">return</span> -1;
<a name="l00243"></a>00243 }
<a name="l00244"></a>00244 }
<a name="l00245"></a>00245
<a name="l00246"></a><a class="code" href="class_t_m_c26_x_stepper.html#aed5d81f1549615529c723600a68ba415">00246</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#aed5d81f1549615529c723600a68ba415" title="Central movement method, must be called as often as possible in the lopp function and is very fast...">TMC26XStepper::move</a>(<span class="keywordtype">void</span>) {
<a name="l00247"></a>00247 <span class="comment">// decrement the number of steps, moving one step each time:</span>
<a name="l00248"></a>00248 <span class="keywordflow">if</span>(this-&gt;steps_left&gt;0) {
<a name="l00249"></a>00249 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> time = micros();
<a name="l00250"></a>00250 <span class="comment">// move only if the appropriate delay has passed:</span>
<a name="l00251"></a>00251 <span class="keywordflow">if</span> (time &gt;= this-&gt;next_step_time) {
<a name="l00252"></a>00252 <span class="comment">// increment or decrement the step number,</span>
<a name="l00253"></a>00253 <span class="comment">// depending on direction:</span>
<a name="l00254"></a>00254 <span class="keywordflow">if</span> (this-&gt;direction == 1) {
<a name="l00255"></a>00255 digitalWrite(step_pin, HIGH);
<a name="l00256"></a>00256 } <span class="keywordflow">else</span> {
<a name="l00257"></a>00257 digitalWrite(dir_pin, HIGH);
<a name="l00258"></a>00258 digitalWrite(step_pin, HIGH);
<a name="l00259"></a>00259 }
<a name="l00260"></a>00260 <span class="comment">// get the timeStamp of when you stepped:</span>
<a name="l00261"></a>00261 this-&gt;last_step_time = time;
<a name="l00262"></a>00262 this-&gt;next_step_time = time+this-&gt;step_delay;
<a name="l00263"></a>00263 <span class="comment">// decrement the steps left:</span>
<a name="l00264"></a>00264 steps_left--;
<a name="l00265"></a>00265 <span class="comment">//disable the step &amp; dir pins</span>
<a name="l00266"></a>00266 digitalWrite(step_pin, LOW);
<a name="l00267"></a>00267 digitalWrite(dir_pin, LOW);
<a name="l00268"></a>00268 }
<a name="l00269"></a>00269 <span class="keywordflow">return</span> -1;
<a name="l00270"></a>00270 }
<a name="l00271"></a>00271 <span class="keywordflow">return</span> 0;
<a name="l00272"></a>00272 }
<a name="l00273"></a>00273
<a name="l00274"></a><a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e">00274</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e" title="checks if the motor still has to move to fulfill the last movement command.">TMC26XStepper::isMoving</a>(<span class="keywordtype">void</span>) {
<a name="l00275"></a>00275 <span class="keywordflow">return</span> (this-&gt;steps_left&gt;0);
<a name="l00276"></a>00276 }
<a name="l00277"></a>00277
<a name="l00278"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa6c3211f85301ca0fb2e7b73cb8142a7">00278</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa6c3211f85301ca0fb2e7b73cb8142a7" title="Get the number of steps left in the current movement.">TMC26XStepper::getStepsLeft</a>(<span class="keywordtype">void</span>) {
<a name="l00279"></a>00279 <span class="keywordflow">return</span> this-&gt;steps_left;
<a name="l00280"></a>00280 }
<a name="l00281"></a>00281
<a name="l00282"></a><a class="code" href="class_t_m_c26_x_stepper.html#a6315c18eadbc6bf4f3d81a6f80296123">00282</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a6315c18eadbc6bf4f3d81a6f80296123" title="Stops the motor regardless if it moves or not.">TMC26XStepper::stop</a>(<span class="keywordtype">void</span>) {
<a name="l00283"></a>00283 <span class="comment">//note to self if the motor is currently moving</span>
<a name="l00284"></a>00284 <span class="keywordtype">char</span> state = <a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e" title="checks if the motor still has to move to fulfill the last movement command.">isMoving</a>();
<a name="l00285"></a>00285 <span class="comment">//stop the motor</span>
<a name="l00286"></a>00286 this-&gt;steps_left = 0;
<a name="l00287"></a>00287 this-&gt;direction = 0;
<a name="l00288"></a>00288 <span class="comment">//return if it was moving</span>
<a name="l00289"></a>00289 <span class="keywordflow">return</span> state;
<a name="l00290"></a>00290 }
<a name="l00291"></a>00291
<a name="l00292"></a><a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2">00292</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2" title="set the maximum motor current in mA (1000 is 1 Amp) Keep in mind this is the maximum peak Current...">TMC26XStepper::setCurrent</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> current) {
<a name="l00293"></a>00293 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_scaling = 0;
<a name="l00294"></a>00294 <span class="comment">//calculate the current scaling from the max current setting (in mA)</span>
<a name="l00295"></a>00295 <span class="keywordtype">double</span> mASetting = (double)current;
<a name="l00296"></a>00296 <span class="keywordtype">double</span> resistor_value = (double) this-&gt;resistor;
<a name="l00297"></a>00297 <span class="comment">// remove vesense flag</span>
<a name="l00298"></a>00298 this-&gt;driver_configuration_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>);
<a name="l00299"></a>00299 <span class="comment">//this is derrived from I=(cs+1)/32*(Vsense/Rsense)</span>
<a name="l00300"></a>00300 <span class="comment">//leading to cs = CS = 32*R*I/V (with V = 0,31V oder 0,165V and I = 1000*current)</span>
<a name="l00301"></a>00301 <span class="comment">//with Rsense=0,15</span>
<a name="l00302"></a>00302 <span class="comment">//for vsense = 0,310V (VSENSE not set)</span>
<a name="l00303"></a>00303 <span class="comment">//or vsense = 0,165V (VSENSE set)</span>
<a name="l00304"></a>00304 current_scaling = (byte)((resistor_value*mASetting*32.0/(0.31*1000.0*1000.0))-0.5); <span class="comment">//theoretically - 1.0 for better rounding it is 0.5</span>
<a name="l00305"></a>00305
<a name="l00306"></a>00306 <span class="comment">//check if the current scalingis too low</span>
<a name="l00307"></a>00307 <span class="keywordflow">if</span> (current_scaling&lt;16) {
<a name="l00308"></a>00308 <span class="comment">//set the csense bit to get a use half the sense voltage (to support lower motor currents)</span>
<a name="l00309"></a>00309 this-&gt;driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>;
<a name="l00310"></a>00310 <span class="comment">//and recalculate the current setting</span>
<a name="l00311"></a>00311 current_scaling = (byte)((resistor_value*mASetting*32.0/(0.165*1000.0*1000.0))-0.5); <span class="comment">//theoretically - 1.0 for better rounding it is 0.5</span>
<a name="l00312"></a>00312 <span class="preprocessor">#ifdef DEBUG</span>
<a name="l00313"></a>00313 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;CS (Vsense=1): &quot;</span>);
<a name="l00314"></a>00314 Serial.println(current_scaling);
<a name="l00315"></a>00315 } <span class="keywordflow">else</span> {
<a name="l00316"></a>00316 Serial.print(<span class="stringliteral">&quot;CS: &quot;</span>);
<a name="l00317"></a>00317 Serial.println(current_scaling);
<a name="l00318"></a>00318 <span class="preprocessor">#endif</span>
<a name="l00319"></a>00319 <span class="preprocessor"></span> }
<a name="l00320"></a>00320
<a name="l00321"></a>00321 <span class="comment">//do some sanity checks</span>
<a name="l00322"></a>00322 <span class="keywordflow">if</span> (current_scaling&gt;31) {
<a name="l00323"></a>00323 current_scaling=31;
<a name="l00324"></a>00324 }
<a name="l00325"></a>00325 <span class="comment">//delete the old value</span>
<a name="l00326"></a>00326 stall_guard2_current_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">CURRENT_SCALING_PATTERN</a>);
<a name="l00327"></a>00327 <span class="comment">//set the new current scaling</span>
<a name="l00328"></a>00328 stall_guard2_current_register_value |= current_scaling;
<a name="l00329"></a>00329 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00330"></a>00330 <span class="keywordflow">if</span> (started) {
<a name="l00331"></a>00331 send262(driver_configuration_register_value);
<a name="l00332"></a>00332 send262(stall_guard2_current_register_value);
<a name="l00333"></a>00333 }
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335
<a name="l00336"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0c544e23efe3e4a912aacf57de84b71f">00336</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0c544e23efe3e4a912aacf57de84b71f" title="readout the motor maximum current in mA (1000 is an Amp) This is the maximum current. to get the current current - which may be affected by CoolStep us getCurrentCurrent()">TMC26XStepper::getCurrent</a>(<span class="keywordtype">void</span>) {
<a name="l00337"></a>00337 <span class="comment">//we calculate the current according to the datasheet to be on the safe side</span>
<a name="l00338"></a>00338 <span class="comment">//this is not the fastest but the most accurate and illustrative way</span>
<a name="l00339"></a>00339 <span class="keywordtype">double</span> result = (double)(stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">CURRENT_SCALING_PATTERN</a>);
<a name="l00340"></a>00340 <span class="keywordtype">double</span> resistor_value = (double)this-&gt;resistor;
<a name="l00341"></a>00341 <span class="keywordtype">double</span> voltage = (driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>)? 0.165:0.31;
<a name="l00342"></a>00342 result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
<a name="l00343"></a>00343 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)result;
<a name="l00344"></a>00344 }
<a name="l00345"></a>00345
<a name="l00346"></a><a class="code" href="class_t_m_c26_x_stepper.html#af1a5abc23757860baf8ff421689a425a">00346</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af1a5abc23757860baf8ff421689a425a" title="set the StallGuard threshold in order to get sensible StallGuard readings.">TMC26XStepper::setStallGuardThreshold</a>(<span class="keywordtype">char</span> stall_guard_threshold, <span class="keywordtype">char</span> stall_guard_filter_enabled) {
<a name="l00347"></a>00347 <span class="keywordflow">if</span> (stall_guard_threshold&lt;-64) {
<a name="l00348"></a>00348 stall_guard_threshold = -64;
<a name="l00349"></a>00349 <span class="comment">//We just have 5 bits </span>
<a name="l00350"></a>00350 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (stall_guard_threshold &gt; 63) {
<a name="l00351"></a>00351 stall_guard_threshold = 63;
<a name="l00352"></a>00352 }
<a name="l00353"></a>00353 <span class="comment">//add trim down to 7 bits</span>
<a name="l00354"></a>00354 stall_guard_threshold &amp;=0x7f;
<a name="l00355"></a>00355 <span class="comment">//delete old stall guard settings</span>
<a name="l00356"></a>00356 stall_guard2_current_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">STALL_GUARD_CONFIG_PATTERN</a>);
<a name="l00357"></a>00357 <span class="keywordflow">if</span> (stall_guard_filter_enabled) {
<a name="l00358"></a>00358 stall_guard2_current_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">STALL_GUARD_FILTER_ENABLED</a>;
<a name="l00359"></a>00359 }
<a name="l00360"></a>00360 <span class="comment">//Set the new stall guard threshold</span>
<a name="l00361"></a>00361 stall_guard2_current_register_value |= (((<span class="keywordtype">unsigned</span> long)stall_guard_threshold &lt;&lt; 8) &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">STALL_GUARD_CONFIG_PATTERN</a>);
<a name="l00362"></a>00362 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00363"></a>00363 <span class="keywordflow">if</span> (started) {
<a name="l00364"></a>00364 send262(stall_guard2_current_register_value);
<a name="l00365"></a>00365 }
<a name="l00366"></a>00366 }
<a name="l00367"></a>00367
<a name="l00368"></a><a class="code" href="class_t_m_c26_x_stepper.html#a056661f444725c3ae15696d1e8d91def">00368</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a056661f444725c3ae15696d1e8d91def" title="reads out the StallGuard threshold">TMC26XStepper::getStallGuardThreshold</a>(<span class="keywordtype">void</span>) {
<a name="l00369"></a>00369 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> stall_guard_threshold = stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6a1cb1fd61cf7c570f94376fa11fe55b">STALL_GUARD_VALUE_PATTERN</a>;
<a name="l00370"></a>00370 <span class="comment">//shift it down to bit 0</span>
<a name="l00371"></a>00371 stall_guard_threshold &gt;&gt;=8;
<a name="l00372"></a>00372 <span class="comment">//convert the value to an int to correctly handle the negative numbers</span>
<a name="l00373"></a>00373 <span class="keywordtype">char</span> result = stall_guard_threshold;
<a name="l00374"></a>00374 <span class="comment">//check if it is negative and fill it up with leading 1 for proper negative number representation</span>
<a name="l00375"></a>00375 <span class="keywordflow">if</span> (result &amp; _BV(6)) {
<a name="l00376"></a>00376 result |= 0xC0;
<a name="l00377"></a>00377 }
<a name="l00378"></a>00378 <span class="keywordflow">return</span> result;
<a name="l00379"></a>00379 }
<a name="l00380"></a>00380
<a name="l00381"></a><a class="code" href="class_t_m_c26_x_stepper.html#a47e3443e3e786314c1099b8f14a91b8a">00381</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a47e3443e3e786314c1099b8f14a91b8a" title="returns the current setting of the StallGuard filter">TMC26XStepper::getStallGuardFilter</a>(<span class="keywordtype">void</span>) {
<a name="l00382"></a>00382 <span class="keywordflow">if</span> (stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">STALL_GUARD_FILTER_ENABLED</a>) {
<a name="l00383"></a>00383 <span class="keywordflow">return</span> -1;
<a name="l00384"></a>00384 } <span class="keywordflow">else</span> {
<a name="l00385"></a>00385 <span class="keywordflow">return</span> 0;
<a name="l00386"></a>00386 }
<a name="l00387"></a>00387 }
<a name="l00388"></a>00388 <span class="comment">/*</span>
<a name="l00389"></a>00389 <span class="comment"> * Set the number of microsteps per step.</span>
<a name="l00390"></a>00390 <span class="comment"> * 0,2,4,8,16,32,64,128,256 is supported</span>
<a name="l00391"></a>00391 <span class="comment"> * any value in between will be mapped to the next smaller value</span>
<a name="l00392"></a>00392 <span class="comment"> * 0 and 1 set the motor in full step mode</span>
<a name="l00393"></a>00393 <span class="comment"> */</span>
<a name="l00394"></a><a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0">00394</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0" title="Set the number of microsteps in 2^i values (rounded) up to 256.">TMC26XStepper::setMicrosteps</a>(<span class="keywordtype">int</span> number_of_steps) {
<a name="l00395"></a>00395 <span class="keywordtype">long</span> setting_pattern;
<a name="l00396"></a>00396 <span class="comment">//poor mans log</span>
<a name="l00397"></a>00397 <span class="keywordflow">if</span> (number_of_steps&gt;=256) {
<a name="l00398"></a>00398 setting_pattern=0;
<a name="l00399"></a>00399 microsteps=256;
<a name="l00400"></a>00400 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=128) {
<a name="l00401"></a>00401 setting_pattern=1;
<a name="l00402"></a>00402 microsteps=128;
<a name="l00403"></a>00403 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=64) {
<a name="l00404"></a>00404 setting_pattern=2;
<a name="l00405"></a>00405 microsteps=64;
<a name="l00406"></a>00406 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=32) {
<a name="l00407"></a>00407 setting_pattern=3;
<a name="l00408"></a>00408 microsteps=32;
<a name="l00409"></a>00409 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=16) {
<a name="l00410"></a>00410 setting_pattern=4;
<a name="l00411"></a>00411 microsteps=16;
<a name="l00412"></a>00412 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=8) {
<a name="l00413"></a>00413 setting_pattern=5;
<a name="l00414"></a>00414 microsteps=8;
<a name="l00415"></a>00415 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=4) {
<a name="l00416"></a>00416 setting_pattern=6;
<a name="l00417"></a>00417 microsteps=4;
<a name="l00418"></a>00418 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=2) {
<a name="l00419"></a>00419 setting_pattern=7;
<a name="l00420"></a>00420 microsteps=2;
<a name="l00421"></a>00421 <span class="comment">//1 and 0 lead to full step</span>
<a name="l00422"></a>00422 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&lt;=1) {
<a name="l00423"></a>00423 setting_pattern=8;
<a name="l00424"></a>00424 microsteps=1;
<a name="l00425"></a>00425 }
<a name="l00426"></a>00426 <span class="preprocessor">#ifdef DEBUG</span>
<a name="l00427"></a>00427 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Microstepping: &quot;</span>);
<a name="l00428"></a>00428 Serial.println(microsteps);
<a name="l00429"></a>00429 <span class="preprocessor">#endif</span>
<a name="l00430"></a>00430 <span class="preprocessor"></span> <span class="comment">//delete the old value</span>
<a name="l00431"></a>00431 this-&gt;driver_control_register_value &amp;=0xFFFF0ul;
<a name="l00432"></a>00432 <span class="comment">//set the new value</span>
<a name="l00433"></a>00433 this-&gt;driver_control_register_value |=setting_pattern;
<a name="l00434"></a>00434
<a name="l00435"></a>00435 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00436"></a>00436 <span class="keywordflow">if</span> (started) {
<a name="l00437"></a>00437 send262(driver_control_register_value);
<a name="l00438"></a>00438 }
<a name="l00439"></a>00439 <span class="comment">//recalculate the stepping delay by simply setting the speed again</span>
<a name="l00440"></a>00440 this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd" title="Sets the rotation speed in revolutions per minute.">setSpeed</a>(this-&gt;speed);
<a name="l00441"></a>00441 }
<a name="l00442"></a>00442
<a name="l00443"></a>00443 <span class="comment">/*</span>
<a name="l00444"></a>00444 <span class="comment"> * returns the effective number of microsteps at the moment</span>
<a name="l00445"></a>00445 <span class="comment"> */</span>
<a name="l00446"></a><a class="code" href="class_t_m_c26_x_stepper.html#a5808551ced98b79c09bbb4bf47ecfec3">00446</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a5808551ced98b79c09bbb4bf47ecfec3" title="returns the effective current number of microsteps selected.">TMC26XStepper::getMicrosteps</a>(<span class="keywordtype">void</span>) {
<a name="l00447"></a>00447 <span class="keywordflow">return</span> microsteps;
<a name="l00448"></a>00448 }
<a name="l00449"></a>00449
<a name="l00450"></a>00450 <span class="comment">/*</span>
<a name="l00451"></a>00451 <span class="comment"> * constant_off_time: The off time setting controls the minimum chopper frequency. </span>
<a name="l00452"></a>00452 <span class="comment"> * For most applications an off time within the range of 5μs to 20μs will fit.</span>
<a name="l00453"></a>00453 <span class="comment"> * 2...15: off time setting</span>
<a name="l00454"></a>00454 <span class="comment"> *</span>
<a name="l00455"></a>00455 <span class="comment"> * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the</span>
<a name="l00456"></a>00456 <span class="comment"> * duration of the ringing on the sense resistor. For</span>
<a name="l00457"></a>00457 <span class="comment"> * 0: min. setting 3: max. setting</span>
<a name="l00458"></a>00458 <span class="comment"> *</span>
<a name="l00459"></a>00459 <span class="comment"> * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.</span>
<a name="l00460"></a>00460 <span class="comment"> * 0: slow decay only</span>
<a name="l00461"></a>00461 <span class="comment"> * 1...15: duration of fast decay phase</span>
<a name="l00462"></a>00462 <span class="comment"> *</span>
<a name="l00463"></a>00463 <span class="comment"> * sine_wave_offset: Sine wave offset. With CHM=1, these bits control the sine wave offset. </span>
<a name="l00464"></a>00464 <span class="comment"> * A positive offset corrects for zero crossing error.</span>
<a name="l00465"></a>00465 <span class="comment"> * -3..-1: negative offset 0: no offset 1...12: positive offset</span>
<a name="l00466"></a>00466 <span class="comment"> *</span>
<a name="l00467"></a>00467 <span class="comment"> * use_current_comparator: Selects usage of the current comparator for termination of the fast decay cycle. </span>
<a name="l00468"></a>00468 <span class="comment"> * If current comparator is enabled, it terminates the fast decay cycle in case the current </span>
<a name="l00469"></a>00469 <span class="comment"> * reaches a higher negative value than the actual positive value.</span>
<a name="l00470"></a>00470 <span class="comment"> * 1: enable comparator termination of fast decay cycle</span>
<a name="l00471"></a>00471 <span class="comment"> * 0: end by time only</span>
<a name="l00472"></a>00472 <span class="comment"> */</span>
<a name="l00473"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649">00473</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649" title="Sets and configure the classical Constant Off Timer Chopper.">TMC26XStepper::setConstantOffTimeChopper</a>(<span class="keywordtype">char</span> constant_off_time, <span class="keywordtype">char</span> blank_time, <span class="keywordtype">char</span> fast_decay_time_setting, <span class="keywordtype">char</span> sine_wave_offset, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> use_current_comparator) {
<a name="l00474"></a>00474 <span class="comment">//perform some sanity checks</span>
<a name="l00475"></a>00475 <span class="keywordflow">if</span> (constant_off_time&lt;2) {
<a name="l00476"></a>00476 constant_off_time=2;
<a name="l00477"></a>00477 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (constant_off_time&gt;15) {
<a name="l00478"></a>00478 constant_off_time=15;
<a name="l00479"></a>00479 }
<a name="l00480"></a>00480 <span class="comment">//save the constant off time</span>
<a name="l00481"></a>00481 this-&gt;constant_off_time = constant_off_time;
<a name="l00482"></a>00482 <span class="keywordtype">char</span> blank_value;
<a name="l00483"></a>00483 <span class="comment">//calculate the value acc to the clock cycles</span>
<a name="l00484"></a>00484 <span class="keywordflow">if</span> (blank_time&gt;=54) {
<a name="l00485"></a>00485 blank_value=3;
<a name="l00486"></a>00486 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=36) {
<a name="l00487"></a>00487 blank_value=2;
<a name="l00488"></a>00488 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=24) {
<a name="l00489"></a>00489 blank_value=1;
<a name="l00490"></a>00490 } <span class="keywordflow">else</span> {
<a name="l00491"></a>00491 blank_value=0;
<a name="l00492"></a>00492 }
<a name="l00493"></a>00493 <span class="keywordflow">if</span> (fast_decay_time_setting&lt;0) {
<a name="l00494"></a>00494 fast_decay_time_setting=0;
<a name="l00495"></a>00495 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fast_decay_time_setting&gt;15) {
<a name="l00496"></a>00496 fast_decay_time_setting=15;
<a name="l00497"></a>00497 }
<a name="l00498"></a>00498 <span class="keywordflow">if</span> (sine_wave_offset &lt; -3) {
<a name="l00499"></a>00499 sine_wave_offset = -3;
<a name="l00500"></a>00500 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sine_wave_offset&gt;12) {
<a name="l00501"></a>00501 sine_wave_offset = 12;
<a name="l00502"></a>00502 }
<a name="l00503"></a>00503 <span class="comment">//shift the sine_wave_offset</span>
<a name="l00504"></a>00504 sine_wave_offset +=3;
<a name="l00505"></a>00505
<a name="l00506"></a>00506 <span class="comment">//calculate the register setting</span>
<a name="l00507"></a>00507 <span class="comment">//first of all delete all the values for this</span>
<a name="l00508"></a>00508 chopper_config_register &amp;= ~((1&lt;&lt;12) | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">BLANK_TIMING_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">HYSTERESIS_DECREMENT_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">HYSTERESIS_LOW_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">HYSTERESIS_START_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">T_OFF_TIMING_PATERN</a>);
<a name="l00509"></a>00509 <span class="comment">//set the constant off pattern</span>
<a name="l00510"></a>00510 chopper_config_register |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">CHOPPER_MODE_T_OFF_FAST_DECAY</a>;
<a name="l00511"></a>00511 <span class="comment">//set the blank timing value</span>
<a name="l00512"></a>00512 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
<a name="l00513"></a>00513 <span class="comment">//setting the constant off time</span>
<a name="l00514"></a>00514 chopper_config_register |= constant_off_time;
<a name="l00515"></a>00515 <span class="comment">//set the fast decay time</span>
<a name="l00516"></a>00516 <span class="comment">//set msb</span>
<a name="l00517"></a>00517 chopper_config_register |= (((<span class="keywordtype">unsigned</span> long)(fast_decay_time_setting &amp; 0x8))&lt;&lt;<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a36e554a87785ce6ba998b79aae9e74e0">HYSTERESIS_DECREMENT_SHIFT</a>);
<a name="l00518"></a>00518 <span class="comment">//other bits</span>
<a name="l00519"></a>00519 chopper_config_register |= (((<span class="keywordtype">unsigned</span> long)(fast_decay_time_setting &amp; 0x7))&lt;&lt;<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">HYSTERESIS_START_VALUE_SHIFT</a>);
<a name="l00520"></a>00520 <span class="comment">//set the sine wave offset</span>
<a name="l00521"></a>00521 chopper_config_register |= (<span class="keywordtype">unsigned</span> long)sine_wave_offset &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">HYSTERESIS_LOW_SHIFT</a>;
<a name="l00522"></a>00522 <span class="comment">//using the current comparator?</span>
<a name="l00523"></a>00523 <span class="keywordflow">if</span> (!use_current_comparator) {
<a name="l00524"></a>00524 chopper_config_register |= (1&lt;&lt;12);
<a name="l00525"></a>00525 }
<a name="l00526"></a>00526 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00527"></a>00527 <span class="keywordflow">if</span> (started) {
<a name="l00528"></a>00528 send262(driver_control_register_value);
<a name="l00529"></a>00529 }
<a name="l00530"></a>00530 }
<a name="l00531"></a>00531
<a name="l00532"></a>00532 <span class="comment">/*</span>
<a name="l00533"></a>00533 <span class="comment"> * constant_off_time: The off time setting controls the minimum chopper frequency. </span>
<a name="l00534"></a>00534 <span class="comment"> * For most applications an off time within the range of 5μs to 20μs will fit.</span>
<a name="l00535"></a>00535 <span class="comment"> * 2...15: off time setting</span>
<a name="l00536"></a>00536 <span class="comment"> *</span>
<a name="l00537"></a>00537 <span class="comment"> * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the</span>
<a name="l00538"></a>00538 <span class="comment"> * duration of the ringing on the sense resistor. For</span>
<a name="l00539"></a>00539 <span class="comment"> * 0: min. setting 3: max. setting</span>
<a name="l00540"></a>00540 <span class="comment"> *</span>
<a name="l00541"></a>00541 <span class="comment"> * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.</span>
<a name="l00542"></a>00542 <span class="comment"> * 1...8</span>
<a name="l00543"></a>00543 <span class="comment"> *</span>
<a name="l00544"></a>00544 <span class="comment"> * hysteresis_end: Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by HDEC. </span>
<a name="l00545"></a>00545 <span class="comment"> * The sum HSTRT+HEND must be &lt;16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.</span>
<a name="l00546"></a>00546 <span class="comment"> * -3..-1: negative HEND 0: zero HEND 1...12: positive HEND</span>
<a name="l00547"></a>00547 <span class="comment"> *</span>
<a name="l00548"></a>00548 <span class="comment"> * hysteresis_decrement: Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time.</span>
<a name="l00549"></a>00549 <span class="comment"> * 0: fast decrement 3: very slow decrement</span>
<a name="l00550"></a>00550 <span class="comment"> */</span>
<a name="l00551"></a>00551
<a name="l00552"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa152bb7ddb72a2bc8465553a39232df2">00552</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa152bb7ddb72a2bc8465553a39232df2" title="Sets and configures with spread cycle chopper.">TMC26XStepper::setSpreadCycleChopper</a>(<span class="keywordtype">char</span> constant_off_time, <span class="keywordtype">char</span> blank_time, <span class="keywordtype">char</span> hysteresis_start, <span class="keywordtype">char</span> hysteresis_end, <span class="keywordtype">char</span> hysteresis_decrement) {
<a name="l00553"></a>00553 <span class="comment">//perform some sanity checks</span>
<a name="l00554"></a>00554 <span class="keywordflow">if</span> (constant_off_time&lt;2) {
<a name="l00555"></a>00555 constant_off_time=2;
<a name="l00556"></a>00556 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (constant_off_time&gt;15) {
<a name="l00557"></a>00557 constant_off_time=15;
<a name="l00558"></a>00558 }
<a name="l00559"></a>00559 <span class="comment">//save the constant off time</span>
<a name="l00560"></a>00560 this-&gt;constant_off_time = constant_off_time;
<a name="l00561"></a>00561 <span class="keywordtype">char</span> blank_value;
<a name="l00562"></a>00562 <span class="comment">//calculate the value acc to the clock cycles</span>
<a name="l00563"></a>00563 <span class="keywordflow">if</span> (blank_time&gt;=54) {
<a name="l00564"></a>00564 blank_value=3;
<a name="l00565"></a>00565 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=36) {
<a name="l00566"></a>00566 blank_value=2;
<a name="l00567"></a>00567 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=24) {
<a name="l00568"></a>00568 blank_value=1;
<a name="l00569"></a>00569 } <span class="keywordflow">else</span> {
<a name="l00570"></a>00570 blank_value=0;
<a name="l00571"></a>00571 }
<a name="l00572"></a>00572 <span class="keywordflow">if</span> (hysteresis_start&lt;1) {
<a name="l00573"></a>00573 hysteresis_start=1;
<a name="l00574"></a>00574 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_start&gt;8) {
<a name="l00575"></a>00575 hysteresis_start=8;
<a name="l00576"></a>00576 }
<a name="l00577"></a>00577 hysteresis_start--;
<a name="l00578"></a>00578
<a name="l00579"></a>00579 <span class="keywordflow">if</span> (hysteresis_end &lt; -3) {
<a name="l00580"></a>00580 hysteresis_end = -3;
<a name="l00581"></a>00581 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_end&gt;12) {
<a name="l00582"></a>00582 hysteresis_end = 12;
<a name="l00583"></a>00583 }
<a name="l00584"></a>00584 <span class="comment">//shift the hysteresis_end</span>
<a name="l00585"></a>00585 hysteresis_end +=3;
<a name="l00586"></a>00586
<a name="l00587"></a>00587 <span class="keywordflow">if</span> (hysteresis_decrement&lt;0) {
<a name="l00588"></a>00588 hysteresis_decrement=0;
<a name="l00589"></a>00589 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_decrement&gt;3) {
<a name="l00590"></a>00590 hysteresis_decrement=3;
<a name="l00591"></a>00591 }
<a name="l00592"></a>00592
<a name="l00593"></a>00593 <span class="comment">//first of all delete all the values for this</span>
<a name="l00594"></a>00594 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">CHOPPER_MODE_T_OFF_FAST_DECAY</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">BLANK_TIMING_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">HYSTERESIS_DECREMENT_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">HYSTERESIS_LOW_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">HYSTERESIS_START_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">T_OFF_TIMING_PATERN</a>);
<a name="l00595"></a>00595
<a name="l00596"></a>00596 <span class="comment">//set the blank timing value</span>
<a name="l00597"></a>00597 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
<a name="l00598"></a>00598 <span class="comment">//setting the constant off time</span>
<a name="l00599"></a>00599 chopper_config_register |= constant_off_time;
<a name="l00600"></a>00600 <span class="comment">//set the hysteresis_start</span>
<a name="l00601"></a>00601 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)hysteresis_start) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">HYSTERESIS_START_VALUE_SHIFT</a>;
<a name="l00602"></a>00602 <span class="comment">//set the hysteresis end</span>
<a name="l00603"></a>00603 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)hysteresis_end) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">HYSTERESIS_LOW_SHIFT</a>;
<a name="l00604"></a>00604 <span class="comment">//set the hystereis decrement</span>
<a name="l00605"></a>00605 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
<a name="l00606"></a>00606 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00607"></a>00607 <span class="keywordflow">if</span> (started) {
<a name="l00608"></a>00608 send262(driver_control_register_value);
<a name="l00609"></a>00609 }
<a name="l00610"></a>00610 }
<a name="l00611"></a>00611
<a name="l00612"></a>00612 <span class="comment">/*</span>
<a name="l00613"></a>00613 <span class="comment"> * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized. </span>
<a name="l00614"></a>00614 <span class="comment"> * The frequency of each chopper mainly depends on the coil current and the position dependant motor coil inductivity, thus it depends on the microstep position. </span>
<a name="l00615"></a>00615 <span class="comment"> * With some motors a slightly audible beat can occur between the chopper frequencies, especially when they are near to each other. This typically occurs at a </span>
<a name="l00616"></a>00616 <span class="comment"> * few microstep positions within each quarter wave. This effect normally is not audible when compared to mechanical noise generated by ball bearings, etc. </span>
<a name="l00617"></a>00617 <span class="comment"> * Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.</span>
<a name="l00618"></a>00618 <span class="comment"> * Hint: A common factor, which can cause motor noise, is a bad PCB layout causing coupling of both sense resistor voltages </span>
<a name="l00619"></a>00619 <span class="comment"> * (please refer to sense resistor layout hint in chapter 8.1).</span>
<a name="l00620"></a>00620 <span class="comment"> * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided. </span>
<a name="l00621"></a>00621 <span class="comment"> * It modulates the slow decay time setting when switched on by the RNDTF bit. The RNDTF feature further spreads the chopper spectrum, </span>
<a name="l00622"></a>00622 <span class="comment"> * reducing electromagnetic emission on single frequencies.</span>
<a name="l00623"></a>00623 <span class="comment"> */</span>
<a name="l00624"></a><a class="code" href="class_t_m_c26_x_stepper.html#a7ffd602cf4bf385847cba034417d5f0a">00624</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a7ffd602cf4bf385847cba034417d5f0a" title="Use random off time for noise reduction (0 for off, -1 for on).">TMC26XStepper::setRandomOffTime</a>(<span class="keywordtype">char</span> value) {
<a name="l00625"></a>00625 <span class="keywordflow">if</span> (value) {
<a name="l00626"></a>00626 chopper_config_register |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">RANDOM_TOFF_TIME</a>;
<a name="l00627"></a>00627 } <span class="keywordflow">else</span> {
<a name="l00628"></a>00628 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">RANDOM_TOFF_TIME</a>);
<a name="l00629"></a>00629 }
<a name="l00630"></a>00630 <span class="comment">//if started we directly send it to the motor</span>
<a name="l00631"></a>00631 <span class="keywordflow">if</span> (started) {
<a name="l00632"></a>00632 send262(driver_control_register_value);
<a name="l00633"></a>00633 }
<a name="l00634"></a>00634 }
<a name="l00635"></a>00635
<a name="l00636"></a><a class="code" href="class_t_m_c26_x_stepper.html#a381fbcce7c586ca2f1da8f9e704df14e">00636</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a381fbcce7c586ca2f1da8f9e704df14e" title="This method configures the CoolStep smart energy operation. You must have a proper StallGuard configu...">TMC26XStepper::setCoolStepConfiguration</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> lower_SG_threshold, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> SG_hysteresis, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_decrement_step_size,
<a name="l00637"></a>00637 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_increment_step_size, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> lower_current_limit) {
<a name="l00638"></a>00638 <span class="comment">//sanitize the input values</span>
<a name="l00639"></a>00639 <span class="keywordflow">if</span> (lower_SG_threshold&gt;480) {
<a name="l00640"></a>00640 lower_SG_threshold = 480;
<a name="l00641"></a>00641 }
<a name="l00642"></a>00642 <span class="comment">//divide by 32</span>
<a name="l00643"></a>00643 lower_SG_threshold &gt;&gt;=5;
<a name="l00644"></a>00644 <span class="keywordflow">if</span> (SG_hysteresis&gt;480) {
<a name="l00645"></a>00645 SG_hysteresis=480;
<a name="l00646"></a>00646 }
<a name="l00647"></a>00647 <span class="comment">//divide by 32</span>
<a name="l00648"></a>00648 SG_hysteresis &gt;&gt;=5;
<a name="l00649"></a>00649 <span class="keywordflow">if</span> (current_decrement_step_size&gt;3) {
<a name="l00650"></a>00650 current_decrement_step_size=3;
<a name="l00651"></a>00651 }
<a name="l00652"></a>00652 <span class="keywordflow">if</span> (current_increment_step_size&gt;3) {
<a name="l00653"></a>00653 current_increment_step_size=3;
<a name="l00654"></a>00654 }
<a name="l00655"></a>00655 <span class="keywordflow">if</span> (lower_current_limit&gt;1) {
<a name="l00656"></a>00656 lower_current_limit=1;
<a name="l00657"></a>00657 }
<a name="l00658"></a>00658 <span class="comment">//store the lower level in order to enable/disable the cool step</span>
<a name="l00659"></a>00659 this-&gt;cool_step_lower_threshold=lower_SG_threshold;
<a name="l00660"></a>00660 <span class="comment">//if cool step is not enabled we delete the lower value to keep it disabled</span>
<a name="l00661"></a>00661 <span class="keywordflow">if</span> (!this-&gt;cool_step_enabled) {
<a name="l00662"></a>00662 lower_SG_threshold=0;
<a name="l00663"></a>00663 }
<a name="l00664"></a>00664 <span class="comment">//the good news is that we can start with a complete new cool step register value</span>
<a name="l00665"></a>00665 <span class="comment">//and simply set the values in the register</span>
<a name="l00666"></a>00666 cool_step_register_value = ((<span class="keywordtype">unsigned</span> long)lower_SG_threshold) | (((<span class="keywordtype">unsigned</span> long)SG_hysteresis)&lt;&lt;8) | (((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)current_decrement_step_size)&lt;&lt;5)
<a name="l00667"></a>00667 | (((<span class="keywordtype">unsigned</span> long)current_increment_step_size)&lt;&lt;13) | (((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)lower_current_limit)&lt;&lt;15)
<a name="l00668"></a>00668 <span class="comment">//and of course we have to include the signature of the register</span>
<a name="l00669"></a>00669 | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">COOL_STEP_REGISTER</a>;
<a name="l00670"></a>00670 <span class="comment">//Serial.println(cool_step_register_value,HEX);</span>
<a name="l00671"></a>00671 <span class="keywordflow">if</span> (started) {
<a name="l00672"></a>00672 send262(cool_step_register_value);
<a name="l00673"></a>00673 }
<a name="l00674"></a>00674 }
<a name="l00675"></a>00675
<a name="l00676"></a><a class="code" href="class_t_m_c26_x_stepper.html#a15bf0ed5a166a5d9a41f90f3ccbc6157">00676</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a15bf0ed5a166a5d9a41f90f3ccbc6157" title="enables or disables the CoolStep smart energy operation feature. It must be configured before enablin...">TMC26XStepper::setCoolStepEnabled</a>(<span class="keywordtype">boolean</span> enabled) {
<a name="l00677"></a>00677 <span class="comment">//simply delete the lower limit to disable the cool step</span>
<a name="l00678"></a>00678 cool_step_register_value &amp;= ~<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae1862dfb958c03698b0abd95fda033ea">SE_MIN_PATTERN</a>;
<a name="l00679"></a>00679 <span class="comment">//and set it to the proper value if cool step is to be enabled</span>
<a name="l00680"></a>00680 <span class="keywordflow">if</span> (enabled) {
<a name="l00681"></a>00681 cool_step_register_value |=this-&gt;cool_step_lower_threshold;
<a name="l00682"></a>00682 }
<a name="l00683"></a>00683 <span class="comment">//and save the enabled status</span>
<a name="l00684"></a>00684 this-&gt;cool_step_enabled = enabled;
<a name="l00685"></a>00685 <span class="comment">//save the register value</span>
<a name="l00686"></a>00686 <span class="keywordflow">if</span> (started) {
<a name="l00687"></a>00687 send262(cool_step_register_value);
<a name="l00688"></a>00688 }
<a name="l00689"></a>00689 }
<a name="l00690"></a>00690
<a name="l00691"></a><a class="code" href="class_t_m_c26_x_stepper.html#a6de2306b6d8dc1fa2e50fccb66d8e66d">00691</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a6de2306b6d8dc1fa2e50fccb66d8e66d" title="check if the CoolStep feature is enabled">TMC26XStepper::isCoolStepEnabled</a>(<span class="keywordtype">void</span>) {
<a name="l00692"></a>00692 <span class="keywordflow">return</span> this-&gt;cool_step_enabled;
<a name="l00693"></a>00693 }
<a name="l00694"></a>00694
<a name="l00695"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa7469949deaa39a58038b3ddef532bc8">00695</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa7469949deaa39a58038b3ddef532bc8" title="returns the lower StallGuard threshold for the CoolStep operation">TMC26XStepper::getCoolStepLowerSgThreshold</a>() {
<a name="l00696"></a>00696 <span class="comment">//we return our internally stored value - in order to provide the correct setting even if cool step is not enabled</span>
<a name="l00697"></a>00697 <span class="keywordflow">return</span> this-&gt;cool_step_lower_threshold&lt;&lt;5;
<a name="l00698"></a>00698 }
<a name="l00699"></a>00699
<a name="l00700"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac61298fd658773c28823d33ab04e970f">00700</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac61298fd658773c28823d33ab04e970f" title="returns the upper StallGuard threshold for the CoolStep operation">TMC26XStepper::getCoolStepUpperSgThreshold</a>() {
<a name="l00701"></a>00701 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac8f748bf735c447dbed7dd4c7b631a87">SE_MAX_PATTERN</a>)&gt;&gt;8)&lt;&lt;5;
<a name="l00702"></a>00702 }
<a name="l00703"></a>00703
<a name="l00704"></a><a class="code" href="class_t_m_c26_x_stepper.html#ababe688a15f087d23d4ff2094fcee883">00704</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#ababe688a15f087d23d4ff2094fcee883" title="returns the increment steps for the current for the CoolStep operation">TMC26XStepper::getCoolStepCurrentIncrementSize</a>() {
<a name="l00705"></a>00705 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbe13a0464355e42fbe786ca5f58ed8d">CURRENT_DOWN_STEP_SPEED_PATTERN</a>)&gt;&gt;13);
<a name="l00706"></a>00706 }
<a name="l00707"></a>00707
<a name="l00708"></a><a class="code" href="class_t_m_c26_x_stepper.html#aad44ee5ae73bf8e69af05674a304ba46">00708</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#aad44ee5ae73bf8e69af05674a304ba46" title="returns the number of StallGuard readings befor CoolStep adjusts the motor current.">TMC26XStepper::getCoolStepNumberOfSGReadings</a>() {
<a name="l00709"></a>00709 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aba6c07e5672e34e618bb3a550ab0d2bc">SE_CURRENT_STEP_WIDTH_PATTERN</a>)&gt;&gt;5);
<a name="l00710"></a>00710 }
<a name="l00711"></a>00711
<a name="l00712"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0c7e8541abc120a3910e35c6fbf2167c">00712</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0c7e8541abc120a3910e35c6fbf2167c" title="returns the absolut minium current for the CoolStep operation">TMC26XStepper::getCoolStepLowerCurrentLimit</a>() {
<a name="l00713"></a>00713 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8a261a77d198b85f6dd8416387b354b3">MINIMUM_CURRENT_FOURTH</a>)&gt;&gt;15);
<a name="l00714"></a>00714 }
<a name="l00715"></a>00715
<a name="l00716"></a><a class="code" href="class_t_m_c26_x_stepper.html#a4472cd86ad5b65dec5ec45ce69158305">00716</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a4472cd86ad5b65dec5ec45ce69158305" title="enables or disables the motor driver bridges. If disabled the motor can run freely. If enabled not.">TMC26XStepper::setEnabled</a>(<span class="keywordtype">boolean</span> enabled) {
<a name="l00717"></a>00717 <span class="comment">//delete the t_off in the chopper config to get sure</span>
<a name="l00718"></a>00718 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">T_OFF_PATTERN</a>);
<a name="l00719"></a>00719 <span class="keywordflow">if</span> (enabled) {
<a name="l00720"></a>00720 <span class="comment">//and set the t_off time</span>
<a name="l00721"></a>00721 chopper_config_register |= this-&gt;constant_off_time;
<a name="l00722"></a>00722 }
<a name="l00723"></a>00723 <span class="comment">//if not enabled we don&#39;t have to do anything since we already delete t_off from the register</span>
<a name="l00724"></a>00724 <span class="keywordflow">if</span> (started) {
<a name="l00725"></a>00725 send262(chopper_config_register);
<a name="l00726"></a>00726 }
<a name="l00727"></a>00727 }
<a name="l00728"></a>00728
<a name="l00729"></a><a class="code" href="class_t_m_c26_x_stepper.html#a15796c0cbdeab23a343c3f25327283b6">00729</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a15796c0cbdeab23a343c3f25327283b6" title="checks if the output bridges are enabled. If the bridges are not enabled the motor can run freely...">TMC26XStepper::isEnabled</a>() {
<a name="l00730"></a>00730 <span class="keywordflow">if</span> (chopper_config_register &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">T_OFF_PATTERN</a>) {
<a name="l00731"></a>00731 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00732"></a>00732 } <span class="keywordflow">else</span> {
<a name="l00733"></a>00733 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00734"></a>00734 }
<a name="l00735"></a>00735 }
<a name="l00736"></a>00736
<a name="l00737"></a>00737 <span class="comment">/*</span>
<a name="l00738"></a>00738 <span class="comment"> * reads a value from the TMC26X status register. The value is not obtained directly but can then </span>
<a name="l00739"></a>00739 <span class="comment"> * be read by the various status routines.</span>
<a name="l00740"></a>00740 <span class="comment"> *</span>
<a name="l00741"></a>00741 <span class="comment"> */</span>
<a name="l00742"></a><a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967">00742</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">TMC26XStepper::readStatus</a>(<span class="keywordtype">char</span> read_value) {
<a name="l00743"></a>00743 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> old_driver_configuration_register_value = driver_configuration_register_value;
<a name="l00744"></a>00744 <span class="comment">//reset the readout configuration</span>
<a name="l00745"></a>00745 driver_configuration_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">READ_SELECTION_PATTERN</a>);
<a name="l00746"></a>00746 <span class="comment">//this now equals TMC26X_READOUT_POSITION - so we just have to check the other two options</span>
<a name="l00747"></a>00747 <span class="keywordflow">if</span> (read_value == <a class="code" href="_t_m_c26_x_stepper_8h.html#ac864ff8886123039c7d2d3c617f7ef87">TMC26X_READOUT_STALLGUARD</a>) {
<a name="l00748"></a>00748 driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>;
<a name="l00749"></a>00749 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (read_value == <a class="code" href="_t_m_c26_x_stepper_8h.html#a01760ad15e3846536526a990efe47094">TMC26X_READOUT_CURRENT</a>) {
<a name="l00750"></a>00750 driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">READ_STALL_GUARD_AND_COOL_STEP</a>;
<a name="l00751"></a>00751 }
<a name="l00752"></a>00752 <span class="comment">//all other cases are ignored to prevent funny values</span>
<a name="l00753"></a>00753 <span class="comment">//check if the readout is configured for the value we are interested in</span>
<a name="l00754"></a>00754 <span class="keywordflow">if</span> (driver_configuration_register_value!=old_driver_configuration_register_value) {
<a name="l00755"></a>00755 <span class="comment">//because then we need to write the value twice - one time for configuring, second time to get the value, see below</span>
<a name="l00756"></a>00756 send262(driver_configuration_register_value);
<a name="l00757"></a>00757 }
<a name="l00758"></a>00758 <span class="comment">//write the configuration to get the last status </span>
<a name="l00759"></a>00759 send262(driver_configuration_register_value);
<a name="l00760"></a>00760 }
<a name="l00761"></a>00761
<a name="l00762"></a><a class="code" href="class_t_m_c26_x_stepper.html#a1019f6f889acfd3176eecd60a0a20125">00762</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a1019f6f889acfd3176eecd60a0a20125" title="Get the current microstep position for phase A.">TMC26XStepper::getMotorPosition</a>(<span class="keywordtype">void</span>) {
<a name="l00763"></a>00763 <span class="comment">//we read it out even if we are not started yet - perhaps it is useful information for somebody</span>
<a name="l00764"></a>00764 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#aff05d4a47ef8821322ccc2a20785fbee">TMC26X_READOUT_POSITION</a>);
<a name="l00765"></a>00765 <span class="keywordflow">return</span> getReadoutValue();
<a name="l00766"></a>00766 }
<a name="l00767"></a>00767
<a name="l00768"></a>00768 <span class="comment">//reads the stall guard setting from last status</span>
<a name="l00769"></a>00769 <span class="comment">//returns -1 if stallguard information is not present</span>
<a name="l00770"></a><a class="code" href="class_t_m_c26_x_stepper.html#aed570ce3eea640e087b046333015de1e">00770</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aed570ce3eea640e087b046333015de1e" title="Reads the current StallGuard value.">TMC26XStepper::getCurrentStallGuardReading</a>(<span class="keywordtype">void</span>) {
<a name="l00771"></a>00771 <span class="comment">//if we don&#39;t yet started there cannot be a stall guard value</span>
<a name="l00772"></a>00772 <span class="keywordflow">if</span> (!started) {
<a name="l00773"></a>00773 <span class="keywordflow">return</span> -1;
<a name="l00774"></a>00774 }
<a name="l00775"></a>00775 <span class="comment">//not time optimal, but solution optiomal:</span>
<a name="l00776"></a>00776 <span class="comment">//first read out the stall guard value</span>
<a name="l00777"></a>00777 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#ac864ff8886123039c7d2d3c617f7ef87">TMC26X_READOUT_STALLGUARD</a>);
<a name="l00778"></a>00778 <span class="keywordflow">return</span> getReadoutValue();
<a name="l00779"></a>00779 }
<a name="l00780"></a>00780
<a name="l00781"></a><a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1">00781</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1" title="Reads the current current setting value as fraction of the maximum current Returns values between 0 a...">TMC26XStepper::getCurrentCSReading</a>(<span class="keywordtype">void</span>) {
<a name="l00782"></a>00782 <span class="comment">//if we don&#39;t yet started there cannot be a stall guard value</span>
<a name="l00783"></a>00783 <span class="keywordflow">if</span> (!started) {
<a name="l00784"></a>00784 <span class="keywordflow">return</span> 0;
<a name="l00785"></a>00785 }
<a name="l00786"></a>00786 <span class="comment">//not time optimal, but solution optiomal:</span>
<a name="l00787"></a>00787 <span class="comment">//first read out the stall guard value</span>
<a name="l00788"></a>00788 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#a01760ad15e3846536526a990efe47094">TMC26X_READOUT_CURRENT</a>);
<a name="l00789"></a>00789 <span class="keywordflow">return</span> (getReadoutValue() &amp; 0x1f);
<a name="l00790"></a>00790 }
<a name="l00791"></a>00791
<a name="l00792"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa00741168a7def0a7a9d2f2c9d3b99d7">00792</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa00741168a7def0a7a9d2f2c9d3b99d7" title="Reads the current current setting value and recalculates the absolute current in mA (1A would be 1000...">TMC26XStepper::getCurrentCurrent</a>(<span class="keywordtype">void</span>) {
<a name="l00793"></a>00793 <span class="keywordtype">double</span> result = (double)<a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1" title="Reads the current current setting value as fraction of the maximum current Returns values between 0 a...">getCurrentCSReading</a>();
<a name="l00794"></a>00794 <span class="keywordtype">double</span> resistor_value = (double)this-&gt;resistor;
<a name="l00795"></a>00795 <span class="keywordtype">double</span> voltage = (driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>)? 0.165:0.31;
<a name="l00796"></a>00796 result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
<a name="l00797"></a>00797 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)result;
<a name="l00798"></a>00798 }
<a name="l00799"></a>00799
<a name="l00800"></a>00800 <span class="comment">/*</span>
<a name="l00801"></a>00801 <span class="comment"> return true if the stallguard threshold has been reached</span>
<a name="l00802"></a>00802 <span class="comment">*/</span>
<a name="l00803"></a><a class="code" href="class_t_m_c26_x_stepper.html#aea4c6e1fac909116c6b55f902d6cff41">00803</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#aea4c6e1fac909116c6b55f902d6cff41" title="checks if there is a StallGuard warning in the last status">TMC26XStepper::isStallGuardOverThreshold</a>(<span class="keywordtype">void</span>) {
<a name="l00804"></a>00804 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00805"></a>00805 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00806"></a>00806 }
<a name="l00807"></a>00807 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">STATUS_STALL_GUARD_STATUS</a>);
<a name="l00808"></a>00808 }
<a name="l00809"></a>00809
<a name="l00810"></a>00810 <span class="comment">/*</span>
<a name="l00811"></a>00811 <span class="comment"> returns if there is any over temperature condition:</span>
<a name="l00812"></a>00812 <span class="comment"> OVER_TEMPERATURE_PREWARING if pre warning level has been reached</span>
<a name="l00813"></a>00813 <span class="comment"> OVER_TEMPERATURE_SHUTDOWN if the temperature is so hot that the driver is shut down</span>
<a name="l00814"></a>00814 <span class="comment"> Any of those levels are not too good.</span>
<a name="l00815"></a>00815 <span class="comment">*/</span>
<a name="l00816"></a><a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7">00816</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">TMC26XStepper::getOverTemperature</a>(<span class="keywordtype">void</span>) {
<a name="l00817"></a>00817 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00818"></a>00818 <span class="keywordflow">return</span> 0;
<a name="l00819"></a>00819 }
<a name="l00820"></a>00820 <span class="keywordflow">if</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbb23d2f055c9eab55eac29d1a75deb4">STATUS_OVER_TEMPERATURE_SHUTDOWN</a>) {
<a name="l00821"></a>00821 <span class="keywordflow">return</span> <a class="code" href="_t_m_c26_x_stepper_8h.html#adae814ce848677abd87758c7ac79a436" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature shutdown in the TMC...">TMC26X_OVERTEMPERATURE_SHUTDOWN</a>;
<a name="l00822"></a>00822 }
<a name="l00823"></a>00823 <span class="keywordflow">if</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa317fd77f2f26fdfbfd331e21d9069e8">STATUS_OVER_TEMPERATURE_WARNING</a>) {
<a name="l00824"></a>00824 <span class="keywordflow">return</span> <a class="code" href="_t_m_c26_x_stepper_8h.html#add42eee34f674f92c19bcd5266d2445f" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature situation in the TM...">TMC26X_OVERTEMPERATURE_PREWARING</a>;
<a name="l00825"></a>00825 }
<a name="l00826"></a>00826 <span class="keywordflow">return</span> 0;
<a name="l00827"></a>00827 }
<a name="l00828"></a>00828
<a name="l00829"></a>00829 <span class="comment">//is motor channel A shorted to ground</span>
<a name="l00830"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf">00830</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf" title="Is motor channel A shorted to ground detected in the last status readout.">TMC26XStepper::isShortToGroundA</a>(<span class="keywordtype">void</span>) {
<a name="l00831"></a>00831 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00832"></a>00832 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00833"></a>00833 }
<a name="l00834"></a>00834 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8e03041302a092174fa33b3cf837dca2">STATUS_SHORT_TO_GROUND_A</a>);
<a name="l00835"></a>00835 }
<a name="l00836"></a>00836
<a name="l00837"></a>00837 <span class="comment">//is motor channel B shorted to ground</span>
<a name="l00838"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b">00838</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b" title="Is motor channel B shorted to ground detected in the last status readout.">TMC26XStepper::isShortToGroundB</a>(<span class="keywordtype">void</span>) {
<a name="l00839"></a>00839 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00840"></a>00840 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00841"></a>00841 }
<a name="l00842"></a>00842 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a903c3eba99695a32c6736463dcfd93ae">STATUS_SHORT_TO_GROUND_B</a>);
<a name="l00843"></a>00843 }
<a name="l00844"></a>00844
<a name="l00845"></a>00845 <span class="comment">//is motor channel A connected</span>
<a name="l00846"></a><a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c">00846</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c" title="iIs motor channel A connected according to the last statu readout.">TMC26XStepper::isOpenLoadA</a>(<span class="keywordtype">void</span>) {
<a name="l00847"></a>00847 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00848"></a>00848 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00849"></a>00849 }
<a name="l00850"></a>00850 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae9cbbe5af7188e6bff8fe412f8e42f59">STATUS_OPEN_LOAD_A</a>);
<a name="l00851"></a>00851 }
<a name="l00852"></a>00852
<a name="l00853"></a>00853 <span class="comment">//is motor channel B connected</span>
<a name="l00854"></a><a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303">00854</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303" title="iIs motor channel A connected according to the last statu readout.">TMC26XStepper::isOpenLoadB</a>(<span class="keywordtype">void</span>) {
<a name="l00855"></a>00855 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00856"></a>00856 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00857"></a>00857 }
<a name="l00858"></a>00858 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab29dc5cd6c6c4e5bf99e71bd563e1be1">STATUS_OPEN_LOAD_B</a>);
<a name="l00859"></a>00859 }
<a name="l00860"></a>00860
<a name="l00861"></a>00861 <span class="comment">//is chopper inactive since 2^20 clock cycles - defaults to ~0,08s</span>
<a name="l00862"></a><a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0">00862</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0" title="Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s.">TMC26XStepper::isStandStill</a>(<span class="keywordtype">void</span>) {
<a name="l00863"></a>00863 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00864"></a>00864 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00865"></a>00865 }
<a name="l00866"></a>00866 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab96ed1635faee6650e9cce73598a2773">STATUS_STAND_STILL</a>);
<a name="l00867"></a>00867 }
<a name="l00868"></a>00868
<a name="l00869"></a>00869 <span class="comment">//is chopper inactive since 2^20 clock cycles - defaults to ~0,08s</span>
<a name="l00870"></a><a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7">00870</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7" title="checks if there is a StallGuard warning in the last status">TMC26XStepper::isStallGuardReached</a>(<span class="keywordtype">void</span>) {
<a name="l00871"></a>00871 <span class="keywordflow">if</span> (!this-&gt;started) {
<a name="l00872"></a>00872 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00873"></a>00873 }
<a name="l00874"></a>00874 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">STATUS_STALL_GUARD_STATUS</a>);
<a name="l00875"></a>00875 }
<a name="l00876"></a>00876
<a name="l00877"></a>00877 <span class="comment">//reads the stall guard setting from last status</span>
<a name="l00878"></a>00878 <span class="comment">//returns -1 if stallguard inforamtion is not present</span>
<a name="l00879"></a>00879 <span class="keywordtype">int</span> TMC26XStepper::getReadoutValue(<span class="keywordtype">void</span>) {
<a name="l00880"></a>00880 <span class="keywordflow">return</span> (<span class="keywordtype">int</span>)(driver_status_result &gt;&gt; 10);
<a name="l00881"></a>00881 }
<a name="l00882"></a>00882
<a name="l00883"></a><a class="code" href="class_t_m_c26_x_stepper.html#ae1db5ec2ec9bfbfaea83c659e006692e">00883</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ae1db5ec2ec9bfbfaea83c659e006692e" title="Returns the current sense resistor value in milliohm. The default value of ,15 Ohm will return 150...">TMC26XStepper::getResistor</a>() {
<a name="l00884"></a>00884 <span class="keywordflow">return</span> this-&gt;resistor;
<a name="l00885"></a>00885 }
<a name="l00886"></a>00886
<a name="l00887"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad435db189ebb101fb2de90a484f33905">00887</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad435db189ebb101fb2de90a484f33905" title="a convenience method to determine if the current scaling uses 0.31V or 0.165V as reference.">TMC26XStepper::isCurrentScalingHalfed</a>() {
<a name="l00888"></a>00888 <span class="keywordflow">if</span> (this-&gt;driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>) {
<a name="l00889"></a>00889 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00890"></a>00890 } <span class="keywordflow">else</span> {
<a name="l00891"></a>00891 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00892"></a>00892 }
<a name="l00893"></a>00893 }
<a name="l00894"></a>00894 <span class="comment">/*</span>
<a name="l00895"></a>00895 <span class="comment"> version() returns the version of the library:</span>
<a name="l00896"></a>00896 <span class="comment"> */</span>
<a name="l00897"></a><a class="code" href="class_t_m_c26_x_stepper.html#ab040d9df1e85d6fb0c105205a36b0215">00897</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ab040d9df1e85d6fb0c105205a36b0215" title="library version">TMC26XStepper::version</a>(<span class="keywordtype">void</span>)
<a name="l00898"></a>00898 {
<a name="l00899"></a>00899 <span class="keywordflow">return</span> 1;
<a name="l00900"></a>00900 }
<a name="l00901"></a>00901
<a name="l00902"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3">00902</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3" title="Prints out all the information that can be found in the last status read out - it does not force a st...">TMC26XStepper::debugLastStatus</a>() {
<a name="l00903"></a>00903 <span class="preprocessor">#ifdef DEBUG </span>
<a name="l00904"></a>00904 <span class="preprocessor"></span><span class="keywordflow">if</span> (this-&gt;started) {
<a name="l00905"></a>00905 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">getOverTemperature</a>()&amp;<a class="code" href="_t_m_c26_x_stepper_8h.html#add42eee34f674f92c19bcd5266d2445f" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature situation in the TM...">TMC26X_OVERTEMPERATURE_PREWARING</a>) {
<a name="l00906"></a>00906 Serial.println(<span class="stringliteral">&quot;WARNING: Overtemperature Prewarning!&quot;</span>);
<a name="l00907"></a>00907 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">getOverTemperature</a>()&amp;<a class="code" href="_t_m_c26_x_stepper_8h.html#adae814ce848677abd87758c7ac79a436" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature shutdown in the TMC...">TMC26X_OVERTEMPERATURE_SHUTDOWN</a>) {
<a name="l00908"></a>00908 Serial.println(<span class="stringliteral">&quot;ERROR: Overtemperature Shutdown!&quot;</span>);
<a name="l00909"></a>00909 }
<a name="l00910"></a>00910 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf" title="Is motor channel A shorted to ground detected in the last status readout.">isShortToGroundA</a>()) {
<a name="l00911"></a>00911 Serial.println(<span class="stringliteral">&quot;ERROR: SHORT to ground on channel A!&quot;</span>);
<a name="l00912"></a>00912 }
<a name="l00913"></a>00913 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b" title="Is motor channel B shorted to ground detected in the last status readout.">isShortToGroundB</a>()) {
<a name="l00914"></a>00914 Serial.println(<span class="stringliteral">&quot;ERROR: SHORT to ground on channel A!&quot;</span>);
<a name="l00915"></a>00915 }
<a name="l00916"></a>00916 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c" title="iIs motor channel A connected according to the last statu readout.">isOpenLoadA</a>()) {
<a name="l00917"></a>00917 Serial.println(<span class="stringliteral">&quot;ERROR: Channel A seems to be unconnected!&quot;</span>);
<a name="l00918"></a>00918 }
<a name="l00919"></a>00919 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303" title="iIs motor channel A connected according to the last statu readout.">isOpenLoadB</a>()) {
<a name="l00920"></a>00920 Serial.println(<span class="stringliteral">&quot;ERROR: Channel B seems to be unconnected!&quot;</span>);
<a name="l00921"></a>00921 }
<a name="l00922"></a>00922 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7" title="checks if there is a StallGuard warning in the last status">isStallGuardReached</a>()) {
<a name="l00923"></a>00923 Serial.println(<span class="stringliteral">&quot;INFO: Stall Guard level reached!&quot;</span>);
<a name="l00924"></a>00924 }
<a name="l00925"></a>00925 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0" title="Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s.">isStandStill</a>()) {
<a name="l00926"></a>00926 Serial.println(<span class="stringliteral">&quot;INFO: Motor is standing still.&quot;</span>);
<a name="l00927"></a>00927 }
<a name="l00928"></a>00928 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> readout_config = driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">READ_SELECTION_PATTERN</a>;
<a name="l00929"></a>00929 <span class="keywordtype">int</span> value = getReadoutValue();
<a name="l00930"></a>00930 <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a143b7757272f07866d9655bde8303d9a">READ_MICROSTEP_POSTION</a>) {
<a name="l00931"></a>00931 Serial.print(<span class="stringliteral">&quot;Microstep postion phase A: &quot;</span>);
<a name="l00932"></a>00932 Serial.println(value);
<a name="l00933"></a>00933 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>) {
<a name="l00934"></a>00934 Serial.print(<span class="stringliteral">&quot;Stall Guard value:&quot;</span>);
<a name="l00935"></a>00935 Serial.println(value);
<a name="l00936"></a>00936 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">READ_STALL_GUARD_AND_COOL_STEP</a>) {
<a name="l00937"></a>00937 <span class="keywordtype">int</span> stallGuard = value &amp; 0xf;
<a name="l00938"></a>00938 <span class="keywordtype">int</span> current = value &amp; 0x1F0;
<a name="l00939"></a>00939 Serial.print(<span class="stringliteral">&quot;Approx Stall Guard: &quot;</span>);
<a name="l00940"></a>00940 Serial.println(stallGuard);
<a name="l00941"></a>00941 Serial.print(<span class="stringliteral">&quot;Current level&quot;</span>);
<a name="l00942"></a>00942 Serial.println(current);
<a name="l00943"></a>00943 }
<a name="l00944"></a>00944 }
<a name="l00945"></a>00945 <span class="preprocessor">#endif</span>
<a name="l00946"></a>00946 <span class="preprocessor"></span>}
<a name="l00947"></a>00947
<a name="l00948"></a>00948 <span class="comment">/*</span>
<a name="l00949"></a>00949 <span class="comment"> * send register settings to the stepper driver via SPI</span>
<a name="l00950"></a>00950 <span class="comment"> * returns the current status</span>
<a name="l00951"></a>00951 <span class="comment"> */</span>
<a name="l00952"></a>00952 <span class="keyword">inline</span> <span class="keywordtype">void</span> TMC26XStepper::send262(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> datagram) {
<a name="l00953"></a>00953 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> i_datagram;
<a name="l00954"></a>00954
<a name="l00955"></a>00955 <span class="comment">//preserver the previous spi mode</span>
<a name="l00956"></a>00956 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> oldMode = SPCR &amp; SPI_MODE_MASK;
<a name="l00957"></a>00957
<a name="l00958"></a>00958 <span class="comment">//if the mode is not correct set it to mode 3</span>
<a name="l00959"></a>00959 <span class="keywordflow">if</span> (oldMode != SPI_MODE3) {
<a name="l00960"></a>00960 SPI.setDataMode(SPI_MODE3);
<a name="l00961"></a>00961 }
<a name="l00962"></a>00962
<a name="l00963"></a>00963 <span class="comment">//select the TMC driver</span>
<a name="l00964"></a>00964 digitalWrite(cs_pin,LOW);
<a name="l00965"></a>00965
<a name="l00966"></a>00966 <span class="comment">//ensure that only valid bist are set (0-19)</span>
<a name="l00967"></a>00967 <span class="comment">//datagram &amp;=REGISTER_BIT_PATTERN;</span>
<a name="l00968"></a>00968
<a name="l00969"></a>00969 <span class="preprocessor">#ifdef DEBUG</span>
<a name="l00970"></a>00970 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Sending &quot;</span>);
<a name="l00971"></a>00971 Serial.println(datagram,HEX);
<a name="l00972"></a>00972 <span class="preprocessor">#endif</span>
<a name="l00973"></a>00973 <span class="preprocessor"></span>
<a name="l00974"></a>00974 <span class="comment">//write/read the values</span>
<a name="l00975"></a>00975 i_datagram = SPI.transfer((datagram &gt;&gt; 16) &amp; 0xff);
<a name="l00976"></a>00976 i_datagram &lt;&lt;= 8;
<a name="l00977"></a>00977 i_datagram |= SPI.transfer((datagram &gt;&gt; 8) &amp; 0xff);
<a name="l00978"></a>00978 i_datagram &lt;&lt;= 8;
<a name="l00979"></a>00979 i_datagram |= SPI.transfer((datagram) &amp; 0xff);
<a name="l00980"></a>00980 i_datagram &gt;&gt;= 4;
<a name="l00981"></a>00981
<a name="l00982"></a>00982 <span class="preprocessor">#ifdef DEBUG</span>
<a name="l00983"></a>00983 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Received &quot;</span>);
<a name="l00984"></a>00984 Serial.println(i_datagram,HEX);
<a name="l00985"></a>00985 <a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3" title="Prints out all the information that can be found in the last status read out - it does not force a st...">debugLastStatus</a>();
<a name="l00986"></a>00986 <span class="preprocessor">#endif</span>
<a name="l00987"></a>00987 <span class="preprocessor"></span> <span class="comment">//deselect the TMC chip</span>
<a name="l00988"></a>00988 digitalWrite(cs_pin,HIGH);
<a name="l00989"></a>00989
<a name="l00990"></a>00990 <span class="comment">//restore the previous SPI mode if neccessary</span>
<a name="l00991"></a>00991 <span class="comment">//if the mode is not correct set it to mode 3</span>
<a name="l00992"></a>00992 <span class="keywordflow">if</span> (oldMode != SPI_MODE3) {
<a name="l00993"></a>00993 SPI.setDataMode(oldMode);
<a name="l00994"></a>00994 }
<a name="l00995"></a>00995
<a name="l00996"></a>00996
<a name="l00997"></a>00997 <span class="comment">//store the datagram as status result</span>
<a name="l00998"></a>00998 driver_status_result = i_datagram;
<a name="l00999"></a>00999 }
</pre></div></div><!-- contents -->
<hr class="footer"/><address class="footer"><small>
Generated on Mon Nov 19 2012 20:26:21 for Trinamic TMC26X Stepper Driver for Arduino by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>
</body>
</html>