Slightly altered version of Arduino OneStep

This library is specific to the ST L6474 stepper driver.

Committer:
jonebuckman
Date:
Wed Sep 02 15:43:40 2015 +0000
Revision:
0:92e1b5622620
Slightly altered version of Arduino OneStep

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonebuckman 0:92e1b5622620 1 /**
jonebuckman 0:92e1b5622620 2 * @file OneStep.h
jonebuckman 0:92e1b5622620 3 *
jonebuckman 0:92e1b5622620 4 * @author Jon Buckman
jonebuckman 0:92e1b5622620 5 *
jonebuckman 0:92e1b5622620 6 * @section LICENSE
jonebuckman 0:92e1b5622620 7 *
jonebuckman 0:92e1b5622620 8 * Copyright (c) 2014 Jon Buckman
jonebuckman 0:92e1b5622620 9 *
jonebuckman 0:92e1b5622620 10 * Copyright (C) 2009-2013 Mike McCauley
jonebuckman 0:92e1b5622620 11 * $Id: AccelStepper.cpp,v 1.19 2014/10/31 06:05:27 mikem Exp mikem $
jonebuckman 0:92e1b5622620 12 *
jonebuckman 0:92e1b5622620 13 * This program is free software: you can redistribute it and/or modify
jonebuckman 0:92e1b5622620 14 * it under the terms of the GNU General Public License as published by
jonebuckman 0:92e1b5622620 15 * the Free Software Foundation, either version 3 of the License, or
jonebuckman 0:92e1b5622620 16 * (at your option) any later version.
jonebuckman 0:92e1b5622620 17 *
jonebuckman 0:92e1b5622620 18 * This program is distributed in the hope that it will be useful,
jonebuckman 0:92e1b5622620 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jonebuckman 0:92e1b5622620 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
jonebuckman 0:92e1b5622620 21 * GNU General Public License for more details.
jonebuckman 0:92e1b5622620 22 *
jonebuckman 0:92e1b5622620 23 * You should have received a copy of the GNU General Public License
jonebuckman 0:92e1b5622620 24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
jonebuckman 0:92e1b5622620 25 *
jonebuckman 0:92e1b5622620 26 * The above copyright notice and this permission notice shall be included in
jonebuckman 0:92e1b5622620 27 * all copies or substantial portions of the Software.
jonebuckman 0:92e1b5622620 28 *
jonebuckman 0:92e1b5622620 29 * @section DESCRIPTION
jonebuckman 0:92e1b5622620 30 *
jonebuckman 0:92e1b5622620 31 * OneStep stepper motor accelerate and manipulate.
jonebuckman 0:92e1b5622620 32 *
jonebuckman 0:92e1b5622620 33 * Datasheet:
jonebuckman 0:92e1b5622620 34 *
jonebuckman 0:92e1b5622620 35 *
jonebuckman 0:92e1b5622620 36 *
jonebuckman 0:92e1b5622620 37 * Example:
jonebuckman 0:92e1b5622620 38 * @code
jonebuckman 0:92e1b5622620 39 * #include "mbed.h"
jonebuckman 0:92e1b5622620 40 * #include "OneStep.h"
jonebuckman 0:92e1b5622620 41 *
jonebuckman 0:92e1b5622620 42 * Serial pc(USBTX, USBRX); // tx, rx
jonebuckman 0:92e1b5622620 43 *
jonebuckman 0:92e1b5622620 44 * OneStep stepper(D11, D12, D13, D10, D9, D7, D8, D2); // mosi, miso, sck, nCS_(cs), step, direction, reset, flag
jonebuckman 0:92e1b5622620 45 *
jonebuckman 0:92e1b5622620 46 * int main() {
jonebuckman 0:92e1b5622620 47 * while(1)
jonebuckman 0:92e1b5622620 48 * {
jonebuckman 0:92e1b5622620 49 * if (stepper.distanceToGo() == 0)
jonebuckman 0:92e1b5622620 50 * {
jonebuckman 0:92e1b5622620 51 * // Random change to speed, position and acceleration
jonebuckman 0:92e1b5622620 52 * // Make sure we dont get 0 speed or accelerations
jonebuckman 0:92e1b5622620 53 * wait_us(1000);
jonebuckman 0:92e1b5622620 54 * stepper.moveTo(rand() % 200);
jonebuckman 0:92e1b5622620 55 * stepper.setMaxSpeed((rand() % 200) + 1);
jonebuckman 0:92e1b5622620 56 * stepper.setAcceleration((rand() % 200) + 1);
jonebuckman 0:92e1b5622620 57 * if(stepper.getAlarm() == 0) {
jonebuckman 0:92e1b5622620 58 * pc.printf("alarm\r\n");
jonebuckman 0:92e1b5622620 59 * }
jonebuckman 0:92e1b5622620 60 * }
jonebuckman 0:92e1b5622620 61 * stepper.run();
jonebuckman 0:92e1b5622620 62 * }
jonebuckman 0:92e1b5622620 63 * }
jonebuckman 0:92e1b5622620 64 * @endcode
jonebuckman 0:92e1b5622620 65 */
jonebuckman 0:92e1b5622620 66
jonebuckman 0:92e1b5622620 67 #ifndef ONESTEP_H
jonebuckman 0:92e1b5622620 68 #define ONESTEP_H
jonebuckman 0:92e1b5622620 69
jonebuckman 0:92e1b5622620 70 /**
jonebuckman 0:92e1b5622620 71 * Includes
jonebuckman 0:92e1b5622620 72 */
jonebuckman 0:92e1b5622620 73 #include <mbed.h>
jonebuckman 0:92e1b5622620 74
jonebuckman 0:92e1b5622620 75 /**
jonebuckman 0:92e1b5622620 76 * Defines
jonebuckman 0:92e1b5622620 77 */
jonebuckman 0:92e1b5622620 78 #define min(a,b) ((a)<(b)?(a):(b))
jonebuckman 0:92e1b5622620 79 #define max(a,b) ((a)>(b)?(a):(b))
jonebuckman 0:92e1b5622620 80 #define abs(x) ((x)>0?(x):-(x))
jonebuckman 0:92e1b5622620 81 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
jonebuckman 0:92e1b5622620 82 #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
jonebuckman 0:92e1b5622620 83 #define radians(deg) ((deg)*DEG_TO_RAD)
jonebuckman 0:92e1b5622620 84 #define degrees(rad) ((rad)*RAD_TO_DEG)
jonebuckman 0:92e1b5622620 85 #define sq(x) ((x)*(x))
jonebuckman 0:92e1b5622620 86
jonebuckman 0:92e1b5622620 87 /**
jonebuckman 0:92e1b5622620 88 * @brief easySPIN Init structure definition
jonebuckman 0:92e1b5622620 89 */
jonebuckman 0:92e1b5622620 90 typedef struct {
jonebuckman 0:92e1b5622620 91 uint32_t ABS_POS;
jonebuckman 0:92e1b5622620 92 uint16_t EL_POS;
jonebuckman 0:92e1b5622620 93 uint32_t MARK;
jonebuckman 0:92e1b5622620 94 uint8_t TVAL;
jonebuckman 0:92e1b5622620 95 uint8_t T_FAST;
jonebuckman 0:92e1b5622620 96 uint8_t TON_MIN;
jonebuckman 0:92e1b5622620 97 uint8_t TOFF_MIN;
jonebuckman 0:92e1b5622620 98 uint8_t ADC_OUT;
jonebuckman 0:92e1b5622620 99 uint8_t OCD_TH;
jonebuckman 0:92e1b5622620 100 uint8_t STEP_MODE;
jonebuckman 0:92e1b5622620 101 uint8_t ALARM_EN;
jonebuckman 0:92e1b5622620 102 uint16_t CONFIG;
jonebuckman 0:92e1b5622620 103 } RegsStruct_TypeDef;
jonebuckman 0:92e1b5622620 104
jonebuckman 0:92e1b5622620 105 /* easySPIN TVAL register options */
jonebuckman 0:92e1b5622620 106 typedef enum {
jonebuckman 0:92e1b5622620 107 TVAL_31_25mA = ((uint8_t) 0x00),
jonebuckman 0:92e1b5622620 108 TVAL_62_5mA = ((uint8_t) 0x01),
jonebuckman 0:92e1b5622620 109 TVAL_93_75mA = ((uint8_t) 0x02),
jonebuckman 0:92e1b5622620 110 TVAL_125mA = ((uint8_t) 0x03),
jonebuckman 0:92e1b5622620 111 TVAL_156_25mA = ((uint8_t) 0x04),
jonebuckman 0:92e1b5622620 112 TVAL_187_5mA = ((uint8_t) 0x05),
jonebuckman 0:92e1b5622620 113 TVAL_218_75mA = ((uint8_t) 0x06),
jonebuckman 0:92e1b5622620 114 TVAL_250mA = ((uint8_t) 0x07),
jonebuckman 0:92e1b5622620 115 TVAL_281_25mA = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 116 TVAL_312_5mA = ((uint8_t) 0x09),
jonebuckman 0:92e1b5622620 117 TVAL_343_75mA = ((uint8_t) 0x0A),
jonebuckman 0:92e1b5622620 118 TVAL_375mA = ((uint8_t) 0x0B),
jonebuckman 0:92e1b5622620 119 TVAL_406_25mA = ((uint8_t) 0x0C),
jonebuckman 0:92e1b5622620 120 TVAL_437_5mA = ((uint8_t) 0x0D),
jonebuckman 0:92e1b5622620 121 TVAL_468_75mA = ((uint8_t) 0x0E),
jonebuckman 0:92e1b5622620 122 TVAL_500mA = ((uint8_t) 0x0F),
jonebuckman 0:92e1b5622620 123 TVAL_531_25mA = ((uint8_t) 0x10),
jonebuckman 0:92e1b5622620 124 TVAL_562_5mA = ((uint8_t) 0x11),
jonebuckman 0:92e1b5622620 125 TVAL_593_75mA = ((uint8_t) 0x12),
jonebuckman 0:92e1b5622620 126 TVAL_625mA = ((uint8_t) 0x13),
jonebuckman 0:92e1b5622620 127 TVAL_656_25mA = ((uint8_t) 0x14),
jonebuckman 0:92e1b5622620 128 TVAL_687_5mA = ((uint8_t) 0x15),
jonebuckman 0:92e1b5622620 129 TVAL_718_75mA = ((uint8_t) 0x16),
jonebuckman 0:92e1b5622620 130 TVAL_750mA = ((uint8_t) 0x17),
jonebuckman 0:92e1b5622620 131 TVAL_781_25mA = ((uint8_t) 0x18),
jonebuckman 0:92e1b5622620 132 TVAL_812_5mA = ((uint8_t) 0x19),
jonebuckman 0:92e1b5622620 133 TVAL_843_75mA = ((uint8_t) 0x1A),
jonebuckman 0:92e1b5622620 134 TVAL_875mA = ((uint8_t) 0x1B),
jonebuckman 0:92e1b5622620 135 TVAL_906_25mA = ((uint8_t) 0x1C),
jonebuckman 0:92e1b5622620 136 TVAL_937_5mA = ((uint8_t) 0x1D),
jonebuckman 0:92e1b5622620 137 TVAL_968_75mA = ((uint8_t) 0x1E),
jonebuckman 0:92e1b5622620 138 TVAL_1000mA = ((uint8_t) 0x1F),
jonebuckman 0:92e1b5622620 139 TVAL_1031_25mA = ((uint8_t) 0x20),
jonebuckman 0:92e1b5622620 140 TVAL_1062_5mA = ((uint8_t) 0x21),
jonebuckman 0:92e1b5622620 141 TVAL_1093_75mA = ((uint8_t) 0x22),
jonebuckman 0:92e1b5622620 142 TVAL_1125mA = ((uint8_t) 0x23),
jonebuckman 0:92e1b5622620 143 TVAL_1156_25mA = ((uint8_t) 0x24),
jonebuckman 0:92e1b5622620 144 TVAL_1187_5mA = ((uint8_t) 0x25),
jonebuckman 0:92e1b5622620 145 TVAL_1218_75mA = ((uint8_t) 0x26),
jonebuckman 0:92e1b5622620 146 TVAL_1250mA = ((uint8_t) 0x27),
jonebuckman 0:92e1b5622620 147 TVAL_1281_25mA = ((uint8_t) 0x28),
jonebuckman 0:92e1b5622620 148 TVAL_1312_5mA = ((uint8_t) 0x29),
jonebuckman 0:92e1b5622620 149 TVAL_1343_75mA = ((uint8_t) 0x2A),
jonebuckman 0:92e1b5622620 150 TVAL_1375mA = ((uint8_t) 0x2B),
jonebuckman 0:92e1b5622620 151 TVAL_1406_25mA = ((uint8_t) 0x2C),
jonebuckman 0:92e1b5622620 152 TVAL_1437_5mA = ((uint8_t) 0x2D),
jonebuckman 0:92e1b5622620 153 TVAL_1468_75mA = ((uint8_t) 0x2E),
jonebuckman 0:92e1b5622620 154 TVAL_1500mA = ((uint8_t) 0x2F),
jonebuckman 0:92e1b5622620 155 TVAL_1531_25mA = ((uint8_t) 0x30),
jonebuckman 0:92e1b5622620 156 TVAL_1562_5mA = ((uint8_t) 0x31),
jonebuckman 0:92e1b5622620 157 TVAL_1593_75mA = ((uint8_t) 0x32),
jonebuckman 0:92e1b5622620 158 TVAL_1625mA = ((uint8_t) 0x33),
jonebuckman 0:92e1b5622620 159 TVAL_1656_25mA = ((uint8_t) 0x34),
jonebuckman 0:92e1b5622620 160 TVAL_1687_5mA = ((uint8_t) 0x35),
jonebuckman 0:92e1b5622620 161 TVAL_1718_75mA = ((uint8_t) 0x36),
jonebuckman 0:92e1b5622620 162 TVAL_1750mA = ((uint8_t) 0x37),
jonebuckman 0:92e1b5622620 163 TVAL_1781_25mA = ((uint8_t) 0x38),
jonebuckman 0:92e1b5622620 164 TVAL_1812_5mA = ((uint8_t) 0x39),
jonebuckman 0:92e1b5622620 165 TVAL_1843_75mA = ((uint8_t) 0x3A),
jonebuckman 0:92e1b5622620 166 TVAL_1875mA = ((uint8_t) 0x3B),
jonebuckman 0:92e1b5622620 167 TVAL_1906_25mA = ((uint8_t) 0x3C),
jonebuckman 0:92e1b5622620 168 TVAL_1937_5mA = ((uint8_t) 0x3D),
jonebuckman 0:92e1b5622620 169 TVAL_1968_75mA = ((uint8_t) 0x3E),
jonebuckman 0:92e1b5622620 170 TVAL_2000mA = ((uint8_t) 0x3F),
jonebuckman 0:92e1b5622620 171 TVAL_2031_25mA = ((uint8_t) 0x40),
jonebuckman 0:92e1b5622620 172 TVAL_2062_5mA = ((uint8_t) 0x41),
jonebuckman 0:92e1b5622620 173 TVAL_2093_75mA = ((uint8_t) 0x42),
jonebuckman 0:92e1b5622620 174 TVAL_2125mA = ((uint8_t) 0x43),
jonebuckman 0:92e1b5622620 175 TVAL_2156_25mA = ((uint8_t) 0x44),
jonebuckman 0:92e1b5622620 176 TVAL_2187_5mA = ((uint8_t) 0x45),
jonebuckman 0:92e1b5622620 177 TVAL_2218_75mA = ((uint8_t) 0x46),
jonebuckman 0:92e1b5622620 178 TVAL_2250mA = ((uint8_t) 0x47),
jonebuckman 0:92e1b5622620 179 TVAL_2281_25mA = ((uint8_t) 0x48),
jonebuckman 0:92e1b5622620 180 TVAL_2312_5mA = ((uint8_t) 0x49),
jonebuckman 0:92e1b5622620 181 TVAL_2343_75mA = ((uint8_t) 0x4A),
jonebuckman 0:92e1b5622620 182 TVAL_2375mA = ((uint8_t) 0x4B),
jonebuckman 0:92e1b5622620 183 TVAL_2406_25mA = ((uint8_t) 0x4C),
jonebuckman 0:92e1b5622620 184 TVAL_2437_5mA = ((uint8_t) 0x4D),
jonebuckman 0:92e1b5622620 185 TVAL_2468_75mA = ((uint8_t) 0x4E),
jonebuckman 0:92e1b5622620 186 TVAL_2500mA = ((uint8_t) 0x4F),
jonebuckman 0:92e1b5622620 187 TVAL_2531_25mA = ((uint8_t) 0x50),
jonebuckman 0:92e1b5622620 188 TVAL_2562_5mA = ((uint8_t) 0x51),
jonebuckman 0:92e1b5622620 189 TVAL_2593_75mA = ((uint8_t) 0x52),
jonebuckman 0:92e1b5622620 190 TVAL_2625mA = ((uint8_t) 0x53),
jonebuckman 0:92e1b5622620 191 TVAL_2656_25mA = ((uint8_t) 0x54),
jonebuckman 0:92e1b5622620 192 TVAL_2687_5mA = ((uint8_t) 0x55),
jonebuckman 0:92e1b5622620 193 TVAL_2718_75mA = ((uint8_t) 0x56),
jonebuckman 0:92e1b5622620 194 TVAL_2750mA = ((uint8_t) 0x57),
jonebuckman 0:92e1b5622620 195 TVAL_2781_25mA = ((uint8_t) 0x58),
jonebuckman 0:92e1b5622620 196 TVAL_2812_5mA = ((uint8_t) 0x59),
jonebuckman 0:92e1b5622620 197 TVAL_2843_75mA = ((uint8_t) 0x5A),
jonebuckman 0:92e1b5622620 198 TVAL_2875mA = ((uint8_t) 0x5B),
jonebuckman 0:92e1b5622620 199 TVAL_2906_25mA = ((uint8_t) 0x5C),
jonebuckman 0:92e1b5622620 200 TVAL_2937_5mA = ((uint8_t) 0x5D),
jonebuckman 0:92e1b5622620 201 TVAL_2968_75mA = ((uint8_t) 0x5E),
jonebuckman 0:92e1b5622620 202 TVAL_3000mA = ((uint8_t) 0x5F),
jonebuckman 0:92e1b5622620 203 TVAL_3031_25mA = ((uint8_t) 0x60),
jonebuckman 0:92e1b5622620 204 TVAL_3062_5mA = ((uint8_t) 0x61),
jonebuckman 0:92e1b5622620 205 TVAL_3093_75mA = ((uint8_t) 0x62),
jonebuckman 0:92e1b5622620 206 TVAL_3125mA = ((uint8_t) 0x63),
jonebuckman 0:92e1b5622620 207 TVAL_3156_25mA = ((uint8_t) 0x64),
jonebuckman 0:92e1b5622620 208 TVAL_3187_5mA = ((uint8_t) 0x65),
jonebuckman 0:92e1b5622620 209 TVAL_3218_75mA = ((uint8_t) 0x66),
jonebuckman 0:92e1b5622620 210 TVAL_3250mA = ((uint8_t) 0x67),
jonebuckman 0:92e1b5622620 211 TVAL_3281_25mA = ((uint8_t) 0x68),
jonebuckman 0:92e1b5622620 212 TVAL_3312_5mA = ((uint8_t) 0x69),
jonebuckman 0:92e1b5622620 213 TVAL_3343_75mA = ((uint8_t) 0x6A),
jonebuckman 0:92e1b5622620 214 TVAL_3375mA = ((uint8_t) 0x6B),
jonebuckman 0:92e1b5622620 215 TVAL_3406_25mA = ((uint8_t) 0x6C),
jonebuckman 0:92e1b5622620 216 TVAL_3437_5mA = ((uint8_t) 0x6D),
jonebuckman 0:92e1b5622620 217 TVAL_3468_75mA = ((uint8_t) 0x6E),
jonebuckman 0:92e1b5622620 218 TVAL_3500mA = ((uint8_t) 0x6F),
jonebuckman 0:92e1b5622620 219 TVAL_3531_25mA = ((uint8_t) 0x70),
jonebuckman 0:92e1b5622620 220 TVAL_3562_5mA = ((uint8_t) 0x71),
jonebuckman 0:92e1b5622620 221 TVAL_3593_75mA = ((uint8_t) 0x72),
jonebuckman 0:92e1b5622620 222 TVAL_3625mA = ((uint8_t) 0x73),
jonebuckman 0:92e1b5622620 223 TVAL_3656_25mA = ((uint8_t) 0x74),
jonebuckman 0:92e1b5622620 224 TVAL_3687_5mA = ((uint8_t) 0x75),
jonebuckman 0:92e1b5622620 225 TVAL_3718_75mA = ((uint8_t) 0x76),
jonebuckman 0:92e1b5622620 226 TVAL_3750mA = ((uint8_t) 0x77),
jonebuckman 0:92e1b5622620 227 TVAL_3781_25mA = ((uint8_t) 0x78),
jonebuckman 0:92e1b5622620 228 TVAL_3812_5mA = ((uint8_t) 0x79),
jonebuckman 0:92e1b5622620 229 TVAL_3843_75mA = ((uint8_t) 0x7A),
jonebuckman 0:92e1b5622620 230 TVAL_3875mA = ((uint8_t) 0x7B),
jonebuckman 0:92e1b5622620 231 TVAL_3906_25mA = ((uint8_t) 0x7C),
jonebuckman 0:92e1b5622620 232 TVAL_3937_5mA = ((uint8_t) 0x7D),
jonebuckman 0:92e1b5622620 233 TVAL_3968_75mA = ((uint8_t) 0x7E),
jonebuckman 0:92e1b5622620 234 TVAL_4000mA = ((uint8_t) 0x7F)
jonebuckman 0:92e1b5622620 235 } TVAL_TypeDef;
jonebuckman 0:92e1b5622620 236
jonebuckman 0:92e1b5622620 237 /* easySPIN T_FAST register options */
jonebuckman 0:92e1b5622620 238 typedef enum {
jonebuckman 0:92e1b5622620 239 TOFF_FAST_0_5_us = ((uint8_t) 0x00 << 4),
jonebuckman 0:92e1b5622620 240 TOFF_FAST_1_0_us = ((uint8_t) 0x01 << 4),
jonebuckman 0:92e1b5622620 241 TOFF_FAST_1_5_us = ((uint8_t) 0x02 << 4),
jonebuckman 0:92e1b5622620 242 TOFF_FAST_2_0_us = ((uint8_t) 0x03 << 4),
jonebuckman 0:92e1b5622620 243 TOFF_FAST_2_5_us = ((uint8_t) 0x04 << 4),
jonebuckman 0:92e1b5622620 244 TOFF_FAST_3_0_us = ((uint8_t) 0x05 << 4),
jonebuckman 0:92e1b5622620 245 TOFF_FAST_3_5_us = ((uint8_t) 0x06 << 4),
jonebuckman 0:92e1b5622620 246 TOFF_FAST_4_0_us = ((uint8_t) 0x07 << 4),
jonebuckman 0:92e1b5622620 247 TOFF_FAST_4_5_us = ((uint8_t) 0x08 << 4),
jonebuckman 0:92e1b5622620 248 TOFF_FAST_5_0_us = ((uint8_t) 0x09 << 4),
jonebuckman 0:92e1b5622620 249 TOFF_FAST_5_5_us = ((uint8_t) 0x0A << 4),
jonebuckman 0:92e1b5622620 250 TOFF_FAST_6_0_us = ((uint8_t) 0x0B << 4),
jonebuckman 0:92e1b5622620 251 TOFF_FAST_6_5_us = ((uint8_t) 0x0C << 4),
jonebuckman 0:92e1b5622620 252 TOFF_FAST_7_0_us = ((uint8_t) 0x0D << 4),
jonebuckman 0:92e1b5622620 253 TOFF_FAST_7_5_us = ((uint8_t) 0x0E << 4),
jonebuckman 0:92e1b5622620 254 TOFF_FAST_8_0_us = ((uint8_t) 0x0F << 4)
jonebuckman 0:92e1b5622620 255 } TOFF_FAST_TypeDef;
jonebuckman 0:92e1b5622620 256
jonebuckman 0:92e1b5622620 257 typedef enum {
jonebuckman 0:92e1b5622620 258 FAST_STEP_0_5_us = ((uint8_t) 0x00),
jonebuckman 0:92e1b5622620 259 FAST_STEP_1_0_us = ((uint8_t) 0x01),
jonebuckman 0:92e1b5622620 260 FAST_STEP_1_5_us = ((uint8_t) 0x02),
jonebuckman 0:92e1b5622620 261 FAST_STEP_2_0_us = ((uint8_t) 0x03),
jonebuckman 0:92e1b5622620 262 FAST_STEP_2_5_us = ((uint8_t) 0x04),
jonebuckman 0:92e1b5622620 263 FAST_STEP_3_0_us = ((uint8_t) 0x05),
jonebuckman 0:92e1b5622620 264 FAST_STEP_3_5_us = ((uint8_t) 0x06),
jonebuckman 0:92e1b5622620 265 FAST_STEP_4_0_us = ((uint8_t) 0x07),
jonebuckman 0:92e1b5622620 266 FAST_STEP_4_5_us = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 267 FAST_STEP_5_0_us = ((uint8_t) 0x09),
jonebuckman 0:92e1b5622620 268 FAST_STEP_5_5_us = ((uint8_t) 0x0A),
jonebuckman 0:92e1b5622620 269 FAST_STEP_6_0_us = ((uint8_t) 0x0B),
jonebuckman 0:92e1b5622620 270 FAST_STEP_6_5_us = ((uint8_t) 0x0C),
jonebuckman 0:92e1b5622620 271 FAST_STEP_7_0_us = ((uint8_t) 0x0D),
jonebuckman 0:92e1b5622620 272 FAST_STEP_7_5_us = ((uint8_t) 0x0E),
jonebuckman 0:92e1b5622620 273 FAST_STEP_8_0_us = ((uint8_t) 0x0F)
jonebuckman 0:92e1b5622620 274 } FAST_STEP_TypeDef;
jonebuckman 0:92e1b5622620 275
jonebuckman 0:92e1b5622620 276 /* easySPIN overcurrent threshold options */
jonebuckman 0:92e1b5622620 277 typedef enum {
jonebuckman 0:92e1b5622620 278 OCD_TH_375mA = ((uint8_t) 0x00),
jonebuckman 0:92e1b5622620 279 OCD_TH_750mA = ((uint8_t) 0x01),
jonebuckman 0:92e1b5622620 280 OCD_TH_1125mA = ((uint8_t) 0x02),
jonebuckman 0:92e1b5622620 281 OCD_TH_1500mA = ((uint8_t) 0x03),
jonebuckman 0:92e1b5622620 282 OCD_TH_1875mA = ((uint8_t) 0x04),
jonebuckman 0:92e1b5622620 283 OCD_TH_2250mA = ((uint8_t) 0x05),
jonebuckman 0:92e1b5622620 284 OCD_TH_2625mA = ((uint8_t) 0x06),
jonebuckman 0:92e1b5622620 285 OCD_TH_3000mA = ((uint8_t) 0x07),
jonebuckman 0:92e1b5622620 286 OCD_TH_3375mA = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 287 OCD_TH_3750mA = ((uint8_t) 0x09),
jonebuckman 0:92e1b5622620 288 OCD_TH_4125mA = ((uint8_t) 0x0A),
jonebuckman 0:92e1b5622620 289 OCD_TH_4500mA = ((uint8_t) 0x0B),
jonebuckman 0:92e1b5622620 290 OCD_TH_4875mA = ((uint8_t) 0x0C),
jonebuckman 0:92e1b5622620 291 OCD_TH_5250mA = ((uint8_t) 0x0D),
jonebuckman 0:92e1b5622620 292 OCD_TH_5625mA = ((uint8_t) 0x0E),
jonebuckman 0:92e1b5622620 293 OCD_TH_6000mA = ((uint8_t) 0x0F)
jonebuckman 0:92e1b5622620 294 } OCD_TH_TypeDef;
jonebuckman 0:92e1b5622620 295
jonebuckman 0:92e1b5622620 296 /* easySPIN STEP_MODE register masks */
jonebuckman 0:92e1b5622620 297 typedef enum {
jonebuckman 0:92e1b5622620 298 STEP_MODE_STEP_SEL = ((uint8_t) 0x07),
jonebuckman 0:92e1b5622620 299 STEP_MODE_SYNC_SEL = ((uint8_t) 0x70)
jonebuckman 0:92e1b5622620 300 } STEP_MODE_Masks_TypeDef;
jonebuckman 0:92e1b5622620 301
jonebuckman 0:92e1b5622620 302 /* easySPIN STEP_MODE register options */
jonebuckman 0:92e1b5622620 303 /* easySPIN STEP_SEL options */
jonebuckman 0:92e1b5622620 304 typedef enum {
jonebuckman 0:92e1b5622620 305 STEP_SEL_1 = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 306 STEP_SEL_1_2 = ((uint8_t) 0x09),
jonebuckman 0:92e1b5622620 307 STEP_SEL_1_4 = ((uint8_t) 0x0A),
jonebuckman 0:92e1b5622620 308 STEP_SEL_1_8 = ((uint8_t) 0x0B),
jonebuckman 0:92e1b5622620 309 STEP_SEL_1_16 = ((uint8_t) 0x0C)
jonebuckman 0:92e1b5622620 310 } STEP_SEL_TypeDef;
jonebuckman 0:92e1b5622620 311
jonebuckman 0:92e1b5622620 312 /* easySPIN SYNC_SEL options */
jonebuckman 0:92e1b5622620 313 typedef enum {
jonebuckman 0:92e1b5622620 314 SYNC_SEL_1_2 = ((uint8_t) 0x80),
jonebuckman 0:92e1b5622620 315 SYNC_SEL_1 = ((uint8_t) 0x90),
jonebuckman 0:92e1b5622620 316 SYNC_SEL_2 = ((uint8_t) 0xA0),
jonebuckman 0:92e1b5622620 317 SYNC_SEL_4 = ((uint8_t) 0xB0),
jonebuckman 0:92e1b5622620 318 SYNC_SEL_8 = ((uint8_t) 0xC0),
jonebuckman 0:92e1b5622620 319 SYNC_SEL_UNUSED = ((uint8_t) 0xD0)
jonebuckman 0:92e1b5622620 320 } SYNC_SEL_TypeDef;
jonebuckman 0:92e1b5622620 321
jonebuckman 0:92e1b5622620 322 /* easySPIN ALARM_EN register options */
jonebuckman 0:92e1b5622620 323 typedef enum {
jonebuckman 0:92e1b5622620 324 ALARM_EN_OVERCURRENT = ((uint8_t) 0x01),
jonebuckman 0:92e1b5622620 325 ALARM_EN_THERMAL_SHUTDOWN = ((uint8_t) 0x02),
jonebuckman 0:92e1b5622620 326 ALARM_EN_THERMAL_WARNING = ((uint8_t) 0x04),
jonebuckman 0:92e1b5622620 327 ALARM_EN_UNDERVOLTAGE = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 328 ALARM_EN_SW_TURN_ON = ((uint8_t) 0x40),
jonebuckman 0:92e1b5622620 329 ALARM_EN_WRONG_NPERF_CMD = ((uint8_t) 0x80)
jonebuckman 0:92e1b5622620 330 } ALARM_EN_TypeDef;
jonebuckman 0:92e1b5622620 331
jonebuckman 0:92e1b5622620 332 /* easySPIN Config register masks */
jonebuckman 0:92e1b5622620 333 typedef enum {
jonebuckman 0:92e1b5622620 334 CONFIG_OSC_SEL = ((uint16_t) 0x0007),
jonebuckman 0:92e1b5622620 335 CONFIG_EXT_CLK = ((uint16_t) 0x0008),
jonebuckman 0:92e1b5622620 336 CONFIG_EN_TQREG = ((uint16_t) 0x0020),
jonebuckman 0:92e1b5622620 337 CONFIG_OC_SD = ((uint16_t) 0x0080),
jonebuckman 0:92e1b5622620 338 CONFIG_POW_SR = ((uint16_t) 0x0300),
jonebuckman 0:92e1b5622620 339 CONFIG_TSW = ((uint16_t) 0x7C00)
jonebuckman 0:92e1b5622620 340 } CONFIG_Masks_TypeDef;
jonebuckman 0:92e1b5622620 341
jonebuckman 0:92e1b5622620 342 /* easySPIN Config register options */
jonebuckman 0:92e1b5622620 343 typedef enum {
jonebuckman 0:92e1b5622620 344 CONFIG_INT_16MHZ = ((uint16_t) 0x0000),
jonebuckman 0:92e1b5622620 345 CONFIG_INT_16MHZ_OSCOUT_2MHZ = ((uint16_t) 0x0008),
jonebuckman 0:92e1b5622620 346 CONFIG_INT_16MHZ_OSCOUT_4MHZ = ((uint16_t) 0x0009),
jonebuckman 0:92e1b5622620 347 CONFIG_INT_16MHZ_OSCOUT_8MHZ = ((uint16_t) 0x000A),
jonebuckman 0:92e1b5622620 348 CONFIG_INT_16MHZ_OSCOUT_16MHZ = ((uint16_t) 0x000B),
jonebuckman 0:92e1b5622620 349 CONFIG_EXT_8MHZ_XTAL_DRIVE = ((uint16_t) 0x0004),
jonebuckman 0:92e1b5622620 350 CONFIG_EXT_16MHZ_XTAL_DRIVE = ((uint16_t) 0x0005),
jonebuckman 0:92e1b5622620 351 CONFIG_EXT_24MHZ_XTAL_DRIVE = ((uint16_t) 0x0006),
jonebuckman 0:92e1b5622620 352 CONFIG_EXT_32MHZ_XTAL_DRIVE = ((uint16_t) 0x0007),
jonebuckman 0:92e1b5622620 353 CONFIG_EXT_8MHZ_OSCOUT_INVERT = ((uint16_t) 0x000C),
jonebuckman 0:92e1b5622620 354 CONFIG_EXT_16MHZ_OSCOUT_INVERT = ((uint16_t) 0x000D),
jonebuckman 0:92e1b5622620 355 CONFIG_EXT_24MHZ_OSCOUT_INVERT = ((uint16_t) 0x000E),
jonebuckman 0:92e1b5622620 356 CONFIG_EXT_32MHZ_OSCOUT_INVERT = ((uint16_t) 0x000F)
jonebuckman 0:92e1b5622620 357 } CONFIG_OSC_MGMT_TypeDef;
jonebuckman 0:92e1b5622620 358
jonebuckman 0:92e1b5622620 359 typedef enum {
jonebuckman 0:92e1b5622620 360 CONFIG_EN_TQREG_INT_REG = ((uint16_t) 0x0000),
jonebuckman 0:92e1b5622620 361 CONFIG_EN_TQREG_ADC_IN = ((uint16_t) 0x0020)
jonebuckman 0:92e1b5622620 362 } CONFIG_EN_TQREG_TypeDef;
jonebuckman 0:92e1b5622620 363
jonebuckman 0:92e1b5622620 364 typedef enum {
jonebuckman 0:92e1b5622620 365 CONFIG_OC_SD_DISABLE = ((uint16_t) 0x0000),
jonebuckman 0:92e1b5622620 366 CONFIG_OC_SD_ENABLE = ((uint16_t) 0x0080)
jonebuckman 0:92e1b5622620 367 } CONFIG_OC_SD_TypeDef;
jonebuckman 0:92e1b5622620 368
jonebuckman 0:92e1b5622620 369 typedef enum {
jonebuckman 0:92e1b5622620 370 CONFIG_SR_180V_us = ((uint16_t) 0x0000),
jonebuckman 0:92e1b5622620 371 CONFIG_SR_290V_us = ((uint16_t) 0x0200),
jonebuckman 0:92e1b5622620 372 CONFIG_SR_530V_us = ((uint16_t) 0x0300)
jonebuckman 0:92e1b5622620 373 } CONFIG_POW_SR_TypeDef;
jonebuckman 0:92e1b5622620 374
jonebuckman 0:92e1b5622620 375 typedef enum {
jonebuckman 0:92e1b5622620 376 CONFIG_TSW_4_us = (((uint16_t) 0x01) << 10),
jonebuckman 0:92e1b5622620 377 CONFIG_TSW_8_us = (((uint16_t) 0x02) << 10),
jonebuckman 0:92e1b5622620 378 CONFIG_TSW_12_us = (((uint16_t) 0x03) << 10),
jonebuckman 0:92e1b5622620 379 CONFIG_TSW_16_us = (((uint16_t) 0x04) << 10),
jonebuckman 0:92e1b5622620 380 CONFIG_TSW_20_us = (((uint16_t) 0x05) << 10),
jonebuckman 0:92e1b5622620 381 CONFIG_TSW_24_us = (((uint16_t) 0x06) << 10),
jonebuckman 0:92e1b5622620 382 CONFIG_TSW_28_us = (((uint16_t) 0x07) << 10),
jonebuckman 0:92e1b5622620 383 CONFIG_TSW_32_us = (((uint16_t) 0x08) << 10),
jonebuckman 0:92e1b5622620 384 CONFIG_TSW_36_us = (((uint16_t) 0x09) << 10),
jonebuckman 0:92e1b5622620 385 CONFIG_TSW_40_us = (((uint16_t) 0x0A) << 10),
jonebuckman 0:92e1b5622620 386 CONFIG_TSW_44_us = (((uint16_t) 0x0B) << 10),
jonebuckman 0:92e1b5622620 387 CONFIG_TSW_48_us = (((uint16_t) 0x0C) << 10),
jonebuckman 0:92e1b5622620 388 CONFIG_TSW_52_us = (((uint16_t) 0x0D) << 10),
jonebuckman 0:92e1b5622620 389 CONFIG_TSW_56_us = (((uint16_t) 0x0E) << 10),
jonebuckman 0:92e1b5622620 390 CONFIG_TSW_60_us = (((uint16_t) 0x0F) << 10),
jonebuckman 0:92e1b5622620 391 CONFIG_TSW_64_us = (((uint16_t) 0x10) << 10),
jonebuckman 0:92e1b5622620 392 CONFIG_TSW_68_us = (((uint16_t) 0x11) << 10),
jonebuckman 0:92e1b5622620 393 CONFIG_TSW_72_us = (((uint16_t) 0x12) << 10),
jonebuckman 0:92e1b5622620 394 CONFIG_TSW_76_us = (((uint16_t) 0x13) << 10),
jonebuckman 0:92e1b5622620 395 CONFIG_TSW_80_us = (((uint16_t) 0x14) << 10),
jonebuckman 0:92e1b5622620 396 CONFIG_TSW_84_us = (((uint16_t) 0x15) << 10),
jonebuckman 0:92e1b5622620 397 CONFIG_TSW_88_us = (((uint16_t) 0x16) << 10),
jonebuckman 0:92e1b5622620 398 CONFIG_TSW_92_us = (((uint16_t) 0x17) << 10),
jonebuckman 0:92e1b5622620 399 CONFIG_TSW_96_us = (((uint16_t) 0x18) << 10),
jonebuckman 0:92e1b5622620 400 CONFIG_TSW_100_us = (((uint16_t) 0x19) << 10),
jonebuckman 0:92e1b5622620 401 CONFIG_TSW_104_us = (((uint16_t) 0x1A) << 10),
jonebuckman 0:92e1b5622620 402 CONFIG_TSW_108_us = (((uint16_t) 0x1B) << 10),
jonebuckman 0:92e1b5622620 403 CONFIG_TSW_112_us = (((uint16_t) 0x1C) << 10),
jonebuckman 0:92e1b5622620 404 CONFIG_TSW_116_us = (((uint16_t) 0x1D) << 10),
jonebuckman 0:92e1b5622620 405 CONFIG_TSW_120_us = (((uint16_t) 0x1E) << 10),
jonebuckman 0:92e1b5622620 406 CONFIG_TSW_124_us = (((uint16_t) 0x1F) << 10)
jonebuckman 0:92e1b5622620 407 } CONFIG_TSW_TypeDef;
jonebuckman 0:92e1b5622620 408
jonebuckman 0:92e1b5622620 409 /* Status Register bit masks */
jonebuckman 0:92e1b5622620 410 typedef enum {
jonebuckman 0:92e1b5622620 411 STATUS_HIZ = (((uint16_t) 0x0001)),
jonebuckman 0:92e1b5622620 412 STATUS_DIR = (((uint16_t) 0x0010)),
jonebuckman 0:92e1b5622620 413 STATUS_NOTPERF_CMD = (((uint16_t) 0x0080)),
jonebuckman 0:92e1b5622620 414 STATUS_WRONG_CMD = (((uint16_t) 0x0100)),
jonebuckman 0:92e1b5622620 415 STATUS_UVLO = (((uint16_t) 0x0200)),
jonebuckman 0:92e1b5622620 416 STATUS_TH_WRN = (((uint16_t) 0x0400)),
jonebuckman 0:92e1b5622620 417 STATUS_TH_SD = (((uint16_t) 0x0800)),
jonebuckman 0:92e1b5622620 418 STATUS_OCD = (((uint16_t) 0x1000))
jonebuckman 0:92e1b5622620 419 } STATUS_Masks_TypeDef;
jonebuckman 0:92e1b5622620 420
jonebuckman 0:92e1b5622620 421 /* Status Register options */
jonebuckman 0:92e1b5622620 422 typedef enum {
jonebuckman 0:92e1b5622620 423 STATUS_DIR_FORWARD = (((uint16_t) 0x0001) << 4),
jonebuckman 0:92e1b5622620 424 STATUS_DIR_REVERSE = (((uint16_t) 0x0000) << 4)
jonebuckman 0:92e1b5622620 425 } STATUS_DIR_TypeDef;
jonebuckman 0:92e1b5622620 426
jonebuckman 0:92e1b5622620 427 /* easySPIN internal register addresses */
jonebuckman 0:92e1b5622620 428 typedef enum {
jonebuckman 0:92e1b5622620 429 ABS_POS = ((uint8_t) 0x01),
jonebuckman 0:92e1b5622620 430 EL_POS = ((uint8_t) 0x02),
jonebuckman 0:92e1b5622620 431 MARK = ((uint8_t) 0x03),
jonebuckman 0:92e1b5622620 432 RESERVED_REG01 = ((uint8_t) 0x04),
jonebuckman 0:92e1b5622620 433 RESERVED_REG02 = ((uint8_t) 0x05),
jonebuckman 0:92e1b5622620 434 RESERVED_REG03 = ((uint8_t) 0x06),
jonebuckman 0:92e1b5622620 435 RESERVED_REG04 = ((uint8_t) 0x07),
jonebuckman 0:92e1b5622620 436 RESERVED_REG05 = ((uint8_t) 0x08),
jonebuckman 0:92e1b5622620 437 RESERVED_REG06 = ((uint8_t) 0x15),
jonebuckman 0:92e1b5622620 438 TVAL = ((uint8_t) 0x09),
jonebuckman 0:92e1b5622620 439 RESERVED_REG07 = ((uint8_t) 0x0A),
jonebuckman 0:92e1b5622620 440 RESERVED_REG08 = ((uint8_t) 0x0B),
jonebuckman 0:92e1b5622620 441 RESERVED_REG09 = ((uint8_t) 0x0C),
jonebuckman 0:92e1b5622620 442 RESERVED_REG10 = ((uint8_t) 0x0D),
jonebuckman 0:92e1b5622620 443 T_FAST = ((uint8_t) 0x0E),
jonebuckman 0:92e1b5622620 444 TON_MIN = ((uint8_t) 0x0F),
jonebuckman 0:92e1b5622620 445 TOFF_MIN = ((uint8_t) 0x10),
jonebuckman 0:92e1b5622620 446 RESERVED_REG11 = ((uint8_t) 0x11),
jonebuckman 0:92e1b5622620 447 ADC_OUT = ((uint8_t) 0x12),
jonebuckman 0:92e1b5622620 448 OCD_TH = ((uint8_t) 0x13),
jonebuckman 0:92e1b5622620 449 RESERVED_REG12 = ((uint8_t) 0x14),
jonebuckman 0:92e1b5622620 450 STEP_MODE = ((uint8_t) 0x16),
jonebuckman 0:92e1b5622620 451 ALARM_EN = ((uint8_t) 0x17),
jonebuckman 0:92e1b5622620 452 CONFIG = ((uint8_t) 0x18),
jonebuckman 0:92e1b5622620 453 STATUS = ((uint8_t) 0x19),
jonebuckman 0:92e1b5622620 454 RESERVED_REG13 = ((uint8_t) 0x1A),
jonebuckman 0:92e1b5622620 455 RESERVED_REG14 = ((uint8_t) 0x1B)
jonebuckman 0:92e1b5622620 456 } Registers_TypeDef;
jonebuckman 0:92e1b5622620 457
jonebuckman 0:92e1b5622620 458 /* easySPIN command set */
jonebuckman 0:92e1b5622620 459 typedef enum {
jonebuckman 0:92e1b5622620 460 NOP = ((uint8_t) 0x00),
jonebuckman 0:92e1b5622620 461 SET_PARAM = ((uint8_t) 0x00),
jonebuckman 0:92e1b5622620 462 GET_PARAM = ((uint8_t) 0x20),
jonebuckman 0:92e1b5622620 463 ENBL = ((uint8_t) 0xB8),
jonebuckman 0:92e1b5622620 464 DSBL = ((uint8_t) 0xA8),
jonebuckman 0:92e1b5622620 465 GET_STATUS = ((uint8_t) 0xD0),
jonebuckman 0:92e1b5622620 466 RESERVED_CMD1 = ((uint8_t) 0xEB),
jonebuckman 0:92e1b5622620 467 RESERVED_CMD2 = ((uint8_t) 0xF8)
jonebuckman 0:92e1b5622620 468 } Commands_TypeDef;
jonebuckman 0:92e1b5622620 469
jonebuckman 0:92e1b5622620 470 /* easySPIN movement direction options */
jonebuckman 0:92e1b5622620 471 typedef enum {
jonebuckman 0:92e1b5622620 472 DIR_Forward = ((uint8_t) 0x01), DIR_Reverse = ((uint8_t) 0x00)
jonebuckman 0:92e1b5622620 473 } Direction_TypeDef;
jonebuckman 0:92e1b5622620 474
jonebuckman 0:92e1b5622620 475 /* easySPIN action options */
jonebuckman 0:92e1b5622620 476 typedef enum {
jonebuckman 0:92e1b5622620 477 ACTION_RESET = ((uint8_t) 0x00), ACTION_COPY = ((uint8_t) 0x01)
jonebuckman 0:92e1b5622620 478 } Action_TypeDef;
jonebuckman 0:92e1b5622620 479
jonebuckman 0:92e1b5622620 480 /**
jonebuckman 0:92e1b5622620 481 * OneStep stepper motor accelerate and manipulate.
jonebuckman 0:92e1b5622620 482 */
jonebuckman 0:92e1b5622620 483 class OneStep {
jonebuckman 0:92e1b5622620 484
jonebuckman 0:92e1b5622620 485 public:
jonebuckman 0:92e1b5622620 486
jonebuckman 0:92e1b5622620 487 /**
jonebuckman 0:92e1b5622620 488 * Constructor.
jonebuckman 0:92e1b5622620 489 *
jonebuckman 0:92e1b5622620 490 * @param step mbed pin to use for stepper motor interface.
jonebuckman 0:92e1b5622620 491 * @param direction mbed pin to use for stepper motor interface.
jonebuckman 0:92e1b5622620 492 * @param enable mbed pin to use for stepper motor interface.
jonebuckman 0:92e1b5622620 493 */
jonebuckman 0:92e1b5622620 494 OneStep(PinName mosi,
jonebuckman 0:92e1b5622620 495 PinName miso,
jonebuckman 0:92e1b5622620 496 PinName sck,
jonebuckman 0:92e1b5622620 497 PinName csn,
jonebuckman 0:92e1b5622620 498 PinName step,
jonebuckman 0:92e1b5622620 499 PinName dir,
jonebuckman 0:92e1b5622620 500 PinName reset,
jonebuckman 0:92e1b5622620 501 PinName flag);
jonebuckman 0:92e1b5622620 502
jonebuckman 0:92e1b5622620 503 /**
jonebuckman 0:92e1b5622620 504 * No operation.
jonebuckman 0:92e1b5622620 505 *
jonebuckman 0:92e1b5622620 506 */
jonebuckman 0:92e1b5622620 507 void nop(void);
jonebuckman 0:92e1b5622620 508
jonebuckman 0:92e1b5622620 509 /**
jonebuckman 0:92e1b5622620 510 * Enable driver
jonebuckman 0:92e1b5622620 511 *
jonebuckman 0:92e1b5622620 512 */
jonebuckman 0:92e1b5622620 513 void enable(void);
jonebuckman 0:92e1b5622620 514
jonebuckman 0:92e1b5622620 515 /**
jonebuckman 0:92e1b5622620 516 * Disable driver
jonebuckman 0:92e1b5622620 517 *
jonebuckman 0:92e1b5622620 518 */
jonebuckman 0:92e1b5622620 519 void disable(void);
jonebuckman 0:92e1b5622620 520
jonebuckman 0:92e1b5622620 521 /**
jonebuckman 0:92e1b5622620 522 * Move to a position.
jonebuckman 0:92e1b5622620 523 *
jonebuckman 0:92e1b5622620 524 * @param parameter Parameter.
jonebuckman 0:92e1b5622620 525 * @param length The length of the parameter.
jonebuckman 0:92e1b5622620 526 * @param value The value of the parameter.
jonebuckman 0:92e1b5622620 527 */
jonebuckman 0:92e1b5622620 528 void set_param(char parameter, int length, int value);
jonebuckman 0:92e1b5622620 529
jonebuckman 0:92e1b5622620 530 /**
jonebuckman 0:92e1b5622620 531 * Get parameter value.
jonebuckman 0:92e1b5622620 532 *
jonebuckman 0:92e1b5622620 533 * @param parameter Parameter.
jonebuckman 0:92e1b5622620 534 * @param length The length of the parameter.
jonebuckman 0:92e1b5622620 535 * @return Returns parameter.
jonebuckman 0:92e1b5622620 536 */
jonebuckman 0:92e1b5622620 537 int get_param(char parameter, int length);
jonebuckman 0:92e1b5622620 538
jonebuckman 0:92e1b5622620 539 /**
jonebuckman 0:92e1b5622620 540 * Get status.
jonebuckman 0:92e1b5622620 541 *
jonebuckman 0:92e1b5622620 542 * @return Returns status word.
jonebuckman 0:92e1b5622620 543 */
jonebuckman 0:92e1b5622620 544 int get_status(void);
jonebuckman 0:92e1b5622620 545
jonebuckman 0:92e1b5622620 546 /**
jonebuckman 0:92e1b5622620 547 * Move to a position.
jonebuckman 0:92e1b5622620 548 *
jonebuckman 0:92e1b5622620 549 * @param absolute Absolute step count.
jonebuckman 0:92e1b5622620 550 */
jonebuckman 0:92e1b5622620 551 void moveTo(long absolute);
jonebuckman 0:92e1b5622620 552
jonebuckman 0:92e1b5622620 553 /**
jonebuckman 0:92e1b5622620 554 * Move to a position relative to the current position.
jonebuckman 0:92e1b5622620 555 *
jonebuckman 0:92e1b5622620 556 * @param relative Relative step count.
jonebuckman 0:92e1b5622620 557 */
jonebuckman 0:92e1b5622620 558 void move(long relative);
jonebuckman 0:92e1b5622620 559
jonebuckman 0:92e1b5622620 560 /**
jonebuckman 0:92e1b5622620 561 * Implements steps according to the current step interval.
jonebuckman 0:92e1b5622620 562 *
jonebuckman 0:92e1b5622620 563 * @return Returns true if a step occurred.
jonebuckman 0:92e1b5622620 564 */
jonebuckman 0:92e1b5622620 565 bool runSpeed();
jonebuckman 0:92e1b5622620 566
jonebuckman 0:92e1b5622620 567 /**
jonebuckman 0:92e1b5622620 568 * Get distance yet to go.
jonebuckman 0:92e1b5622620 569 *
jonebuckman 0:92e1b5622620 570 * @return The distance count.
jonebuckman 0:92e1b5622620 571 */
jonebuckman 0:92e1b5622620 572 long distanceToGo();
jonebuckman 0:92e1b5622620 573
jonebuckman 0:92e1b5622620 574 /**
jonebuckman 0:92e1b5622620 575 * Get target position.
jonebuckman 0:92e1b5622620 576 *
jonebuckman 0:92e1b5622620 577 * @return The target position count.
jonebuckman 0:92e1b5622620 578 */
jonebuckman 0:92e1b5622620 579 long targetPosition();
jonebuckman 0:92e1b5622620 580
jonebuckman 0:92e1b5622620 581 /**
jonebuckman 0:92e1b5622620 582 * Get current position.
jonebuckman 0:92e1b5622620 583 *
jonebuckman 0:92e1b5622620 584 * @return The current position count.
jonebuckman 0:92e1b5622620 585 */
jonebuckman 0:92e1b5622620 586 long currentPosition();
jonebuckman 0:92e1b5622620 587
jonebuckman 0:92e1b5622620 588 /**
jonebuckman 0:92e1b5622620 589 * Set current position to a count. Set speed to 0.
jonebuckman 0:92e1b5622620 590 *
jonebuckman 0:92e1b5622620 591 * @param position The count at the current position.
jonebuckman 0:92e1b5622620 592 */
jonebuckman 0:92e1b5622620 593 void setCurrentPosition(long position);
jonebuckman 0:92e1b5622620 594
jonebuckman 0:92e1b5622620 595 /**
jonebuckman 0:92e1b5622620 596 * Calculate the new speed.
jonebuckman 0:92e1b5622620 597 *
jonebuckman 0:92e1b5622620 598 */
jonebuckman 0:92e1b5622620 599 void computeNewSpeed();
jonebuckman 0:92e1b5622620 600
jonebuckman 0:92e1b5622620 601 /**
jonebuckman 0:92e1b5622620 602 * Run the motor to implement speed and acceleration in order to proceed to the target position.
jonebuckman 0:92e1b5622620 603 *
jonebuckman 0:92e1b5622620 604 * @return Returns true if the motor is still running to the target position.
jonebuckman 0:92e1b5622620 605 */
jonebuckman 0:92e1b5622620 606 bool run();
jonebuckman 0:92e1b5622620 607
jonebuckman 0:92e1b5622620 608 /**
jonebuckman 0:92e1b5622620 609 * Step once.
jonebuckman 0:92e1b5622620 610 *
jonebuckman 0:92e1b5622620 611 */
jonebuckman 0:92e1b5622620 612 void step();
jonebuckman 0:92e1b5622620 613
jonebuckman 0:92e1b5622620 614 /**
jonebuckman 0:92e1b5622620 615 * Set the max speed.
jonebuckman 0:92e1b5622620 616 *
jonebuckman 0:92e1b5622620 617 * @param speed Speed maximum.
jonebuckman 0:92e1b5622620 618 */
jonebuckman 0:92e1b5622620 619 void setMaxSpeed(float speed);
jonebuckman 0:92e1b5622620 620
jonebuckman 0:92e1b5622620 621 /**
jonebuckman 0:92e1b5622620 622 * Set the acceleration.
jonebuckman 0:92e1b5622620 623 *
jonebuckman 0:92e1b5622620 624 * @param acceleration Acceleration.
jonebuckman 0:92e1b5622620 625 */
jonebuckman 0:92e1b5622620 626 void setAcceleration(float acceleration);
jonebuckman 0:92e1b5622620 627
jonebuckman 0:92e1b5622620 628 /**
jonebuckman 0:92e1b5622620 629 * Set the speed.
jonebuckman 0:92e1b5622620 630 *
jonebuckman 0:92e1b5622620 631 * @param speed Speed.
jonebuckman 0:92e1b5622620 632 */
jonebuckman 0:92e1b5622620 633 void setSpeed(float speed);
jonebuckman 0:92e1b5622620 634
jonebuckman 0:92e1b5622620 635 /**
jonebuckman 0:92e1b5622620 636 * Get current speed.
jonebuckman 0:92e1b5622620 637 *
jonebuckman 0:92e1b5622620 638 * @return The speed.
jonebuckman 0:92e1b5622620 639 */
jonebuckman 0:92e1b5622620 640 float speed();
jonebuckman 0:92e1b5622620 641
jonebuckman 0:92e1b5622620 642 /**
jonebuckman 0:92e1b5622620 643 * Set the minimum pulse width.
jonebuckman 0:92e1b5622620 644 *
jonebuckman 0:92e1b5622620 645 * @param minWidth MinWidth pulse width.
jonebuckman 0:92e1b5622620 646 */
jonebuckman 0:92e1b5622620 647 void setMinPulseWidth(unsigned int minWidth);
jonebuckman 0:92e1b5622620 648
jonebuckman 0:92e1b5622620 649 /**
jonebuckman 0:92e1b5622620 650 * System reset enable.
jonebuckman 0:92e1b5622620 651 *
jonebuckman 0:92e1b5622620 652 */
jonebuckman 0:92e1b5622620 653 void enableReset();
jonebuckman 0:92e1b5622620 654
jonebuckman 0:92e1b5622620 655 /**
jonebuckman 0:92e1b5622620 656 * System reset disable.
jonebuckman 0:92e1b5622620 657 *
jonebuckman 0:92e1b5622620 658 */
jonebuckman 0:92e1b5622620 659 void disableReset();
jonebuckman 0:92e1b5622620 660
jonebuckman 0:92e1b5622620 661 /**
jonebuckman 0:92e1b5622620 662 * Blocks until the target position is reached and stopped.
jonebuckman 0:92e1b5622620 663 *
jonebuckman 0:92e1b5622620 664 */
jonebuckman 0:92e1b5622620 665 void runToPosition();
jonebuckman 0:92e1b5622620 666
jonebuckman 0:92e1b5622620 667 /**
jonebuckman 0:92e1b5622620 668 * Run at speed to a position.
jonebuckman 0:92e1b5622620 669 *
jonebuckman 0:92e1b5622620 670 * @return The run speed.
jonebuckman 0:92e1b5622620 671 */
jonebuckman 0:92e1b5622620 672 bool runSpeedToPosition();
jonebuckman 0:92e1b5622620 673
jonebuckman 0:92e1b5622620 674 /**
jonebuckman 0:92e1b5622620 675 * Blocks until the new target position is reached.
jonebuckman 0:92e1b5622620 676 *
jonebuckman 0:92e1b5622620 677 * @param position Position.
jonebuckman 0:92e1b5622620 678 */
jonebuckman 0:92e1b5622620 679 void runToNewPosition(long position);
jonebuckman 0:92e1b5622620 680
jonebuckman 0:92e1b5622620 681 /**
jonebuckman 0:92e1b5622620 682 * Stop run to position.
jonebuckman 0:92e1b5622620 683 *
jonebuckman 0:92e1b5622620 684 */
jonebuckman 0:92e1b5622620 685 void stop();
jonebuckman 0:92e1b5622620 686
jonebuckman 0:92e1b5622620 687 /**
jonebuckman 0:92e1b5622620 688 * Get the alarm flag state.
jonebuckman 0:92e1b5622620 689 *
jonebuckman 0:92e1b5622620 690 * @return The alarm flag state.
jonebuckman 0:92e1b5622620 691 */
jonebuckman 0:92e1b5622620 692 int getAlarm();
jonebuckman 0:92e1b5622620 693
jonebuckman 0:92e1b5622620 694 protected:
jonebuckman 0:92e1b5622620 695
jonebuckman 0:92e1b5622620 696 /**
jonebuckman 0:92e1b5622620 697 * Symbolic names for the direction the motor is turning.
jonebuckman 0:92e1b5622620 698 *
jonebuckman 0:92e1b5622620 699 */
jonebuckman 0:92e1b5622620 700 typedef enum
jonebuckman 0:92e1b5622620 701 {
jonebuckman 0:92e1b5622620 702 DIRECTION_CCW = 0, ///< Clockwise
jonebuckman 0:92e1b5622620 703 DIRECTION_CW = 1 ///< Counter-Clockwise
jonebuckman 0:92e1b5622620 704 } Direction;
jonebuckman 0:92e1b5622620 705
jonebuckman 0:92e1b5622620 706 private:
jonebuckman 0:92e1b5622620 707
jonebuckman 0:92e1b5622620 708 /**
jonebuckman 0:92e1b5622620 709 * The current absolution position in steps.
jonebuckman 0:92e1b5622620 710 *
jonebuckman 0:92e1b5622620 711 */
jonebuckman 0:92e1b5622620 712 long _currentPos; // Steps
jonebuckman 0:92e1b5622620 713
jonebuckman 0:92e1b5622620 714 /**
jonebuckman 0:92e1b5622620 715 * The current interval between steps in microseconds.
jonebuckman 0:92e1b5622620 716 * 0 means the motor is currently stopped with _speed == 0
jonebuckman 0:92e1b5622620 717 *
jonebuckman 0:92e1b5622620 718 */
jonebuckman 0:92e1b5622620 719 unsigned long _stepInterval;
jonebuckman 0:92e1b5622620 720
jonebuckman 0:92e1b5622620 721 /**
jonebuckman 0:92e1b5622620 722 * The last step time in microseconds.
jonebuckman 0:92e1b5622620 723 *
jonebuckman 0:92e1b5622620 724 */
jonebuckman 0:92e1b5622620 725 unsigned long _lastStepTime;
jonebuckman 0:92e1b5622620 726
jonebuckman 0:92e1b5622620 727 /**
jonebuckman 0:92e1b5622620 728 * The minimum allowed pulse width in microseconds.
jonebuckman 0:92e1b5622620 729 *
jonebuckman 0:92e1b5622620 730 */
jonebuckman 0:92e1b5622620 731 unsigned int _minPulseWidth;
jonebuckman 0:92e1b5622620 732
jonebuckman 0:92e1b5622620 733 /**
jonebuckman 0:92e1b5622620 734 * The target position in steps. The AccelStepper library will move the
jonebuckman 0:92e1b5622620 735 * motor from the _currentPos to the _targetPos, taking into account the
jonebuckman 0:92e1b5622620 736 * max speed, acceleration and deceleration.
jonebuckman 0:92e1b5622620 737 *
jonebuckman 0:92e1b5622620 738 */
jonebuckman 0:92e1b5622620 739 long _targetPos; // Steps
jonebuckman 0:92e1b5622620 740
jonebuckman 0:92e1b5622620 741 /**
jonebuckman 0:92e1b5622620 742 * The current motos speed in steps per second.
jonebuckman 0:92e1b5622620 743 * Positive is clockwise.
jonebuckman 0:92e1b5622620 744 *
jonebuckman 0:92e1b5622620 745 */
jonebuckman 0:92e1b5622620 746 float _speed; // Steps per second
jonebuckman 0:92e1b5622620 747
jonebuckman 0:92e1b5622620 748 /**
jonebuckman 0:92e1b5622620 749 * The acceleration to use to accelerate or decelerate the motor in steps
jonebuckman 0:92e1b5622620 750 * per second per second. Must be > 0.
jonebuckman 0:92e1b5622620 751 *
jonebuckman 0:92e1b5622620 752 */
jonebuckman 0:92e1b5622620 753 float _acceleration;
jonebuckman 0:92e1b5622620 754 float _sqrt_twoa; // Precomputed sqrt(2*_acceleration)
jonebuckman 0:92e1b5622620 755
jonebuckman 0:92e1b5622620 756 /**
jonebuckman 0:92e1b5622620 757 * The maximum permitted speed in steps per second. Must be > 0.
jonebuckman 0:92e1b5622620 758 *
jonebuckman 0:92e1b5622620 759 */
jonebuckman 0:92e1b5622620 760 float _maxSpeed;
jonebuckman 0:92e1b5622620 761
jonebuckman 0:92e1b5622620 762 /**
jonebuckman 0:92e1b5622620 763 * Min step size in microseconds based on maxSpeed.
jonebuckman 0:92e1b5622620 764 *
jonebuckman 0:92e1b5622620 765 */
jonebuckman 0:92e1b5622620 766 float _cmin; // at max speed
jonebuckman 0:92e1b5622620 767
jonebuckman 0:92e1b5622620 768 /**
jonebuckman 0:92e1b5622620 769 * Initial step size in microseconds.
jonebuckman 0:92e1b5622620 770 *
jonebuckman 0:92e1b5622620 771 */
jonebuckman 0:92e1b5622620 772 float _c0;
jonebuckman 0:92e1b5622620 773
jonebuckman 0:92e1b5622620 774 /**
jonebuckman 0:92e1b5622620 775 * Last step size in microseconds.
jonebuckman 0:92e1b5622620 776 *
jonebuckman 0:92e1b5622620 777 */
jonebuckman 0:92e1b5622620 778 float _cn;
jonebuckman 0:92e1b5622620 779
jonebuckman 0:92e1b5622620 780 /**
jonebuckman 0:92e1b5622620 781 * The step counter for speed calculations.
jonebuckman 0:92e1b5622620 782 *
jonebuckman 0:92e1b5622620 783 */
jonebuckman 0:92e1b5622620 784 long _n;
jonebuckman 0:92e1b5622620 785
jonebuckman 0:92e1b5622620 786 /**
jonebuckman 0:92e1b5622620 787 * Current direction motor is spinning in.
jonebuckman 0:92e1b5622620 788 *
jonebuckman 0:92e1b5622620 789 */
jonebuckman 0:92e1b5622620 790 bool _direction; // 1 == CW
jonebuckman 0:92e1b5622620 791
jonebuckman 0:92e1b5622620 792 SPI spi_;
jonebuckman 0:92e1b5622620 793 Timer t_;
jonebuckman 0:92e1b5622620 794 DigitalOut nCS_;
jonebuckman 0:92e1b5622620 795 DigitalOut step_;
jonebuckman 0:92e1b5622620 796 DigitalOut dir_;
jonebuckman 0:92e1b5622620 797 DigitalOut reset_;
jonebuckman 0:92e1b5622620 798 DigitalIn flag_;
jonebuckman 0:92e1b5622620 799 };
jonebuckman 0:92e1b5622620 800
jonebuckman 0:92e1b5622620 801 #endif