|
| 1 | +#ifndef COMMON_INCLUDE_MMC5983MA_H_ |
| 2 | +#define COMMON_INCLUDE_MMC5983MA_H_ |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +#define MMC5983MA_I2C_ADDRESS_READ (0x30) |
| 9 | +#define MMC5983MA_I2C_ADDRESS_WRITE (0x30) |
| 10 | + |
| 11 | +#define MMC5983MA_EXPECTED_PRODUCT_CODE 0x30 |
| 12 | + |
| 13 | + |
| 14 | +#define MMC5983MA_XOUT_0 0x00 |
| 15 | +#define MMC5983MA_STATUS 0x08 |
| 16 | +#define MMC5983MA_CONTROL_0 0x09 |
| 17 | +#define MMC5983MA_CONTROL_1 0x10 |
| 18 | +#define MMC5983MA_CONTROL_2 0x11 |
| 19 | +#define MMC5983MA_CONTROL_3 0x12 |
| 20 | +#define MMC5983MA_PRODUCT_ID 0x2F |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +#define MMC5983MA_STATUS_MEAS_M_DONE (1<<0) |
| 26 | + |
| 27 | +#define MMC5983MA_CONTROL_0_TM_M (1<<0) |
| 28 | +#define MMC5983MA_CONTROL_0_SET (1<<3) |
| 29 | +#define MMC5983MA_CONTROL_0_RESET (1<<4) |
| 30 | + |
| 31 | +#define MMC5983MA_CONTROL_1_SW_RST_CMD (1<<7) |
| 32 | + |
| 33 | +#define MMC5983MA_CONTROL_2_CM_FREQ_1HZ 0x01 |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +/*===========================================================================*/ |
| 39 | +/* Driver pre-compile time settings. */ |
| 40 | +/*===========================================================================*/ |
| 41 | + |
| 42 | +/** |
| 43 | + * @name Configuration options |
| 44 | + * @{ |
| 45 | + */ |
| 46 | +/** |
| 47 | + * @brief MMC5983MA I2C interface switch. |
| 48 | + * @details If set to @p TRUE the support for I2C is included. |
| 49 | + * @note The default is @p TRUE. |
| 50 | + */ |
| 51 | +#if !defined(MMC5983MA_USE_I2C) || defined(__DOXYGEN__) |
| 52 | +#define MMC5983MA_USE_I2C TRUE |
| 53 | +#endif |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief MMC5983MA shared I2C switch. |
| 57 | + * @details If set to @p TRUE the device acquires I2C bus ownership |
| 58 | + * on each transaction. |
| 59 | + * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION. |
| 60 | + */ |
| 61 | +#if !defined(MMC5983MA_SHARED_I2C) || defined(__DOXYGEN__) |
| 62 | +#define MMC5983MA_SHARED_I2C FALSE |
| 63 | +#endif |
| 64 | +/** @} */ |
| 65 | + |
| 66 | +/*===========================================================================*/ |
| 67 | +/* Derived constants and error checks. */ |
| 68 | +/*===========================================================================*/ |
| 69 | + |
| 70 | +#if MMC5983MA_USE_I2C && !HAL_USE_I2C |
| 71 | +#error "MMC5983MA_USE_I2C requires HAL_USE_I2C" |
| 72 | +#endif |
| 73 | + |
| 74 | +#if MMC5983MA_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION |
| 75 | +#error "MMC5983MA_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION" |
| 76 | +#endif |
| 77 | + |
| 78 | +/*===========================================================================*/ |
| 79 | +/* Driver data structures and types. */ |
| 80 | +/*===========================================================================*/ |
| 81 | + |
| 82 | +/** |
| 83 | + * @name MMC5983MA data structures and types. |
| 84 | + * @{ |
| 85 | + */ |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Structure representing a MMC5983MA driver. |
| 90 | + */ |
| 91 | +typedef struct MMC5983MADriver MMC5983MADriver; |
| 92 | + |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief Driver state machine possible states. |
| 96 | + */ |
| 97 | +typedef enum { |
| 98 | + MMC5983MA_UNINIT = 0, /**< Not initialized. */ |
| 99 | + MMC5983MA_STOP = 1, /**< Stopped. */ |
| 100 | + MMC5983MA_READY = 2, /**< Ready. */ |
| 101 | +} mmc5983ma_state_t; |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief MMC5983MA configuration structure. |
| 105 | + */ |
| 106 | +typedef struct { |
| 107 | +#if (MMC5983MA_USE_I2C) || defined(__DOXYGEN__) |
| 108 | + /** |
| 109 | + * @brief I2C driver associated with this MMC5983MA. |
| 110 | + */ |
| 111 | + I2CDriver *i2cp; |
| 112 | + /** |
| 113 | + * @brief I2C configuration associated with this MMC5983MA. |
| 114 | + */ |
| 115 | + const I2CConfig *i2ccfg; |
| 116 | + /** |
| 117 | + * @brief MMC5983MA Slave Address |
| 118 | + */ |
| 119 | + //i2caddr_t saddr; |
| 120 | +#endif /* MMC5983MA_USE_I2C */ |
| 121 | +} MMC5983MAConfig; |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief @p MMC5983MA specific methods. |
| 125 | + */ |
| 126 | +#define _mmc5983ma_methods_alone |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief @p MMC5983MA specific methods with inherited ones. |
| 130 | + */ |
| 131 | +#define _mmc5983ma_methods \ |
| 132 | + _base_object_methods |
| 133 | + |
| 134 | +/** |
| 135 | + * @extends BaseObjectVMT |
| 136 | + * |
| 137 | + * @brief @p MMC5983MA virtual methods table. |
| 138 | + */ |
| 139 | +struct MMC5983MAVMT { |
| 140 | + _mmc5983ma_methods |
| 141 | +}; |
| 142 | + |
| 143 | + |
| 144 | +/** |
| 145 | + * @brief @p MMC5983MADriver specific data. |
| 146 | + */ |
| 147 | +#define _mmc5983ma_data \ |
| 148 | + _base_object_data \ |
| 149 | + /* Driver state.*/ \ |
| 150 | + mmc5983ma_state_t state; \ |
| 151 | + /* Current configuration data.*/ \ |
| 152 | + const MMC5983MAConfig *config; \ |
| 153 | + float bridge_offset_estimate_x; \ |
| 154 | + float bridge_offset_estimate_y; \ |
| 155 | + float bridge_offset_estimate_z; \ |
| 156 | + uint32_t read_call_count; |
| 157 | + |
| 158 | +/** |
| 159 | + * @brief MMC5983MA Power Monitor class. |
| 160 | + */ |
| 161 | +struct MMC5983MADriver { |
| 162 | + /** @brief Virtual Methods Table.*/ |
| 163 | + const struct MMC5983MAVMT *vmt; |
| 164 | + _mmc5983ma_data |
| 165 | +}; |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +typedef struct { |
| 170 | + int16_t mx; |
| 171 | + int16_t my; |
| 172 | + int16_t mz; |
| 173 | +} mmc5983ma_data_t; |
| 174 | + |
| 175 | + |
| 176 | +/*===========================================================================*/ |
| 177 | +/* External declarations. */ |
| 178 | +/*===========================================================================*/ |
| 179 | + |
| 180 | +#ifdef __cplusplus |
| 181 | +extern "C" { |
| 182 | +#endif |
| 183 | +void mmc5983maObjectInit(MMC5983MADriver *devp); |
| 184 | +bool mmc5983maStart(MMC5983MADriver *devp, const MMC5983MAConfig *config); |
| 185 | +void mmc5983maStop(MMC5983MADriver *devp); |
| 186 | +int32_t mmc5983maRawToMilliGauss(const int16_t raw); |
| 187 | +bool mmc5983maReadData(MMC5983MADriver *devp, mmc5983ma_data_t *dest); |
| 188 | +bool mmc5983maI2CReadRegister3(I2CDriver *i2cp, const uint8_t i2c_address, const uint8_t reg_number, uint8_t *dest_value); |
| 189 | +bool mmc5983maI2CReadRegister4(I2CDriver *i2cp, const uint8_t i2c_address, const uint8_t reg_number, uint16_t *dest_value); |
| 190 | +float mmc5983maRawToGauss(const int16_t raw); |
| 191 | +#ifdef __cplusplus |
| 192 | +} |
| 193 | +#endif |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +#endif /* COMMON_INCLUDE_MMC5983MA_H_ */ |
0 commit comments