#include #include #include #include "aos/kernel.h" #include "sensor_drv_api.h" #include "sensor_hal.h" #define LTR390_I2C_SLAVE_ADDR 0x53 #define LTR390_MAIN_CTRL 0x00 /* Operation mode control */ #define LTR390_ALS_UVS_MEAS_RATE 0x04 /* Measurement rate and resolution */ #define LTR390_ALS_UVS_GAIN 0x05 /* Analog gain range*/ #define LTR390_PART_ID 0x06 #define LTR390_MAIN_STATUS 0x07 #define LTR390_ALS_DATA_0 0x0D #define LTR390_ALS_DATA_1 0x0E #define LTR390_ALS_DATA_2 0x0F #define LTR390_UVS_DATA_0 0x10 #define LTR390_UVS_DATA_1 0x11 #define LTR390_UVS_DATA_2 0x12 #define LTR390_INT_CFG 0x19 #define LTR390_INT_PST 0x1A /* Interrupt persist setting */ #define LTR390_ALS_UVS_THRES_UP_0 0x21 /* Interrupt upper threshold, lower byte */ #define LTR390_ALS_UVS_THRES_UP_1 0x22 /* Interrupt upper threshold, middle byte */ #define LTR390_ALS_UVS_THRES_UP_2 0x23 /* Interrupt upper threshold, upper byte */ #define LTR390_ALS_UVS_THRES_LOW_0 0x24 /* Interrupt lower threshold, lower byte */ #define LTR390_ALS_UVS_THRES_LOW_1 0x25 /* Interrupt lower threshold, middle byte */ #define LTR390_ALS_UVS_THRES_LOW_2 0x26 /* Interrupt lower threshold, upper byte */ #define LTR390_ADDR_TRANS(n) ((n) << 1) #define LTR390_I2C_ADDR LTR390_ADDR_TRANS(LTR390_I2C_SLAVE_ADDR) #define LTR390_PART_ID_VAL 0xB2 #define LTR390_MAIN_CTRL_REG_ALS_UVS_EN__POS (1) #define LTR390_MAIN_CTRL_REG_ALS_UVS_EN__MSK (0x02) #define LTR390_MAIN_CTRL_REG_ALS_UVS_EN__REG (LTR390_MAIN_CTRL) #define LTR390_MAIN_CTRL_REG_UVS_MODE__POS (3) #define LTR390_MAIN_CTRL_REG_UVS_MODE__MSK (0x08) #define LTR390_MAIN_CTRL_REG_UVS_MODE__REG (LTR390_MAIN_CTRL) #define LTR390_MAIN_CTRL_REG_SW_RESET__POS (4) #define LTR390_MAIN_CTRL_REG_SW_RESET__MSK (0x10) #define LTR390_MAIN_CTRL_REG_SW_RESET__REG (LTR390_MAIN_CTRL) #define LTR390_ALS_UVS_MEAS_RATE_REG_MEAS_RATE__POS (0) #define LTR390_ALS_UVS_MEAS_RATE_REG_MEAS_RATE__MSK (0x07) #define LTR390_ALS_UVS_MEAS_RATE_REG_MEAS_RATE__REG (LTR390_ALS_UVS_MEAS_RATE) #define LTR390_ALS_UVS_MEAS_RATE_REG_RESOLUTION__POS (4) #define LTR390_ALS_UVS_MEAS_RATE_REG_RESOLUTION__MSK (0x70) #define LTR390_ALS_UVS_MEAS_RATE_REG_RESOLUTION__REG (LTR390_ALS_UVS_MEAS_RATE) #define LTR390_ALS_UVS_GAIN_REG_GAIN_RANGE__POS (0) #define LTR390_ALS_UVS_GAIN_REG_GAIN_RANGE__MSK (0x07) #define LTR390_ALS_UVS_GAIN_REG_GAIN_RANGE__REG (LTR390_ALS_UVS_GAIN) #define LTR390_MAIN_STATUS_REG_ALS_UVS_DATA_STATUS__POS (3) #define LTR390_MAIN_STATUS_REG_ALS_UVS_DATA_STATUS__MSK (0x08) #define LTR390_MAIN_STATUS_REG_ALS_UVS_DATA_STATUS__REG (LTR390_MAIN_STATUS) #define LTR390_MAIN_STATUS_REG_ALS_UVS_INT_STATUS__POS (4) #define LTR390_MAIN_STATUS_REG_ALS_UVS_INT_STATUS__MSK (0x10) #define LTR390_MAIN_STATUS_REG_ALS_UVS_INT_STATUS__REG (LTR390_MAIN_STATUS) #define LTR390_MAIN_STATUS_REG_POWER_ON_STATUS__POS (5) #define LTR390_MAIN_STATUS_REG_POWER_ON_STATUS__MSK (0x20) #define LTR390_MAIN_STATUS_REG_POWER_ON_STATUS__REG (LTR390_MAIN_STATUS) #define LTR390_INT_CFG_REG_LS_INT_EN__POS (2) #define LTR390_INT_CFG_REG_LS_INT_EN__MSK (0x04) #define LTR390_INT_CFG_REG_LS_INT_EN__REG (LTR390_INT_CFG) #define LTR390_INT_CFG_REG_LS_INT_SEL__POS (4) #define LTR390_INT_CFG_REG_LS_INT_SEL__MSK (0x30) #define LTR390_INT_CFG_REG_LS_INT_SEL__REG (LTR390_INT_CFG) #define LTR390_INT_PST_REG_ALS_UV_PERSIST__POS (4) #define LTR390_INT_PST_REG_ALS_UV_PERSIST__MSK (0xF0) #define LTR390_INT_PST_REG_ALS_UV_PERSIST__REG (LTR390_INT_PST) #define LTR390_GET_BITSLICE(regvar, bitname) ((regvar & LTR390_##bitname##__MSK) >> LTR390_##bitname##__POS) #define LTR390_SET_BITSLICE(regvar, bitname, val) ((regvar & ~LTR390_##bitname##__MSK) | ((val<uvi = ((((uint16_t) (reg_data[1] & 0xFF)) << 8) | reg_data[0]); pdata->timestamp = aos_now_ms(); return (int) size; } static int drv_uv_liteon_ltr390_write(const void *buf, size_t len) { (void) buf; (void) len; return 0; } static int drv_uv_liteon_ltr390_ioctl(int cmd, unsigned long arg) { int ret = 0; switch (cmd) { case SENSOR_IOCTL_SET_POWER: { ret = drv_uv_liteon_ltr390_set_power_mode(<r390_ctx, arg); if (unlikely(ret)) { return -1; } } break; case SENSOR_IOCTL_GET_INFO: { /* fill the dev info here */ dev_sensor_info_t *info = (dev_sensor_info_t *) arg; info->vendor = DEV_SENSOR_VENDOR_LITEON; info->model = "LTR390"; info->unit = lux; } break; default: return -1; } LOG("%s %s successfully \n", SENSOR_STR, __func__); return 0; } int drv_uv_liteon_ltr390_init(void) { int ret = 0; sensor_obj_t sensor_uv; memset(&sensor_uv, 0, sizeof(sensor_uv)); if (!g_init_bitwise) { ret = drv_uv_liteon_ltr390_validate_id(<r390_ctx, LTR390_PART_ID_VAL); if (unlikely(ret)) { return -1; } } if (!g_init_bitwise) { /* fill the sensor_uv obj parameters here */ sensor_uv.tag = TAG_DEV_UV; sensor_uv.path = dev_uv_path; sensor_uv.io_port = I2C_PORT; sensor_uv.mode = DEV_POLLING; sensor_uv.power = DEV_POWER_OFF; sensor_uv.open = drv_uv_liteon_ltr390_open; sensor_uv.close = drv_uv_liteon_ltr390_close; sensor_uv.read = drv_uv_liteon_ltr390_read; sensor_uv.write = drv_uv_liteon_ltr390_write; sensor_uv.ioctl = drv_uv_liteon_ltr390_ioctl; sensor_uv.irq_handle = drv_uv_liteon_ltr390_irq_handle; ret = sensor_create_obj(&sensor_uv); if (unlikely(ret)) { return -1; } ret = drv_uv_liteon_ltr390_set_default_config(<r390_ctx); if (unlikely(ret)) { return -1; } g_init_bitwise = 1; } LOG("%s %s successfully \n", SENSOR_STR, __func__); return 0; } SENSOR_DRV_ADD(drv_uv_liteon_ltr390_init);