summaryrefslogtreecommitdiff
path: root/board/casta/board.c
diff options
context:
space:
mode:
Diffstat (limited to 'board/casta/board.c')
-rw-r--r--board/casta/board.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/board/casta/board.c b/board/casta/board.c
index 124601fa75..bcea197537 100644
--- a/board/casta/board.c
+++ b/board/casta/board.c
@@ -88,6 +88,49 @@ const struct temp_sensor_t temp_sensors[] = {
};
BUILD_ASSERT(ARRAY_SIZE(temp_sensors) == TEMP_SENSOR_COUNT);
+/*
+ * I2C callbacks to ensure bus free time for battery I2C transactions is at
+ * least 5ms.
+ */
+#define BATTERY_FREE_MIN_DELTA_US (5 * MSEC)
+static timestamp_t battery_last_i2c_time;
+
+static int is_battery_i2c(int port, int slave_addr)
+{
+ return (port == I2C_PORT_BATTERY) && (slave_addr == BATTERY_ADDR);
+}
+
+static int is_battery_port(int port)
+{
+ return (port == I2C_PORT_BATTERY);
+}
+
+void i2c_start_xfer_notify(int port, int slave_addr)
+{
+ unsigned int time_delta_us;
+
+ if (!is_battery_i2c(port, slave_addr))
+ return;
+
+ time_delta_us = time_since32(battery_last_i2c_time);
+ if (time_delta_us >= BATTERY_FREE_MIN_DELTA_US)
+ return;
+
+ usleep(BATTERY_FREE_MIN_DELTA_US - time_delta_us);
+}
+
+void i2c_end_xfer_notify(int port, int slave_addr)
+{
+ /*
+ * The bus free time needs to be maintained from last transaction
+ * on I2C bus to any device on it to the next transaction to battery.
+ */
+ if (!is_battery_port(port))
+ return;
+
+ battery_last_i2c_time = get_time();
+}
+
void board_overcurrent_event(int port, int is_overcurrented)
{
/* Sanity check the port. */