Skip to content

Commit 3c97e91

Browse files
Merge pull request #6623 from iNavFlight/de_rm3100_mtk450can
Add MATEKF405CAN board; Add support for RM3100 compass on SPI bus
2 parents ad3dfac + 5bfbf90 commit 3c97e91

File tree

12 files changed

+485
-2
lines changed

12 files changed

+485
-2
lines changed

src/main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ main_sources(COMMON_SRC
154154
drivers/compass/compass_mpu9250.h
155155
drivers/compass/compass_qmc5883l.c
156156
drivers/compass/compass_qmc5883l.h
157+
drivers/compass/compass_rm3100.c
158+
drivers/compass/compass_rm3100.h
157159
drivers/compass/compass_msp.c
158160
drivers/compass/compass_msp.h
159161

src/main/drivers/bus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef enum {
118118
DEVHW_QMC5883,
119119
DEVHW_MAG3110,
120120
DEVHW_LIS3MDL,
121+
DEVHW_RM3100,
121122

122123
/* Temp sensor chips */
123124
DEVHW_LM75_0,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#include <stdbool.h>
26+
#include <stdint.h>
27+
28+
#include <math.h>
29+
30+
#include "platform.h"
31+
32+
#ifdef USE_MAG_RM3100
33+
34+
#include "build/build_config.h"
35+
#include "build/debug.h"
36+
37+
#include "common/axis.h"
38+
#include "common/maths.h"
39+
#include "common/utils.h"
40+
41+
#include "drivers/time.h"
42+
#include "drivers/bus_i2c.h"
43+
44+
#include "sensors/boardalignment.h"
45+
#include "sensors/sensors.h"
46+
47+
#include "drivers/sensor.h"
48+
#include "drivers/compass/compass.h"
49+
50+
#include "drivers/compass/compass_rm3100.h"
51+
52+
#define RM3100_REG_POLL 0x00
53+
#define RM3100_REG_CMM 0x01
54+
#define RM3100_REG_CCX1 0x04
55+
#define RM3100_REG_CCX0 0x05
56+
#define RM3100_REG_CCY1 0x06
57+
#define RM3100_REG_CCY0 0x07
58+
#define RM3100_REG_CCZ1 0x08
59+
#define RM3100_REG_CCZ0 0x09
60+
#define RM3100_REG_TMRC 0x0B
61+
#define RM3100_REG_MX 0x24
62+
#define RM3100_REG_MY 0x27
63+
#define RM3100_REG_MZ 0x2A
64+
#define RM3100_REG_BIST 0x33
65+
#define RM3100_REG_STATUS 0x34
66+
#define RM3100_REG_HSHAKE 0x35
67+
#define RM3100_REG_REVID 0x36
68+
69+
#define RM3100_REVID 0x22
70+
71+
#define CCX_DEFAULT_MSB 0x00
72+
#define CCX_DEFAULT_LSB 0xC8
73+
#define CCY_DEFAULT_MSB CCX_DEFAULT_MSB
74+
#define CCY_DEFAULT_LSB CCX_DEFAULT_LSB
75+
#define CCZ_DEFAULT_MSB CCX_DEFAULT_MSB
76+
#define CCZ_DEFAULT_LSB CCX_DEFAULT_LSB
77+
#define CMM_DEFAULT 0x71 // Continuous mode
78+
#define TMRC_DEFAULT 0x94
79+
80+
81+
static bool deviceInit(magDev_t * mag)
82+
{
83+
busWrite(mag->busDev, RM3100_REG_TMRC, TMRC_DEFAULT);
84+
85+
busWrite(mag->busDev, RM3100_REG_CMM, CMM_DEFAULT);
86+
87+
busWrite(mag->busDev, RM3100_REG_CCX1, CCX_DEFAULT_MSB);
88+
busWrite(mag->busDev, RM3100_REG_CCX0, CCX_DEFAULT_LSB);
89+
90+
busWrite(mag->busDev, RM3100_REG_CCY1, CCY_DEFAULT_MSB);
91+
busWrite(mag->busDev, RM3100_REG_CCY0, CCY_DEFAULT_LSB);
92+
93+
busWrite(mag->busDev, RM3100_REG_CCZ1, CCZ_DEFAULT_MSB);
94+
busWrite(mag->busDev, RM3100_REG_CCZ0, CCZ_DEFAULT_LSB);
95+
96+
return true;
97+
}
98+
99+
static bool deviceRead(magDev_t * mag)
100+
{
101+
uint8_t status;
102+
103+
#pragma pack(push, 1)
104+
struct {
105+
uint8_t x[3];
106+
uint8_t y[3];
107+
uint8_t z[3];
108+
} rm_report;
109+
#pragma pack(pop)
110+
111+
mag->magADCRaw[X] = 0;
112+
mag->magADCRaw[Y] = 0;
113+
mag->magADCRaw[Z] = 0;
114+
115+
/* Check if new measurement is ready */
116+
bool ack = busRead(mag->busDev, RM3100_REG_STATUS, &status);
117+
118+
if (!ack || (status & 0x80) == 0) {
119+
return false;
120+
}
121+
122+
ack = busReadBuf(mag->busDev, RM3100_REG_MX, (uint8_t *)&rm_report, sizeof(rm_report));
123+
if (!ack) {
124+
return false;
125+
}
126+
127+
int32_t xraw;
128+
int32_t yraw;
129+
int32_t zraw;
130+
131+
/* Rearrange mag data */
132+
xraw = ((rm_report.x[0] << 24) | (rm_report.x[1] << 16) | (rm_report.x[2]) << 8);
133+
yraw = ((rm_report.y[0] << 24) | (rm_report.y[1] << 16) | (rm_report.y[2]) << 8);
134+
zraw = ((rm_report.z[0] << 24) | (rm_report.z[1] << 16) | (rm_report.z[2]) << 8);
135+
136+
/* Truncate to 16-bit integers and pass along */
137+
mag->magADCRaw[X] = (int16_t)(xraw >> 16);
138+
mag->magADCRaw[Y] = (int16_t)(yraw >> 16);
139+
mag->magADCRaw[Z] = (int16_t)(zraw >> 16);
140+
141+
return true;
142+
}
143+
144+
#define DETECTION_MAX_RETRY_COUNT 5
145+
static bool deviceDetect(magDev_t * mag)
146+
{
147+
for (int retryCount = 0; retryCount < DETECTION_MAX_RETRY_COUNT; retryCount++) {
148+
uint8_t revid = 0;
149+
bool ack = busRead(mag->busDev, RM3100_REG_REVID, &revid);
150+
151+
if (ack && revid == RM3100_REVID) {
152+
return true;
153+
}
154+
}
155+
156+
return false;
157+
}
158+
159+
bool rm3100MagDetect(magDev_t * mag)
160+
{
161+
busSetSpeed(mag->busDev, BUS_SPEED_STANDARD);
162+
163+
mag->busDev = busDeviceInit(BUSTYPE_ANY, DEVHW_RM3100, mag->magSensorToUse, OWNER_COMPASS);
164+
if (mag->busDev == NULL) {
165+
return false;
166+
}
167+
168+
if (!deviceDetect(mag)) {
169+
busDeviceDeInit(mag->busDev);
170+
return false;
171+
}
172+
173+
mag->init = deviceInit;
174+
mag->read = deviceRead;
175+
176+
return true;
177+
}
178+
179+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#pragma once
26+
27+
bool rm3100MagDetect(magDev_t *mag);

src/main/fc/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tables:
1010
values: ["NONE", "HCSR04", "SRF10", "INAV_I2C", "VL53L0X", "MSP", "UNUSED", "BENEWAKE"]
1111
enum: rangefinderType_e
1212
- name: mag_hardware
13-
values: ["NONE", "AUTO", "HMC5883", "AK8975", "GPSMAG", "MAG3110", "AK8963", "IST8310", "QMC5883", "MPU9250", "IST8308", "LIS3MDL", "MSP", "FAKE"]
13+
values: ["NONE", "AUTO", "HMC5883", "AK8975", "GPSMAG", "MAG3110", "AK8963", "IST8310", "QMC5883", "MPU9250", "IST8308", "LIS3MDL", "MSP", "RM3100", FAKE"]
1414
enum: magSensor_e
1515
- name: opflow_hardware
1616
values: ["NONE", "CXOF", "MSP", "FAKE"]

src/main/sensors/compass.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "drivers/compass/compass_qmc5883l.h"
4141
#include "drivers/compass/compass_mpu9250.h"
4242
#include "drivers/compass/compass_lis3mdl.h"
43+
#include "drivers/compass/compass_rm3100.h"
4344
#include "drivers/compass/compass_msp.h"
4445
#include "drivers/io.h"
4546
#include "drivers/light_led.h"
@@ -264,6 +265,22 @@ bool compassDetect(magDev_t *dev, magSensor_e magHardwareToUse)
264265
}
265266
FALLTHROUGH;
266267

268+
case MAG_RM3100:
269+
#ifdef USE_MAG_RM3100
270+
if (rm3100MagDetect(dev)) {
271+
#ifdef MAG_RM3100_ALIGN
272+
dev->magAlign.onBoard = MAG_RM3100_ALIGN;
273+
#endif
274+
magHardware = MAG_RM3100;
275+
break;
276+
}
277+
#endif
278+
/* If we are asked for a specific sensor - break out, otherwise - fall through and continue */
279+
if (magHardwareToUse != MAG_AUTODETECT) {
280+
break;
281+
}
282+
FALLTHROUGH;
283+
267284
case MAG_FAKE:
268285
#ifdef USE_FAKE_MAG
269286
if (fakeMagDetect(dev)) {

src/main/sensors/compass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ typedef enum {
4242
MAG_IST8308 = 10,
4343
MAG_LIS3MDL = 11,
4444
MAG_MSP = 12,
45-
MAG_FAKE = 13,
45+
MAG_RM3100 = 13,
46+
MAG_FAKE = 14,
4647
MAG_MAX = MAG_FAKE
4748
} magSensor_e;
4849

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target_stm32f405xg(MATEKF405CAN)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of Cleanflight.
3+
*
4+
* Cleanflight is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Cleanflight is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdint.h>
19+
#include "platform.h"
20+
#include "config/config_master.h"
21+
#include "config/feature.h"
22+
#include "io/serial.h"
23+
24+
25+
void targetConfiguration(void)
26+
{
27+
28+
serialConfigMutable()->portConfigs[findSerialPortIndexByIdentifier(SERIAL_PORT_USART4)].functionMask = FUNCTION_GPS;
29+
// serialConfigMutable()->portConfigs[findSerialPortIndexByIdentifier(SERIAL_PORT_USART4)].gps_baudrateIndex = BAUD_115200;
30+
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of INAV.
3+
*
4+
* INAV is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* INAV is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with INAV. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdbool.h>
19+
#include <platform.h>
20+
#include "drivers/io.h"
21+
#include "drivers/pwm_mapping.h"
22+
#include "drivers/timer.h"
23+
24+
const timerHardware_t timerHardware[] = {
25+
DEF_TIM(TIM8, CH1, PC6, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 1), // S1 D(2,2,7)
26+
DEF_TIM(TIM8, CH2, PC7, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 1), // S2 D(2,3,7)
27+
DEF_TIM(TIM8, CH3, PC8, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 1), // S3 D(2,4,7)
28+
DEF_TIM(TIM8, CH4, PC9, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 0), // S4 D(2,7,7)
29+
DEF_TIM(TIM3, CH3, PB0, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 0), // S5 D(1,7,5)
30+
DEF_TIM(TIM3, CH4, PB1, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 1, 0), // S6 D(1,2,5)
31+
32+
DEF_TIM(TIM4, CH1, PB6, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 1, 0), // S7 D(1,0,2)
33+
DEF_TIM(TIM4, CH2, PB7, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 1, 0), // S8 D(1,3,2)
34+
35+
DEF_TIM(TIM1, CH1, PA8, TIM_USE_BEEPER, 0, 0), // BEEPER PWM
36+
37+
DEF_TIM(TIM2, CH1, PA15, TIM_USE_LED, 0, 0), //2812LED D(1,5,3)
38+
39+
DEF_TIM(TIM9, CH2, PA3, TIM_USE_PPM, 0, 0), //RX2
40+
DEF_TIM(TIM5, CH3, PA2, TIM_USE_ANY, 0, 0), //TX2 softserial1_Tx
41+
};
42+
43+
const int timerHardwareCount = sizeof(timerHardware) / sizeof(timerHardware[0]);
44+

0 commit comments

Comments
 (0)