From d422820408cbf3263f1b7ca13420846213568429 Mon Sep 17 00:00:00 2001 From: Adriano Date: Wed, 23 Mar 2016 13:58:37 +0100 Subject: [PATCH 1/3] Update AS5048A.h settings added to class, so that different SPI devices can be connected to the same Arduino --- lib/AS5048A/AS5048A.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/AS5048A/AS5048A.h b/lib/AS5048A/AS5048A.h index 109fdfe..3bb7c36 100644 --- a/lib/AS5048A/AS5048A.h +++ b/lib/AS5048A/AS5048A.h @@ -1,15 +1,8 @@ #ifndef as5048_h #define as5048_h -#define LIBRARY_VERSION 1.0.0 +#define LIBRARY_VERSION 1.0.1 - -#define AS5048A_CLEAR_ERROR_FLAG 0x0001 -#define AS5048A_PROGRAMMING_CONTROL 0x0003 -#define AS5048A_OTP_REGISTER_ZERO_POS_HIGH 0x0016 -#define AS5048A_OTP_REGISTER_ZERO_POS_LOW 0x0017 -#define AS5048A_DIAG_AGC 0x3FFD -#define AS5048A_MAGNITUDE 0x3FFE -#define AS5048A_ANGLE 0x3FFF +#include class AS5048A{ @@ -21,7 +14,9 @@ class AS5048A{ byte clk; word position; word transaction(word data); - + + SPISettings settings; + public: /** From 0fa99d2392bddd5fc1d9fca63d6c8f2d914e7023 Mon Sep 17 00:00:00 2001 From: Adriano Date: Wed, 23 Mar 2016 14:01:48 +0100 Subject: [PATCH 2/3] Update AS5048A.cpp - settings added to class, so that different SPI devices can be connected to the same Arduino. - added SPI.beginTransaction(settings) / SPI.endTransaction() for SPI transfers - removed WProgram.h (this is too old :P) - replaced #defines with const int (https://www.arduino.cc/en/Reference/Define -> the const keyword is preferred for defining constants and should be used instead of #define) --- lib/AS5048A/AS5048A.cpp | 59 ++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/AS5048A/AS5048A.cpp b/lib/AS5048A/AS5048A.cpp index dbf3504..8bb7267 100644 --- a/lib/AS5048A/AS5048A.cpp +++ b/lib/AS5048A/AS5048A.cpp @@ -1,14 +1,17 @@ -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif +#include "Arduino.h" #include -#include //#define AS5048A_DEBUG +const int AS5048A_CLEAR_ERROR_FLAG = 0x0001; +const int AS5048A_PROGRAMMING_CONTROL = 0x0003; +const int AS5048A_OTP_REGISTER_ZERO_POS_HIGH = 0x0016; +const int AS5048A_OTP_REGISTER_ZERO_POS_LOW = 0x0017; +const int AS5048A_DIAG_AGC = 0x3FFD; +const int AS5048A_MAGNITUDE = 0x3FFE; +const int AS5048A_ANGLE = 0x3FFF; + /** * Constructor */ @@ -16,10 +19,6 @@ AS5048A::AS5048A(byte arg_cs){ _cs = arg_cs; errorFlag = false; position = 0; - - //setup pins - pinMode(_cs,OUTPUT); - } @@ -27,27 +26,33 @@ AS5048A::AS5048A(byte arg_cs){ * Initialiser * Sets up the SPI interface */ -void AS5048A::init() -{ - SPI.setDataMode(SPI_MODE1); - SPI.setClockDivider(SPI_CLOCK_DIV64); - SPI.setBitOrder(MSBFIRST); +void AS5048A::init(){ + // 1MHz clock (AMS should be able to accept up to 10MHz) + settings = SPISettings(1000000, MSBFIRST, SPI_MODE1); + + //setup pins + pinMode(_cs, OUTPUT); + + //SPI.setDataMode(SPI_MODE1); + //SPI.setClockDivider(SPI_CLOCK_DIV64); + //SPI.setBitOrder(MSBFIRST); + + //SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices SPI.begin(); } /** * Closes the SPI connection + * SPI has an internal SPI-device counter, for each init()-call the close() function must be called exactly 1 time */ -void AS5048A::close() -{ +void AS5048A::close(){ SPI.end(); } /** * Utility function used to calculate even parity of word */ -byte AS5048A::spiCalcEvenParity(word value) -{ +byte AS5048A::spiCalcEvenParity(word value){ byte cnt = 0; byte i; @@ -169,6 +174,9 @@ word AS5048A::read(word registerAddress){ Serial.println(command, BIN); #endif + //SPI - begin transaction + SPI.beginTransaction(settings); + //Send the command digitalWrite(_cs, LOW); SPI.transfer(left_byte); @@ -181,6 +189,9 @@ word AS5048A::read(word registerAddress){ right_byte = SPI.transfer(0x00); digitalWrite(_cs, HIGH); + //SPI - end transaction + SPI.endTransaction(); + #ifdef AS5048A_DEBUG Serial.print("Read returned: "); Serial.print(left_byte, BIN); @@ -228,12 +239,15 @@ word AS5048A::write(word registerAddress, word data) { Serial.println(command, BIN); #endif + //SPI - begin transaction + SPI.beginTransaction(settings); + //Start the write command with the target address digitalWrite(_cs, LOW); SPI.transfer(left_byte); SPI.transfer(right_byte); digitalWrite(_cs,HIGH); - + word dataToSend = 0b0000000000000000; dataToSend |= data; @@ -252,13 +266,16 @@ word AS5048A::write(word registerAddress, word data) { SPI.transfer(left_byte); SPI.transfer(right_byte); digitalWrite(_cs,HIGH); - + //Send a NOP to get the new data in the register digitalWrite(_cs, LOW); left_byte =-SPI.transfer(0x00); right_byte = SPI.transfer(0x00); digitalWrite(_cs, HIGH); + //SPI - end transaction + SPI.endTransaction(); + //Return the data, stripping the parity and error bits return (( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF )) & ~0xC000; } From 70a5401a92371d468a231eedcedd457b349b5ccb Mon Sep 17 00:00:00 2001 From: Adriano Date: Wed, 23 Mar 2016 16:37:08 +0100 Subject: [PATCH 3/3] removed comments Removed commented SPI commands --- lib/AS5048A/AS5048A.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/AS5048A/AS5048A.cpp b/lib/AS5048A/AS5048A.cpp index 8bb7267..e15b7ce 100644 --- a/lib/AS5048A/AS5048A.cpp +++ b/lib/AS5048A/AS5048A.cpp @@ -32,10 +32,6 @@ void AS5048A::init(){ //setup pins pinMode(_cs, OUTPUT); - - //SPI.setDataMode(SPI_MODE1); - //SPI.setClockDivider(SPI_CLOCK_DIV64); - //SPI.setBitOrder(MSBFIRST); //SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices SPI.begin();