diff --git a/library.properties b/library.properties index af95908..4571e9c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun MMC5983MA Magnetometer Arduino Library -version=1.0.1 +version=1.0.2 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=A I2C/SPI library for the MMC5983MA magnetic compass sensor. diff --git a/src/SparkFun_MMC5983MA_IO.cpp b/src/SparkFun_MMC5983MA_IO.cpp index 2cdc179..7638ae0 100644 --- a/src/SparkFun_MMC5983MA_IO.cpp +++ b/src/SparkFun_MMC5983MA_IO.cpp @@ -25,6 +25,13 @@ bool SFE_MMC5983MA_IO::begin(TwoWire &i2cPort) return isConnected(); } +void SFE_MMC5983MA_IO::initSPISettings() +{ + // CPOL = 1, CPHA = 1 : SPI Mode 3 according to datasheet + // In practice SPI_MODE0 is what worked. + _mmcSpiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0); +} + bool SFE_MMC5983MA_IO::begin(const uint8_t csPin, SPIClass &spiPort) { useSPI = true; @@ -32,6 +39,22 @@ bool SFE_MMC5983MA_IO::begin(const uint8_t csPin, SPIClass &spiPort) digitalWrite(_csPin, HIGH); pinMode(_csPin, OUTPUT); _spiPort = &spiPort; + + initSPISettings(); + + return isConnected(); +} + +bool SFE_MMC5983MA_IO::begin(const uint8_t csPin, SPISettings userSettings, SPIClass &spiPort) +{ + useSPI = true; + _csPin = csPin; + digitalWrite(_csPin, HIGH); + pinMode(_csPin, OUTPUT); + _spiPort = &spiPort; + + _mmcSpiSettings = userSettings; + return isConnected(); } @@ -40,10 +63,12 @@ bool SFE_MMC5983MA_IO::isConnected() bool result = false; if (useSPI) { + _spiPort->beginTransaction(_mmcSpiSettings); digitalWrite(_csPin, LOW); _spiPort->transfer(READ_REG(PROD_ID_REG)); uint8_t readback = _spiPort->transfer(DUMMY); digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); result = (readback == PROD_ID); } else @@ -59,10 +84,12 @@ void SFE_MMC5983MA_IO::writeMultipleBytes(const uint8_t registerAddress, uint8_t { if (useSPI) { + _spiPort->beginTransaction(_mmcSpiSettings); digitalWrite(_csPin, LOW); _spiPort->transfer(registerAddress); _spiPort->transfer(buffer, packetLength); digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); } else { @@ -79,10 +106,12 @@ void SFE_MMC5983MA_IO::readMultipleBytes(const uint8_t registerAddress, uint8_t { if (useSPI) { + _spiPort->beginTransaction(_mmcSpiSettings); digitalWrite(_csPin, LOW); _spiPort->transfer(READ_REG(registerAddress)); _spiPort->transfer(buffer, packetLength); digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); } else { @@ -101,10 +130,12 @@ uint8_t SFE_MMC5983MA_IO::readSingleByte(const uint8_t registerAddress) uint8_t result = 0; if (useSPI) { + _spiPort->beginTransaction(_mmcSpiSettings); digitalWrite(_csPin, LOW); _spiPort->transfer(READ_REG(registerAddress)); result = _spiPort->transfer(DUMMY); digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); } else { @@ -121,10 +152,12 @@ void SFE_MMC5983MA_IO::writeSingleByte(const uint8_t registerAddress, const uint { if (useSPI) { + _spiPort->beginTransaction(_mmcSpiSettings); digitalWrite(_csPin, LOW); _spiPort->transfer(registerAddress); _spiPort->transfer(value); digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); } else { @@ -158,4 +191,4 @@ bool SFE_MMC5983MA_IO::isBitSet(const uint8_t registerAddress, const uint8_t bit bool SFE_MMC5983MA_IO::spiInUse() { return useSPI; -} \ No newline at end of file +} diff --git a/src/SparkFun_MMC5983MA_IO.h b/src/SparkFun_MMC5983MA_IO.h index 0f51bdf..c32bacd 100644 --- a/src/SparkFun_MMC5983MA_IO.h +++ b/src/SparkFun_MMC5983MA_IO.h @@ -22,51 +22,59 @@ class SFE_MMC5983MA_IO { private: - SPIClass* _spiPort = nullptr; - uint8_t _csPin = 0; - TwoWire *_i2cPort = nullptr; - uint8_t _address = 0; - bool useSPI = false; + SPIClass *_spiPort = nullptr; + uint8_t _csPin = 0; + SPISettings _mmcSpiSettings; + + TwoWire *_i2cPort = nullptr; + uint8_t _address = 0; + bool useSPI = false; public: - // Default empty constructor. - SFE_MMC5983MA_IO() = default; + // Default empty constructor. + SFE_MMC5983MA_IO() = default; + + // Default empty destructor + ~SFE_MMC5983MA_IO() = default; + + // Builds default SPI settings if none are provided. + void initSPISettings(); - // Default empty destructor - ~SFE_MMC5983MA_IO() = default; + // Configures and starts the I2C I/O layer. + bool begin(TwoWire &wirePort); - // Configures and starts the I2C I/O layer. - bool begin(TwoWire &wirePort); + // Configures and starts the SPI I/O layer. + bool begin(const uint8_t csPin, SPIClass &spiPort = SPI); - // Configures and starts the SPI I/O layer. - bool begin(const uint8_t csPin, SPIClass& spiPort = SPI); + // Configures the SPI I/O layer with the given chip select and SPI settings provided by the user. + bool begin(const uint8_t csPin, SPISettings userSettings, SPIClass &spiPort = SPI); - // Returns true if we get the correct product ID from the device. - bool isConnected(); + // Returns true if we get the correct product ID from the device. + bool isConnected(); - // Read a single uint8_t from a register. - uint8_t readSingleByte(const uint8_t registerAddress); + // Read a single uint8_t from a register. + uint8_t readSingleByte(const uint8_t registerAddress); - // Writes a single uint8_t into a register. - void writeSingleByte(const uint8_t registerAddress, const uint8_t value); + // Writes a single uint8_t into a register. + void writeSingleByte(const uint8_t registerAddress, const uint8_t value); - // Reads multiple bytes from a register into buffer uint8_t array. - void readMultipleBytes(const uint8_t registerAddress, uint8_t* const buffer , const uint8_t packetLength); + // Reads multiple bytes from a register into buffer uint8_t array. + void readMultipleBytes(const uint8_t registerAddress, uint8_t *const buffer, const uint8_t packetLength); - // Writes multiple bytes to register from buffer uint8_t array. - void writeMultipleBytes(const uint8_t registerAddress, uint8_t* const buffer, const uint8_t packetLength); + // Writes multiple bytes to register from buffer uint8_t array. + void writeMultipleBytes(const uint8_t registerAddress, uint8_t *const buffer, const uint8_t packetLength); - // Sets a single bit in a specific register. Bit position ranges from 0 (lsb) to 7 (msb). - void setRegisterBit(const uint8_t registerAddress, const uint8_t bitMask); + // Sets a single bit in a specific register. Bit position ranges from 0 (lsb) to 7 (msb). + void setRegisterBit(const uint8_t registerAddress, const uint8_t bitMask); - // Clears a single bit in a specific register. Bit position ranges from 0 (lsb) to 7 (msb). - void clearRegisterBit(const uint8_t registerAddress, const uint8_t bitMask); + // Clears a single bit in a specific register. Bit position ranges from 0 (lsb) to 7 (msb). + void clearRegisterBit(const uint8_t registerAddress, const uint8_t bitMask); - // Returns true if a specific bit is set in a register. Bit position ranges from 0 (lsb) to 7 (msb). - bool isBitSet(const uint8_t registerAddress, const uint8_t bitMask); + // Returns true if a specific bit is set in a register. Bit position ranges from 0 (lsb) to 7 (msb). + bool isBitSet(const uint8_t registerAddress, const uint8_t bitMask); - // Returns true if the interface in use is SPI - bool spiInUse(); + // Returns true if the interface in use is SPI + bool spiInUse(); }; -#endif \ No newline at end of file +#endif