betaflight icon indicating copy to clipboard operation
betaflight copied to clipboard

Some I2C MAG does not work

Open McGiverGim opened this issue 7 years ago • 317 comments

I've a Omnibus F4 Pro (V3). Two months ago I added a Radiolink SE100 (M8N + HMC5983) to my quad. It worked perfectly but after some hours the hardware was dead, I don't know why. I bought another, the same model, but this does not work. Betaflight does not detect the MAG.

# status
System Uptime: 19 seconds
Current Time: 0000-01-01T00:00:00.000+00:00
Voltage: 0 * 0.1V (0S battery - NOT PRESENT)
CPU Clock=168MHz, GYRO=MPU6000, ACC=MPU6000, BARO=BMP280
SD card: Manufacturer 0x3, 15558144kB, 06/2017, v8.0, 'SC16G'
Filesystem: Ready
Stack size: 2048, Stack address: 0x10010000
I2C Errors: 0, config size: 1840, max available config: 16384
CPU:9%, cycle time: 125, GYRO rate: 8000, RX rate: 49, System rate: 9
Arming disable flags: RXLOSS CLI MSP

First, I thinked it will be a hardware issue, so I tried with iNav, and iNav detected it perfectly out of the box, so the hardware is working. I Have observed that the board in the old version (that worked in Betaflight) and the new (that don't work) are slightly different being the same model. Here you have the complete diff:

# diff

# version
# Betaflight / OMNIBUSF4SD (OBSD) 3.3.0 Nov  9 2017 / 18:53:40 (6f46ba7) MSP API: 1.37

# name
name migmul martian

# resources
resource MOTOR 5 NONE
resource MOTOR 6 NONE
resource SERIAL_TX 3 NONE
resource SERIAL_TX 11 A01
resource SERIAL_RX 3 NONE
resource SERIAL_RX 11 A08
resource I2C_SCL 2 B10
resource I2C_SDA 2 B11

# mixer

# servo

# servo mix


# feature
feature SOFTSERIAL
feature GPS
feature TELEMETRY
feature DYNAMIC_FILTER

# beeper

# map

# serial
serial 0 4160 115200 57600 0 115200
serial 5 2 115200 57600 0 115200
serial 30 16384 115200 57600 0 115200

# led

# color

# mode_color

# aux
aux 0 0 0 1950 2050
aux 1 1 1 1250 1350
aux 2 1 1 1450 1550
aux 3 1 1 1650 1850
aux 4 2 1 1350 1450
aux 5 3 1 1250 1350
aux 6 3 1 1650 1850
aux 7 6 1 1750 1850
aux 8 10 1 1250 1350
aux 9 11 1 1250 1350
aux 10 11 1 1650 1850
aux 11 13 1 1250 1350
aux 12 26 0 1950 2050
aux 13 28 1 1050 1150
aux 14 32 5 1950 2050
aux 15 33 4 950 1200
aux 16 34 4 1800 2050

# adjrange
adjrange 0 0 2 1950 2050 18 3

# rxrange

# vtx

# rxfail
rxfail 5 s 1500

# master
set gyro_notch1_hz = 0
set gyro_notch1_cutoff = 0
set gyro_notch2_hz = 0
set gyro_notch2_cutoff = 0
set mag_declination = -110
set max_check = 1950
set serialrx_provider = IBUS
set motor_pwm_protocol = MULTISHOT
set failsafe_delay = 3
set failsafe_off_delay = 20
set failsafe_throttle = 1200
set failsafe_procedure = AUTO-LAND
set bat_capacity = 1800
set vbat_detect_cell_voltage = 55
set ibata_scale = 333
set small_angle = 180
set disarm_kill_switch = OFF
set gps_provider = UBLOX
set gps_sbas_mode = EGNOS
set gps_auto_baud = ON
set deadband = 3
set yaw_deadband = 5
set pid_process_denom = 1
set osd_rssi_alarm = 0
set osd_cap_alarm = 1600
set osd_tim1 = 0
set osd_tim2 = 1793
set osd_vbat_pos = 2433
set osd_rssi_pos = 46
set osd_tim_1_pos = 54
set osd_tim_2_pos = 2455
set osd_flymode_pos = 2145
set osd_throttle_pos = 2424
set osd_vtx_channel_pos = 376
set osd_crosshairs = 2250
set osd_ah_sbar = 2250
set osd_ah_pos = 2250
set osd_current_pos = 2400
set osd_mah_drawn_pos = 2370
set osd_craft_name_pos = 2440
set osd_gps_speed_pos = 2264
set osd_gps_lon_pos = 2129
set osd_gps_lat_pos = 2097
set osd_gps_sats_pos = 2119
set osd_home_dir_pos = 2126
set osd_home_dist_pos = 2092
set osd_compass_bar_pos = 2154
set osd_altitude_pos = 2294
set osd_pid_roll_pos = 391
set osd_pid_pitch_pos = 455
set osd_pid_yaw_pos = 487
set osd_debug_pos = 391
set osd_power_pos = 321
set osd_pidrate_profile_pos = 344
set osd_avg_cell_voltage_pos = 76
set osd_pit_ang_pos = 257
set osd_rol_ang_pos = 289
set osd_battery_usage_pos = 302
set osd_disarmed_pos = 2411
set osd_nheading_pos = 106
set osd_nvario_pos = 1
set osd_esc_tmp_pos = 82
set osd_esc_rpm_pos = 83
set osd_stat_max_dist = ON
set osd_stat_min_batt = OFF
set osd_stat_min_rssi = OFF
set osd_stat_max_alt = ON
set osd_stat_endbatt = ON
set debug_mode = FFT
set vcd_video_system = 2

# profile
profile 0

set dterm_lowpass_type = PT1
set anti_gravity_gain = 2000
set setpoint_relax_ratio = 50
set dterm_setpoint_weight = 105
set p_pitch = 65
set d_pitch = 40
set p_roll = 50
set d_roll = 36
set p_yaw = 90

# rateprofile
rateprofile 0

set rc_expo = 40
set rc_expo_yaw = 10
set thr_mid = 40
set roll_srate = 65
set pitch_srate = 61
set yaw_srate = 60
set tpa_rate = 0

But I've tested with defaults too, only adding this:

resource I2C_SCL 2 B10
resource I2C_SDA 2 B11

Things that I've tested:

  • Ask for help in slack (thanks to @jflyper for the effort) without luck. So I generate this issue to see if we can find more people with the same problem.
  • Betaflight 3.1.7, 3.2.1 and master without luck.
  • iNav -> WORKING
  • Disable the i2c overclock, and modify the i2c timeout in the code, without luck
  • Compare the iNav code with the Betaflight code but my knowledge is very limited, so I didn't see nothing. Maybe someone with more knowledge can compare the iNav and Betaflight code to see where the difference can be?

McGiverGim avatar Nov 10 '17 08:11 McGiverGim

I2C mag repeatedly come up as not working properly in various symptoms (including "it works on iNav"), but it is usually the case that only those with proper measurement apparatus (e.g., DSO, logic analyzers - LAs) really help trouble shooting the issue.

@McGiverGim Do you have an LA?

jflyper avatar Nov 10 '17 09:11 jflyper

No, sorry, I don't :(

McGiverGim avatar Nov 10 '17 09:11 McGiverGim

Hm... Usually it's the other way arouind - hardware is reported working in Betaflight and not working in INAV :smile:

In INAV it's usually an edge cases with marginal hardware quality. Some compass sensors will only work at lower I2C speeds (200 kHz). As far as I know Betaflight doesn't have the code to select low I2C speeds.

digitalentity avatar Nov 10 '17 09:11 digitalentity

Taking a look at the code, Betaflight works at 800 and 400, and iNav has 100 and 200 too, but I think by default it is configured to work at 400.

McGiverGim avatar Nov 10 '17 09:11 McGiverGim

You want to try 200?

I'm building for default 200 now. Or, you can do it yourself?

jflyper avatar Nov 10 '17 09:11 jflyper

I can try, for sure. If the changes in the code are minimum, you can told me where to change and what values you want to try and I can compile the code by myself, if it is easier for you. I don't want to give you a lot of work ;)

If I'm not wrong, is here? https://github.com/betaflight/betaflight/blob/8b334e5674f3a53ac2cd359d7ab2eb037d901dbb/src/main/drivers/bus_i2c_stm32f10x.c#L414-L417

McGiverGim avatar Nov 10 '17 09:11 McGiverGim

Yeap. That it, line 417. Change 400000 to whatever you want, 200000, 100000, ...

jflyper avatar Nov 10 '17 09:11 jflyper

I need to disable the overclock too I think?

https://github.com/betaflight/betaflight/blob/8b334e5674f3a53ac2cd359d7ab2eb037d901dbb/src/main/target/common_fc_pre.h#L31-L32

I will remove the if completelly and put to 200000 the value always.

McGiverGim avatar Nov 10 '17 09:11 McGiverGim

Good finding!

jflyper avatar Nov 10 '17 09:11 jflyper

Some advance:

  • Removing the if and using 200000 always, MAG not detected, and the configurator shows one I2C error.
  • Removing the if, and using 400000, THE MAG IS DETECTED!!!! but the configurator shows the I2C error counting constantly incrementing.
  • Letting the if, and putting the overclock to false, to let it automatically to 400000, MAG not detected and no errors.
  • Removing the if, and using 400000, and putting the overclock to false, MAG detected but the configurator shows the I2C error counting constantly incrementing.

So the constant overclock does not seem to work. Any other ideas to try? :)

McGiverGim avatar Nov 10 '17 10:11 McGiverGim

More info :)

Using 400000 and changing: https://github.com/betaflight/betaflight/blob/8b334e5674f3a53ac2cd359d7ab2eb037d901dbb/src/main/drivers/bus_i2c_impl.h#L25-L27 The default timeout to the long timeout... the MAG is detected and I've no errors. I need to test if it works, if the north is detected right, but is an advance :)

McGiverGim avatar Nov 10 '17 10:11 McGiverGim

Result of some tests: it does not seem to work. When I start the FC the configurator always point to the north, ignoring the position of the quad.

So the MAG is detected, there're no errors, but the data does not seem to be readed correctly.

EDIT: the MAG does not work because the calibration is not working. If I add some "random" values like

set magzero_x = -32
set magzero_y = 153
set magzero_z = 284

Then the mag is "working" (not too good because the random values are not the correct), but it works. Maybe a bug in the configurator or some problem with my changes. I will test more.

McGiverGim avatar Nov 10 '17 10:11 McGiverGim

After a lot of tests, now I think that the mag is faulty. With the changes, betaflight detects it, but the sensors tab of the mag always is 0, so I think they were not readed. I've installed iNav, that detects the MAG out of the box... and the problem seems the same. It does not detect the values in the sensors tab. So if nobody has another idea, I think that I need to buy another MAG :( and I will close this issue. Sorry for the time spended by all trying to help me.

McGiverGim avatar Nov 11 '17 13:11 McGiverGim

😭

jflyper avatar Nov 11 '17 13:11 jflyper

I have bought another, the same model, pigheaded that I am... The thursday will be here and we will see what happens...

McGiverGim avatar Nov 11 '17 14:11 McGiverGim

Hi please keep up the good work i have the same problem and would love to be able to use my mag but programing is beyond me. The problem is still in the new ver. 3.2.2 and i can confirm that for me inav works with my neoM8N+HMC5883 with no problems but betaflight doesn't even if i use set mag_hardware = HMC5883

catchra avatar Nov 12 '17 17:11 catchra

@catchra: At least telling us what board you are using, or how you are connecting your I2C device, whould give us a chance to help you with your problem... ;-)

mikeller avatar Nov 12 '17 18:11 mikeller

I am using the omnibus f4 v3 with sdcard on firmware 3.2.2 as for the mag it is connected to SCL/TX3 and SDA/RX3 on the ports tab i made no changes to uart3 on the configuration tab i turned on the mag and in the CLI i used set mag_hardware = HMC5883 and than save these are the same setting i used in Inav

in the betaflight flasher i am using OMNIBUSF4SD at version 3.2.2

in Inav flasher i am using OMNIBUSF4SD at version 1.6-ALPHA2 with show unstable set to on

catchra avatar Nov 12 '17 19:11 catchra

You must put this commands in cli:

resource I2C_SCL 2 B10
resource I2C_SDA 2 B11
save

McGiverGim avatar Nov 12 '17 19:11 McGiverGim

Thank you that work

but this is the output from the CLI

Entering CLI Mode, type 'exit' to return, or 'help'

# resource I2C_SCL 2 B10

NOTE: B10 already assigned to SERIAL_TX 3.

Resource is set to B10

# resource I2C_SDA 2 B11

NOTE: B11 already assigned to SERIAL_RX 3.

Resource is set to B11

it did not work until i used the commands that you gave me.

catchra avatar Nov 12 '17 20:11 catchra

Yes, don't worry. They are warnings.

McGiverGim avatar Nov 12 '17 20:11 McGiverGim

To let more info about this problem, until I receive the new unit, I have found an iNav user with the same problem and the same product:

https://github.com/iNavFlight/inav/issues/2299

One of the comments say that the MAG maybe is a QMC5883 and not a HMC5983L. The photo shows an "DA 5883 7002". Is there a way to know if is HMC or QMC?

McGiverGim avatar Nov 13 '17 13:11 McGiverGim

@McGiverGim The "DA 5883" looks like a QMC5883; there are numerous Chinese sites listing the DA chip as "HMC5883 alternative".

EDIT Strange thing is that it could have been detected as HMC5883 even these don't have compatible address.

jflyper avatar Nov 13 '17 14:11 jflyper

So I have two problems:

  • The first, Betaflight does not support it
  • The second, it seems that is not a standard QMC5883, because iNav detects it as a HMC5883 and if a configure it as a QMC5883 it appears as unavailable. Maybe is a QMC5883 listening to the address of the HMC5883?

McGiverGim avatar Nov 13 '17 14:11 McGiverGim

I think the manufacturer is offering custom chips with non-standard I2C address; data sheet says

The default I2C address is 0D: 0001101 If other I2C address options are required, please contact factory.

http://img.filipeflop.com/files/download/Datasheet-QMC5883L-1.0%20.pdf

jflyper avatar Nov 13 '17 15:11 jflyper

Here's the whole story. https://github.com/iNavFlight/inav/issues/1903

jflyper avatar Nov 13 '17 15:11 jflyper

Thanks! I will try to compile inav using QMC and the address of HMC, to see if it works... Maybe this will be the only "customization".

McGiverGim avatar Nov 13 '17 15:11 McGiverGim

Data sheet mentions "OTP Load" immediately after POR and Soft Reset. I suspect I2C address is written in the OTP area...

jflyper avatar Nov 13 '17 15:11 jflyper

Oh yeah, that QMC5883 chip appears to be a real pain the back. When it's on the default address - all is ok, but if manufacturer configures it for something different - funny things starts to happen.

digitalentity avatar Nov 13 '17 15:11 digitalentity

Yes, I'm playing a little changing values and registers, mixing the values neede to the HMC5883L and the QMC5883L, to see if I have luck... but not. Making it in a blind way seems like a pain... If someone has some idea, I can try, but I think I have simply the "incorrect" MAG :(

McGiverGim avatar Nov 13 '17 16:11 McGiverGim