Yilin Sun
1b5868d4cb
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: Yilin Sun <imi415@imi.moe>
209 lines
5.0 KiB
C
209 lines
5.0 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
/* MX Generated headers */
|
|
#include "adc.h"
|
|
#include "gpio.h"
|
|
#include "i2c.h"
|
|
#include "main.h"
|
|
#include "rtc.h"
|
|
|
|
/* BSP */
|
|
#include "stm32wlxx_nucleo.h"
|
|
#include "stm32wlxx_nucleo_radio.h"
|
|
|
|
/* Sensors */
|
|
#include "imsensors/als/als_ltr_303als.h"
|
|
#include "imsensors/dht/dht_bme280.h"
|
|
#include "imsensors/imu/imu_lsm6dsl.h"
|
|
#include "vl53l1_api.h"
|
|
|
|
/* App */
|
|
#include "app_ranging_impl.h"
|
|
#include "app_sensors_impl.h"
|
|
|
|
extern void SystemClock_Config(void);
|
|
|
|
static app_sensors_impl_t s_dht_impl = {
|
|
.hi2c = &hi2c1,
|
|
.dev_addr = 0x76,
|
|
};
|
|
|
|
static ims_bme280_t s_dht = {
|
|
.pdev = &s_dht_impl,
|
|
.cb =
|
|
{
|
|
.i2c_xfer = app_sensors_i2c_xfer,
|
|
.delay = app_sensors_delay,
|
|
},
|
|
};
|
|
|
|
static app_sensors_impl_t s_als_impl = {
|
|
.hi2c = &hi2c1,
|
|
.dev_addr = 0x29,
|
|
};
|
|
|
|
static ims_ltr_303als_t s_als = {
|
|
.pdev = &s_als_impl,
|
|
.cb =
|
|
{
|
|
.i2c_xfer = app_sensors_i2c_xfer,
|
|
.delay = app_sensors_delay,
|
|
},
|
|
};
|
|
|
|
static app_sensors_impl_t s_imu_impl = {
|
|
.hi2c = &hi2c1,
|
|
.dev_addr = 0x6A,
|
|
};
|
|
|
|
static ims_lsm6dsl_t s_imu = {
|
|
.pdev = &s_imu_impl,
|
|
.cb =
|
|
{
|
|
.i2c_xfer = app_sensors_i2c_xfer,
|
|
.delay = app_sensors_delay,
|
|
},
|
|
};
|
|
|
|
static app_ranging_impl_t s_ranging_impl = {
|
|
.hi2c = &hi2c1,
|
|
.dev_addr = 0x29,
|
|
};
|
|
|
|
static VL53L1_Dev_t s_ranging = {
|
|
.Ops =
|
|
{
|
|
.Delay_Us = app_ranging_delay_us,
|
|
.Delay_Ms = app_ranging_delay_ms,
|
|
.I2C_Xfer = app_ranging_i2c_xfer,
|
|
},
|
|
.OpsData = &s_ranging_impl,
|
|
};
|
|
|
|
int main(void) {
|
|
/* HAL */
|
|
HAL_Init();
|
|
|
|
/* Hardware initialization */
|
|
SystemClock_Config();
|
|
MX_GPIO_Init();
|
|
MX_ADC_Init();
|
|
MX_RTC_Init();
|
|
MX_I2C1_Init();
|
|
|
|
printf("LWNode Functional Test Program:\r\n");
|
|
|
|
if (ims_bme280_init(&s_dht) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
ims_bme280_config_t dht_cfg;
|
|
ims_bme280_preset_config(&dht_cfg, IMS_BME280_PRESET_WEATHER);
|
|
|
|
dht_cfg.mode = IMS_BME280_MODE_NORMAL;
|
|
dht_cfg.t_sb = IMS_BME280_STANDBY_TIME_500_MS;
|
|
|
|
ims_bme280_config(&s_dht, &dht_cfg);
|
|
|
|
if (ims_ltr_303als_init(&s_als) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
ims_ltr_303als_config_t als_cfg;
|
|
ims_ltr_303als_preset_config(&als_cfg);
|
|
|
|
als_cfg.gain = IMS_LTR_303ALS_GAIN_48X;
|
|
|
|
if (ims_ltr_303als_config(&s_als, &als_cfg) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
if (ims_ltr_303als_set_mode(&s_als, IMS_LTR_303ALS_MODE_ACTIVE) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
if (ims_lsm6dsl_init(&s_imu) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
HAL_GPIO_WritePin(TOF_XSHUT_GPIO_Port, TOF_XSHUT_Pin, 1U);
|
|
|
|
HAL_Delay(5);
|
|
|
|
VL53L1_SetDeviceAddress(&s_ranging, 0x6E);
|
|
|
|
s_ranging_impl.dev_addr = 0x37U;
|
|
|
|
VL53L1_WaitDeviceBooted(&s_ranging);
|
|
VL53L1_DataInit(&s_ranging);
|
|
VL53L1_StaticInit(&s_ranging);
|
|
VL53L1_SetDistanceMode(&s_ranging, VL53L1_DISTANCEMODE_MEDIUM);
|
|
VL53L1_SetMeasurementTimingBudgetMicroSeconds(&s_ranging, 50000);
|
|
VL53L1_SetInterMeasurementPeriodMilliSeconds(&s_ranging, 500);
|
|
VL53L1_StartMeasurement(&s_ranging);
|
|
|
|
if (HAL_ADCEx_Calibration_Start(&hadc) != HAL_OK) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
ims_ltr_303als_result_t als_result;
|
|
ims_bme280_result_t dht_result;
|
|
VL53L1_RangingMeasurementData_t ranging_result;
|
|
|
|
uint8_t ranging_ready;
|
|
int16_t imu_temp;
|
|
uint16_t vbat;
|
|
|
|
for (;;) {
|
|
HAL_Delay(1000);
|
|
|
|
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
|
|
|
|
HAL_ADC_Start(&hadc);
|
|
HAL_ADC_PollForConversion(&hadc, 1000);
|
|
|
|
if (HAL_ADC_GetState(&hadc) & HAL_ADC_STATE_EOC_REG) {
|
|
vbat = HAL_ADC_GetValue(&hadc);
|
|
}
|
|
|
|
VL53L1_GetMeasurementDataReady(&s_ranging, &ranging_ready);
|
|
if (ranging_ready) {
|
|
VL53L1_GetRangingMeasurementData(&s_ranging, &ranging_result);
|
|
if (VL53L1_ClearInterruptAndStartMeasurement(&s_ranging) != VL53L1_ERROR_NONE) {
|
|
goto dead_loop;
|
|
}
|
|
}
|
|
|
|
if (ims_bme280_read(&s_dht, &dht_result) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
if (ims_ltr_303als_read(&s_als, &als_result) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
if (ims_lsm6dsl_read_temperature(&s_imu, &imu_temp) != IMS_SUCCESS) {
|
|
goto dead_loop;
|
|
}
|
|
|
|
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
|
|
|
|
printf(
|
|
"T: %5.2fC,\tH: %6.2f%%,\tP: %6.2fhPa,\tI0: %6.2flx,\tI1: %6.2flx,\tT_IMU: %5.2fC\tR: %4dmm\tV: "
|
|
"%6.2fmV\r\n",
|
|
dht_result.temperature, dht_result.humidity, dht_result.pressure / 100.0f, als_result.ch0_value / 48.0,
|
|
als_result.ch1_value / 48.0, (imu_temp / 256.0) + 25.0, ranging_result.RangeMilliMeter,
|
|
(vbat * VDD_VALUE / 4096.0));
|
|
}
|
|
|
|
dead_loop:
|
|
for (;;) {
|
|
/* -- */
|
|
}
|
|
}
|
|
|
|
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
|
/* -- */
|
|
}
|