244 lines
9.3 KiB
C
244 lines
9.3 KiB
C
|
#ifndef MISSION_DEVICEHANDLING_MGMLIS3MDLHANDLER_H_
|
||
|
#define MISSION_DEVICEHANDLING_MGMLIS3MDLHANDLER_H_
|
||
|
|
||
|
#include <fsfw/devicehandlers/DeviceHandlerBase.h>
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief Device handler object for the LIS3MDL 3-axis magnetometer
|
||
|
* by STMicroeletronics
|
||
|
* @details
|
||
|
* Datasheet can be found online by googling LIS3MDL.
|
||
|
* @author L. Loidold, R. Mueller
|
||
|
*/
|
||
|
class MGMHandlerLIS3MDL: public DeviceHandlerBase {
|
||
|
public:
|
||
|
|
||
|
enum set {
|
||
|
ON, OFF
|
||
|
};
|
||
|
enum opMode {
|
||
|
LOW, MEDIUM, HIGH, ULTRA
|
||
|
};
|
||
|
|
||
|
static const uint8_t INTERFACE_ID = CLASS_ID::MGM_LIS3MDL;
|
||
|
|
||
|
|
||
|
static const DeviceCommandId_t SETUP_MGM = 0x00;
|
||
|
static const DeviceCommandId_t READALL_MGM = 0x01;
|
||
|
static const DeviceCommandId_t IDENTIFY_DEVICE = 0x02;
|
||
|
static const DeviceCommandId_t TEMP_SENSOR_ENABLE = 0x03;
|
||
|
static const DeviceCommandId_t ACCURACY_OP_MODE_SET = 0x04;
|
||
|
|
||
|
// This Command is needed to use for lastSentCommand, if there is no
|
||
|
// command set, you have to set this.
|
||
|
static const DeviceCommandId_t NO_COMMAND = 0xffffffff;
|
||
|
|
||
|
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::MGM_LIS3MDL;
|
||
|
//Notifies a command to change the setup parameters
|
||
|
static const Event CHANGE_OF_SETUP_PARAMETER = MAKE_EVENT(0, SEVERITY::LOW);
|
||
|
|
||
|
MGMHandlerLIS3MDL(uint32_t objectId, object_id_t deviceCommunication,
|
||
|
CookieIF* comCookie);
|
||
|
virtual ~MGMHandlerLIS3MDL();
|
||
|
|
||
|
protected:
|
||
|
|
||
|
/** DeviceHandlerBase overrides */
|
||
|
virtual void doShutDown();
|
||
|
virtual void doStartUp();
|
||
|
virtual void doTransition(Mode_t modeFrom, Submode_t subModeFrom);
|
||
|
virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to);
|
||
|
virtual ReturnValue_t buildCommandFromCommand(
|
||
|
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
|
||
|
size_t commandDataLen);
|
||
|
virtual ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id);
|
||
|
virtual ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id);
|
||
|
virtual ReturnValue_t scanForReply(const uint8_t *start, uint32_t len,
|
||
|
DeviceCommandId_t *foundId, uint32_t *foundLen);
|
||
|
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id,
|
||
|
const uint8_t *packet);
|
||
|
virtual void fillCommandAndReplyMap();
|
||
|
virtual void modeChanged(void);
|
||
|
void setNormalDatapoolEntriesInvalid();
|
||
|
|
||
|
private:
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/* Device specific commands and variables */
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/**
|
||
|
* Sets the read bit for the command
|
||
|
* @param single command to set the read-bit at
|
||
|
* @param boolean to select a continuous read bit, default = false
|
||
|
*/
|
||
|
uint8_t readCommand(uint8_t command, bool continuousCom = false);
|
||
|
|
||
|
/**
|
||
|
* Sets the write bit for the command
|
||
|
* @param single command to set the write-bit at
|
||
|
* @param boolean to select a continuous write bit, default = false
|
||
|
*/
|
||
|
uint8_t writeCommand(uint8_t command, bool continuousCom = false);
|
||
|
|
||
|
/**
|
||
|
* This Method gets the full scale for the measurement range
|
||
|
* e.g.: +- 4 gauss
|
||
|
*
|
||
|
* @return The ReturnValue does not contain the sign of the value
|
||
|
*/
|
||
|
uint8_t getFullScale(uint8_t *reg2);
|
||
|
|
||
|
/**
|
||
|
* after detecting the fullScale the 16bit Value for the data is
|
||
|
* devided with the fullScale to the sensitivity of the scale
|
||
|
*
|
||
|
* @param scale is the return value of the getFulscale Method
|
||
|
*/
|
||
|
float getSensitivity(uint8_t scale);
|
||
|
|
||
|
/**
|
||
|
* This Command detects the device ID
|
||
|
*/
|
||
|
ReturnValue_t identifyDevice();
|
||
|
|
||
|
virtual ReturnValue_t setupMGM();
|
||
|
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/* Non normal commands */
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/**
|
||
|
* Enables/Disables the integrated Temperaturesensor
|
||
|
* @param commandData On or Off
|
||
|
* @param length of the commandData: has to be 1
|
||
|
*/
|
||
|
virtual ReturnValue_t enableTemperatureSensor(const uint8_t *commandData,
|
||
|
size_t commandDataLen);
|
||
|
|
||
|
/**
|
||
|
* Sets the accuracy of the measurement of the axis. The noise is changing.
|
||
|
* @param commandData LOW, MEDIUM, HIGH, ULTRA
|
||
|
* @param length of the command, has to be 1
|
||
|
*/
|
||
|
virtual ReturnValue_t setOperatingMode(const uint8_t *commandData,
|
||
|
size_t commandDataLen);
|
||
|
|
||
|
static const uint8_t NR_OF_CTRL_REGISTERS = 5; //Number of all control registers
|
||
|
static const uint8_t NR_OF_REGISTERS = 19; //Number of registers in the MGM
|
||
|
static const uint8_t TOTAL_NR_OF_ADRESSES = 52; //Total number of adresses for all registers
|
||
|
static const uint8_t SETUP_REPLY = 6;
|
||
|
static const uint8_t SINGLE_COMMAND_ANSWER_LEN = 2; //Length a sindgle command SPI answer
|
||
|
//static const uint8_t LIS3MDL_IDENTIFIER = 0b00111101; //Identifier for Device
|
||
|
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/* Register adresses */
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
// Register adress returns identifier of device with default 0b00111101
|
||
|
static const uint8_t IDENTIFYDEVICE = 0b00001111;
|
||
|
static const uint8_t DEVICEID = 0b00111101; // Identifier for Device
|
||
|
|
||
|
//Register adress to access register 1
|
||
|
static const uint8_t CTRL_REG1 = 0b00100000;
|
||
|
//Register adress to access register 2
|
||
|
static const uint8_t CTRL_REG2 = 0b00100001;
|
||
|
//Register adress to access register 3
|
||
|
static const uint8_t CTRL_REG3 = 0b00100010;
|
||
|
//Register adress to access register 4
|
||
|
static const uint8_t CTRL_REG4 = 0b00100011;
|
||
|
//Register adress to access register 5
|
||
|
static const uint8_t CTRL_REG5 = 0b00100100;
|
||
|
|
||
|
//Register adress to access status register
|
||
|
static const uint8_t STATUS_REG = 0b00100111;
|
||
|
|
||
|
//Register adress to access low byte of x-axis
|
||
|
static const uint8_t X_LOWBYTE = 0b00101000;
|
||
|
//Register adress to access high byte of x-axis
|
||
|
static const uint8_t X_HIGHBYTE = 0b00101001;
|
||
|
//Register adress to access low byte of y-axis
|
||
|
static const uint8_t Y_LOWBYTE = 0b00101010;
|
||
|
//Register adress to access high byte of y-axis
|
||
|
static const uint8_t Y_HIGHBYTE = 0b00101011;
|
||
|
//Register adress to access low byte of z-axis
|
||
|
static const uint8_t Z_LOWBYTE = 0b00101100;
|
||
|
//Register adress to access high byte of z-axis
|
||
|
static const uint8_t Z_HIGHBYTE = 0b00101101;
|
||
|
|
||
|
//Register adress to access low byte of temperature sensor
|
||
|
static const uint8_t TEMP_LOWBYTE = 0b00101110;
|
||
|
//Register adress to access high byte of temperature sensor
|
||
|
static const uint8_t TEMP_HIGHBYTE = 0b00101111;
|
||
|
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
/* Initialize Setup Register set bits
|
||
|
/*------------------------------------------------------------------------*/
|
||
|
//general transfer bits
|
||
|
static const uint8_t RW_BIT = 7; //Read=1 / Write=0 Bit
|
||
|
static const uint8_t MS_BIT = 6; //Continous Read/Write Bit, increment adress
|
||
|
|
||
|
//CTRL_REG1 bits
|
||
|
static const uint8_t ST = 0; //Self test enable bit, enabled = 1
|
||
|
static const uint8_t FAST_ODR = 1; //Enable rates higher than 80 Hz enabled = 1
|
||
|
static const uint8_t DO0 = 2; //Output data rate bit 2
|
||
|
static const uint8_t DO1 = 3; //Output data rate bit 3
|
||
|
static const uint8_t DO2 = 4; //Output data rate bit 4
|
||
|
static const uint8_t OM0 = 5; //XY operating mode bit 5
|
||
|
static const uint8_t OM1 = 6; //XY operating mode bit 6
|
||
|
static const uint8_t TEMP_EN = 7; //Temperature sensor enable enabled = 1
|
||
|
|
||
|
//CTRL_REG2 bits
|
||
|
static const uint8_t SOFT_RST = 2; //reset configuration registers and user registers
|
||
|
static const uint8_t REBOOT = 3; //reboot memory content
|
||
|
static const uint8_t FSO = 5; //full-scale selection bit 5
|
||
|
static const uint8_t FS1 = 6; //full-scale selection bit 6
|
||
|
|
||
|
//CTRL_REG3 bits
|
||
|
static const uint8_t MD0 = 0; //Operating mode bit 0
|
||
|
static const uint8_t MD1 = 1; //Operating mode bit 1
|
||
|
static const uint8_t SIM = 2; //SPI serial interface mode selection enabled = 3-wire-mode
|
||
|
static const uint8_t LP = 5; //low-power mode
|
||
|
|
||
|
//CTRL_REG4 bits
|
||
|
static const uint8_t BLE = 1; //big/little endian data selection enabled = MSb at lower adress
|
||
|
static const uint8_t OMZ0 = 2; //Z operating mode bit 2
|
||
|
static const uint8_t OMZ1 = 3; //Z operating mode bit 3
|
||
|
|
||
|
//CTRL_REG5 bits
|
||
|
static const uint8_t BDU = 6; //Block data update
|
||
|
static const uint8_t FAST_READ = 7; //Fast read enabled = 1
|
||
|
|
||
|
//Single SPIcommand has 2 bytes, first for adress, second for content
|
||
|
size_t singleComandSize = 2;
|
||
|
//has the size for all adresses of the lis3mdl + the continous write bit
|
||
|
uint8_t commandBuffer[TOTAL_NR_OF_ADRESSES + 1];
|
||
|
|
||
|
/**
|
||
|
* We want to save the registers we set, so we dont have to read the registers
|
||
|
* when we want to change something
|
||
|
* --> everytime we change set a register we have to save it
|
||
|
*/
|
||
|
uint8_t registers[NR_OF_CTRL_REGISTERS];
|
||
|
|
||
|
/**
|
||
|
* As this is a SPI Device, we get the Answer of the last sent command in
|
||
|
* the next read cycle, so we could check the command for identification.
|
||
|
*/
|
||
|
DeviceCommandId_t lastSentCommand = 0xffffffff;
|
||
|
|
||
|
/**
|
||
|
* We always update all registers together, so this method updates
|
||
|
* the rawpacket and rawpacketLen, so we just manipulate the local
|
||
|
* saved register
|
||
|
*
|
||
|
*/
|
||
|
ReturnValue_t prepareRegisterWrite();
|
||
|
|
||
|
enum InternalState {
|
||
|
STATE_NONE, STATE_FIRST_CONTACT, STATE_SETUP, STATE_CHECK_REGISTERS
|
||
|
};
|
||
|
|
||
|
InternalState internalState = STATE_NONE;
|
||
|
|
||
|
};
|
||
|
|
||
|
#endif /* MISSION_DEVICEHANDLING_MGMLIS3MDLHANDLER_H_ */
|