Merge branch 'dev' into reborned/some_api_adjustments

This commit is contained in:
あく
2024-10-14 14:59:52 +01:00
committed by GitHub
128 changed files with 12019 additions and 1454 deletions
+387 -81
View File
@@ -1,29 +1,77 @@
#include "bq27220.h"
#include "bq27220_reg.h"
#include "bq27220_data_memory.h"
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");
#include <furi.h>
#include <stdbool.h>
#define TAG "Gauge"
static uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
uint16_t buf = 0;
#define BQ27220_ID (0x0220u)
furi_hal_i2c_read_mem(
handle, BQ27220_ADDRESS, address, (uint8_t*)&buf, 2, BQ27220_I2C_TIMEOUT);
/** Delay between 2 writes into Subclass/MAC area. Fails at ~120us. */
#define BQ27220_MAC_WRITE_DELAY_US (250u)
return buf;
/** Delay between we ask chip to load data to MAC and it become valid. Fails at ~500us. */
#define BQ27220_SELECT_DELAY_US (1000u)
/** Delay between 2 control operations(like unseal or full access). Fails at ~2500us.*/
#define BQ27220_MAGIC_DELAY_US (5000u)
/** Delay before freshly written configuration can be read. Fails at ? */
#define BQ27220_CONFIG_DELAY_US (10000u)
/** Config apply delay. Must wait, or DM read returns garbage. */
#define BQ27220_CONFIG_APPLY_US (2000000u)
/** Timeout for common operations. */
#define BQ27220_TIMEOUT_COMMON_US (2000000u)
/** Timeout for reset operation. Normally reset takes ~2s. */
#define BQ27220_TIMEOUT_RESET_US (4000000u)
/** Timeout cycle interval */
#define BQ27220_TIMEOUT_CYCLE_INTERVAL_US (1000u)
/** Timeout cycles count helper */
#define BQ27220_TIMEOUT(timeout_us) ((timeout_us) / (BQ27220_TIMEOUT_CYCLE_INTERVAL_US))
#ifdef BQ27220_DEBUG
#define BQ27220_DEBUG_LOG(...) FURI_LOG_D(TAG, ##__VA_ARGS__)
#else
#define BQ27220_DEBUG_LOG(...)
#endif
static inline bool bq27220_read_reg(
FuriHalI2cBusHandle* handle,
uint8_t address,
uint8_t* buffer,
size_t buffer_size) {
return furi_hal_i2c_trx(
handle, BQ27220_ADDRESS, &address, 1, buffer, buffer_size, BQ27220_I2C_TIMEOUT);
}
static bool bq27220_control(FuriHalI2cBusHandle* handle, uint16_t control) {
bool ret = furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandControl, (uint8_t*)&control, 2, BQ27220_I2C_TIMEOUT);
static inline bool bq27220_write(
FuriHalI2cBusHandle* handle,
uint8_t address,
const uint8_t* buffer,
size_t buffer_size) {
return furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, address, buffer, buffer_size, BQ27220_I2C_TIMEOUT);
}
return ret;
static inline bool bq27220_control(FuriHalI2cBusHandle* handle, uint16_t control) {
return bq27220_write(handle, CommandControl, (uint8_t*)&control, 2);
}
static uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
uint16_t buf = BQ27220_ERROR;
if(!bq27220_read_reg(handle, address, (uint8_t*)&buf, 2)) {
FURI_LOG_E(TAG, "bq27220_read_word failed");
}
return buf;
}
static uint8_t bq27220_get_checksum(uint8_t* data, uint16_t len) {
@@ -56,49 +104,49 @@ static bool bq27220_parameter_check(
if(update) {
// Datasheet contains incorrect procedure for memory update, more info:
// https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/719878/bq27220-technical-reference-manual-sluubd4-is-missing-extended-data-commands-chapter
// Also see note in the header
// 2. Write the address AND the parameter data to 0x3E+ (auto increment)
if(!furi_hal_i2c_write_mem(
handle,
BQ27220_ADDRESS,
CommandSelectSubclass,
buffer,
size + 2,
BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM write failed");
// Write the address AND the parameter data to 0x3E+ (auto increment)
if(!bq27220_write(handle, CommandSelectSubclass, buffer, size + 2)) {
FURI_LOG_E(TAG, "DM write failed");
break;
}
furi_delay_us(10000);
// We must wait, otherwise write will fail
furi_delay_us(BQ27220_MAC_WRITE_DELAY_US);
// 3. Calculate the check sum: 0xFF - (sum of address and data) OR 0xFF
// Calculate the check sum: 0xFF - (sum of address and data) OR 0xFF
uint8_t checksum = bq27220_get_checksum(buffer, size + 2);
// 4. Write the check sum to 0x60 and the total length of (address + parameter data + check sum + length) to 0x61
// Write the check sum to 0x60 and the total length of (address + parameter data + check sum + length) to 0x61
buffer[0] = checksum;
// 2 bytes address, `size` bytes data, 1 byte check sum, 1 byte length
buffer[1] = 2 + size + 1 + 1;
if(!furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandMACDataSum, buffer, 2, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "CRC write failed");
if(!bq27220_write(handle, CommandMACDataSum, buffer, 2)) {
FURI_LOG_E(TAG, "CRC write failed");
break;
}
furi_delay_us(10000);
// Final wait as in gm.fs specification
furi_delay_us(BQ27220_CONFIG_DELAY_US);
ret = true;
} else {
if(!furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandSelectSubclass, buffer, 2, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM SelectSubclass for read failed");
if(!bq27220_write(handle, CommandSelectSubclass, buffer, 2)) {
FURI_LOG_E(TAG, "DM SelectSubclass for read failed");
break;
}
if(!furi_hal_i2c_rx(handle, BQ27220_ADDRESS, old_data, size, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM read failed");
// bqstudio uses 15ms wait delay here
furi_delay_us(BQ27220_SELECT_DELAY_US);
if(!bq27220_read_reg(handle, CommandMACData, old_data, size)) {
FURI_LOG_E(TAG, "DM read failed");
break;
}
// bqstudio uses burst reads with continue(CommandSelectSubclass without argument) and ~5ms between burst
furi_delay_us(BQ27220_SELECT_DELAY_US);
if(*(uint32_t*)&(old_data[0]) != *(uint32_t*)&(buffer[2])) {
FURI_LOG_W( //-V641
FURI_LOG_E( //-V641
TAG,
"Data at 0x%04x(%zu): 0x%08lx!=0x%08lx",
address,
@@ -119,22 +167,34 @@ static bool bq27220_data_memory_check(
const BQ27220DMData* data_memory,
bool update) {
if(update) {
if(!bq27220_control(handle, Control_ENTER_CFG_UPDATE)) {
const uint16_t cfg_request = Control_ENTER_CFG_UPDATE;
if(!bq27220_write(
handle, CommandSelectSubclass, (uint8_t*)&cfg_request, sizeof(cfg_request))) {
FURI_LOG_E(TAG, "ENTER_CFG_UPDATE command failed");
return false;
};
// Wait for enter CFG update mode
uint32_t timeout = 100;
OperationStatus status = {0};
while((status.CFGUPDATE != true) && (timeout-- > 0)) {
bq27220_get_operation_status(handle, &status);
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
Bq27220OperationStatus operation_status;
while(--timeout > 0) {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
} else if(operation_status.CFGUPDATE) {
break;
};
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
}
if(timeout == 0) {
FURI_LOG_E(TAG, "CFGUPDATE mode failed");
FURI_LOG_E(
TAG,
"Enter CFGUPDATE mode failed, CFG %u, SEC %u",
operation_status.CFGUPDATE,
operation_status.SEC);
return false;
}
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
}
// Process data memory records
@@ -179,43 +239,283 @@ static bool bq27220_data_memory_check(
}
// Finalize configuration update
if(update) {
if(update && result) {
bq27220_control(handle, Control_EXIT_CFG_UPDATE_REINIT);
furi_delay_us(10000);
// Wait for gauge to apply new configuration
furi_delay_us(BQ27220_CONFIG_APPLY_US);
// ensure that we exited config update mode
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
Bq27220OperationStatus operation_status;
while(--timeout > 0) {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
} else if(operation_status.CFGUPDATE != true) {
break;
}
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
}
// Check timeout
if(timeout == 0) {
FURI_LOG_E(TAG, "Exit CFGUPDATE mode failed");
return false;
}
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
}
return result;
}
bool bq27220_init(FuriHalI2cBusHandle* handle) {
// Request device number(chip PN)
if(!bq27220_control(handle, Control_DEVICE_NUMBER)) {
FURI_LOG_E(TAG, "Device is not present");
return false;
};
// Check control response
uint16_t data = 0;
data = bq27220_read_word(handle, CommandControl);
if(data != 0xFF00) {
FURI_LOG_E(TAG, "Invalid control response: %x", data);
return false;
};
bool bq27220_init(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
bool result = false;
bool reset_and_provisioning_required = false;
data = bq27220_read_word(handle, CommandMACData);
FURI_LOG_I(TAG, "Device Number %04x", data);
do {
// Request device number(chip PN)
BQ27220_DEBUG_LOG("Checking device ID");
if(!bq27220_control(handle, Control_DEVICE_NUMBER)) {
FURI_LOG_E(TAG, "ID: Device is not responding");
break;
};
// Enterprise wait(MAC read fails if less than 500us)
// bqstudio uses ~15ms
furi_delay_us(BQ27220_SELECT_DELAY_US);
// Read id data from MAC scratch space
uint16_t data = bq27220_read_word(handle, CommandMACData);
if(data != BQ27220_ID) {
FURI_LOG_E(TAG, "Invalid Device Number %04x != 0x0220", data);
break;
}
return data == 0x0220;
// Unseal device since we are going to read protected configuration
BQ27220_DEBUG_LOG("Unsealing");
if(!bq27220_unseal(handle)) {
break;
}
// Try to recover gauge from forever init
BQ27220_DEBUG_LOG("Checking initialization status");
Bq27220OperationStatus operation_status;
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Failed to get operation status");
break;
}
if(!operation_status.INITCOMP || operation_status.CFGUPDATE) {
FURI_LOG_E(TAG, "Incorrect state, reset needed");
reset_and_provisioning_required = true;
}
// Ensure correct profile is selected
BQ27220_DEBUG_LOG("Checking chosen profile");
Bq27220ControlStatus control_status;
if(!bq27220_get_control_status(handle, &control_status)) {
FURI_LOG_E(TAG, "Failed to get control status");
break;
}
if(control_status.BATT_ID != 0) {
FURI_LOG_E(TAG, "Incorrect profile, reset needed");
reset_and_provisioning_required = true;
}
// Ensure correct configuration loaded into gauge DataMemory
// Only if reset is not required, otherwise we don't
if(!reset_and_provisioning_required) {
BQ27220_DEBUG_LOG("Checking data memory");
if(!bq27220_data_memory_check(handle, data_memory, false)) {
FURI_LOG_E(TAG, "Incorrect configuration data, reset needed");
reset_and_provisioning_required = true;
}
}
// Reset needed
if(reset_and_provisioning_required) {
FURI_LOG_W(TAG, "Resetting device");
if(!bq27220_reset(handle)) {
FURI_LOG_E(TAG, "Failed to reset device");
break;
}
// Get full access to read and modify parameters
// Also it looks like this step is totally unnecessary
BQ27220_DEBUG_LOG("Acquiring Full Access");
if(!bq27220_full_access(handle)) {
break;
}
// Update memory
FURI_LOG_W(TAG, "Updating data memory");
bq27220_data_memory_check(handle, data_memory, true);
if(!bq27220_data_memory_check(handle, data_memory, false)) {
FURI_LOG_E(TAG, "Data memory update failed");
break;
}
}
BQ27220_DEBUG_LOG("Sealing");
if(!bq27220_seal(handle)) {
FURI_LOG_E(TAG, "Seal failed");
break;
}
result = true;
} while(0);
return result;
}
bool bq27220_apply_data_memory(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
FURI_LOG_I(TAG, "Verifying data memory");
if(!bq27220_data_memory_check(handle, data_memory, false)) {
FURI_LOG_I(TAG, "Updating data memory");
bq27220_data_memory_check(handle, data_memory, true);
}
FURI_LOG_I(TAG, "Data memory verification complete");
bool bq27220_reset(FuriHalI2cBusHandle* handle) {
bool result = false;
do {
if(!bq27220_control(handle, Control_RESET)) {
FURI_LOG_E(TAG, "Reset request failed");
break;
};
return true;
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_RESET_US);
Bq27220OperationStatus operation_status;
while(--timeout > 0) {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
} else if(operation_status.INITCOMP == true) {
break;
};
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
}
if(timeout == 0) {
FURI_LOG_E(TAG, "INITCOMP timeout after reset");
break;
}
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
result = true;
} while(0);
return result;
}
bool bq27220_seal(FuriHalI2cBusHandle* handle) {
Bq27220OperationStatus operation_status = {0};
bool result = false;
do {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Status query failed");
break;
}
if(operation_status.SEC == Bq27220OperationStatusSecSealed) {
result = true;
break;
}
if(!bq27220_control(handle, Control_SEALED)) {
FURI_LOG_E(TAG, "Seal request failed");
break;
}
furi_delay_us(BQ27220_SELECT_DELAY_US);
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Status query failed");
break;
}
if(operation_status.SEC != Bq27220OperationStatusSecSealed) {
FURI_LOG_E(TAG, "Seal failed");
break;
}
result = true;
} while(0);
return result;
}
bool bq27220_unseal(FuriHalI2cBusHandle* handle) {
Bq27220OperationStatus operation_status = {0};
bool result = false;
do {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Status query failed");
break;
}
if(operation_status.SEC != Bq27220OperationStatusSecSealed) {
result = true;
break;
}
// Hai, Kazuma desu
bq27220_control(handle, UnsealKey1);
furi_delay_us(BQ27220_MAGIC_DELAY_US);
bq27220_control(handle, UnsealKey2);
furi_delay_us(BQ27220_MAGIC_DELAY_US);
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Status query failed");
break;
}
if(operation_status.SEC != Bq27220OperationStatusSecUnsealed) {
FURI_LOG_E(TAG, "Unseal failed %u", operation_status.SEC);
break;
}
result = true;
} while(0);
return result;
}
bool bq27220_full_access(FuriHalI2cBusHandle* handle) {
bool result = false;
do {
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
Bq27220OperationStatus operation_status;
while(--timeout > 0) {
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
} else {
break;
};
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
}
if(timeout == 0) {
FURI_LOG_E(TAG, "Failed to get operation status");
break;
}
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
// Already full access
if(operation_status.SEC == Bq27220OperationStatusSecFull) {
result = true;
break;
}
// Must be unsealed to get full access
if(operation_status.SEC != Bq27220OperationStatusSecUnsealed) {
FURI_LOG_E(TAG, "Not in unsealed state");
break;
}
// Explosion!!!
bq27220_control(handle, FullAccessKey); //-V760
furi_delay_us(BQ27220_MAGIC_DELAY_US);
bq27220_control(handle, FullAccessKey);
furi_delay_us(BQ27220_MAGIC_DELAY_US);
if(!bq27220_get_operation_status(handle, &operation_status)) {
FURI_LOG_E(TAG, "Status query failed");
break;
}
if(operation_status.SEC != Bq27220OperationStatusSecFull) {
FURI_LOG_E(TAG, "Full access failed %u", operation_status.SEC);
break;
}
result = true;
} while(0);
return result;
}
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle) {
@@ -226,24 +526,30 @@ int16_t bq27220_get_current(FuriHalI2cBusHandle* handle) {
return bq27220_read_word(handle, CommandCurrent);
}
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status) {
uint16_t data = bq27220_read_word(handle, CommandBatteryStatus);
if(data == BQ27220_ERROR) {
return false;
} else {
*(uint16_t*)battery_status = data;
return true;
}
bool bq27220_get_control_status(FuriHalI2cBusHandle* handle, Bq27220ControlStatus* control_status) {
return bq27220_read_reg(handle, CommandControl, (uint8_t*)control_status, 2);
}
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status) {
uint16_t data = bq27220_read_word(handle, CommandOperationStatus);
if(data == BQ27220_ERROR) {
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, Bq27220BatteryStatus* battery_status) {
return bq27220_read_reg(handle, CommandBatteryStatus, (uint8_t*)battery_status, 2);
}
bool bq27220_get_operation_status(
FuriHalI2cBusHandle* handle,
Bq27220OperationStatus* operation_status) {
return bq27220_read_reg(handle, CommandOperationStatus, (uint8_t*)operation_status, 2);
}
bool bq27220_get_gauging_status(FuriHalI2cBusHandle* handle, Bq27220GaugingStatus* gauging_status) {
// Request gauging data to be loaded to MAC
if(!bq27220_control(handle, Control_GAUGING_STATUS)) {
FURI_LOG_E(TAG, "DM SelectSubclass for read failed");
return false;
} else {
*(uint16_t*)operation_status = data;
return true;
}
// Wait for data being loaded to MAC
furi_delay_us(BQ27220_SELECT_DELAY_US);
// Read id data from MAC scratch space
return bq27220_read_reg(handle, CommandMACData, (uint8_t*)gauging_status, 2);
}
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle) {
+224 -41
View File
@@ -1,3 +1,31 @@
/**
* @file bq27220.h
*
* Quite problematic chip with quite bad documentation.
*
* Couple things to keep in mind:
*
* - Datasheet and technical reference manual are full of bullshit
* - bqstudio is ignoring them
* - bqstudio i2c exchange tracing gives some ideas on timings that works, but there is a catch
* - bqstudio timings contradicts to gm.fs file specification
* - it's impossible to reproduce all situations in bqstudio
* - experiments with blackbox can not cover all edge cases
* - final timings are kinda blend between all of those sources
* - device behavior differs depending on i2c clock speed
* - The Hero Himmel would not have used this gauge in the first place
*
* Couple advises if you'll need to modify this driver:
* - Reset and wait for INITCOMP if something is not right.
* - Do not do partial config update, it takes unpredictable amount of time to apply.
* - Don't forget to reset chip before writing new config.
* - If something fails at config update stage, wait for 4 seconds before doing next cycle.
* - If you can program and lock chip at factory stage - do it. It will save you a lot of time.
* - Keep sealed or strange things may happen.
* - There is a condition when it may stuck at INITCOMP state, just "press reset button".
*
*/
#pragma once
#include <stdint.h>
@@ -9,26 +37,45 @@
typedef struct {
// Low byte, Low bit first
bool DSG : 1; // The device is in DISCHARGE
bool SYSDWN : 1; // System down bit indicating the system should shut down
bool TDA : 1; // Terminate Discharge Alarm
bool BATTPRES : 1; // Battery Present detected
bool AUTH_GD : 1; // Detect inserted battery
bool OCVGD : 1; // Good OCV measurement taken
bool TCA : 1; // Terminate Charge Alarm
bool RSVD : 1; // Reserved
uint8_t BATT_ID : 3; /**< Battery Identification */
bool SNOOZE : 1; /**< SNOOZE mode is enabled */
bool BCA : 1; /**< fuel gauge board calibration routine is active */
bool CCA : 1; /**< Coulomb Counter Calibration routine is active */
uint8_t RSVD0 : 2; /**< Reserved */
// High byte, Low bit first
bool CHGINH : 1; // Charge inhibit
bool FC : 1; // Full-charged is detected
bool OTD : 1; // Overtemperature in discharge condition is detected
bool OTC : 1; // Overtemperature in charge condition is detected
bool SLEEP : 1; // Device is operating in SLEEP mode when set
bool OCVFAIL : 1; // Status bit indicating that the OCV reading failed due to current
bool OCVCOMP : 1; // An OCV measurement update is complete
bool FD : 1; // Full-discharge is detected
} BatteryStatus;
uint8_t RSVD1; /**< Reserved */
} Bq27220ControlStatus;
_Static_assert(sizeof(BatteryStatus) == 2, "Incorrect structure size");
_Static_assert(sizeof(Bq27220ControlStatus) == 2, "Incorrect Bq27220ControlStatus structure size");
typedef struct {
// Low byte, Low bit first
bool DSG : 1; /**< The device is in DISCHARGE */
bool SYSDWN : 1; /**< System down bit indicating the system should shut down */
bool TDA : 1; /**< Terminate Discharge Alarm */
bool BATTPRES : 1; /**< Battery Present detected */
bool AUTH_GD : 1; /**< Detect inserted battery */
bool OCVGD : 1; /**< Good OCV measurement taken */
bool TCA : 1; /**< Terminate Charge Alarm */
bool RSVD : 1; /**< Reserved */
// High byte, Low bit first
bool CHGINH : 1; /**< Charge inhibit */
bool FC : 1; /**< Full-charged is detected */
bool OTD : 1; /**< Overtemperature in discharge condition is detected */
bool OTC : 1; /**< Overtemperature in charge condition is detected */
bool SLEEP : 1; /**< Device is operating in SLEEP mode when set */
bool OCVFAIL : 1; /**< Status bit indicating that the OCV reading failed due to current */
bool OCVCOMP : 1; /**< An OCV measurement update is complete */
bool FD : 1; /**< Full-discharge is detected */
} Bq27220BatteryStatus;
_Static_assert(sizeof(Bq27220BatteryStatus) == 2, "Incorrect Bq27220BatteryStatus structure size");
typedef enum {
Bq27220OperationStatusSecSealed = 0b11,
Bq27220OperationStatusSecUnsealed = 0b10,
Bq27220OperationStatusSecFull = 0b01,
} Bq27220OperationStatusSec;
typedef struct {
// Low byte, Low bit first
@@ -40,53 +87,189 @@ typedef struct {
bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
bool BTPINT : 1; /**< BTP threshold has been crossed */
// High byte, Low bit first
uint8_t RSVD1 : 2;
uint8_t RSVD1 : 2; /**< Reserved */
bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
uint8_t RSVD0 : 5;
} OperationStatus;
uint8_t RSVD0 : 5; /**< Reserved */
} Bq27220OperationStatus;
_Static_assert(sizeof(OperationStatus) == 2, "Incorrect structure size");
_Static_assert(
sizeof(Bq27220OperationStatus) == 2,
"Incorrect Bq27220OperationStatus structure size");
typedef struct {
// Low byte, Low bit first
bool FD : 1; /**< Full Discharge */
bool FC : 1; /**< Full Charge */
bool TD : 1; /**< Terminate Discharge */
bool TC : 1; /**< Terminate Charge */
bool RSVD0 : 1; /**< Reserved */
bool EDV : 1; /**< Cell voltage is above or below EDV0 threshold */
bool DSG : 1; /**< DISCHARGE or RELAXATION */
bool CF : 1; /**< Battery conditioning is needed */
// High byte, Low bit first
uint8_t RSVD1 : 2; /**< Reserved */
bool FCCX : 1; /**< fcc1hz clock going into CC: 0 = 1 Hz, 1 = 16 Hz*/
uint8_t RSVD2 : 2; /**< Reserved */
bool EDV1 : 1; /**< Cell voltage is above or below EDV1 threshold */
bool EDV2 : 1; /**< Cell voltage is above or below EDV2 threshold */
bool VDQ : 1; /**< Charge cycle FCC update qualification */
} Bq27220GaugingStatus;
_Static_assert(sizeof(Bq27220GaugingStatus) == 2, "Incorrect Bq27220GaugingStatus structure size");
typedef struct BQ27220DMData BQ27220DMData;
/** Initialize Driver
* @return true on success, false otherwise
*
* This routine performs a lot of things under the hood:
* - Verifies that gauge is present on i2c bus and got correct ID(0220)
* - Unseals gauge
* - Checks various internal statuses
* - Checks that current profile is 0
* - Checks configuration again provided data_memory
* - Reset gauge if something on previous stages was fishy
* - Updates configuration if needed
* - Sealing gauge to prevent configuration and state from accidental damage
*
* @param handle The I2C Bus handle
* @param[in] data_memory The data memory to be uploaded into gauge
*
* @return true on success, false otherwise
*/
bool bq27220_init(FuriHalI2cBusHandle* handle);
bool bq27220_init(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory);
/** Initialize Driver
* @return true on success, false otherwise
/** Reset gauge
*
* @param handle The I2C Bus handle
*
* @return true on success, false otherwise
*/
bool bq27220_apply_data_memory(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory);
bool bq27220_reset(FuriHalI2cBusHandle* handle);
/** Get battery voltage in mV or error */
/** Seal gauge access
*
* @param handle The I2C Bus handle
*
* @return true on success, false otherwise
*/
bool bq27220_seal(FuriHalI2cBusHandle* handle);
/** Unseal gauge access
*
* @param handle The I2C Bus handle
*
* @return true on success, false otherwise
*/
bool bq27220_unseal(FuriHalI2cBusHandle* handle);
/** Get full access
*
* @warning must be done in unsealed state
*
* @param handle The I2C Bus handle
*
* @return true on success, false otherwise
*/
bool bq27220_full_access(FuriHalI2cBusHandle* handle);
/** Get battery voltage
*
* @param handle The I2C Bus handle
*
* @return voltage in mV or BQ27220_ERROR
*/
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle);
/** Get current in mA or error*/
/** Get current
*
* @param handle The I2C Bus handle
*
* @return current in mA or BQ27220_ERROR
*/
int16_t bq27220_get_current(FuriHalI2cBusHandle* handle);
/** Get battery status */
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status);
/** Get control status
*
* @param handle The handle
* @param control_status The control status
*
* @return true on success, false otherwise
*/
bool bq27220_get_control_status(FuriHalI2cBusHandle* handle, Bq27220ControlStatus* control_status);
/** Get operation status */
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status);
/** Get battery status
*
* @param handle The handle
* @param battery_status The battery status
*
* @return true on success, false otherwise
*/
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, Bq27220BatteryStatus* battery_status);
/** Get temperature in units of 0.1°K */
/** Get operation status
*
* @param handle The handle
* @param operation_status The operation status
*
* @return true on success, false otherwise
*/
bool bq27220_get_operation_status(
FuriHalI2cBusHandle* handle,
Bq27220OperationStatus* operation_status);
/** Get gauging status
*
* @param handle The handle
* @param gauging_status The gauging status
*
* @return true on success, false otherwise
*/
bool bq27220_get_gauging_status(FuriHalI2cBusHandle* handle, Bq27220GaugingStatus* gauging_status);
/** Get temperature
*
* @param handle The I2C Bus handle
*
* @return temperature in units of 0.1°K
*/
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle);
/** Get compensated full charge capacity in in mAh */
/** Get compensated full charge capacity
*
* @param handle The I2C Bus handle
*
* @return full charge capacity in mAh or BQ27220_ERROR
*/
uint16_t bq27220_get_full_charge_capacity(FuriHalI2cBusHandle* handle);
/** Get design capacity in mAh */
/** Get design capacity
*
* @param handle The I2C Bus handle
*
* @return design capacity in mAh or BQ27220_ERROR
*/
uint16_t bq27220_get_design_capacity(FuriHalI2cBusHandle* handle);
/** Get remaining capacity in in mAh */
/** Get remaining capacity
*
* @param handle The I2C Bus handle
*
* @return remaining capacity in mAh or BQ27220_ERROR
*/
uint16_t bq27220_get_remaining_capacity(FuriHalI2cBusHandle* handle);
/** Get predicted remaining battery capacity in percents */
/** Get predicted remaining battery capacity
*
* @param handle The I2C Bus handle
*
* @return state of charge in percents or BQ27220_ERROR
*/
uint16_t bq27220_get_state_of_charge(FuriHalI2cBusHandle* handle);
/** Get ratio of full charge capacity over design capacity in percents */
/** Get ratio of full charge capacity over design capacity
*
* @param handle The I2C Bus handle
*
* @return state of health in percents or BQ27220_ERROR
*/
uint16_t bq27220_get_state_of_health(FuriHalI2cBusHandle* handle);
void bq27220_change_design_capacity(FuriHalI2cBusHandle* handle, uint16_t capacity);
+2
View File
@@ -82,3 +82,5 @@ typedef struct {
const bool SME0 : 1;
const uint8_t RSVD3 : 3;
} BQ27220DMGaugingConfig;
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");
+72 -64
View File
@@ -1,68 +1,76 @@
#pragma once
#define BQ27220_ADDRESS 0xAA
#define BQ27220_I2C_TIMEOUT 50
#define BQ27220_ADDRESS (0xAAu)
#define BQ27220_I2C_TIMEOUT (50u)
#define CommandControl 0x00
#define CommandAtRate 0x02
#define CommandAtRateTimeToEmpty 0x04
#define CommandTemperature 0x06
#define CommandVoltage 0x08
#define CommandBatteryStatus 0x0A
#define CommandCurrent 0x0C
#define CommandRemainingCapacity 0x10
#define CommandFullChargeCapacity 0x12
#define CommandAverageCurrent 0x14
#define CommandTimeToEmpty 0x16
#define CommandTimeToFull 0x18
#define CommandStandbyCurrent 0x1A
#define CommandStandbyTimeToEmpty 0x1C
#define CommandMaxLoadCurrent 0x1E
#define CommandMaxLoadTimeToEmpty 0x20
#define CommandRawCoulombCount 0x22
#define CommandAveragePower 0x24
#define CommandInternalTemperature 0x28
#define CommandCycleCount 0x2A
#define CommandStateOfCharge 0x2C
#define CommandStateOfHealth 0x2E
#define CommandChargeVoltage 0x30
#define CommandChargeCurrent 0x32
#define CommandBTPDischargeSet 0x34
#define CommandBTPChargeSet 0x36
#define CommandOperationStatus 0x3A
#define CommandDesignCapacity 0x3C
#define CommandSelectSubclass 0x3E
#define CommandMACData 0x40
#define CommandMACDataSum 0x60
#define CommandMACDataLen 0x61
#define CommandAnalogCount 0x79
#define CommandRawCurrent 0x7A
#define CommandRawVoltage 0x7C
#define CommandRawIntTemp 0x7E
#define CommandControl (0x00u)
#define CommandAtRate (0x02u)
#define CommandAtRateTimeToEmpty (0x04u)
#define CommandTemperature (0x06u)
#define CommandVoltage (0x08u)
#define CommandBatteryStatus (0x0Au)
#define CommandCurrent (0x0Cu)
#define CommandRemainingCapacity (0x10u)
#define CommandFullChargeCapacity (0x12u)
#define CommandAverageCurrent (0x14u)
#define CommandTimeToEmpty (0x16u)
#define CommandTimeToFull (0x18u)
#define CommandStandbyCurrent (0x1Au)
#define CommandStandbyTimeToEmpty (0x1Cu)
#define CommandMaxLoadCurrent (0x1Eu)
#define CommandMaxLoadTimeToEmpty (0x20u)
#define CommandRawCoulombCount (0x22u)
#define CommandAveragePower (0x24u)
#define CommandInternalTemperature (0x28u)
#define CommandCycleCount (0x2Au)
#define CommandStateOfCharge (0x2Cu)
#define CommandStateOfHealth (0x2Eu)
#define CommandChargeVoltage (0x30u)
#define CommandChargeCurrent (0x32u)
#define CommandBTPDischargeSet (0x34u)
#define CommandBTPChargeSet (0x36u)
#define CommandOperationStatus (0x3Au)
#define CommandDesignCapacity (0x3Cu)
#define CommandSelectSubclass (0x3Eu)
#define CommandMACData (0x40u)
#define CommandMACDataSum (0x60u)
#define CommandMACDataLen (0x61u)
#define CommandAnalogCount (0x79u)
#define CommandRawCurrent (0x7Au)
#define CommandRawVoltage (0x7Cu)
#define CommandRawIntTemp (0x7Eu)
#define Control_CONTROL_STATUS 0x0000
#define Control_DEVICE_NUMBER 0x0001
#define Control_FW_VERSION 0x0002
#define Control_BOARD_OFFSET 0x0009
#define Control_CC_OFFSET 0x000A
#define Control_CC_OFFSET_SAVE 0x000B
#define Control_OCV_CMD 0x000C
#define Control_BAT_INSERT 0x000D
#define Control_BAT_REMOVE 0x000E
#define Control_SET_SNOOZE 0x0013
#define Control_CLEAR_SNOOZE 0x0014
#define Control_SET_PROFILE_1 0x0015
#define Control_SET_PROFILE_2 0x0016
#define Control_SET_PROFILE_3 0x0017
#define Control_SET_PROFILE_4 0x0018
#define Control_SET_PROFILE_5 0x0019
#define Control_SET_PROFILE_6 0x001A
#define Control_CAL_TOGGLE 0x002D
#define Control_SEALED 0x0030
#define Control_RESET 0x0041
#define Control_EXIT_CAL 0x0080
#define Control_ENTER_CAL 0x0081
#define Control_ENTER_CFG_UPDATE 0x0090
#define Control_EXIT_CFG_UPDATE_REINIT 0x0091
#define Control_EXIT_CFG_UPDATE 0x0092
#define Control_RETURN_TO_ROM 0x0F00
#define Control_CONTROL_STATUS (0x0000u)
#define Control_DEVICE_NUMBER (0x0001u)
#define Control_FW_VERSION (0x0002u)
#define Control_HW_VERSION (0x0003u)
#define Control_BOARD_OFFSET (0x0009u)
#define Control_CC_OFFSET (0x000Au)
#define Control_CC_OFFSET_SAVE (0x000Bu)
#define Control_OCV_CMD (0x000Cu)
#define Control_BAT_INSERT (0x000Du)
#define Control_BAT_REMOVE (0x000Eu)
#define Control_SET_SNOOZE (0x0013u)
#define Control_CLEAR_SNOOZE (0x0014u)
#define Control_SET_PROFILE_1 (0x0015u)
#define Control_SET_PROFILE_2 (0x0016u)
#define Control_SET_PROFILE_3 (0x0017u)
#define Control_SET_PROFILE_4 (0x0018u)
#define Control_SET_PROFILE_5 (0x0019u)
#define Control_SET_PROFILE_6 (0x001Au)
#define Control_CAL_TOGGLE (0x002Du)
#define Control_SEALED (0x0030u)
#define Control_RESET (0x0041u)
#define Control_OERATION_STATUS (0x0054u)
#define Control_GAUGING_STATUS (0x0056u)
#define Control_EXIT_CAL (0x0080u)
#define Control_ENTER_CAL (0x0081u)
#define Control_ENTER_CFG_UPDATE (0x0090u)
#define Control_EXIT_CFG_UPDATE_REINIT (0x0091u)
#define Control_EXIT_CFG_UPDATE (0x0092u)
#define Control_RETURN_TO_ROM (0x0F00u)
#define UnsealKey1 (0x0414u)
#define UnsealKey2 (0x3672u)
#define FullAccessKey (0xffffu)
+2 -2
View File
@@ -335,8 +335,8 @@ bool protocol_em4100_write_data(ProtocolEM4100* protocol, void* data) {
request->t5577.block[0] =
(LFRFID_T5577_MODULATION_MANCHESTER | protocol_em4100_get_t5577_bitrate(protocol) |
(2 << LFRFID_T5577_MAXBLOCK_SHIFT));
request->t5577.block[1] = protocol->encoded_data;
request->t5577.block[2] = protocol->encoded_data >> 32;
request->t5577.block[1] = protocol->encoded_data >> 32;
request->t5577.block[2] = protocol->encoded_data;
request->t5577.blocks_to_write = 3;
result = true;
}
+208 -170
View File
@@ -1,3 +1,9 @@
/** Bit Buffer
*
* Various bits and bytes manipulation tools.
*
* @file bit_buffer.h
*/
#pragma once
#include <stddef.h>
@@ -10,118 +16,128 @@ extern "C" {
typedef struct BitBuffer BitBuffer;
// Construction, deletion, reset
/**
* Allocate a BitBuffer instance.
/** Allocate a BitBuffer instance.
*
* @param [in] capacity_bytes maximum buffer capacity, in bytes
* @return pointer to the allocated BitBuffer instance
* @param[in] capacity_bytes maximum buffer capacity, in bytes
*
* @return pointer to the allocated BitBuffer instance
*/
BitBuffer* bit_buffer_alloc(size_t capacity_bytes);
/**
* Delete a BitBuffer instance.
/** Delete a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance
* @param[in,out] buf pointer to a BitBuffer instance
*/
void bit_buffer_free(BitBuffer* buf);
/**
* Clear all data from a BitBuffer instance.
/** Clear all data from a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance
* @param[in,out] buf pointer to a BitBuffer instance
*/
void bit_buffer_reset(BitBuffer* buf);
// Copy and write
/**
* Copy another BitBuffer instance's contents to this one, replacing
* all of the original data.
* The destination capacity must be no less than the source data size.
/** Copy another BitBuffer instance's contents to this one, replacing all of the
* original data.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] other pointer to a BitBuffer instance to copy from
* @note
* @warning The destination capacity must be no less than the source data
* size.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] other pointer to a BitBuffer instance to copy from
*/
void bit_buffer_copy(BitBuffer* buf, const BitBuffer* other);
/**
* Copy all BitBuffer instance's contents to this one, starting from start_index,
* replacing all of the original data.
* The destination capacity must be no less than the source data size
* counting from start_index.
/** Copy all BitBuffer instance's contents to this one, starting from
* start_index, replacing all of the original data.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] other pointer to a BitBuffer instance to copy from
* @param [in] start_index index to begin copying source data from
* @warning The destination capacity must be no less than the source data
* size counting from start_index.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] other pointer to a BitBuffer instance to copy from
* @param[in] start_index index to begin copying source data from
*/
void bit_buffer_copy_right(BitBuffer* buf, const BitBuffer* other, size_t start_index);
/**
* Copy all BitBuffer instance's contents to this one, ending with end_index,
/** Copy all BitBuffer instance's contents to this one, ending with end_index,
* replacing all of the original data.
* The destination capacity must be no less than the source data size
* counting to end_index.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] other pointer to a BitBuffer instance to copy from
* @param [in] end_index index to end copying source data at
* @warning The destination capacity must be no less than the source data
* size counting to end_index.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] other pointer to a BitBuffer instance to copy from
* @param[in] end_index index to end copying source data at
*/
void bit_buffer_copy_left(BitBuffer* buf, const BitBuffer* other, size_t end_index);
/**
* Copy a byte array to a BitBuffer instance, replacing all of the original data.
* The destination capacity must be no less than the source data size.
/** Copy a byte array to a BitBuffer instance, replacing all of the original
* data.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] data pointer to the byte array to be copied
* @param [in] size_bytes size of the data to be copied, in bytes
* @warning The destination capacity must be no less than the source data
* size.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] data pointer to the byte array to be copied
* @param[in] size_bytes size of the data to be copied, in bytes
*/
void bit_buffer_copy_bytes(BitBuffer* buf, const uint8_t* data, size_t size_bytes);
/**
* Copy a byte array to a BitBuffer instance, replacing all of the original data.
* The destination capacity must be no less than the source data size.
/** Copy a byte array to a BitBuffer instance, replacing all of the original
* data.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] data pointer to the byte array to be copied
* @param [in] size_bits size of the data to be copied, in bits
* @warning The destination capacity must be no less than the source data
* size.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] data pointer to the byte array to be copied
* @param[in] size_bits size of the data to be copied, in bits
*/
void bit_buffer_copy_bits(BitBuffer* buf, const uint8_t* data, size_t size_bits);
/**
* Copy a byte with parity array to a BitBuffer instance, replacing all of the original data.
* The destination capacity must be no less than the source data size.
/** Copy a byte with parity array to a BitBuffer instance, replacing all of the
* original data.
*
* @param [in,out] buf pointer to a BitBuffer instance to copy into
* @param [in] data pointer to the byte array to be copied
* @param [in] size_bitss size of the data to be copied, in bits
* @warning The destination capacity must be no less than the source data
* size.
*
* @param[in,out] buf pointer to a BitBuffer instance to copy into
* @param[in] data pointer to the byte array to be copied
* @param[in] size_bits size of the data to be copied, in bits
* @note Parity bits are placed starting with the most significant bit
* of each byte and moving up.
* @note Example: DDDDDDDD PDDDDDDD DPDDDDDD DDP...
*/
void bit_buffer_copy_bytes_with_parity(BitBuffer* buf, const uint8_t* data, size_t size_bits);
/**
* Write a BitBuffer instance's entire contents to an arbitrary memory location.
* The destination memory must be allocated. Additionally, the destination
* capacity must be no less than the source data size.
/** Write a BitBuffer instance's entire contents to an arbitrary memory location.
*
* @param [in] buf pointer to a BitBuffer instance to write from
* @param [out] dest pointer to the destination memory location
* @param [in] size_bytes maximum destination data size, in bytes
* @warning The destination memory must be allocated. Additionally, the
* destination capacity must be no less than the source data size.
*
* @param[in] buf pointer to a BitBuffer instance to write from
* @param[out] dest pointer to the destination memory location
* @param[in] size_bytes maximum destination data size, in bytes
*/
void bit_buffer_write_bytes(const BitBuffer* buf, void* dest, size_t size_bytes);
/**
* Write a BitBuffer instance's entire contents to an arbitrary memory location.
* Additionally, place a parity bit after each byte.
* The destination memory must be allocated. Additionally, the destination
* capacity must be no less than the source data size plus parity.
/** Write a BitBuffer instance's entire contents to an arbitrary memory location.
*
* @param [in] buf pointer to a BitBuffer instance to write from
* @param [out] dest pointer to the destination memory location
* @param [in] size_bytes maximum destination data size, in bytes
* @param [out] bits_written actual number of bits writen, in bits
* Additionally, place a parity bit after each byte.
*
* @warning The destination memory must be allocated. Additionally, the
* destination capacity must be no less than the source data size
* plus parity.
*
* @param[in] buf pointer to a BitBuffer instance to write from
* @param[out] dest pointer to the destination memory location
* @param[in] size_bytes maximum destination data size, in bytes
* @param[out] bits_written actual number of bits written, in bits
* @note Parity bits are placed starting with the most significant bit of
* each byte and moving up.
* @note Example: DDDDDDDD PDDDDDDD DPDDDDDD DDP...
*/
void bit_buffer_write_bytes_with_parity(
const BitBuffer* buf,
@@ -129,15 +145,17 @@ void bit_buffer_write_bytes_with_parity(
size_t size_bytes,
size_t* bits_written);
/**
* Write a slice of BitBuffer instance's contents to an arbitrary memory location.
* The destination memory must be allocated. Additionally, the destination
* capacity must be no less than the requested slice size.
/** Write a slice of BitBuffer instance's contents to an arbitrary memory
* location.
*
* @param [in] buf pointer to a BitBuffer instance to write from
* @param [out] dest pointer to the destination memory location
* @param [in] start_index index to begin copying source data from
* @param [in] size_bytes data slice size, in bytes
* @warning The destination memory must be allocated. Additionally, the
* destination capacity must be no less than the requested slice
* size.
*
* @param[in] buf pointer to a BitBuffer instance to write from
* @param[out] dest pointer to the destination memory location
* @param[in] start_index index to begin copying source data from
* @param[in] size_bytes data slice size, in bytes
*/
void bit_buffer_write_bytes_mid(
const BitBuffer* buf,
@@ -147,176 +165,196 @@ void bit_buffer_write_bytes_mid(
// Checks
/**
* Check whether a BitBuffer instance contains a partial byte (i.e. the bit count
* is not divisible by 8).
/** Check whether a BitBuffer instance contains a partial byte (i.e.\ the bit
* count is not divisible by 8).
*
* @param [in] buf pointer to a BitBuffer instance to be checked
* @return true if the instance contains a partial byte, false otherwise
* @param[in] buf pointer to a BitBuffer instance to be checked
*
* @return true if the instance contains a partial byte, false otherwise
*/
bool bit_buffer_has_partial_byte(const BitBuffer* buf);
/**
* Check whether a BitBuffer instance's contents start with the designated byte.
/** Check whether a BitBuffer instance's contents start with the designated byte.
*
* @param [in] buf pointer to a BitBuffer instance to be checked
* @param [in] byte byte value to be checked against
* @return true if data starts with designated byte, false otherwise
* @param[in] buf pointer to a BitBuffer instance to be checked
* @param[in] byte byte value to be checked against
*
* @return true if data starts with designated byte, false otherwise
*/
bool bit_buffer_starts_with_byte(const BitBuffer* buf, uint8_t byte);
// Getters
/**
* Get a BitBuffer instance's capacity (i.e. the maximum possible amount of data), in bytes.
/** Get a BitBuffer instance's capacity (i.e.\ the maximum possible amount of
* data), in bytes.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @return capacity, in bytes
* @param[in] buf pointer to a BitBuffer instance to be queried
*
* @return capacity, in bytes
*/
size_t bit_buffer_get_capacity_bytes(const BitBuffer* buf);
/**
* Get a BitBuffer instance's data size (i.e. the amount of stored data), in bits.
* Might be not divisible by 8 (see bit_buffer_is_partial_byte).
/** Get a BitBuffer instance's data size (i.e.\ the amount of stored data), in
* bits.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @return data size, in bits.
* @warning Might be not divisible by 8 (see bit_buffer_is_partial_byte).
*
* @param[in] buf pointer to a BitBuffer instance to be queried
*
* @return data size, in bits.
*/
size_t bit_buffer_get_size(const BitBuffer* buf);
/**
* Get a BitBuffer instance's data size (i.e. the amount of stored data), in bytes.
* If a partial byte is present, it is also counted.
* Get a BitBuffer instance's data size (i.e.\ the amount of stored data), in
* bytes.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @return data size, in bytes.
* @warning If a partial byte is present, it is also counted.
*
* @param[in] buf pointer to a BitBuffer instance to be queried
*
* @return data size, in bytes.
*/
size_t bit_buffer_get_size_bytes(const BitBuffer* buf);
/**
* Get a byte value at a specified index in a BitBuffer instance.
* The index must be valid (i.e. less than the instance's data size in bytes).
/** Get a byte value at a specified index in a BitBuffer instance.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @param [in] index index of the byte in question
* @warning The index must be valid (i.e.\ less than the instance's data size
* in bytes).
*
* @param[in] buf pointer to a BitBuffer instance to be queried
* @param[in] index index of the byte in question
*
* @return byte value
*/
uint8_t bit_buffer_get_byte(const BitBuffer* buf, size_t index);
/**
* Get a byte value starting from the specified bit index in a BitBuffer instance.
* The resulting byte might correspond to a single byte (if the index is a multiple
* of 8), or two overlapping bytes combined.
* The index must be valid (i.e. less than the instance's data size in bits).
/** Get a byte value starting from the specified bit index in a BitBuffer
* instance.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @param [in] index bit index of the byte in question
* @warning The resulting byte might correspond to a single byte (if the
* index is a multiple of 8), or two overlapping bytes combined. The
* index must be valid (i.e.\ less than the instance's data size in
* bits).
*
* @param[in] buf pointer to a BitBuffer instance to be queried
* @param[in] index_bits bit index of the byte in question
*
* @return byte value
*/
uint8_t bit_buffer_get_byte_from_bit(const BitBuffer* buf, size_t index_bits);
/**
* Get the pointer to a BitBuffer instance's underlying data.
/** Get the pointer to a BitBuffer instance's underlying data.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @return pointer to the underlying data
* @param[in] buf pointer to a BitBuffer instance to be queried
*
* @return pointer to the underlying data
*/
const uint8_t* bit_buffer_get_data(const BitBuffer* buf);
/**
* Get the pointer to a BitBuffer instance's underlying data.
/** Get the pointer to the parity data of a BitBuffer instance.
*
* @param [in] buf pointer to a BitBuffer instance to be queried
* @return pointer to the underlying data
* @param[in] buf pointer to a BitBuffer instance to be queried
*
* @return pointer to the parity data
*/
const uint8_t* bit_buffer_get_parity(const BitBuffer* buf);
// Setters
/**
* Set byte value at a specified index in a BitBuffer instance.
* The index must be valid (i.e. less than the instance's data size in bytes).
/** Set byte value at a specified index in a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance to be modified
* @param [in] index index of the byte in question
* @param [in] byte byte value to be set at index
* @warning The index must be valid (i.e.\ less than the instance's data
* size in bytes).
*
* @param[in,out] buf pointer to a BitBuffer instance to be modified
* @param[in] index index of the byte in question
* @param[in] byte byte value to be set at index
*/
void bit_buffer_set_byte(BitBuffer* buf, size_t index, uint8_t byte);
/**
* Set byte and parity bit value at a specified index in a BitBuffer instance.
* The index must be valid (i.e. less than the instance's data size in bytes).
/** Set byte and parity bit value at a specified index in a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance to be modified
* @param [in] index index of the byte in question
* @param [in] byte byte value to be set at index
* @param [in] parity parity bit value to be set at index
* @warning The index must be valid (i.e.\ less than the instance's data
* size in bytes).
*
* @param[in,out] buff pointer to a BitBuffer instance to be modified
* @param[in] index index of the byte in question
* @param[in] byte byte value to be set at index
* @param[in] parity parity bit value to be set at index
*/
void bit_buffer_set_byte_with_parity(BitBuffer* buff, size_t index, uint8_t byte, bool parity);
/**
* Resize a BitBuffer instance to a new size, in bits.
* @warning May cause bugs. Use only if absolutely necessary.
/** Resize a BitBuffer instance to a new size, in bits.
*
* @param [in,out] buf pointer to a BitBuffer instance to be resized
* @param [in] new_size the new size of the buffer, in bits
* @warning May cause bugs. Use only if absolutely necessary.
*
* @param[in,out] buf pointer to a BitBuffer instance to be resized
* @param[in] new_size the new size of the buffer, in bits
*/
void bit_buffer_set_size(BitBuffer* buf, size_t new_size);
/**
* Resize a BitBuffer instance to a new size, in bytes.
* @warning May cause bugs. Use only if absolutely necessary.
/** Resize a BitBuffer instance to a new size, in bytes.
*
* @param [in,out] buf pointer to a BitBuffer instance to be resized
* @param [in] new_size_bytes the new size of the buffer, in bytes
* @warning May cause bugs. Use only if absolutely necessary.
*
* @param[in,out] buf pointer to a BitBuffer instance to be resized
* @param[in] new_size_bytes the new size of the buffer, in bytes
*/
void bit_buffer_set_size_bytes(BitBuffer* buf, size_t new_size_bytes);
// Modification
/**
* Append all BitBuffer's instance contents to this one. The destination capacity
* must be no less than its original data size plus source data size.
/** Append all BitBuffer's instance contents to this one.
*
* @param [in,out] buf pointer to a BitBuffer instance to be appended to
* @param [in] other pointer to a BitBuffer instance to be appended
* @warning The destination capacity must be no less than its original
* data size plus source data size.
*
* @param[in,out] buf pointer to a BitBuffer instance to be appended to
* @param[in] other pointer to a BitBuffer instance to be appended
*/
void bit_buffer_append(BitBuffer* buf, const BitBuffer* other);
/**
* Append a BitBuffer's instance contents to this one, starting from start_index.
* The destination capacity must be no less than the source data size
* counting from start_index.
/** Append a BitBuffer's instance contents to this one, starting from
* start_index.
*
* @param [in,out] buf pointer to a BitBuffer instance to be appended to
* @param [in] other pointer to a BitBuffer instance to be appended
* @param [in] start_index index to begin copying source data from
* @warning The destination capacity must be no less than the source data
* size counting from start_index.
*
* @param[in,out] buf pointer to a BitBuffer instance to be appended to
* @param[in] other pointer to a BitBuffer instance to be appended
* @param[in] start_index index to begin copying source data from
*/
void bit_buffer_append_right(BitBuffer* buf, const BitBuffer* other, size_t start_index);
/**
* Append a byte to a BitBuffer instance.
* The destination capacity must be no less its original data size plus one.
/** Append a byte to a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance to be appended to
* @param [in] byte byte value to be appended
* @warning The destination capacity must be no less its original data
* size plus one.
*
* @param[in,out] buf pointer to a BitBuffer instance to be appended to
* @param[in] byte byte value to be appended
*/
void bit_buffer_append_byte(BitBuffer* buf, uint8_t byte);
/**
* Append a byte array to a BitBuffer instance.
* The destination capacity must be no less its original data size plus source data size.
/** Append a byte array to a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance to be appended to
* @param [in] data pointer to the byte array to be appended
* @param [in] size_bytes size of the data to be appended, in bytes
* @warning The destination capacity must be no less its original data
* size plus source data size.
*
* @param[in,out] buf pointer to a BitBuffer instance to be appended to
* @param[in] data pointer to the byte array to be appended
* @param[in] size_bytes size of the data to be appended, in bytes
*/
void bit_buffer_append_bytes(BitBuffer* buf, const uint8_t* data, size_t size_bytes);
/**
* Append a bit to a BitBuffer instance.
* The destination capacity must be sufficient to accomodate the additional bit.
/** Append a bit to a BitBuffer instance.
*
* @param [in,out] buf pointer to a BitBuffer instance to be appended to
* @param [in] bit bit value to be appended
* @warning The destination capacity must be sufficient to accommodate the
* additional bit.
*
* @param[in,out] buf pointer to a BitBuffer instance to be appended to
* @param[in] bit bit value to be appended
*/
void bit_buffer_append_bit(BitBuffer* buf, bool bit);