Skip to content

Commit

Permalink
Merge pull request #95 from oresat/master-adcs-v1.2-mmc5983ma
Browse files Browse the repository at this point in the history
Implementation of MMC5983MA magnetometer driver and fix to pin assignments for magnetorquer outputs.
  • Loading branch information
ThirteenFish authored May 8, 2024
2 parents 076398a + f5589f8 commit 4b3ac5d
Show file tree
Hide file tree
Showing 7 changed files with 739 additions and 85 deletions.
28 changes: 14 additions & 14 deletions boards/ORESAT_ADCS_V1_2/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
#define GPIOB_IMU_INT3 10U
#define GPIOB_MT_X_PHASE 11U
#define GPIOB_IMU_INT1 12U
#define GPIOB_MT_X_PWM 13U
#define GPIOB_MT_Z_PWM 13U
#define GPIOB_MT_Y_PWM 14U
#define GPIOB_MT_Z_PWM 15U
#define GPIOB_MT_X_PWM 15U

//#define GPIOB_IMU_INT2 13U
//#define GPIOB_IMU_INT4 14U
Expand Down Expand Up @@ -402,9 +402,9 @@
PIN_MODE_INPUT(GPIOB_IMU_INT3) | \
PIN_MODE_OUTPUT(GPIOB_MT_X_PHASE) | \
PIN_MODE_INPUT(GPIOB_IMU_INT1) | \
PIN_MODE_ALTERNATE(GPIOB_MT_X_PWM) | \
PIN_MODE_ALTERNATE(GPIOB_MT_Z_PWM) | \
PIN_MODE_ALTERNATE(GPIOB_MT_Y_PWM) | \
PIN_MODE_ALTERNATE(GPIOB_MT_Z_PWM))
PIN_MODE_ALTERNATE(GPIOB_MT_X_PWM))

#define VAL_GPIOB_OTYPER (PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PHASE) | \
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT2) | \
Expand All @@ -419,9 +419,9 @@
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT3) | \
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PHASE) | \
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT1) | \
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PWM) | \
PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PWM) | \
PIN_OTYPE_PUSHPULL(GPIOB_MT_Y_PWM) | \
PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PWM))
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PWM))

#define VAL_GPIOB_OSPEEDR (PIN_OSPEED_HIGH(GPIOB_MT_Z_PHASE) | \
PIN_OSPEED_HIGH(GPIOB_IMU_INT2) | \
Expand All @@ -436,9 +436,9 @@
PIN_OSPEED_HIGH(GPIOB_IMU_INT3) | \
PIN_OSPEED_HIGH(GPIOB_MT_X_PHASE) | \
PIN_OSPEED_HIGH(GPIOB_IMU_INT1) | \
PIN_OSPEED_HIGH(GPIOB_MT_X_PWM) | \
PIN_OSPEED_HIGH(GPIOB_MT_Z_PWM) | \
PIN_OSPEED_HIGH(GPIOB_MT_Y_PWM) | \
PIN_OSPEED_HIGH(GPIOB_MT_Z_PWM))
PIN_OSPEED_HIGH(GPIOB_MT_X_PWM))
#define VAL_GPIOB_PUPDR (PIN_PUPDR_FLOATING(GPIOB_MT_Z_PHASE) | \
PIN_PUPDR_FLOATING(GPIOB_IMU_INT2) | \
PIN_PUPDR_FLOATING(GPIOB_MT_Y_PHASE) | \
Expand All @@ -452,9 +452,9 @@
PIN_PUPDR_FLOATING(GPIOB_IMU_INT3) | \
PIN_PUPDR_FLOATING(GPIOB_MT_X_PHASE) | \
PIN_PUPDR_FLOATING(GPIOB_IMU_INT1) | \
PIN_PUPDR_FLOATING(GPIOB_MT_X_PWM) | \
PIN_PUPDR_FLOATING(GPIOB_MT_Z_PWM) | \
PIN_PUPDR_FLOATING(GPIOB_MT_Y_PWM) | \
PIN_PUPDR_FLOATING(GPIOB_MT_Z_PWM))
PIN_PUPDR_FLOATING(GPIOB_MT_X_PWM))
#define VAL_GPIOB_ODR (PIN_ODR_LOW(GPIOB_MT_Z_PHASE) | \
PIN_ODR_HIGH(GPIOB_IMU_INT2) | \
PIN_ODR_LOW(GPIOB_MT_Y_PHASE) | \
Expand All @@ -468,9 +468,9 @@
PIN_ODR_HIGH(GPIOB_IMU_INT3) | \
PIN_ODR_LOW(GPIOB_MT_X_PHASE) | \
PIN_ODR_HIGH(GPIOB_IMU_INT1) | \
PIN_ODR_LOW(GPIOB_MT_X_PWM) | \
PIN_ODR_LOW(GPIOB_MT_Z_PWM) | \
PIN_ODR_LOW(GPIOB_MT_Y_PWM) | \
PIN_ODR_LOW(GPIOB_MT_Z_PWM))
PIN_ODR_LOW(GPIOB_MT_X_PWM))
#define VAL_GPIOB_AFRL (PIN_AFIO_AF(GPIOB_MT_Z_PHASE, 0U) | \
PIN_AFIO_AF(GPIOB_IMU_INT2, 0U) | \
PIN_AFIO_AF(GPIOB_MT_Y_PHASE, 0U) | \
Expand All @@ -484,9 +484,9 @@
PIN_AFIO_AF(GPIOB_IMU_INT3, 0U) | \
PIN_AFIO_AF(GPIOB_MT_X_PHASE, 0U) | \
PIN_AFIO_AF(GPIOB_IMU_INT1, 0U) | \
PIN_AFIO_AF(GPIOB_MT_X_PWM, 2U) | \
PIN_AFIO_AF(GPIOB_MT_Z_PWM, 2U) | \
PIN_AFIO_AF(GPIOB_MT_Y_PWM, 2U) | \
PIN_AFIO_AF(GPIOB_MT_Z_PWM, 2U))
PIN_AFIO_AF(GPIOB_MT_X_PWM, 2U))

/*
* GPIOC setup:
Expand Down
203 changes: 203 additions & 0 deletions common/include/mmc5983ma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#ifndef COMMON_INCLUDE_MMC5983MA_H_
#define COMMON_INCLUDE_MMC5983MA_H_





#define MMC5983MA_I2C_ADDRESS_READ (0x30)
#define MMC5983MA_I2C_ADDRESS_WRITE (0x30)

#define MMC5983MA_EXPECTED_PRODUCT_CODE 0x30


#define MMC5983MA_XOUT_0 0x00
#define MMC5983MA_STATUS 0x08
#define MMC5983MA_CONTROL_0 0x09
#define MMC5983MA_CONTROL_1 0x10
#define MMC5983MA_CONTROL_2 0x11
#define MMC5983MA_CONTROL_3 0x12
#define MMC5983MA_PRODUCT_ID 0x2F




#define MMC5983MA_STATUS_MEAS_M_DONE (1<<0)

#define MMC5983MA_CONTROL_0_TM_M (1<<0)
#define MMC5983MA_CONTROL_0_SET (1<<3)
#define MMC5983MA_CONTROL_0_RESET (1<<4)

#define MMC5983MA_CONTROL_1_SW_RST_CMD (1<<7)

#define MMC5983MA_CONTROL_2_CM_FREQ_1HZ 0x01




/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/

/**
* @name Configuration options
* @{
*/
/**
* @brief MMC5983MA I2C interface switch.
* @details If set to @p TRUE the support for I2C is included.
* @note The default is @p TRUE.
*/
#if !defined(MMC5983MA_USE_I2C) || defined(__DOXYGEN__)
#define MMC5983MA_USE_I2C TRUE
#endif

/**
* @brief MMC5983MA shared I2C switch.
* @details If set to @p TRUE the device acquires I2C bus ownership
* on each transaction.
* @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
*/
#if !defined(MMC5983MA_SHARED_I2C) || defined(__DOXYGEN__)
#define MMC5983MA_SHARED_I2C FALSE
#endif
/** @} */

/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/

#if MMC5983MA_USE_I2C && !HAL_USE_I2C
#error "MMC5983MA_USE_I2C requires HAL_USE_I2C"
#endif

#if MMC5983MA_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
#error "MMC5983MA_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
#endif

/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/

/**
* @name MMC5983MA data structures and types.
* @{
*/


/**
* @brief Structure representing a MMC5983MA driver.
*/
typedef struct MMC5983MADriver MMC5983MADriver;


/**
* @brief Driver state machine possible states.
*/
typedef enum {
MMC5983MA_UNINIT = 0, /**< Not initialized. */
MMC5983MA_STOP = 1, /**< Stopped. */
MMC5983MA_READY = 2, /**< Ready. */
} mmc5983ma_state_t;

/**
* @brief MMC5983MA configuration structure.
*/
typedef struct {
#if (MMC5983MA_USE_I2C) || defined(__DOXYGEN__)
/**
* @brief I2C driver associated with this MMC5983MA.
*/
I2CDriver *i2cp;
/**
* @brief I2C configuration associated with this MMC5983MA.
*/
const I2CConfig *i2ccfg;
/**
* @brief MMC5983MA Slave Address
*/
//i2caddr_t saddr;
#endif /* MMC5983MA_USE_I2C */
} MMC5983MAConfig;

/**
* @brief @p MMC5983MA specific methods.
*/
#define _mmc5983ma_methods_alone

/**
* @brief @p MMC5983MA specific methods with inherited ones.
*/
#define _mmc5983ma_methods \
_base_object_methods

/**
* @extends BaseObjectVMT
*
* @brief @p MMC5983MA virtual methods table.
*/
struct MMC5983MAVMT {
_mmc5983ma_methods
};


/**
* @brief @p MMC5983MADriver specific data.
*/
#define _mmc5983ma_data \
_base_object_data \
/* Driver state.*/ \
mmc5983ma_state_t state; \
/* Current configuration data.*/ \
const MMC5983MAConfig *config; \
float bridge_offset_estimate_x; \
float bridge_offset_estimate_y; \
float bridge_offset_estimate_z; \
uint32_t read_call_count;

/**
* @brief MMC5983MA Power Monitor class.
*/
struct MMC5983MADriver {
/** @brief Virtual Methods Table.*/
const struct MMC5983MAVMT *vmt;
_mmc5983ma_data
};



typedef struct {
int16_t mx;
int16_t my;
int16_t mz;
} mmc5983ma_data_t;


/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/

#ifdef __cplusplus
extern "C" {
#endif
void mmc5983maObjectInit(MMC5983MADriver *devp);
bool mmc5983maStart(MMC5983MADriver *devp, const MMC5983MAConfig *config);
void mmc5983maStop(MMC5983MADriver *devp);
int32_t mmc5983maRawToMilliGauss(const int16_t raw);
bool mmc5983maReadData(MMC5983MADriver *devp, mmc5983ma_data_t *dest);
bool mmc5983maI2CReadRegister3(I2CDriver *i2cp, const uint8_t i2c_address, const uint8_t reg_number, uint8_t *dest_value);
bool mmc5983maI2CReadRegister4(I2CDriver *i2cp, const uint8_t i2c_address, const uint8_t reg_number, uint16_t *dest_value);
float mmc5983maRawToGauss(const int16_t raw);
#ifdef __cplusplus
}
#endif









#endif /* COMMON_INCLUDE_MMC5983MA_H_ */
18 changes: 9 additions & 9 deletions common/mmc5883ma.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void mmc5883maObjectInit(MMC5883MADriver *devp) {
}

bool mmc5883maSoftReset(MMC5883MADriver *devp) {
chprintf(DEBUG_SD, "Resetting MMC4883...\r\n");
chprintf(DEBUG_SD, "Resetting MMC5883...\r\n");
bool r = mmc5883maI2CWriteRegister2(devp->config->i2cp, MMC5883MA_AD_INTRNLCTRL1, MMC5883MA_INTRNLCTRL1_SW_RST_CMD);
chThdSleepMilliseconds(5);
return(r);
Expand All @@ -249,7 +249,7 @@ bool mmc5883maSoftReset(MMC5883MADriver *devp) {
*
* @api
*/
uint8_t mmc_product_id_readback;
uint8_t mmc5883ma_product_id_readback;

bool mmc5883maStart(MMC5883MADriver *devp, const MMC5883MAConfig *config) {
osalDbgCheck((devp != NULL) && (config != NULL));
Expand All @@ -273,25 +273,25 @@ bool mmc5883maStart(MMC5883MADriver *devp, const MMC5883MAConfig *config) {
bool found_mmc5883ma_flag = false;

//Probe I2C bus to see what's attached
if( ! mmc5883maI2CReadRegister3(devp->config->i2cp, MMC5883MA_I2C_ADDRESS_READ, MMC5883MA_AD_PRDCT_ID_1, &mmc_product_id_readback) ) {
if( ! mmc5883maI2CReadRegister3(devp->config->i2cp, MMC5883MA_I2C_ADDRESS_READ, MMC5883MA_AD_PRDCT_ID_1, &mmc5883ma_product_id_readback) ) {
chprintf(DEBUG_SD, "Failed to read product code from MMC5883MA chip, i2c comm failure\r\n");
} else {
if( mmc_product_id_readback == MMC5883MA_EXPECTED_PRODUCT_CODE ) {
chprintf(DEBUG_SD, "Successfully read product code from MMC5883MA (good), 0x%X\r\n", mmc_product_id_readback);
if( mmc5883ma_product_id_readback == MMC5883MA_EXPECTED_PRODUCT_CODE ) {
chprintf(DEBUG_SD, "Successfully read product code from MMC5883MA (good), 0x%X\r\n", mmc5883ma_product_id_readback);
found_mmc5883ma_flag = true;
} else {
chprintf(DEBUG_SD, "ERROR: unexpected product ID code from MMC5883MA, read 0x%X, expectex 0x%X\r\n", mmc_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
chprintf(DEBUG_SD, "ERROR: unexpected product ID code from MMC5883MA, read 0x%X, expected 0x%X\r\n", mmc5883ma_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
}
}

if( found_mmc5883ma_flag ) {
mmc5883maSoftReset(devp);

if( ! mmc5883maI2CReadRegister2(devp->config->i2cp, MMC5883MA_AD_PRDCT_ID_1, &mmc_product_id_readback) ) {
if( ! mmc5883maI2CReadRegister2(devp->config->i2cp, MMC5883MA_AD_PRDCT_ID_1, &mmc5883ma_product_id_readback) ) {
devp->state = MMC5883MA_STOP;
} else {
chprintf(DEBUG_SD, "Read MMC5883MA product code as 0x%X, expected 0x%X\r\n", mmc_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
if( mmc_product_id_readback != MMC5883MA_EXPECTED_PRODUCT_CODE ) {
chprintf(DEBUG_SD, "Read MMC5883MA product code as 0x%X, expected 0x%X\r\n", mmc5883ma_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
if( mmc5883ma_product_id_readback != MMC5883MA_EXPECTED_PRODUCT_CODE ) {
chprintf(DEBUG_SD, "ERROR: Read incorrect MMC5883MA product code!!!\r\n");
devp->state = MMC5883MA_STOP;
}
Expand Down
Loading

0 comments on commit 4b3ac5d

Please sign in to comment.