GY-63_MS5611/libraries/hmc6352/hmc6352.cpp

401 lines
7.7 KiB
C++
Raw Normal View History

//
2013-09-30 11:00:55 -04:00
// FILE: hmc6352.cpp
// AUTHOR: Rob Tillaart
2022-11-09 08:34:02 -05:00
// VERSION: 0.3.2
2020-11-27 05:16:22 -05:00
// PURPOSE: Arduino library for HMC6352 digital compass sensor
2013-09-30 11:00:55 -04:00
//
2022-11-09 08:34:02 -05:00
// HISTORY: see changelog.md
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
2020-11-27 05:16:22 -05:00
#include "hmc6352.h"
2013-09-30 11:00:55 -04:00
2021-06-07 12:21:42 -04:00
2020-11-27 05:16:22 -05:00
#define HMC_GET_DATA 0x41
#define HMC_WAKE 0x57
#define HMC_SLEEP 0x53
#define HMC_SAVE_OP_MODE 0x4C
#define HMC_CALLIBRATE_ON 0x43
#define HMC_CALLIBRATE_OFF 0x45
#define HMC_UPDATE_OFFSETS 0x4F
#define HMC_WRITE_RAM 0x47
#define HMC_READ_RAM 0x67
#define HMC_WRITE_EEPROM 0x77
#define HMC_READ_EEPROM 0x72
2017-09-13 03:18:55 -04:00
2021-06-07 12:21:42 -04:00
// ERROR CODES ALL FUNCTIONS
//
// * twi_writeTo codes (== endTransmission commands)
2021-06-07 12:21:42 -04:00
// HMC6532_I2C_OK 0 .. OK
// HMC6532_I2C_ERROR_BUFFEROVERFLOW -1 .. length to long for buffer
// HMC6532_I2C_ERROR_ADDR_NACK -2 .. address send, NACK received
// HMC6532_I2C_ERROR_DATA_NACK -3 .. data send, NACK received
// HMC6532_I2C_ERROR_OTHER -4 ..
// other twi error (lost bus arbitration, bus error, ..)
//
// * requestFrom
2021-06-07 12:21:42 -04:00
// HMC6352_I2C_ERROR_REQ_FROM -10 .. not enough values returned
//
// * function calls
2021-06-07 12:21:42 -04:00
// HMC6532_OK 0
// HMC6352_ERROR_PARAM1 -20
// HMC6352_ERROR_PARAM2 -21
//
2013-09-30 11:00:55 -04:00
2021-06-07 12:21:42 -04:00
hmc6352::hmc6352(uint8_t address, TwoWire *wire)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
_address = constrain(address, 0x10, 0xF6);
_wire = wire;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2020-11-27 05:16:22 -05:00
#if defined (ESP8266) || defined(ESP32)
2021-06-07 12:21:42 -04:00
bool hmc6352::begin(uint8_t sda, uint8_t scl)
2020-11-27 05:16:22 -05:00
{
2021-06-07 12:21:42 -04:00
_wire->begin(sda, scl);
if (! isConnected())
{
return false;
}
return true;
2020-11-27 05:16:22 -05:00
}
#endif
2021-06-07 12:21:42 -04:00
bool hmc6352::begin()
{
_wire->begin();
if (! isConnected())
{
return false;
}
return true;
}
bool hmc6352::isConnected()
2020-11-27 05:16:22 -05:00
{
2021-06-07 12:21:42 -04:00
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
2020-11-27 05:16:22 -05:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:04:25 -04:00
int hmc6352::getHeading()
{
int rv = askHeading();
if (rv != 0) return rv;
return readHeading();
2013-09-30 11:04:25 -04:00
}
2021-06-07 12:21:42 -04:00
// Ask the device to make a new reading
int hmc6352::askHeading()
2013-09-30 11:00:55 -04:00
{
int rv = cmd(HMC_GET_DATA);
if (rv != 0) return -rv; // problem with handshake
2021-06-07 12:21:42 -04:00
yield();
delay(6); // see datasheet, p8
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2020-11-27 05:16:22 -05:00
// read the last value from the sensor
int hmc6352::readHeading()
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
int rv = _wire->requestFrom(_address, (uint8_t)2); // remove ambiguity
if (rv != 2) return HMC6352_I2C_ERROR_REQ_FROM;
rv = _wire->read() * 256; // MSB
rv += _wire->read(); // LSB
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// wake up from energy saving modus
2013-09-30 11:00:55 -04:00
int hmc6352::wakeUp()
{
int rv = cmd(HMC_WAKE);
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(100);
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// go into energy saving modus
2013-09-30 11:00:55 -04:00
int hmc6352::sleep()
{
int rv = cmd(HMC_SLEEP);
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(10);
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
// values obtained from dump
int hmc6352::factoryReset()
{
writeRAM(0x74, 0x50); // is needed !!
writeCmd(HMC_WRITE_EEPROM, 0, 66);
writeCmd(HMC_WRITE_EEPROM, 1, 0);
writeCmd(HMC_WRITE_EEPROM, 2, 0);
writeCmd(HMC_WRITE_EEPROM, 3, 0);
writeCmd(HMC_WRITE_EEPROM, 4, 0);
writeCmd(HMC_WRITE_EEPROM, 5, 1);
writeCmd(HMC_WRITE_EEPROM, 6, 4);
writeCmd(HMC_WRITE_EEPROM, 7, 6);
writeCmd(HMC_WRITE_EEPROM, 8, 0x50);
cmd(HMC_SAVE_OP_MODE);
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(125);
2021-06-07 12:21:42 -04:00
return HMC6532_OK;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
// HANDLE WITH CARE - RESTART NECESSARY
2021-06-07 12:21:42 -04:00
// mode = 0, 1, 2
// frequemcy = 1, 5, 10, 20 Hz.
// periodicReset =
// Returns Operational Mode Control Byte
int hmc6352::setOperationalModus(hmcMode mode, uint8_t frequency, bool periodicReset)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
// Operational Mode Control Byte
// bit 2,3,7 are allways 0
byte omcb = 0;
switch(frequency)
{
2020-11-27 05:16:22 -05:00
case 1: break;
case 5: omcb |= 0x20; break;
case 10: omcb |= 0x40; break;
case 20: omcb |= 0x60; break;
2021-06-07 12:21:42 -04:00
default: return HMC6352_ERROR_PARAM2;
}
if (periodicReset) omcb |= 0x10;
2021-06-07 12:21:42 -04:00
switch(mode)
{
2020-11-27 05:16:22 -05:00
case STANDBY: omcb |= 0x00; break;
case QUERY: omcb |= 0x01; break;
case CONT: omcb |= 0x02; break;
2021-06-07 12:21:42 -04:00
default: return HMC6352_ERROR_PARAM1;
}
2020-11-27 05:16:22 -05:00
saveOpMode(omcb);
return omcb;
}
2013-09-30 11:00:55 -04:00
2021-06-07 12:21:42 -04:00
// read the Operational Modus as byte from EEPROM
// TODO: split into 3 items
//
2013-09-30 11:00:55 -04:00
int hmc6352::getOperationalModus()
{
// datasheet state that at startup the OM is copied from EEPROM
// and that after writing to RAM a reboot is needed to enable new settings
// my interpretation ==> EEPROM is leading
2021-06-07 12:21:42 -04:00
int operationalMode = readCmd(HMC_READ_RAM, 0x74);
// int operationalMode = readCmd(HMC_READ_EEPROM, 0x08);
return operationalMode;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// Switch between normal heading and raw modes
// default = 0 ==> normal headings
// Note: after a reboot the output modus will be 0 again.
2021-06-07 12:21:42 -04:00
// as it is only stored in RAM
// 0 = HEADING
int hmc6352::setOutputModus(uint8_t outputModus)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
if (outputModus > 4) // 4 = MAGY
{
return HMC6352_ERROR_PARAM1;
}
return writeCmd(HMC_WRITE_RAM, 0x4E, outputModus);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// Read the output modus from RAM
2013-09-30 11:00:55 -04:00
int hmc6352::getOutputModus()
{
return readCmd(HMC_READ_RAM, 0x4E);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
int hmc6352::callibrationOn()
{
int rv = cmd(HMC_CALLIBRATE_ON);
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(10);
return rv;
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
int hmc6352::callibrationOff()
{
int rv = cmd(HMC_CALLIBRATE_OFF);
2021-06-07 12:21:42 -04:00
yield();
delay(15);
return rv;
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
int hmc6352::setI2CAddress(uint8_t address)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
if ((address < 0x10) || (address > 0xF6) )
{
return HMC6352_ERROR_PARAM1;
}
return writeCmd(HMC_WRITE_EEPROM, 0, address);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// returns I2C address from EEPROM
int hmc6352::getI2CAddress()
2013-09-30 11:00:55 -04:00
{
return readCmd(HMC_READ_EEPROM, 0);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
2021-06-07 12:21:42 -04:00
int hmc6352::setTimeDelay(uint8_t milliSeconds)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
return writeCmd(HMC_WRITE_EEPROM, 5, milliSeconds);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
int hmc6352::getTimeDelay()
{
return readCmd(HMC_READ_EEPROM, 5);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
2021-06-07 12:21:42 -04:00
// nosm = NumberOfSummedMeasurements
int hmc6352::setMeasurementSumming(uint8_t nosm)
2013-09-30 11:00:55 -04:00
{
2021-06-07 12:21:42 -04:00
uint8_t _nosm = nosm;
if (_nosm < 1) _nosm = 1;
else if (_nosm > 16 ) _nosm = 16;
return writeCmd(HMC_WRITE_EEPROM, 6, _nosm);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
int hmc6352::getMeasurementSumming()
{
return readCmd(HMC_READ_EEPROM, 6);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// NOT TESTED
int hmc6352::getSWVersionNumber()
{
return readCmd(HMC_READ_EEPROM, 7);
}
2020-11-27 05:16:22 -05:00
// used by setOperationalModus()
int hmc6352::saveOpMode(byte OpMode)
2013-09-30 11:00:55 -04:00
{
2020-11-27 05:16:22 -05:00
writeCmd(HMC_WRITE_RAM, 0x74, OpMode);
int rv = cmd(HMC_SAVE_OP_MODE);
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(125);
return rv;
2013-09-30 11:00:55 -04:00
}
// NOT TESTED
// meaning UpdateOffsets unknown
// therefore removed from lib for now
int hmc6352::updateOffsets()
2013-09-30 11:00:55 -04:00
{
int rv = cmd(HMC_UPDATE_OFFSETS);
2021-06-07 12:21:42 -04:00
yield();
delay(6);
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// idem
// use at own risk ...
2013-09-30 11:00:55 -04:00
int hmc6352::writeEEPROM(uint8_t address, uint8_t data)
{
return writeCmd(HMC_WRITE_EEPROM, address, data);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// idem
2013-09-30 11:00:55 -04:00
int hmc6352::readEEPROM(uint8_t address)
{
return readCmd(HMC_READ_EEPROM, address);
2013-09-30 11:00:55 -04:00
}
// idem
// Most RAM locations have an unknown meaning
// use at own risk ...
2013-09-30 11:00:55 -04:00
int hmc6352::writeRAM(uint8_t address, uint8_t data)
{
return writeCmd(HMC_WRITE_RAM, address, data);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// idem
2013-09-30 11:00:55 -04:00
int hmc6352::readRAM(uint8_t address)
{
return readCmd(HMC_READ_RAM, address);
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
///////////////////////////////////////////////////////////
//
// PRIVATE FUNCTIONS
//
2013-09-30 11:00:55 -04:00
int hmc6352::cmd(uint8_t c)
{
2021-06-07 12:21:42 -04:00
_wire->beginTransmission(_address);
_wire->write(c);
int rv = _wire->endTransmission();
yield();
delay(10);
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
int hmc6352::readCmd(uint8_t c, uint8_t address)
{
2021-06-07 12:21:42 -04:00
_wire->beginTransmission(_address);
_wire->write(c);
_wire->write(address);
int rv = _wire->endTransmission();
if (rv != 0) return -rv;
2021-06-07 12:21:42 -04:00
yield();
delayMicroseconds(70);
2021-06-07 12:21:42 -04:00
rv = _wire->requestFrom(_address, (uint8_t)1);
if (rv != 1) return HMC6352_I2C_ERROR_REQ_FROM;
rv = _wire->read();
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
2013-09-30 11:00:55 -04:00
int hmc6352::writeCmd(uint8_t c, uint8_t address, uint8_t data)
{
2021-06-07 12:21:42 -04:00
_wire->beginTransmission(_address);
_wire->write(c);
_wire->write(address);
_wire->write(data);
int rv = _wire->endTransmission();
yield();
delayMicroseconds(70);
return rv;
2013-09-30 11:00:55 -04:00
}
2021-06-07 12:21:42 -04:00
// -- END OF FILE --