Tab5 features, StackChan, fixes, drivers.... (#526)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
|
||||
|
||||
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
|
||||
|
||||
tactility_add_module(ina226-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
# INA226
|
||||
|
||||
Texas Instruments INA226 High-Side or Low-Side Measurement, Bi-Directional Current and Power Monitor.
|
||||
|
||||
- Datasheet: https://www.ti.com/lit/ds/symlink/ina226.pdf
|
||||
@@ -0,0 +1,12 @@
|
||||
description: TI INA226 voltage/current monitor
|
||||
|
||||
include: ["i2c-device.yaml"]
|
||||
|
||||
compatible: "ti,ina226"
|
||||
|
||||
properties:
|
||||
shunt-milliohms:
|
||||
type: int
|
||||
required: true
|
||||
# shunt value should be a minimum of 1
|
||||
description: Shunt resistor value in milliohms
|
||||
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
- TactilityKernel
|
||||
bindings: bindings
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/ina226.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(ina226, struct Ina226Config)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <tactility/error.h>
|
||||
|
||||
struct Device;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Ina226Config {
|
||||
uint8_t address;
|
||||
uint16_t shunt_milliohms;
|
||||
};
|
||||
|
||||
/** Bus voltage in volts (1.25 mV/LSB) */
|
||||
error_t ina226_read_bus_voltage(struct Device* device, float* volts);
|
||||
|
||||
/** Shunt current in amps (positive = charging, negative = discharging) */
|
||||
error_t ina226_read_shunt_current(struct Device* device, float* amps);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/module.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern struct Module ina226_module;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,138 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/ina226.h>
|
||||
#include <ina226_module.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#define TAG "INA226"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Register map
|
||||
// ---------------------------------------------------------------------------
|
||||
static constexpr uint8_t REG_CONFIG = 0x00; ///< RW - Configuration
|
||||
static constexpr uint8_t REG_SHUNT_VOLT = 0x01; ///< R - Shunt voltage (2.5 µV/LSB, signed)
|
||||
static constexpr uint8_t REG_BUS_VOLT = 0x02; ///< R - Bus voltage (1.25 mV/LSB)
|
||||
static constexpr uint8_t REG_CURRENT = 0x04; ///< R - Current (CURRENT_LSB/LSB, signed; valid after calibration)
|
||||
static constexpr uint8_t REG_CALIBRATION = 0x05; ///< RW - Calibration
|
||||
|
||||
static constexpr uint8_t REG_MANUFACTURER_ID = 0xFE;
|
||||
static constexpr uint16_t EXPECTED_MFR_ID = 0x5449; // "TI"
|
||||
|
||||
// CONFIG: AVG=16 (010), VBUSCT=1100µs (100), VSHCT=1100µs (100), MODE=continuous shunt+bus (111)
|
||||
static constexpr uint16_t CONFIG_VALUE = 0x4527;
|
||||
static constexpr uint16_t CONFIG_SHUTDOWN = 0x4520; // Same as CONFIG_VALUE but MODE=000
|
||||
|
||||
static constexpr float MAX_CURRENT_A = 8.192f;
|
||||
static constexpr float CURRENT_LSB = MAX_CURRENT_A / 32768.0f;
|
||||
|
||||
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
|
||||
|
||||
#define GET_CONFIG(device) (static_cast<const Ina226Config*>((device)->config))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Driver lifecycle
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static error_t start(Device* device) {
|
||||
Device* i2c = device_get_parent(device);
|
||||
if (device_get_type(i2c) != &I2C_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "Parent is not an I2C controller");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
const uint8_t addr = GET_CONFIG(device)->address;
|
||||
|
||||
uint16_t mfr_id = 0;
|
||||
error_t err = i2c_controller_register16be_get(i2c, addr, REG_MANUFACTURER_ID, &mfr_id, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to read INA226 manufacturer ID");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (mfr_id != EXPECTED_MFR_ID) {
|
||||
LOG_E(TAG, "Wrong device detected (mfr_id=0x%04X, expected=0x%04X)", mfr_id, EXPECTED_MFR_ID);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (i2c_controller_register16be_set(i2c, addr, REG_CONFIG, CONFIG_VALUE, TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to configure INA226");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
const uint16_t shunt_milliohms = GET_CONFIG(device)->shunt_milliohms;
|
||||
if (shunt_milliohms == 0) {
|
||||
LOG_E(TAG, "Invalid shunt value: 0 mOhms");
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
const float shunt_ohms = shunt_milliohms / 1000.0f;
|
||||
const float cal_f = 0.00512f / (CURRENT_LSB * shunt_ohms);
|
||||
if (cal_f < 1.0f || cal_f > 65535.0f) {
|
||||
LOG_E(TAG, "Calibration out of range (shunt=%u mOhms)", shunt_milliohms);
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
const uint16_t cal_value = static_cast<uint16_t>(cal_f);
|
||||
if (i2c_controller_register16be_set(i2c, addr, REG_CALIBRATION, cal_value, TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to calibrate INA226");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
LOG_I(TAG, "INA226 started (addr=0x%02X, shunt=%umΩ, cal=0x%04X)", addr, GET_CONFIG(device)->shunt_milliohms, cal_value);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* i2c = device_get_parent(device);
|
||||
if (device_get_type(i2c) != &I2C_CONTROLLER_TYPE) {
|
||||
LOG_E(TAG, "Parent is not an I2C controller");
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto addr = GET_CONFIG(device)->address;
|
||||
|
||||
// Put device into shutdown mode to save power
|
||||
if (i2c_controller_register16be_set(i2c, addr, REG_CONFIG, CONFIG_SHUTDOWN, TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to shutdown INA226 (ignored)");
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
extern "C" {
|
||||
|
||||
error_t ina226_read_bus_voltage(Device* device, float* volts) {
|
||||
if (device == nullptr) return ERROR_INVALID_ARGUMENT;
|
||||
if (volts == nullptr) return ERROR_INVALID_ARGUMENT;
|
||||
uint16_t raw = 0;
|
||||
error_t err = i2c_controller_register16be_get(device_get_parent(device), GET_CONFIG(device)->address, REG_BUS_VOLT, &raw, TIMEOUT);
|
||||
if (err != ERROR_NONE) return err;
|
||||
*volts = static_cast<float>(raw) * 0.00125f;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t ina226_read_shunt_current(Device* device, float* amps) {
|
||||
if (device == nullptr) return ERROR_INVALID_ARGUMENT;
|
||||
if (amps == nullptr) return ERROR_INVALID_ARGUMENT;
|
||||
uint16_t raw = 0;
|
||||
error_t err = i2c_controller_register16be_get(device_get_parent(device), GET_CONFIG(device)->address, REG_CURRENT, &raw, TIMEOUT);
|
||||
if (err != ERROR_NONE) return err;
|
||||
*amps = static_cast<float>(static_cast<int16_t>(raw)) * CURRENT_LSB;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
Driver ina226_driver = {
|
||||
.name = "ina226",
|
||||
.compatible = (const char*[]) { "ti,ina226", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &ina226_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,34 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver ina226_driver;
|
||||
|
||||
static error_t start() {
|
||||
/* We crash when construct fails, because if a single driver fails to construct,
|
||||
* there is no guarantee that the previously constructed drivers can be destroyed */
|
||||
check(driver_construct_add(&ina226_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
/* We crash when destruct fails, because if a single driver fails to destruct,
|
||||
* there is no guarantee that the previously destroyed drivers can be recovered */
|
||||
check(driver_remove_destruct(&ina226_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
extern const ModuleSymbol ina226_module_symbols[];
|
||||
|
||||
Module ina226_module = {
|
||||
.name = "ina226",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = ina226_module_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,9 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/ina226.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
const struct ModuleSymbol ina226_module_symbols[] = {
|
||||
DEFINE_MODULE_SYMBOL(ina226_read_bus_voltage),
|
||||
DEFINE_MODULE_SYMBOL(ina226_read_shunt_current),
|
||||
MODULE_SYMBOL_TERMINATOR
|
||||
};
|
||||
Reference in New Issue
Block a user