Shared I2C1 bus (i2c1.c/h, PB8=SCL PB9=SDA 100kHz): - i2c1_init() called once in main() before sensor probes. - hi2c1 exported globally; baro and mag drivers use it directly. Barometer (bmp280.c): - Probes I2C1 at 0x76 then 0x77 (covers both SDO options). - bmp280_init() returns chip_id (0x58/0x60) on success, neg if absent. - Added bmp280_pressure_to_alt_cm() — ISA barometric formula. - Added bmp280.h (was missing). Magnetometer (mag.c / mag.h): - Auto-detects QMC5883L (0x0D, id=0xFF), HMC5883L (0x1E, id='H43'), IST8310 (0x0E, id=0x10) in that order. - mag_read_heading() returns degrees×10 (0–3599) or -1 if not ready. - HMC5883L: correct XZY byte order applied. - IST8310: single-measurement trigger mode. main.c: - i2c1_init() + bmp280_init() + mag_init() after all other inits. - Both skip gracefully (baro_ok=0, mag_type=MAG_NONE) if not present. - Telemetry JSON: incremental builder appends ",\"hd\":<n>" when mag found and ",\"alt\":<n>" when baro found. No extra bytes when absent. UI (index.html): - HEADING and ALT rows hidden until first packet with that field. - Heading shown in degrees, alt in metres (firmware sends cm). Closes #24. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
34 lines
1.0 KiB
C
34 lines
1.0 KiB
C
/*
|
|
* i2c1.c — Shared I2C1 bus (PB8=SCL, PB9=SDA, 100 kHz)
|
|
*
|
|
* Used by barometer and magnetometer drivers.
|
|
* Call i2c1_init() once before any I2C probes.
|
|
*/
|
|
#include "i2c1.h"
|
|
#include "stm32f7xx_hal.h"
|
|
|
|
I2C_HandleTypeDef hi2c1;
|
|
|
|
int i2c1_init(void) {
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_I2C1_CLK_ENABLE();
|
|
|
|
GPIO_InitTypeDef gpio = {0};
|
|
gpio.Pin = GPIO_PIN_8 | GPIO_PIN_9; /* PB8=SCL, PB9=SDA */
|
|
gpio.Mode = GPIO_MODE_AF_OD;
|
|
gpio.Pull = GPIO_PULLUP;
|
|
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
gpio.Alternate = GPIO_AF4_I2C1;
|
|
HAL_GPIO_Init(GPIOB, &gpio);
|
|
|
|
hi2c1.Instance = I2C1;
|
|
hi2c1.Init.Timing = 0x20404768; /* 100 kHz @ 54 MHz APB1 */
|
|
hi2c1.Init.OwnAddress1 = 0;
|
|
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
|
|
return (HAL_I2C_Init(&hi2c1) == HAL_OK) ? 0 : -1;
|
|
}
|