Library for ST's new dSpin stepper motor driver. Works with their demo board L6470EVAL.
dSPIN.cpp@0:3a9b958ba85f, 2012-07-09 (annotated)
- Committer:
- thorlabs
- Date:
- Mon Jul 09 14:40:14 2012 +0000
- Revision:
- 0:3a9b958ba85f
[mbed] converted /RS485_Repeater_11U24_beta/dSPIN
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thorlabs | 0:3a9b958ba85f | 1 | /** |
thorlabs | 0:3a9b958ba85f | 2 | * @author Eric Lieser |
thorlabs | 0:3a9b958ba85f | 3 | * |
thorlabs | 0:3a9b958ba85f | 4 | * @section LICENSE |
thorlabs | 0:3a9b958ba85f | 5 | * |
thorlabs | 0:3a9b958ba85f | 6 | * Copyright (c) 2010 ARM Limited |
thorlabs | 0:3a9b958ba85f | 7 | * |
thorlabs | 0:3a9b958ba85f | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
thorlabs | 0:3a9b958ba85f | 9 | * of this software and associated documentation files (the "Software"), to deal |
thorlabs | 0:3a9b958ba85f | 10 | * in the Software without restriction, including without limitation the rights |
thorlabs | 0:3a9b958ba85f | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
thorlabs | 0:3a9b958ba85f | 12 | * copies of the Software, and to permit persons to whom the Software is |
thorlabs | 0:3a9b958ba85f | 13 | * furnished to do so, subject to the following conditions: |
thorlabs | 0:3a9b958ba85f | 14 | * |
thorlabs | 0:3a9b958ba85f | 15 | * The above copyright notice and this permission notice shall be included in |
thorlabs | 0:3a9b958ba85f | 16 | * all copies or substantial portions of the Software. |
thorlabs | 0:3a9b958ba85f | 17 | * |
thorlabs | 0:3a9b958ba85f | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
thorlabs | 0:3a9b958ba85f | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
thorlabs | 0:3a9b958ba85f | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
thorlabs | 0:3a9b958ba85f | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
thorlabs | 0:3a9b958ba85f | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
thorlabs | 0:3a9b958ba85f | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
thorlabs | 0:3a9b958ba85f | 24 | * THE SOFTWARE. |
thorlabs | 0:3a9b958ba85f | 25 | * |
thorlabs | 0:3a9b958ba85f | 26 | * @section DESCRIPTION |
thorlabs | 0:3a9b958ba85f | 27 | * |
thorlabs | 0:3a9b958ba85f | 28 | * ST Microelectronics L6470 dSPIN fully integrated microstepping motor driver |
thorlabs | 0:3a9b958ba85f | 29 | * with motion engine and SPI |
thorlabs | 0:3a9b958ba85f | 30 | * |
thorlabs | 0:3a9b958ba85f | 31 | * Datasheet: http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00255075.pdf |
thorlabs | 0:3a9b958ba85f | 32 | * |
thorlabs | 0:3a9b958ba85f | 33 | * |
thorlabs | 0:3a9b958ba85f | 34 | */ |
thorlabs | 0:3a9b958ba85f | 35 | |
thorlabs | 0:3a9b958ba85f | 36 | /** |
thorlabs | 0:3a9b958ba85f | 37 | * Includes |
thorlabs | 0:3a9b958ba85f | 38 | */ |
thorlabs | 0:3a9b958ba85f | 39 | #include "dSPIN.h" |
thorlabs | 0:3a9b958ba85f | 40 | |
thorlabs | 0:3a9b958ba85f | 41 | dSPIN::dSPIN(PinName mosi, |
thorlabs | 0:3a9b958ba85f | 42 | PinName miso, |
thorlabs | 0:3a9b958ba85f | 43 | PinName sck, |
thorlabs | 0:3a9b958ba85f | 44 | PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { |
thorlabs | 0:3a9b958ba85f | 45 | |
thorlabs | 0:3a9b958ba85f | 46 | //5MHz, see page 13 of datasheet for max clock. May need to be reduces if using long wires |
thorlabs | 0:3a9b958ba85f | 47 | spi_.frequency(5000000); |
thorlabs | 0:3a9b958ba85f | 48 | spi_.format(8,3); |
thorlabs | 0:3a9b958ba85f | 49 | |
thorlabs | 0:3a9b958ba85f | 50 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 51 | |
thorlabs | 0:3a9b958ba85f | 52 | wait_us(500); |
thorlabs | 0:3a9b958ba85f | 53 | |
thorlabs | 0:3a9b958ba85f | 54 | } |
thorlabs | 0:3a9b958ba85f | 55 | |
thorlabs | 0:3a9b958ba85f | 56 | void dSPIN::run(int dir, int speed) { |
thorlabs | 0:3a9b958ba85f | 57 | char val[3]; |
thorlabs | 0:3a9b958ba85f | 58 | val[0] = (speed & 0xff0000UL) >> 16; |
thorlabs | 0:3a9b958ba85f | 59 | val[1] = (speed & 0x00ff00UL) >> 8; |
thorlabs | 0:3a9b958ba85f | 60 | val[2] = (speed & 0x0000ffUL) ; |
thorlabs | 0:3a9b958ba85f | 61 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 62 | if (dir == 1) |
thorlabs | 0:3a9b958ba85f | 63 | spi_.write(0x51); |
thorlabs | 0:3a9b958ba85f | 64 | else spi_.write(0x50); |
thorlabs | 0:3a9b958ba85f | 65 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 66 | for (int i = 0; i < 3; i++) { |
thorlabs | 0:3a9b958ba85f | 67 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 68 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 69 | spi_.write(val[i]); |
thorlabs | 0:3a9b958ba85f | 70 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 71 | } |
thorlabs | 0:3a9b958ba85f | 72 | } |
thorlabs | 0:3a9b958ba85f | 73 | |
thorlabs | 0:3a9b958ba85f | 74 | void dSPIN::move(int dir, int steps) { |
thorlabs | 0:3a9b958ba85f | 75 | char val[3]; |
thorlabs | 0:3a9b958ba85f | 76 | val[0] = (steps & 0xff0000UL) >> 16; |
thorlabs | 0:3a9b958ba85f | 77 | val[1] = (steps & 0x00ff00UL) >> 8; |
thorlabs | 0:3a9b958ba85f | 78 | val[2] = (steps & 0x0000ffUL) ; |
thorlabs | 0:3a9b958ba85f | 79 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 80 | if (dir == 1) |
thorlabs | 0:3a9b958ba85f | 81 | spi_.write(0x41); |
thorlabs | 0:3a9b958ba85f | 82 | else spi_.write(0x40); |
thorlabs | 0:3a9b958ba85f | 83 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 84 | for (int i = 0; i < 3; i++) { |
thorlabs | 0:3a9b958ba85f | 85 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 86 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 87 | spi_.write(val[i]); |
thorlabs | 0:3a9b958ba85f | 88 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 89 | } |
thorlabs | 0:3a9b958ba85f | 90 | } |
thorlabs | 0:3a9b958ba85f | 91 | |
thorlabs | 0:3a9b958ba85f | 92 | void dSPIN::soft_stop() { |
thorlabs | 0:3a9b958ba85f | 93 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 94 | spi_.write(0xB0); |
thorlabs | 0:3a9b958ba85f | 95 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 96 | } |
thorlabs | 0:3a9b958ba85f | 97 | |
thorlabs | 0:3a9b958ba85f | 98 | void dSPIN::set_param(char parameter, int length, int value) { |
thorlabs | 0:3a9b958ba85f | 99 | char val[3]; |
thorlabs | 0:3a9b958ba85f | 100 | switch (length) { |
thorlabs | 0:3a9b958ba85f | 101 | case 1: |
thorlabs | 0:3a9b958ba85f | 102 | val[0] = (value & 0x0000ffUL) ; |
thorlabs | 0:3a9b958ba85f | 103 | break; |
thorlabs | 0:3a9b958ba85f | 104 | case 2: |
thorlabs | 0:3a9b958ba85f | 105 | val[0] = (value & 0x00ff00UL) >> 8; |
thorlabs | 0:3a9b958ba85f | 106 | val[1] = (value & 0x0000ffUL) ; |
thorlabs | 0:3a9b958ba85f | 107 | break; |
thorlabs | 0:3a9b958ba85f | 108 | case 3: |
thorlabs | 0:3a9b958ba85f | 109 | val[0] = (value & 0xff0000UL) >> 16; |
thorlabs | 0:3a9b958ba85f | 110 | val[1] = (value & 0x00ff00UL) >> 8; |
thorlabs | 0:3a9b958ba85f | 111 | val[2] = (value & 0x0000ffUL) ; |
thorlabs | 0:3a9b958ba85f | 112 | break; |
thorlabs | 0:3a9b958ba85f | 113 | } |
thorlabs | 0:3a9b958ba85f | 114 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 115 | spi_.write(parameter); |
thorlabs | 0:3a9b958ba85f | 116 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 117 | for (int i = 0; i < length; i++) { |
thorlabs | 0:3a9b958ba85f | 118 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 119 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 120 | spi_.write(val[i]); |
thorlabs | 0:3a9b958ba85f | 121 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 122 | } |
thorlabs | 0:3a9b958ba85f | 123 | |
thorlabs | 0:3a9b958ba85f | 124 | } |
thorlabs | 0:3a9b958ba85f | 125 | |
thorlabs | 0:3a9b958ba85f | 126 | int dSPIN::get_param(char parameter, int length) { |
thorlabs | 0:3a9b958ba85f | 127 | char output = 0x20 | parameter; |
thorlabs | 0:3a9b958ba85f | 128 | char buf[3] = {0, 0, 0}; |
thorlabs | 0:3a9b958ba85f | 129 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 130 | spi_.write(output); |
thorlabs | 0:3a9b958ba85f | 131 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 132 | for (int i = 0; i < length; i++) { |
thorlabs | 0:3a9b958ba85f | 133 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 134 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 135 | buf[i] = spi_.write(0x00); |
thorlabs | 0:3a9b958ba85f | 136 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 137 | } |
thorlabs | 0:3a9b958ba85f | 138 | switch (length) { |
thorlabs | 0:3a9b958ba85f | 139 | case 1: |
thorlabs | 0:3a9b958ba85f | 140 | return buf[0]; |
thorlabs | 0:3a9b958ba85f | 141 | case 2: |
thorlabs | 0:3a9b958ba85f | 142 | return buf[0] << 8 | buf[1]; |
thorlabs | 0:3a9b958ba85f | 143 | case 3: |
thorlabs | 0:3a9b958ba85f | 144 | return buf[0] << 16 | buf[1] << 8 | buf[2]; |
thorlabs | 0:3a9b958ba85f | 145 | } |
thorlabs | 0:3a9b958ba85f | 146 | return 0; |
thorlabs | 0:3a9b958ba85f | 147 | } |
thorlabs | 0:3a9b958ba85f | 148 | |
thorlabs | 0:3a9b958ba85f | 149 | int dSPIN::get_status() { |
thorlabs | 0:3a9b958ba85f | 150 | int ret_bytes[2]; |
thorlabs | 0:3a9b958ba85f | 151 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 152 | spi_.write(0xD0); //write request for status return |
thorlabs | 0:3a9b958ba85f | 153 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 154 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 155 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 156 | ret_bytes[0] = spi_.write(0x00); //read first byte |
thorlabs | 0:3a9b958ba85f | 157 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 158 | wait_us(1); |
thorlabs | 0:3a9b958ba85f | 159 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 160 | ret_bytes[1] = spi_.write(0x00); //read second byte |
thorlabs | 0:3a9b958ba85f | 161 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 162 | return ret_bytes[0] << 8 | ret_bytes[1]; //return i16 response |
thorlabs | 0:3a9b958ba85f | 163 | } |
thorlabs | 0:3a9b958ba85f | 164 | |
thorlabs | 0:3a9b958ba85f | 165 | void dSPIN::reset_device() { |
thorlabs | 0:3a9b958ba85f | 166 | nCS_ = 0; |
thorlabs | 0:3a9b958ba85f | 167 | spi_.write(0xC0); |
thorlabs | 0:3a9b958ba85f | 168 | nCS_ = 1; |
thorlabs | 0:3a9b958ba85f | 169 | } |