Added LTR-303ALS, common API changed.
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
6ee72440c7
commit
582f887dff
|
@ -1,9 +1,9 @@
|
|||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
AlignConsecutiveDeclarations: true
|
||||
AlignConsecutiveAssignments: true
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterEnum: false
|
||||
|
|
|
@ -5,11 +5,14 @@ project(imsensors)
|
|||
option(IMSENSORS_FLOAT_SUPPORT "Enable floating point support" OFF)
|
||||
|
||||
set(IMSENSORS_SRCS
|
||||
"src/als/als_ltr_303als.c"
|
||||
"src/dht/dht_aht10.c"
|
||||
"src/dht/dht_bme280.c"
|
||||
)
|
||||
|
||||
set(IMSENSORS_INCS
|
||||
"include/imsensors"
|
||||
"include/imsensors/common"
|
||||
"include/imsensors/als"
|
||||
"include/imsensors/dht"
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
#ifndef IMS_ALS_LTR303ALS_H
|
||||
#define IMS_ALS_LTR303ALS_H
|
||||
|
||||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef enum {
|
||||
IMS_LTR_303ALS_INT_100MS = 0U,
|
||||
IMS_LTR_303ALS_INT_50MS = 1U,
|
||||
IMS_LTR_303ALS_INT_200MS = 2U,
|
||||
IMS_LTR_303ALS_INT_400MS = 3U,
|
||||
IMS_LTR_303ALS_INT_150MS = 4U,
|
||||
IMS_LTR_303ALS_INT_250MS = 5U,
|
||||
IMS_LTR_303ALS_INT_300MS = 6U,
|
||||
IMS_LTR_303ALS_INT_350MS = 7U,
|
||||
} ims_ltr_303als_integration_time_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LTR_303ALS_MEAS_50MS = 0U,
|
||||
IMS_LTR_303ALS_MEAS_100MS = 1U,
|
||||
IMS_LTR_303ALS_MEAS_200MS = 2U,
|
||||
IMS_LTR_303ALS_MEAS_500MS = 3U,
|
||||
IMS_LTR_303ALS_MEAS_1000MS = 4U,
|
||||
IMS_LTR_303ALS_MEAS_2000MS = 5U,
|
||||
} ims_ltr_303als_measurement_rate_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LTR_303ALS_GAIN_1X = 0U,
|
||||
IMS_LTR_303ALS_GAIN_2X = 1U,
|
||||
IMS_LTR_303ALS_GAIN_4X = 2U,
|
||||
IMS_LTR_303ALS_GAIN_8X = 3U,
|
||||
IMS_LTR_303ALS_GAIN_48X = 6U,
|
||||
IMS_LTR_303ALS_GAIN_96X = 7U,
|
||||
} ims_ltr_303als_gain_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LTR_303ALS_INT_ACTIVE_LOW = 0U,
|
||||
IMS_LTR_303ALS_INT_ACTIVE_HIGH = 1U,
|
||||
} ims_ltr_303als_int_polarity_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LTR_303ALS_MODE_STANDBY = 0U,
|
||||
IMS_LTR_303ALS_MODE_ACTIVE = 1U,
|
||||
} ims_ltr_303als_mode_t;
|
||||
|
||||
typedef struct {
|
||||
ims_ltr_303als_gain_t gain;
|
||||
bool interrupt_en;
|
||||
ims_ltr_303als_int_polarity_t interrupt_polarity;
|
||||
uint16_t threshold_high;
|
||||
uint16_t threshold_low;
|
||||
ims_ltr_303als_integration_time_t integration_time;
|
||||
ims_ltr_303als_measurement_rate_t measurement_rate;
|
||||
} ims_ltr_303als_config_t;
|
||||
|
||||
typedef struct {
|
||||
bool data_valid;
|
||||
bool data_new;
|
||||
bool interrupt_active;
|
||||
ims_ltr_303als_gain_t gain;
|
||||
uint16_t ch0_value;
|
||||
uint16_t ch1_value;
|
||||
} ims_ltr_303als_result_t;
|
||||
|
||||
typedef struct {
|
||||
ims_i2c_xfer_t i2c_xfer;
|
||||
ims_delay_t delay;
|
||||
} ims_ltr_303als_cb_t;
|
||||
|
||||
typedef struct {
|
||||
void *pdev;
|
||||
ims_ltr_303als_cb_t cb;
|
||||
} ims_ltr_303als_t;
|
||||
|
||||
ims_ret_t ims_ltr_303als_init(ims_ltr_303als_t *als);
|
||||
void ims_ltr_303als_preset_config(ims_ltr_303als_config_t *cfg);
|
||||
ims_ret_t ims_ltr_303als_config(ims_ltr_303als_t *als, ims_ltr_303als_config_t *config);
|
||||
ims_ret_t ims_ltr_303als_set_mode(ims_ltr_303als_t *als, ims_ltr_303als_mode_t mode);
|
||||
ims_ret_t ims_ltr_303als_get_mode(ims_ltr_303als_t *als, ims_ltr_303als_mode_t *mode);
|
||||
ims_ret_t ims_ltr_303als_read(ims_ltr_303als_t *als, ims_ltr_303als_result_t *result);
|
||||
|
||||
#endif // IMS_ALS_LTR303ALS_H
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef SENSORS_COMMON_H
|
||||
#define SENSORS_COMMON_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
|
@ -16,7 +18,7 @@ typedef struct {
|
|||
} ims_i2c_xfer_desc_t;
|
||||
|
||||
/* I2C xfer functions */
|
||||
typedef ims_ret_t (*ims_i2c_xfer_t)(void *pdev, uint16_t addr, ims_i2c_xfer_desc_t *xfer);
|
||||
typedef ims_ret_t (*ims_i2c_xfer_t)(void *pdev, ims_i2c_xfer_desc_t *xfer);
|
||||
|
||||
/* Delay */
|
||||
typedef ims_ret_t (*ims_delay_t)(void *pdev, uint32_t delay_msec);
|
|
@ -1,42 +1,31 @@
|
|||
#ifndef AHT10_H
|
||||
#define AHT10_H
|
||||
#ifndef IMS_AHT10_H
|
||||
#define IMS_AHT10_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define AHT10_SKIP_BUSY_CHECK
|
||||
|
||||
typedef enum {
|
||||
AHT10_OK,
|
||||
AHT10_FAIL,
|
||||
} aht10_ret_t;
|
||||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t *tx_data;
|
||||
uint8_t *rx_data;
|
||||
uint16_t tx_size;
|
||||
uint16_t rx_size;
|
||||
} aht10_xfer_desc_t;
|
||||
|
||||
typedef aht10_ret_t (*aht10_i2c_xfer_t)(void *pdev, aht10_xfer_desc_t *xfer);
|
||||
typedef aht10_ret_t (*aht10_delay_t)(void *pdev, uint32_t delay_msec);
|
||||
} ims_aht10_xfer_desc_t;
|
||||
|
||||
typedef struct {
|
||||
double temperature;
|
||||
double humidity;
|
||||
} aht10_result_t;
|
||||
} ims_aht10_result_t;
|
||||
|
||||
typedef struct {
|
||||
aht10_i2c_xfer_t xfer;
|
||||
aht10_delay_t delay;
|
||||
} aht10_cb_t;
|
||||
ims_i2c_xfer_t i2c_xfer;
|
||||
ims_delay_t delay;
|
||||
} ims_aht10_cb_t;
|
||||
|
||||
typedef struct {
|
||||
aht10_cb_t cb;
|
||||
void *user_data;
|
||||
} aht10_t;
|
||||
void *pdev;
|
||||
ims_aht10_cb_t cb;
|
||||
} ims_aht10_t;
|
||||
|
||||
aht10_ret_t aht10_init(aht10_t *aht);
|
||||
aht10_ret_t aht10_measure(aht10_t *aht, aht10_result_t *result);
|
||||
ims_ret_t ims_aht10_init(ims_aht10_t *aht);
|
||||
ims_ret_t ims_aht10_measure(ims_aht10_t *aht, ims_aht10_result_t *result);
|
||||
|
||||
|
||||
#endif
|
||||
#endif // IMS_AHT10_H
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef ims_bme280_DHT_H
|
||||
#define ims_bme280_DHT_H
|
||||
#ifndef IMS_BME280_DHT_H
|
||||
#define IMS_BME280_DHT_H
|
||||
|
||||
#include "imsensors/sensors_common.h"
|
||||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef enum {
|
||||
IMS_BME280_MODE_SLEEP,
|
||||
|
@ -63,18 +63,20 @@ typedef struct {
|
|||
ims_bme280_fiter_config_t filter;
|
||||
} ims_bme280_config_t;
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
typedef struct {
|
||||
double temperature;
|
||||
double pressure;
|
||||
double humidity;
|
||||
} ims_bme280_result_t;
|
||||
#else
|
||||
/* Result is multiplied by 10000. */
|
||||
typedef struct {
|
||||
int32_t temperature;
|
||||
uint32_t pressure;
|
||||
int32_t humidity;
|
||||
} ims_bme280_result_int32_t;
|
||||
|
||||
typedef struct {
|
||||
double temperature;
|
||||
double pressure;
|
||||
double humidity;
|
||||
} ims_bme280_result_float_t;
|
||||
} ims_bme280_result_t;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
ims_i2c_xfer_t i2c_xfer;
|
||||
|
@ -82,17 +84,20 @@ typedef struct {
|
|||
} ims_bme280_cb_t;
|
||||
|
||||
typedef struct {
|
||||
void *user_data;
|
||||
void *pdev;
|
||||
ims_bme280_trim_t trim;
|
||||
ims_bme280_cb_t cb;
|
||||
uint8_t i2c_addr;
|
||||
int32_t t_fine;
|
||||
} ims_bme280_t;
|
||||
|
||||
ims_ret_t ims_bme280_init(ims_bme280_t *bme);
|
||||
ims_ret_t ims_bme280_preset_config(ims_bme280_config_t *config, ims_bme280_mode_preset_t preset);
|
||||
ims_ret_t ims_bme280_apply_config(ims_bme280_t *bme, ims_bme280_config_t *config);
|
||||
ims_ret_t ims_bme280_measure_int32(ims_bme280_t *bme, ims_bme280_result_int32_t *result);
|
||||
ims_ret_t ims_bme280_measure_float(ims_bme280_t *bme, ims_bme280_result_float_t *result);
|
||||
|
||||
#endif
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
#else
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
#endif
|
||||
|
||||
#endif // IMS_BME280_DHT_H
|
|
@ -0,0 +1,346 @@
|
|||
#include "als_ltr_303als.h"
|
||||
|
||||
#define ALS_REG_CONTR 0x80U
|
||||
#define ALS_REG_MEAS_RATE 0x85U
|
||||
#define ALS_REG_PART_ID 0x86U
|
||||
#define ALS_REG_MANUFAC_ID 0x87U
|
||||
#define ALS_REG_DATA_CH1_0 0x88U
|
||||
#define ALS_REG_DATA_CH1_1 0x89U
|
||||
#define ALS_REG_DATA_CH0_0 0x8AU
|
||||
#define ALS_REG_DATA_CH0_1 0x8BU
|
||||
#define ALS_REG_STATUS 0x8CU
|
||||
#define ALS_REG_INTERRUPT 0x8FU
|
||||
#define ALS_REG_THRES_UP_0 0x97U
|
||||
#define ALS_REG_THRES_UP_1 0x98U
|
||||
#define ALS_REG_THRES_LOW_0 0x99U
|
||||
#define ALS_REG_THRES_LOW_1 0x9AU
|
||||
#define ALS_REG_INTERRUPT_PERSIST 0x9EU
|
||||
|
||||
#define ALS_REG_CONTR_MODE_Pos (0U)
|
||||
#define ALS_REG_CONTR_MODE_Msk (1U << ALS_REG_CONTR_MODE_Pos)
|
||||
|
||||
#define ALS_REG_CONTR_SW_RST_Pos (1U)
|
||||
#define ALS_REG_CONTR_SW_RST_Msk (1U << ALS_REG_CONTR_SW_RST_Pos)
|
||||
|
||||
#define ALS_REG_CONTR_GAIN_Pos (2U)
|
||||
#define ALS_REG_CONTR_GAIN_Msk (7U << ALS_REG_CONTR_GAIN_Pos)
|
||||
|
||||
#define ALS_REG_MEAS_RATE_Pos (0U)
|
||||
#define ALS_REG_MEAS_RATE_Msk (7U << ALS_REG_MEAS_RATE_Pos)
|
||||
|
||||
#define ALS_REG_MEAS_INT_Pos (3U)
|
||||
#define ALS_REG_MEAS_INT_Msk (7U << ALS_REG_MEAS_INT_Pos)
|
||||
|
||||
#define ALS_REG_STATUS_DATA_VALID_Pos (7U)
|
||||
#define ALS_REG_STATUS_DATA_VALID_Msk (1U << ALS_REG_STATUS_DATA_VALID_Pos)
|
||||
|
||||
#define ALS_REG_STATUS_GAIN_Pos (4U)
|
||||
#define ALS_REG_STATUS_GAIN_Msk (7U << ALS_REG_STATUS_GAIN_Pos)
|
||||
|
||||
#define ALS_REG_STATUS_INT_Pos (3U)
|
||||
#define ALS_REG_STATUS_INT_Msk (1U << ALS_REG_STATUS_INT_Pos)
|
||||
|
||||
#define ALS_REG_STATUS_DATA_Pos (2U)
|
||||
#define ALS_REG_STATUS_DATA_Msk (1U << ALS_REG_STATUS_DATA_Pos)
|
||||
|
||||
#define ALS_REG_INTERRUPT_MODE_Pos (0U)
|
||||
#define ALS_REG_INTERRUPT_MODE_Msk (1U << ALS_REG_INTERRUPT_MODE_Pos)
|
||||
|
||||
#define ALS_REG_INTERRUPT_POL_Pos (1U)
|
||||
#define ALS_REG_INTERRUPT_POL_Msk (1U << ALS_REG_INTERRUPT_POL_Pos)
|
||||
|
||||
#define ALS_CONST_MFG_ID 0x05U
|
||||
#define ALS_CONST_PART_ID 0x0AU
|
||||
|
||||
typedef enum {
|
||||
IMS_ALS_THRESH_LOW = ALS_REG_THRES_LOW_0,
|
||||
IMS_ALS_THRESH_HIGH = ALS_REG_THRES_UP_0,
|
||||
} ims_als_thresh_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t mfg_id;
|
||||
uint8_t part_id;
|
||||
} ims_als_id_t;
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_reset(ims_ltr_303als_t *als);
|
||||
static ims_ret_t ims_als_ltr_303als_read_id(ims_ltr_303als_t *als, ims_als_id_t *id);
|
||||
static ims_ret_t ims_als_ltr_303als_set_gain(ims_ltr_303als_t *als, ims_ltr_303als_gain_t gain);
|
||||
static ims_ret_t ims_als_ltr_303als_set_threshold(ims_ltr_303als_t *als, ims_als_thresh_t type, uint16_t thresh);
|
||||
static ims_ret_t ims_ltr_303als_config_irq(ims_ltr_303als_t *als, ims_ltr_303als_int_polarity_t pol, bool enable);
|
||||
static ims_ret_t ims_ltr_303als_config_timing(ims_ltr_303als_t *als, ims_ltr_303als_measurement_rate_t rate,
|
||||
ims_ltr_303als_integration_time_t time);
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_read_register(ims_ltr_303als_t *als, uint8_t reg, uint8_t *value);
|
||||
static ims_ret_t ims_als_ltr_303als_write_register(ims_ltr_303als_t *als, uint8_t reg, uint8_t value);
|
||||
|
||||
ims_ret_t ims_ltr_303als_init(ims_ltr_303als_t *als) {
|
||||
ims_als_id_t id;
|
||||
|
||||
if (ims_als_ltr_303als_reset(als) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
/* Read Mfg. ID and Part ID */
|
||||
if (ims_als_ltr_303als_read_id(als, &id) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
/* Validate IDs */
|
||||
if (id.mfg_id != ALS_CONST_MFG_ID || ((id.part_id & 0xF0U) >> 4U) != ALS_CONST_PART_ID) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
void ims_ltr_303als_preset_config(ims_ltr_303als_config_t *cfg) {
|
||||
cfg->gain = IMS_LTR_303ALS_GAIN_1X;
|
||||
cfg->interrupt_en = true;
|
||||
cfg->interrupt_polarity = IMS_LTR_303ALS_INT_ACTIVE_LOW;
|
||||
cfg->threshold_low = 0x0000U;
|
||||
cfg->threshold_high = 0xFFFFU;
|
||||
cfg->integration_time = IMS_LTR_303ALS_INT_250MS;
|
||||
cfg->measurement_rate = IMS_LTR_303ALS_MEAS_500MS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_ltr_303als_config(ims_ltr_303als_t *als, ims_ltr_303als_config_t *config) {
|
||||
ims_ret_t ret;
|
||||
|
||||
ret = ims_ltr_303als_config_timing(als, config->measurement_rate, config->integration_time);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_als_ltr_303als_set_gain(als, config->gain);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_als_ltr_303als_set_threshold(als, IMS_ALS_THRESH_LOW, config->threshold_low);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_als_ltr_303als_set_threshold(als, IMS_ALS_THRESH_HIGH, config->threshold_high);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_ltr_303als_config_irq(als, config->interrupt_polarity, config->interrupt_en);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ims_ret_t ims_ltr_303als_set_mode(ims_ltr_303als_t *als, ims_ltr_303als_mode_t mode) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_cont;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_CONTR, &r_cont);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
r_cont &= ~ALS_REG_CONTR_MODE_Msk;
|
||||
r_cont |= ((mode << ALS_REG_CONTR_MODE_Pos) & ALS_REG_CONTR_MODE_Msk);
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, ALS_REG_CONTR, r_cont);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ims_ret_t ims_ltr_303als_get_mode(ims_ltr_303als_t *als, ims_ltr_303als_mode_t *mode) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_cont;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_CONTR, &r_cont);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (r_cont & ALS_REG_CONTR_MODE_Msk) {
|
||||
*mode = IMS_LTR_303ALS_MODE_ACTIVE;
|
||||
} else {
|
||||
*mode = IMS_LTR_303ALS_MODE_STANDBY;
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_ltr_303als_read(ims_ltr_303als_t *als, ims_ltr_303als_result_t *result) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_status;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_STATUS, &r_status);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
result->gain = (r_status & ALS_REG_STATUS_GAIN_Msk) >> ALS_REG_STATUS_GAIN_Pos;
|
||||
|
||||
if (r_status & ALS_REG_STATUS_DATA_VALID_Msk) {
|
||||
result->data_valid = true;
|
||||
} else {
|
||||
result->data_valid = false;
|
||||
}
|
||||
|
||||
if (r_status & ALS_REG_STATUS_DATA_Msk) {
|
||||
result->data_new = true;
|
||||
} else {
|
||||
result->data_new = false;
|
||||
}
|
||||
|
||||
uint8_t r_data[2];
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_DATA_CH0_0, &r_data[0]);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_DATA_CH0_1, &r_data[1]);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
result->ch0_value = r_data[1] << 8U | r_data[0];
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_DATA_CH1_0, &r_data[0]);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_DATA_CH1_1, &r_data[1]);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
result->ch1_value = r_data[1] << 8U | r_data[0];
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_reset(ims_ltr_303als_t *als) {
|
||||
uint8_t r_cont;
|
||||
|
||||
if (ims_als_ltr_303als_read_register(als, ALS_REG_CONTR, &r_cont) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
r_cont |= ALS_REG_CONTR_SW_RST_Msk;
|
||||
|
||||
if (ims_als_ltr_303als_write_register(als, ALS_REG_CONTR, r_cont) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
do {
|
||||
if (ims_als_ltr_303als_read_register(als, ALS_REG_CONTR, &r_cont) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
als->cb.delay(als->pdev, 5);
|
||||
} while (r_cont & ALS_REG_CONTR_SW_RST_Msk);
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_read_id(ims_ltr_303als_t *als, ims_als_id_t *id) {
|
||||
if (ims_als_ltr_303als_read_register(als, ALS_REG_MANUFAC_ID, &id->mfg_id) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
if (ims_als_ltr_303als_read_register(als, ALS_REG_PART_ID, &id->part_id) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_set_gain(ims_ltr_303als_t *als, ims_ltr_303als_gain_t gain) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_cont;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_CONTR, &r_cont);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
r_cont &= ~ALS_REG_CONTR_GAIN_Msk;
|
||||
r_cont |= ((gain << ALS_REG_CONTR_GAIN_Pos) & ALS_REG_CONTR_GAIN_Msk);
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, ALS_REG_CONTR, r_cont);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_set_threshold(ims_ltr_303als_t *als, ims_als_thresh_t type, uint16_t thresh) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_thr;
|
||||
|
||||
r_thr = (thresh >> 8U) & 0xFFU;
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, type, r_thr);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
r_thr = thresh & 0xFFU;
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, type + 1, r_thr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_ltr_303als_config_irq(ims_ltr_303als_t *als, ims_ltr_303als_int_polarity_t pol, bool enable) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_interrupt;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_INTERRUPT, &r_interrupt);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
r_interrupt &= ~(ALS_REG_INTERRUPT_MODE_Msk | ALS_REG_INTERRUPT_POL_Msk);
|
||||
r_interrupt |= ((pol << ALS_REG_INTERRUPT_POL_Pos) & ALS_REG_INTERRUPT_POL_Msk);
|
||||
|
||||
if (enable) {
|
||||
r_interrupt |= ALS_REG_INTERRUPT_MODE_Msk;
|
||||
}
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, ALS_REG_INTERRUPT, r_interrupt);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_ltr_303als_config_timing(ims_ltr_303als_t *als, ims_ltr_303als_measurement_rate_t rate,
|
||||
ims_ltr_303als_integration_time_t time) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t r_measure_rate;
|
||||
|
||||
ret = ims_als_ltr_303als_read_register(als, ALS_REG_MEAS_RATE, &r_measure_rate);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
r_measure_rate &= ~ALS_REG_MEAS_INT_Msk;
|
||||
r_measure_rate |= ((time << ALS_REG_MEAS_INT_Pos) & ALS_REG_MEAS_INT_Msk);
|
||||
|
||||
r_measure_rate &= ~ALS_REG_MEAS_RATE_Msk;
|
||||
r_measure_rate |= ((rate << ALS_REG_MEAS_RATE_Pos) & ALS_REG_MEAS_RATE_Msk);
|
||||
|
||||
ret = ims_als_ltr_303als_write_register(als, ALS_REG_MEAS_RATE, r_measure_rate);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_read_register(ims_ltr_303als_t *als, uint8_t reg, uint8_t *value) {
|
||||
ims_i2c_xfer_desc_t xfer = {
|
||||
.tx_size = 1U,
|
||||
.tx_data = ®,
|
||||
.rx_size = 1U,
|
||||
.rx_data = value,
|
||||
};
|
||||
|
||||
if (als->cb.i2c_xfer(als->pdev, &xfer) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_als_ltr_303als_write_register(ims_ltr_303als_t *als, uint8_t reg, uint8_t value) {
|
||||
uint8_t tx_buf[2] = {reg, value};
|
||||
|
||||
ims_i2c_xfer_desc_t xfer = {
|
||||
.tx_size = 2U,
|
||||
.tx_data = tx_buf,
|
||||
.rx_size = 0U,
|
||||
.rx_data = NULL,
|
||||
};
|
||||
|
||||
if (als->cb.i2c_xfer(als->pdev, &xfer) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
|
@ -1,35 +1,35 @@
|
|||
#include <stddef.h>
|
||||
|
||||
/* Private header */
|
||||
#include "aht10.h"
|
||||
#include "dht_aht10.h"
|
||||
|
||||
#define AHT10_STATUS_CALIBRATED_MASK (1U << 3U)
|
||||
#define AHT10_STATUS_BUSY_MASK (1U << 7U)
|
||||
|
||||
#define AHT10_ERROR_CHECK(x) \
|
||||
if (x != AHT10_OK) return AHT10_FAIL
|
||||
if (x != IMS_SUCCESS) return IMS_FAIL
|
||||
|
||||
typedef struct {
|
||||
uint8_t status;
|
||||
uint32_t temperature;
|
||||
uint32_t humidity;
|
||||
} aht10_raw_t;
|
||||
} ims_aht10_raw_t;
|
||||
|
||||
static aht10_ret_t aht10_software_reset(aht10_t *aht) {
|
||||
static ims_ret_t aht10_software_reset(ims_aht10_t *aht) {
|
||||
/* Reset and initialize device */
|
||||
|
||||
uint8_t tx_data[3] = {0xBA};
|
||||
|
||||
aht10_xfer_desc_t xfer = {
|
||||
ims_i2c_xfer_desc_t xfer = {
|
||||
.tx_data = tx_data,
|
||||
.tx_size = 1U,
|
||||
.rx_data = NULL,
|
||||
.rx_size = 0U,
|
||||
};
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.xfer(aht->user_data, &xfer));
|
||||
AHT10_ERROR_CHECK(aht->cb.i2c_xfer(aht->pdev, &xfer));
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->user_data, 20));
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->pdev, 20));
|
||||
|
||||
tx_data[0] = 0xE1;
|
||||
tx_data[1] = 0x08;
|
||||
|
@ -37,42 +37,42 @@ static aht10_ret_t aht10_software_reset(aht10_t *aht) {
|
|||
|
||||
xfer.tx_size = 3U;
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.xfer(aht->user_data, &xfer));
|
||||
AHT10_ERROR_CHECK(aht->cb.i2c_xfer(aht->pdev, &xfer));
|
||||
|
||||
return AHT10_OK;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static aht10_ret_t aht10_start_measurement(aht10_t *aht) {
|
||||
static ims_ret_t aht10_start_measurement(ims_aht10_t *aht) {
|
||||
/* Start measurement */
|
||||
|
||||
uint8_t tx_data[3] = {0xAC, 0x33, 0x00};
|
||||
|
||||
aht10_xfer_desc_t xfer = {
|
||||
ims_i2c_xfer_desc_t xfer = {
|
||||
.tx_data = tx_data,
|
||||
.tx_size = 3U,
|
||||
.rx_data = NULL,
|
||||
.rx_size = 0U,
|
||||
};
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.xfer(aht->user_data, &xfer));
|
||||
AHT10_ERROR_CHECK(aht->cb.i2c_xfer(aht->pdev, &xfer));
|
||||
|
||||
return AHT10_OK;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static aht10_ret_t aht10_read_result(aht10_t *aht, aht10_raw_t *raw) {
|
||||
static ims_ret_t aht10_read_result(ims_aht10_t *aht, ims_aht10_raw_t *raw) {
|
||||
/* Read conversion result */
|
||||
|
||||
uint8_t tx_data[] = {0x71};
|
||||
uint8_t rx_data[6];
|
||||
|
||||
aht10_xfer_desc_t xfer = {
|
||||
ims_i2c_xfer_desc_t xfer = {
|
||||
.tx_data = tx_data,
|
||||
.tx_size = 1U,
|
||||
.rx_data = rx_data,
|
||||
.rx_size = 6U,
|
||||
};
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.xfer(aht->user_data, &xfer));
|
||||
AHT10_ERROR_CHECK(aht->cb.i2c_xfer(aht->pdev, &xfer));
|
||||
|
||||
raw->humidity = rx_data[1] << 12U;
|
||||
raw->humidity |= rx_data[2] << 4U;
|
||||
|
@ -84,16 +84,16 @@ static aht10_ret_t aht10_read_result(aht10_t *aht, aht10_raw_t *raw) {
|
|||
|
||||
raw->status = rx_data[0];
|
||||
|
||||
return AHT10_OK;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
aht10_ret_t aht10_init(aht10_t *aht) {
|
||||
ims_ret_t aht10_init(ims_aht10_t *aht) {
|
||||
/* Check if we need initialization */
|
||||
|
||||
/* Delay 40ms after POR */
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->user_data, 40));
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->pdev, 40));
|
||||
|
||||
aht10_raw_t raw;
|
||||
ims_aht10_raw_t raw;
|
||||
|
||||
/* Check 0x71 status byte, if uncalibrated then reset and calibrate it. */
|
||||
AHT10_ERROR_CHECK(aht10_read_result(aht, &raw));
|
||||
|
@ -101,17 +101,17 @@ aht10_ret_t aht10_init(aht10_t *aht) {
|
|||
AHT10_ERROR_CHECK(aht10_software_reset(aht));
|
||||
}
|
||||
|
||||
return AHT10_OK;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
aht10_ret_t aht10_measure(aht10_t *aht, aht10_result_t *result) {
|
||||
ims_ret_t aht10_measure(ims_aht10_t *aht, ims_aht10_result_t *result) {
|
||||
/* Start a measurement and read back result... */
|
||||
|
||||
AHT10_ERROR_CHECK(aht10_start_measurement(aht));
|
||||
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->user_data, 80));
|
||||
AHT10_ERROR_CHECK(aht->cb.delay(aht->pdev, 80));
|
||||
|
||||
aht10_raw_t raw;
|
||||
ims_aht10_raw_t raw;
|
||||
|
||||
uint8_t max_retries = 5U;
|
||||
|
||||
|
@ -119,10 +119,10 @@ aht10_ret_t aht10_measure(aht10_t *aht, aht10_result_t *result) {
|
|||
AHT10_ERROR_CHECK(aht10_read_result(aht, &raw));
|
||||
max_retries--;
|
||||
if (max_retries == 0) {
|
||||
return AHT10_FAIL;
|
||||
return IMS_FAIL;
|
||||
}
|
||||
}
|
||||
#ifndef AHT10_SKIP_BUSY_CHECK
|
||||
#ifdef IMS_CFG_DHT_AHT10_BUSY_CHECK
|
||||
while (raw.status & AHT10_STATUS_BUSY_MASK);
|
||||
#else
|
||||
while (0);
|
||||
|
@ -131,5 +131,5 @@ aht10_ret_t aht10_measure(aht10_t *aht, aht10_result_t *result) {
|
|||
result->temperature = ((double)raw.temperature * 200.0 / 1048576.0) - 50.0;
|
||||
result->humidity = (double)raw.humidity * 100.0 / 1048576.0;
|
||||
|
||||
return AHT10_OK;
|
||||
return IMS_SUCCESS;
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
#include <string.h>
|
||||
|
||||
/* Common */
|
||||
#include "sensors_common.h"
|
||||
|
||||
/* Private */
|
||||
#include "dht_bme280.h"
|
||||
|
||||
#define IMS_BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
|
||||
|
@ -23,7 +26,7 @@ static ims_ret_t ims_bme280_read_register(ims_bme280_t *bme, uint8_t reg, uint8_
|
|||
.rx_data = data,
|
||||
};
|
||||
|
||||
return bme->cb.i2c_xfer(bme->user_data, bme->i2c_addr, &xfer_desc);
|
||||
return bme->cb.i2c_xfer(bme->pdev, &xfer_desc);
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_write_register(ims_bme280_t *bme, uint8_t reg, uint8_t data) {
|
||||
|
@ -35,7 +38,7 @@ static ims_ret_t ims_bme280_write_register(ims_bme280_t *bme, uint8_t reg, uint8
|
|||
.rx_data = NULL,
|
||||
};
|
||||
|
||||
return bme->cb.i2c_xfer(bme->user_data, bme->i2c_addr, &xfer_desc);
|
||||
return bme->cb.i2c_xfer(bme->pdev, &xfer_desc);
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_reset(ims_bme280_t *bme) {
|
||||
|
@ -130,7 +133,7 @@ static ims_ret_t ims_bme280_measure_raw(ims_bme280_t *bme, uint32_t *raw_t, uint
|
|||
do {
|
||||
if (ims_bme280_read_register(bme, 0xF3, &status, 0x01) != IMS_SUCCESS) return IMS_FAIL;
|
||||
loop_count++;
|
||||
bme->cb.delay(bme->user_data, 100);
|
||||
bme->cb.delay(bme->pdev, 5);
|
||||
} while (status & 0x08 && (loop_count < 12));
|
||||
|
||||
if (ims_bme280_read_register(bme, 0xF7, measure_data, 0x08) != IMS_SUCCESS) return IMS_FAIL;
|
||||
|
@ -146,7 +149,7 @@ static ims_ret_t ims_bme280_measure_raw(ims_bme280_t *bme, uint32_t *raw_t, uint
|
|||
ims_ret_t ims_bme280_init(ims_bme280_t *bme) {
|
||||
if (!bme) return IMS_FAIL;
|
||||
if (ims_bme280_reset(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
bme->cb.delay(bme->user_data, 100);
|
||||
bme->cb.delay(bme->pdev, 100);
|
||||
if (ims_bme280_read_trim_data(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
@ -197,7 +200,7 @@ ims_ret_t ims_bme280_apply_config(ims_bme280_t *bme, ims_bme280_config_t *config
|
|||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
|
||||
ims_ret_t ims_bme280_measure_float(ims_bme280_t *bme, ims_bme280_result_float_t *result) {
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
|
@ -258,7 +261,7 @@ static double ims_bme280_compensate_H_double(ims_bme280_t *bme, int32_t adc_H) {
|
|||
}
|
||||
#else
|
||||
|
||||
ims_ret_t ims_bme280_measure_int32(ims_bme280_t *bme, ims_bme280_result_int32_t *result) {
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
|
|
Loading…
Reference in New Issue