Skip to content

Commit 4b3ac5d

Browse files
authored
Merge pull request #95 from oresat/master-adcs-v1.2-mmc5983ma
Implementation of MMC5983MA magnetometer driver and fix to pin assignments for magnetorquer outputs.
2 parents 076398a + f5589f8 commit 4b3ac5d

File tree

7 files changed

+739
-85
lines changed

7 files changed

+739
-85
lines changed

boards/ORESAT_ADCS_V1_2/board.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
#define GPIOB_IMU_INT3 10U
9191
#define GPIOB_MT_X_PHASE 11U
9292
#define GPIOB_IMU_INT1 12U
93-
#define GPIOB_MT_X_PWM 13U
93+
#define GPIOB_MT_Z_PWM 13U
9494
#define GPIOB_MT_Y_PWM 14U
95-
#define GPIOB_MT_Z_PWM 15U
95+
#define GPIOB_MT_X_PWM 15U
9696

9797
//#define GPIOB_IMU_INT2 13U
9898
//#define GPIOB_IMU_INT4 14U
@@ -402,9 +402,9 @@
402402
PIN_MODE_INPUT(GPIOB_IMU_INT3) | \
403403
PIN_MODE_OUTPUT(GPIOB_MT_X_PHASE) | \
404404
PIN_MODE_INPUT(GPIOB_IMU_INT1) | \
405-
PIN_MODE_ALTERNATE(GPIOB_MT_X_PWM) | \
405+
PIN_MODE_ALTERNATE(GPIOB_MT_Z_PWM) | \
406406
PIN_MODE_ALTERNATE(GPIOB_MT_Y_PWM) | \
407-
PIN_MODE_ALTERNATE(GPIOB_MT_Z_PWM))
407+
PIN_MODE_ALTERNATE(GPIOB_MT_X_PWM))
408408

409409
#define VAL_GPIOB_OTYPER (PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PHASE) | \
410410
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT2) | \
@@ -419,9 +419,9 @@
419419
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT3) | \
420420
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PHASE) | \
421421
PIN_OTYPE_PUSHPULL(GPIOB_IMU_INT1) | \
422-
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PWM) | \
422+
PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PWM) | \
423423
PIN_OTYPE_PUSHPULL(GPIOB_MT_Y_PWM) | \
424-
PIN_OTYPE_PUSHPULL(GPIOB_MT_Z_PWM))
424+
PIN_OTYPE_PUSHPULL(GPIOB_MT_X_PWM))
425425

426426
#define VAL_GPIOB_OSPEEDR (PIN_OSPEED_HIGH(GPIOB_MT_Z_PHASE) | \
427427
PIN_OSPEED_HIGH(GPIOB_IMU_INT2) | \
@@ -436,9 +436,9 @@
436436
PIN_OSPEED_HIGH(GPIOB_IMU_INT3) | \
437437
PIN_OSPEED_HIGH(GPIOB_MT_X_PHASE) | \
438438
PIN_OSPEED_HIGH(GPIOB_IMU_INT1) | \
439-
PIN_OSPEED_HIGH(GPIOB_MT_X_PWM) | \
439+
PIN_OSPEED_HIGH(GPIOB_MT_Z_PWM) | \
440440
PIN_OSPEED_HIGH(GPIOB_MT_Y_PWM) | \
441-
PIN_OSPEED_HIGH(GPIOB_MT_Z_PWM))
441+
PIN_OSPEED_HIGH(GPIOB_MT_X_PWM))
442442
#define VAL_GPIOB_PUPDR (PIN_PUPDR_FLOATING(GPIOB_MT_Z_PHASE) | \
443443
PIN_PUPDR_FLOATING(GPIOB_IMU_INT2) | \
444444
PIN_PUPDR_FLOATING(GPIOB_MT_Y_PHASE) | \
@@ -452,9 +452,9 @@
452452
PIN_PUPDR_FLOATING(GPIOB_IMU_INT3) | \
453453
PIN_PUPDR_FLOATING(GPIOB_MT_X_PHASE) | \
454454
PIN_PUPDR_FLOATING(GPIOB_IMU_INT1) | \
455-
PIN_PUPDR_FLOATING(GPIOB_MT_X_PWM) | \
455+
PIN_PUPDR_FLOATING(GPIOB_MT_Z_PWM) | \
456456
PIN_PUPDR_FLOATING(GPIOB_MT_Y_PWM) | \
457-
PIN_PUPDR_FLOATING(GPIOB_MT_Z_PWM))
457+
PIN_PUPDR_FLOATING(GPIOB_MT_X_PWM))
458458
#define VAL_GPIOB_ODR (PIN_ODR_LOW(GPIOB_MT_Z_PHASE) | \
459459
PIN_ODR_HIGH(GPIOB_IMU_INT2) | \
460460
PIN_ODR_LOW(GPIOB_MT_Y_PHASE) | \
@@ -468,9 +468,9 @@
468468
PIN_ODR_HIGH(GPIOB_IMU_INT3) | \
469469
PIN_ODR_LOW(GPIOB_MT_X_PHASE) | \
470470
PIN_ODR_HIGH(GPIOB_IMU_INT1) | \
471-
PIN_ODR_LOW(GPIOB_MT_X_PWM) | \
471+
PIN_ODR_LOW(GPIOB_MT_Z_PWM) | \
472472
PIN_ODR_LOW(GPIOB_MT_Y_PWM) | \
473-
PIN_ODR_LOW(GPIOB_MT_Z_PWM))
473+
PIN_ODR_LOW(GPIOB_MT_X_PWM))
474474
#define VAL_GPIOB_AFRL (PIN_AFIO_AF(GPIOB_MT_Z_PHASE, 0U) | \
475475
PIN_AFIO_AF(GPIOB_IMU_INT2, 0U) | \
476476
PIN_AFIO_AF(GPIOB_MT_Y_PHASE, 0U) | \
@@ -484,9 +484,9 @@
484484
PIN_AFIO_AF(GPIOB_IMU_INT3, 0U) | \
485485
PIN_AFIO_AF(GPIOB_MT_X_PHASE, 0U) | \
486486
PIN_AFIO_AF(GPIOB_IMU_INT1, 0U) | \
487-
PIN_AFIO_AF(GPIOB_MT_X_PWM, 2U) | \
487+
PIN_AFIO_AF(GPIOB_MT_Z_PWM, 2U) | \
488488
PIN_AFIO_AF(GPIOB_MT_Y_PWM, 2U) | \
489-
PIN_AFIO_AF(GPIOB_MT_Z_PWM, 2U))
489+
PIN_AFIO_AF(GPIOB_MT_X_PWM, 2U))
490490

491491
/*
492492
* GPIOC setup:

common/include/mmc5983ma.h

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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_ */

common/mmc5883ma.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void mmc5883maObjectInit(MMC5883MADriver *devp) {
235235
}
236236

237237
bool mmc5883maSoftReset(MMC5883MADriver *devp) {
238-
chprintf(DEBUG_SD, "Resetting MMC4883...\r\n");
238+
chprintf(DEBUG_SD, "Resetting MMC5883...\r\n");
239239
bool r = mmc5883maI2CWriteRegister2(devp->config->i2cp, MMC5883MA_AD_INTRNLCTRL1, MMC5883MA_INTRNLCTRL1_SW_RST_CMD);
240240
chThdSleepMilliseconds(5);
241241
return(r);
@@ -249,7 +249,7 @@ bool mmc5883maSoftReset(MMC5883MADriver *devp) {
249249
*
250250
* @api
251251
*/
252-
uint8_t mmc_product_id_readback;
252+
uint8_t mmc5883ma_product_id_readback;
253253

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

275275
//Probe I2C bus to see what's attached
276-
if( ! mmc5883maI2CReadRegister3(devp->config->i2cp, MMC5883MA_I2C_ADDRESS_READ, MMC5883MA_AD_PRDCT_ID_1, &mmc_product_id_readback) ) {
276+
if( ! mmc5883maI2CReadRegister3(devp->config->i2cp, MMC5883MA_I2C_ADDRESS_READ, MMC5883MA_AD_PRDCT_ID_1, &mmc5883ma_product_id_readback) ) {
277277
chprintf(DEBUG_SD, "Failed to read product code from MMC5883MA chip, i2c comm failure\r\n");
278278
} else {
279-
if( mmc_product_id_readback == MMC5883MA_EXPECTED_PRODUCT_CODE ) {
280-
chprintf(DEBUG_SD, "Successfully read product code from MMC5883MA (good), 0x%X\r\n", mmc_product_id_readback);
279+
if( mmc5883ma_product_id_readback == MMC5883MA_EXPECTED_PRODUCT_CODE ) {
280+
chprintf(DEBUG_SD, "Successfully read product code from MMC5883MA (good), 0x%X\r\n", mmc5883ma_product_id_readback);
281281
found_mmc5883ma_flag = true;
282282
} else {
283-
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);
283+
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);
284284
}
285285
}
286286

287287
if( found_mmc5883ma_flag ) {
288288
mmc5883maSoftReset(devp);
289289

290-
if( ! mmc5883maI2CReadRegister2(devp->config->i2cp, MMC5883MA_AD_PRDCT_ID_1, &mmc_product_id_readback) ) {
290+
if( ! mmc5883maI2CReadRegister2(devp->config->i2cp, MMC5883MA_AD_PRDCT_ID_1, &mmc5883ma_product_id_readback) ) {
291291
devp->state = MMC5883MA_STOP;
292292
} else {
293-
chprintf(DEBUG_SD, "Read MMC5883MA product code as 0x%X, expected 0x%X\r\n", mmc_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
294-
if( mmc_product_id_readback != MMC5883MA_EXPECTED_PRODUCT_CODE ) {
293+
chprintf(DEBUG_SD, "Read MMC5883MA product code as 0x%X, expected 0x%X\r\n", mmc5883ma_product_id_readback, MMC5883MA_EXPECTED_PRODUCT_CODE);
294+
if( mmc5883ma_product_id_readback != MMC5883MA_EXPECTED_PRODUCT_CODE ) {
295295
chprintf(DEBUG_SD, "ERROR: Read incorrect MMC5883MA product code!!!\r\n");
296296
devp->state = MMC5883MA_STOP;
297297
}

0 commit comments

Comments
 (0)