aboutsummaryrefslogtreecommitdiff
path: root/driver_fw/ina226.c
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2019-04-15 12:28:22 +0900
committerjaseg <git@jaseg.net>2019-04-15 12:28:22 +0900
commita8448faccabdcf5ca1fe05760bbbfb78a1c5e917 (patch)
tree465e8f9159bec0086300f2ededbe726ad3c95826 /driver_fw/ina226.c
parenta860bd27a0950e4dd8ae530f9095a91ced7436a3 (diff)
download8seg-a8448faccabdcf5ca1fe05760bbbfb78a1c5e917.tar.gz
8seg-a8448faccabdcf5ca1fe05760bbbfb78a1c5e917.tar.bz2
8seg-a8448faccabdcf5ca1fe05760bbbfb78a1c5e917.zip
driver/fw: current sensor works
Diffstat (limited to 'driver_fw/ina226.c')
-rw-r--r--driver_fw/ina226.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/driver_fw/ina226.c b/driver_fw/ina226.c
new file mode 100644
index 0000000..d73703b
--- /dev/null
+++ b/driver_fw/ina226.c
@@ -0,0 +1,27 @@
+
+#include "global.h"
+#include "i2c.h"
+#include "ina226.h"
+
+void ina226_init() {
+ /* FIXME plug in final values for I meas backchannel */
+ uint16_t config = INA226_CONFIG_AVG_128 | INA226_CONFIG_VBUSCT_204u | INA226_CONFIG_VSHCT_1m1 |
+ INA226_CONFIG_MODE_SHUNT | INA226_CONFIG_MODE_BUS | INA226_CONFIG_MODE_CONT;
+ ina226_write_reg(INA226_REG_CONFIG, config);
+
+ ina226_write_reg(INA226_REG_CAL, INA226_CAL);
+}
+
+void ina226_write_reg(uint8_t reg, uint16_t val) {
+ uint8_t buf[3] = { reg, val>>8, val&0xff };
+ i2c_transmit(INA226_I2C_PERIPH, buf, sizeof(buf), INA226_I2C_ADDR, I2C_GENSTOP_YES);
+}
+
+uint16_t ina226_read_reg(uint8_t reg) {
+ uint8_t buf2[1] = { reg };
+ i2c_transmit(INA226_I2C_PERIPH, buf2, sizeof(buf2), INA226_I2C_ADDR, I2C_GENSTOP_NO);
+ uint8_t rx[2];
+ i2c_receive(INA226_I2C_PERIPH, rx, sizeof(rx), INA226_I2C_ADDR);
+ return (rx[0]<<8) | rx[1];
+}
+