282 lines
8.6 KiB
C
282 lines
8.6 KiB
C
/*
|
|
* i2c_sensors.h
|
|
*
|
|
* Created on: Jan 23, 2023
|
|
* Author: Sword
|
|
*/
|
|
|
|
#ifndef MAIN_I2C_SENSORS_H_
|
|
#define MAIN_I2C_SENSORS_H_
|
|
|
|
// Sensor Addresses
|
|
#define ENS210_ADDR 0x43 // Base address of 0x43 left shifted one bit to make room for the read/#write bit. Temperature and humidity sensor
|
|
#define MCP9600_ADDR 0x60 // Base address of 0x60 left shifted one bit to make room for the read/#write bit. Thermocouple interface chip
|
|
#define MC3419_ADDR 0xD8 // Base address of 0x6C left shifted one bit to make room for the read/#write bit. Accelerometer
|
|
#define OPT3001_ADDR 0x44
|
|
|
|
// ENS210 Registers
|
|
#define ENS210_PART_ID 0x00
|
|
#define ENS210_DIE_REV 0x02
|
|
#define ENS210_UID 0x04
|
|
#define ENS210_SYS_CTRL 0x10
|
|
#define ENS210_SYS_STAT 0x11
|
|
#define ENS210_SENS_RUN 0x21
|
|
#define ENS210_SENS_START 0x22
|
|
#define ENS210_SENS_STOP 0x23
|
|
#define ENS210_SENS_STAT 0x24
|
|
#define ENS210_T_VAL 0x30
|
|
#define ENS210_H_VAL 0x33
|
|
|
|
// MCP9600 Registers
|
|
#define MCP9600_HOT_JUNCTION_REG 0x00
|
|
#define MCP9600_DELTA_JUNCTION_REG 0x01
|
|
#define MCP9600_COLD_JUNCTION_REG 0x02
|
|
#define MCP9600_STATUS_REG 0x04
|
|
#define MCP9600_THERMOCOUPLE_CONFIG 0x05
|
|
#define MCP9600_DEVICE_CONFIG_REG 0x06
|
|
#define MCP9600_ALERT1_CONFIG_REG 0x08
|
|
#define MCP9600_ALERT2_CONFIG_REG 0x09
|
|
#define MCP9600_ALERT3_CONFIG_REG 0x0A
|
|
#define MCP9600_ALERT4_CONFIG_REG 0x0B
|
|
#define MCP9600_ALERT1_HYST_REG 0x0C
|
|
#define MCP9600_ALERT2_HYST_REG 0x0D
|
|
#define MCP9600_ALERT3_HYST_REG 0x0E
|
|
#define MCP9600_ALERT4_HYST_REG 0x0F
|
|
#define MCP9600_ALERT1_LIMIT_REG 0x10
|
|
#define MCP9600_ALERT2_LIMIT_REG 0x11
|
|
#define MCP9600_ALERT3_LIMIT_REG 0x12
|
|
#define MCP9600_ALERT4_LIMIT_REG 0x13
|
|
#define MCP9600_DEVICE_ID_REVISION 0x20
|
|
|
|
// MCP9600 Bits
|
|
#define MCP9600_INPUT_RANGE_BIT 0x10
|
|
#define MCP9600_BURST_COMPLETE_BIT 0x80
|
|
#define MCP9600_TH_UPDATE_BIT 0x40
|
|
#define MCP9600_ALERT_ENABLE_BIT 0x01
|
|
|
|
#define MCP9600_ALERT1_STATUS 0x01
|
|
#define MCP9600_ALERT2_STATUS 0x02
|
|
#define MCP9600_ALERT3_STATUS 0x04
|
|
#define MCP9600_ALERT4_STATUS 0x08
|
|
|
|
// Thermocouple types:
|
|
#define THERMOCOUPLE_TYPE_K 0x00
|
|
#define THERMOCOUPLE_TYPE_J 0x10
|
|
#define THERMOCOUPLE_TYPE_T 0x20
|
|
#define THERMOCOUPLE_TYPE_N 0x30
|
|
#define THERMOCOUPLE_TYPE_S 0x40
|
|
#define THERMOCOUPLE_TYPE_E 0x50
|
|
#define THERMOCOUPLE_TYPE_B 0x60
|
|
#define THERMOCOUPLE_TYPE_R 0x70
|
|
|
|
// MACRO DEFINITIONS
|
|
|
|
//MC3419
|
|
#define MC3419_ADDR 0x6C
|
|
|
|
//MC3419 Modes
|
|
#define MC3419_STANDBY_MODE 0x00
|
|
#define MC3419_WAKE_MODE 0x01
|
|
|
|
// MC3419 Registers
|
|
#define DEVICE_STATUS_REGISTER 0x05
|
|
#define INTERRUPT_ENABLE_REGISTER 0x06
|
|
#define GPIO_CONTROL_REGISTER 0x33
|
|
#define MODE_REGISTER 0x07
|
|
#define SAMPLE_RATE_REGISTER 0x08
|
|
#define MOTION_CONTROL_REGISTER 0x09
|
|
|
|
#define FIFO_STATUS_REGISTER 0x0A
|
|
#define FIFO_READ_POINTER_REGISTER 0x0B
|
|
#define FIFO_WRITE_POINTER_REGISTER 0x0C
|
|
|
|
#define XOUT_ACCELEROMETER_DATA_LSB_REGISTER 0x0D
|
|
#define XOUT_ACCELEROMETER_DATA_MSB_REGISTER 0x0E
|
|
#define YOUT_ACCELEROMETER_DATA_LSB_REGISTER 0x0F
|
|
#define YOUT_ACCELEROMETER_DATA_MSB_REGISTER 0x10
|
|
#define ZOUT_ACCELEROMETER_DATA_LSB_REGISTER 0x11
|
|
#define ZOUT_ACCELEROMETER_DATA_MSB_REGISTER 0x12
|
|
|
|
#define STATUS_REGISTER 0x13
|
|
#define INTERRUPT_STATUS_REGISTER 0x14
|
|
#define CHIP_IDENTIFICATION_REGISTER 0x18
|
|
#define RESET_REGISTER 0x1C
|
|
#define RANGE_SELECT_CONTROL_REGISTER 0x20
|
|
|
|
#define X_OFFSET_LSB_REGISTER 0x21
|
|
#define X_OFFSET_MSB_REGISTER 0x22
|
|
#define Y_OFFSET_LSB_REGISTER 0x23
|
|
#define Y_OFFSET_MSB_REGISTER 0x24
|
|
#define Z_OFFSET_LSB_REGISTER 0x25
|
|
#define Z_OFFSET_MSB_REGISTER 0x26
|
|
|
|
#define X_GAIN_REGISTER 0x27
|
|
#define Y_GAIN_REGISTER 0x28
|
|
#define Z_GAIN_REGISTER 0x29
|
|
|
|
#define FIFO_CONTROL_REGISTER 0x2D
|
|
#define FIFO_THRESHOLD_REGISTER 0x2E
|
|
#define FIFO_INTERRUPT_STATUS_REGISTER 0x2F
|
|
#define FIFO_CONTROL_2_AND_SAMPLE_RATE_2_REGISTER 0x30
|
|
|
|
#define COMM_CONTROL_REGISTER 0x31
|
|
#define GPIO_CONTROL_REGISTER 0x33
|
|
#define SECURITY_OPT_REGISTER 0x3B
|
|
|
|
#define TILT_FLIP_THRESHOLD_LSB_REGISTER 0x40
|
|
#define TILT_FLIP_THRESHOLD_MSB_REGISTER 0x41
|
|
#define TILT_FLIP_DEBOUNCE_REGISTER 0x42
|
|
|
|
#define ANY_MOTION_THRESHOLD_LSB_REGISTER 0x43
|
|
#define ANY_MOTION_THRESHOLD_MSB_REGISTER 0x44
|
|
#define ANY_MOTION_DEBOUNCE_REGISTER 0x45
|
|
|
|
#define SHAKE_THRESHOLD_LSB_REGISTER 0x46
|
|
#define SHAKE_THRESHOLD_MSB_REGISTER 0x47
|
|
#define PEAK_TO_PEAK_DURATION_LSB_REGISTER 0x48
|
|
#define SHAKE_DURATION_AND_PEAK_TO_PEAK_DURATION_MSB_REGISTER 0x49
|
|
|
|
#define TIMER_CONTROL_REGISTER 0x4A
|
|
#define READ_COUNT_REGISTER 0x4B
|
|
|
|
|
|
//OPT3001 Registers addresses
|
|
#define RESULT_REGISTER 0x00
|
|
#define CONFIGURATION_REGISTER 0x01
|
|
#define LOW_LIMIT_REGISTER 0x02
|
|
#define HIGH_LIMIT_REGISTER 0x03
|
|
#define MANUFACTURER_ID_REGISTER 0x7E
|
|
#define DEVICE_ID_REGISTER 0x7F
|
|
|
|
|
|
//Enable/Disable ACK
|
|
#define ACK_EN 1
|
|
#define NACK_EN 0
|
|
|
|
|
|
typedef enum
|
|
{
|
|
ALERT1 = 1,
|
|
ALERT2,
|
|
ALERT3,
|
|
ALERT4,
|
|
}alertNum_t;
|
|
|
|
typedef enum
|
|
{
|
|
CONVERSION_TIME_100Ms,
|
|
CONVERSION_TIME_800Ms
|
|
}lightConversionTime_t;
|
|
|
|
typedef enum
|
|
{
|
|
SHUT_DOWN_CONVERSION_MODE,
|
|
SINGLE_SHOT_CONVERSION_MODE,
|
|
CONTINUOUS_CONVERSION_MODE
|
|
}lightConversionMode_t;
|
|
|
|
typedef enum
|
|
{
|
|
CONVERSION_READY_FLAG_IS_SET,
|
|
CONVERSION_READY_FLAG_IS_NOT_SET_YET,
|
|
FAILED_TO_READ_CONVERSION_READY_FLAG,
|
|
}conversionFlagState_t;
|
|
|
|
typedef enum
|
|
{
|
|
FULL_SCALE_RANGE_LUX_40,
|
|
FULL_SCALE_RANGE_LUX_82,
|
|
FULL_SCALE_RANGE_LUX_164,
|
|
FULL_SCALE_RANGE_LUX_327,
|
|
FULL_SCALE_RANGE_LUX_655,
|
|
FULL_SCALE_RANGE_LUX_1314,
|
|
FULL_SCALE_RANGE_LUX_2621,
|
|
FULL_SCALE_RANGE_LUX_5241,
|
|
FULL_SCALE_RANGE_LUX_10483,
|
|
FULL_SCALE_RANGE_LUX_20966,
|
|
FULL_SCALE_RANGE_LUX_41933,
|
|
FULL_SCALE_RANGE_LUX_83865,
|
|
}fullScaleRangeLux_t;
|
|
|
|
/*union for 2 bytes access and indivisual byte access*/
|
|
union byteAccessibleInt16
|
|
{
|
|
int16_t accessAsInt16;
|
|
uint8_t accessAsByte[2];
|
|
};
|
|
|
|
union byteAccessibleUint16
|
|
{
|
|
uint16_t accessAsUint16;
|
|
uint8_t accessAsByte[2];
|
|
};
|
|
|
|
//Initialize the esp32 as i2c-master
|
|
void i2c_master_init(void);
|
|
|
|
//Start-measuring and Reading function for the temperature and humidity sensor
|
|
bool i2c_initialize_temp_humidity_sensor(void);
|
|
esp_err_t i2c_ens210_fetch_dev_id(void);
|
|
uint16_t get_ens210_devId(void);
|
|
esp_err_t i2c_start_temp_humid(void);
|
|
esp_err_t i2c_stop_temp_humid(void);
|
|
esp_err_t i2c_ens210_set_active_mode(void);
|
|
esp_err_t i2c_fetch_temp(void);
|
|
esp_err_t i2c_fetch_humid(void);
|
|
float get_temperature_data(void);
|
|
float get_prev_temperature_data(void);
|
|
float get_humidity_data(void);
|
|
float get_prev_humidity_data(void);
|
|
|
|
//Read the accelerometer input interrupt-flag
|
|
void readAccelerometerInterruptFlag(void);
|
|
|
|
//Start-measuring and Reading functions for the Thermocouple sensor
|
|
void detectThermocoupleChip(void);
|
|
void startThermocoupleConversion(void);
|
|
void readThermocoupleTemperature(void);
|
|
|
|
bool i2c_initialize_accel_sensor(void);
|
|
esp_err_t i2c_fetch_mc3419_chipId(void);
|
|
uint16_t get_mc3419_chipId(void);
|
|
esp_err_t i2c_fetch_accel_pos(void);
|
|
esp_err_t i2c_set_mc3419_mode(uint8_t mode);
|
|
esp_err_t i2c_get_mc3419_state(void);
|
|
esp_err_t i2c_clear_mc3419_int_flag(void);
|
|
esp_err_t i2c_get_mc3419_int_flags(uint8_t *flags);
|
|
esp_err_t i2c_enable_mc3419_motion_int(bool enableFlag);
|
|
esp_err_t i2c_set_mc3419_motionThreshold(uint16_t threshold);
|
|
uint16_t get_accel_x(void);
|
|
uint16_t get_accel_y(void);
|
|
uint16_t get_accel_z(void);
|
|
|
|
|
|
bool i2c_initialize_thermocouple_sensor(void);
|
|
esp_err_t i2c_mcp9600_fetch_dev_id(void);
|
|
uint16_t get_mcp9600_devId(void);
|
|
esp_err_t i2c_start_thermocoupleConversion(void);
|
|
esp_err_t i2c_fetch_thermocoupleTemp(void);
|
|
float get_thermocouple_data(void);
|
|
float get_prev_thermocouple_data(void);
|
|
esp_err_t i2c_mcp9600_set_burst_mode(void);
|
|
esp_err_t i2c_mcp9600_set_alert_config(alertNum_t alertNum);
|
|
esp_err_t i2c_mcp9600_set_alert_limit(alertNum_t alertNum, float alertLimit);
|
|
esp_err_t i2c_mcp9600_set_alert_hyst(alertNum_t alertNum, float alertHyst);
|
|
esp_err_t i2c_mcp9600_set_sleep(void);
|
|
esp_err_t i2c_mcp9600_set_type(uint8_t thermocoupleType);
|
|
|
|
|
|
bool i2c_initialize_light_sensor(void);
|
|
esp_err_t i2c_opt3001_fetch_dev_id(void);
|
|
esp_err_t i2c_opt3001_fetch_manufact_id(void);
|
|
uint16_t i2c_opt3001_fetch_configuration(void);
|
|
esp_err_t i2c_opt3001_fetch_light_data(void);
|
|
conversionFlagState_t i2c_opt3001_get_conv_ready_flag(void);
|
|
uint32_t get_light_data(void);
|
|
uint32_t get_prev_light_data(void);
|
|
esp_err_t i2c_opt3001_set_full_lux_scale(fullScaleRangeLux_t scale);
|
|
esp_err_t i2c_opt3001_set_conversion_time(lightConversionTime_t CTime);
|
|
esp_err_t i2c_opt3001_set_conversion_mode(lightConversionMode_t CMode);
|
|
|
|
#endif /* MAIN_I2C_SENSORS_H_ */
|