Library for ST's new dSpin stepper motor driver. Works with their demo board L6470EVAL.
Revision 0:3a9b958ba85f, committed 2012-07-09
- Comitter:
- thorlabs
- Date:
- Mon Jul 09 14:40:14 2012 +0000
- Commit message:
- [mbed] converted /RS485_Repeater_11U24_beta/dSPIN
Changed in this revision
dSPIN.cpp | Show annotated file Show diff for this revision Revisions of this file |
dSPIN.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 3a9b958ba85f dSPIN.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dSPIN.cpp Mon Jul 09 14:40:14 2012 +0000 @@ -0,0 +1,169 @@ +/** + * @author Eric Lieser + * + * @section LICENSE + * + * Copyright (c) 2010 ARM Limited + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @section DESCRIPTION + * + * ST Microelectronics L6470 dSPIN fully integrated microstepping motor driver + * with motion engine and SPI + * + * Datasheet: http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00255075.pdf + * + * + */ + +/** +* Includes +*/ +#include "dSPIN.h" + +dSPIN::dSPIN(PinName mosi, + PinName miso, + PinName sck, + PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { + + //5MHz, see page 13 of datasheet for max clock. May need to be reduces if using long wires + spi_.frequency(5000000); + spi_.format(8,3); + + nCS_ = 1; + + wait_us(500); + +} + +void dSPIN::run(int dir, int speed) { + char val[3]; + val[0] = (speed & 0xff0000UL) >> 16; + val[1] = (speed & 0x00ff00UL) >> 8; + val[2] = (speed & 0x0000ffUL) ; + nCS_ = 0; + if (dir == 1) + spi_.write(0x51); + else spi_.write(0x50); + nCS_ = 1; + for (int i = 0; i < 3; i++) { + wait_us(1); + nCS_ = 0; + spi_.write(val[i]); + nCS_ = 1; + } +} + +void dSPIN::move(int dir, int steps) { + char val[3]; + val[0] = (steps & 0xff0000UL) >> 16; + val[1] = (steps & 0x00ff00UL) >> 8; + val[2] = (steps & 0x0000ffUL) ; + nCS_ = 0; + if (dir == 1) + spi_.write(0x41); + else spi_.write(0x40); + nCS_ = 1; + for (int i = 0; i < 3; i++) { + wait_us(1); + nCS_ = 0; + spi_.write(val[i]); + nCS_ = 1; + } +} + +void dSPIN::soft_stop() { + nCS_ = 0; + spi_.write(0xB0); + nCS_ = 1; +} + +void dSPIN::set_param(char parameter, int length, int value) { + char val[3]; + switch (length) { + case 1: + val[0] = (value & 0x0000ffUL) ; + break; + case 2: + val[0] = (value & 0x00ff00UL) >> 8; + val[1] = (value & 0x0000ffUL) ; + break; + case 3: + val[0] = (value & 0xff0000UL) >> 16; + val[1] = (value & 0x00ff00UL) >> 8; + val[2] = (value & 0x0000ffUL) ; + break; + } + nCS_ = 0; + spi_.write(parameter); + nCS_ = 1; + for (int i = 0; i < length; i++) { + wait_us(1); + nCS_ = 0; + spi_.write(val[i]); + nCS_ = 1; + } + +} + +int dSPIN::get_param(char parameter, int length) { + char output = 0x20 | parameter; + char buf[3] = {0, 0, 0}; + nCS_ = 0; + spi_.write(output); + nCS_ = 1; + for (int i = 0; i < length; i++) { + wait_us(1); + nCS_ = 0; + buf[i] = spi_.write(0x00); + nCS_ = 1; + } + switch (length) { + case 1: + return buf[0]; + case 2: + return buf[0] << 8 | buf[1]; + case 3: + return buf[0] << 16 | buf[1] << 8 | buf[2]; + } + return 0; +} + +int dSPIN::get_status() { + int ret_bytes[2]; + nCS_ = 0; + spi_.write(0xD0); //write request for status return + nCS_ = 1; + wait_us(1); + nCS_ = 0; + ret_bytes[0] = spi_.write(0x00); //read first byte + nCS_ = 1; + wait_us(1); + nCS_ = 0; + ret_bytes[1] = spi_.write(0x00); //read second byte + nCS_ = 1; + return ret_bytes[0] << 8 | ret_bytes[1]; //return i16 response +} + +void dSPIN::reset_device() { + nCS_ = 0; + spi_.write(0xC0); + nCS_ = 1; +} \ No newline at end of file
diff -r 000000000000 -r 3a9b958ba85f dSPIN.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dSPIN.h Mon Jul 09 14:40:14 2012 +0000 @@ -0,0 +1,114 @@ +/** + * @author Eric Lieser + * + * @section LICENSE + * + * Copyright (c) 2010 ARM Limited + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @section DESCRIPTION + * + * ST Microelectronics L6470 dSPIN fully integrated microstepping motor driver + * with motion engine and SPI + * + * Datasheet: http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00255075.pdf + * + * + */ + +#ifndef dSPIN_H +#define dSPIN_H + + +/** + * Includes + */ +#include "mbed.h" + +/** + * Defines + */ +#define ABS_POS 0x01 //length: 22 reset value: 0 +#define EL_POS 0x02 //length: 9 reset value: 0 +#define MARK 0x03 //length: 22 reset value: 0 +#define SPEED 0x04 //length: 20 reset value: 0 +#define ACC 0x05 //length: 12 reset value: 08A +#define DEC 0x06 //length: 12 reset value: 08A +#define MAX_SPEED 0x07 //length: 10 reset value: 41 +#define MIN_SPEED 0x08 //length: 13 reset value: 0 +#define FS_SPD 0x15 //length: 10 reset value: 027 +#define KVAL_HOLD 0x09 //length: 8 reset value: 29 +#define KVAL_RUN 0x0A //length: 8 reset value: 29 +#define KVAL_ACC 0x0B //length: 8 reset value: 29 +#define KVAL_DEC 0x0C //length: 8 reset value: 29 +#define INT_SPD 0x0D //length: 14 reset value: 0408 +#define ST_SLP 0x0E //length: 8 reset value: 19 +#define FN_SLP_ACC 0x0F //length: 8 reset value: 29 +#define FN_SLP_DEC 0x10 //length: 8 reset value: 29 +#define K_THERM 0x11 //length: 4 reset value: 0 +#define ADC_OUT 0x12 //length: 5 reset value: +#define OCD_TH 0x13 //length: 4 reset value: 8 +#define STALL_TH 0x14 //length: 7 reset value: 40 +#define STEP_MODE 0x16 //length: 8 reset value: 7 +#define ALARM_EN 0x17 //length: 8 reset value: FF +#define CONFIG 0x18 //length: 16 reset value: 2E88 +#define STATUS 0x19 //length: 16 reset value: + +/** + * ST Micro dSPIN stepper motor controller + */ +class dSPIN { + +public: + + /** + * Constructor. + * + * @param mosi mbed pin to use for MOSI line of SPI interface. + * @param miso mbed pin to use for MISO line of SPI interface. + * @param sck mbed pin to use for SCK line of SPI interface. + * @param cs mbed pin to use for not chip select line of SPI interface. + */ + dSPIN(PinName mosi, PinName miso, PinName sck, PinName cs); + + void run(int dir, int speed); + + void move(int dir, int steps); + + void soft_stop(void); + + void set_param(char parameter, int length, int value); + + int get_param(char parameter, int length); + + int get_status(void); + + void reset_device(void); + +private: + + SPI spi_; + DigitalOut nCS_; + +}; + + + +#endif /* dSPIN */