1 /* 2 * Copyright (c) 2021 Leonard Pollak 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef ZEPHYR_DRIVERS_SENSOR_INA219_INA219_H_ 8 #define ZEPHYR_DRIVERS_SENSOR_INA219_INA219_H_ 9 10 /* Device register addresses */ 11 #define INA219_REG_CONF 0x00 12 #define INA219_REG_V_SHUNT 0x01 13 #define INA219_REG_V_BUS 0x02 14 #define INA219_REG_POWER 0x03 15 #define INA219_REG_CURRENT 0x04 16 #define INA219_REG_CALIB 0x05 17 18 /* Config register shifts and masks */ 19 #define INA219_RST BIT(15) 20 #define INA219_BRNG_MASK 0x1 21 #define INA219_BRNG_SHIFT 13 22 #define INA219_PG_MASK 0x3 23 #define INA219_PG_SHIFT 11 24 #define INA219_ADC_MASK 0xF 25 #define INA219_BADC_SHIFT 7 26 #define INA219_SADC_SHIFT 3 27 #define INA219_MODE_MASK 0x7 28 29 /* Bus voltage register */ 30 #define INA219_VBUS_GET(x) ((x) >> 3) & 0x3FFF 31 #define INA219_CNVR_RDY(x) ((x) >> 1) & 0x1 32 #define INA219_OVF_STATUS(x) x & 0x1 33 34 /* Mode fields */ 35 #define INA219_MODE_NORMAL 0x3 /* shunt and bus, triggered */ 36 #define INA219_MODE_SLEEP 0x4 /* ADC off */ 37 #define INA219_MODE_OFF 0x0 /* Power off */ 38 39 /* Others */ 40 #define INA219_SIGN_BIT(x) ((x) >> 15) & 0x1 41 #define INA219_V_BUS_MUL 0.004 42 #define INA219_SI_MUL 0.00001 43 #define INA219_POWER_MUL 20 44 #define INA219_WAIT_STARTUP 40 45 #define INA219_WAIT_MSR_RETRY 100 46 #define INA219_SCALING_FACTOR 4096000 47 48 struct ina219_config { 49 struct i2c_dt_spec bus; 50 uint16_t current_lsb; 51 uint16_t r_shunt; 52 uint8_t brng; 53 uint8_t pg; 54 uint8_t badc; 55 uint8_t sadc; 56 uint8_t mode; 57 }; 58 59 struct ina219_data { 60 uint16_t config; 61 uint16_t v_bus; 62 uint16_t power; 63 uint16_t current; 64 uint16_t calib; 65 uint32_t msr_delay; 66 }; 67 68 static int ina219_init(const struct device *dev); 69 ina219_conv_delay(uint8_t delay_idx)70static inline int ina219_conv_delay(uint8_t delay_idx) 71 { 72 switch (delay_idx) { 73 case 0: 74 return 84; 75 case 1: 76 return 148; 77 case 2: 78 return 276; 79 case 3: 80 return 532; 81 case 9: 82 return 1060; 83 case 10: 84 return 2013; 85 case 11: 86 return 4260; 87 case 12: 88 return 8510; 89 case 13: 90 return 17020; 91 case 14: 92 return 34050; 93 case 15: 94 return 68100; 95 default: 96 return -EINVAL; 97 } 98 } 99 100 #endif /* ZEPHYR_DRIVERS_SENSOR_INA219_INA219_H_ */ 101