TOS-100 Stepper Motor Shield (SPI)

I would like to control a new stepper motor shield I just bought (Shield Web page); it runs over SPI and seems pretty versatile.

Looking at the code it seems to me like it should not take much to get it to compile, but I get a lot of faults such as:

In file included from TMC26XStepper.cpp:34:0: TMC26XStepper.h:376:29: error: 'boolean' has not been declared void setCoolStepEnabled(boolean enabled); ^ TMC26XStepper.h:383:5: error: 'boolean' does not name a type boolean isCoolStepEnabled(); ^ TMC26XStepper.h:444:5: error: 'boolean' does not name a type boolean isCurrentScalingHalfed();

The only thing I did so far was basically to exclude these files in the .cpp file; I guess they should be there as part of the standard libraries anyway?

//#if defined(ARDUINO) && ARDUINO >= 100
//	#include <Arduino.h>
//#else
//	#include <WProgram.h>
//#endif
//#include "SPI.h"

I put the files on Github for easy access Github files.

Any pointers would be appreciated!

Thanks!

try this

#include "application.h"

to keep compatibility with both devices i think is possible to something like this

#if defined(SPARK)
    #include "application.h"
#else if defined(ARDUINO) && ARDUINO >= 100
    #include <Arduino.h>
#else
    #include <WProgram.h>
#endif 
#include "SPI.h"
#endif

So I did a bit of testing.

First, I tried to include the code below, and this worked!

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#elif defined(SPARK)
#include "application.h"
#endif

In the .cpp file I had to change a _BV(6) to (1>>6), I hope this is equivalent.

But then I still have a fault I can get rid off if I exclude this part, but I guess I want to leave it in there if possible.

In one of the functions a reference is made to SPCR & SPI_MODE_MASK, and since I don’t have SPI.h file included (the arduino one) it does not compile. When I look in the spark_wiring_spi.h these parameters are not defined so I am unsure what to replace them with - any ideas?

inline void TMC26XStepper::send262(unsigned long datagram) {
unsigned long i_datagram;

//preserver the previous spi mode
unsigned char oldMode =  SPCR & SPI_MODE_MASK;

//if the mode is not correct set it to mode 3
if (oldMode != SPI_MODE3) {
    SPI.setDataMode(SPI_MODE3);
}

ok so I think what that does is saves the previous SPI mode and if its not mode 3 then it changes it to mode 3. Does it also change the value back after? something like SPI.setDataMode(oldMode); ?

best way would be to wrap it up something like this, I’m not sure if there is an easier way to see what mode the core is in?

inline void TMC26XStepper::send262(unsigned long datagram) {
unsigned long i_datagram;

//preserver the previous spi mode
#if defined(ARDUINO)
unsigned char oldMode =  SPCR & SPI_MODE_MASK;
#elif defined(SPARK)
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
    oldMode =  SPI_MODE0;
}
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
    oldMode =  SPI_MODE1;
}
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
    oldMode =  SPI_MODE2:
}
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
    oldMode =  SPI_MODE3:
#endif


//if the mode is not correct set it to mode 3
if (oldMode != SPI_MODE3) {
    SPI.setDataMode(SPI_MODE3);
}

Looks like a good idea! I fixed a couple of typos (; instead of : etc) but I now get the following errors (are parameters like SPI_InitStructure part of the Spark SPI framework?:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from TMC26XStepper.cpp:37:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
TMC26XStepper.cpp: In member function 'void TMC26XStepper::send262(long unsigned int)':
TMC26XStepper.cpp:968:12: error: 'SPI_InitStructure' was not declared in this scope
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
TMC26XStepper.cpp:969:13: error: 'oldMode' was not declared in this scope
oldMode = SPI_MODE0;
^
TMC26XStepper.cpp:971:12: error: 'SPI_InitStructure' was not declared in this scope
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
TMC26XStepper.cpp:972:13: error: 'oldMode' was not declared in this scope
oldMode = SPI_MODE1;
^
TMC26XStepper.cpp:974:12: error: 'SPI_InitStructure' was not declared in this scope
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI_InitStructure.SPI_CPHA =    SPI_CPHA_1Edge){
^
TMC26XStepper.cpp:975:13: error: 'oldMode' was not declared in this scope
oldMode = SPI_MODE2;
^
TMC26XStepper.cpp:977:12: error: 'SPI_InitStructure' was not declared in this scope
if(SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI_InitStructure.SPI_CPHA =    SPI_CPHA_2Edge){
^
TMC26XStepper.cpp:978:13: error: 'oldMode' was not declared in this scope
oldMode = SPI_MODE3;
^
TMC26XStepper.cpp:986:9: error: 'oldMode' was not declared in this scope
if (oldMode != SPI_MODE3) {
^
TMC26XStepper.cpp:1019:9: error: 'oldMode' was not declared in this scope
if (oldMode != SPI_MODE3) {
^
make: *** [TMC26XStepper.o] Error 1

Sorry about that, cut and paste issues!

try this

inline void TMC26XStepper::send262(unsigned long datagram) {
unsigned long i_datagram;

//preserver the previous spi mode
#if defined(ARDUINO)
unsigned char oldMode =  SPCR & SPI_MODE_MASK;
#elif defined(SPARK)
unsigned char oldMode =  SPI_MODE3; //lets set the default to mode 3 in case none of the below are true
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
    oldMode =  SPI_MODE0;
}
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
    oldMode =  SPI_MODE1;
}
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
    oldMode =  SPI_MODE2;
}
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
    oldMode =  SPI_MODE3;
}
#endif

//if the mode is not correct set it to mode 3
if (oldMode != SPI_MODE3) {
    SPI.setDataMode(SPI_MODE3);
}

I think we are moving forward :slight_smile: sorry I can’t be of more help and I appreciate your help, but I now get the following errors:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from TMC26XStepper.cpp:37:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h: In member function 'void TMC26XStepper::send262(long unsigned int)':
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:968:16: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:968:65: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
TMC26XStepper.cpp:968:92: error: lvalue required as left operand of assignment
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:971:16: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:971:65: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
TMC26XStepper.cpp:971:92: error: lvalue required as left operand of assignment
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:974:16: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:974:66: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
TMC26XStepper.cpp:974:93: error: lvalue required as left operand of assignment
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:977:16: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
In file included from ../inc/application.h:35:0,
from TMC26XStepper.cpp:37:
../inc/spark_wiring_spi.h:48:25: error: 'SPI_InitTypeDef SPIClass::SPI_InitStructure' is private
static SPI_InitTypeDef SPI_InitStructure;
^
TMC26XStepper.cpp:977:66: error: within this context
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
TMC26XStepper.cpp:977:93: error: lvalue required as left operand of assignment
if(SPI.SPI_InitStructure.SPI_CPOL = SPI_CPOL_High && SPI.SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge){
^
make: *** [TMC26XStepper.o] Error 1

Use this for now, it should at least get you further, for checking the rest of the code. the core defaults to mode3 anyway and if your not using any other SPI devices it should be fine. Maybe @peekay123 may be able to suggest a way to read what mode the SPI is in…

inline void TMC26XStepper::send262(unsigned long datagram) {
unsigned long i_datagram;

//preserver the previous spi mode
#if defined(ARDUINO)
unsigned char oldMode =  SPCR & SPI_MODE_MASK;
#else
unsigned char oldMode =  SPI_MODE3;
#endif

//if the mode is not correct set it to mode 3
if (oldMode != SPI_MODE3) {
    SPI.setDataMode(SPI_MODE3);
}

Thanks it now compiles without error.
I’ll try and make the darn motor spin now, I’ll let you know the outcome and then I’ll try and dig deeper on the SPI topic!