ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
board_periph_i2c_devices.c File Reference

LSM6DSO IMU + MAX17048 fuel-gauge device models on the I2C bus. More...

#include <string.h>
#include "board_periph.h"
#include "board_periph_i2c_internal.h"
Include dependency graph for board_periph_i2c_devices.c:

Go to the source code of this file.

Data Structures

struct  lsm6dso_state_t
 One ST LSM6DSO IMU device: a flat auto-incrementing register file. More...
struct  battery_state_t
 User-settable battery state surfaced by the MAX17048 fuel gauge. More...
struct  max17048_state_t
 One MAX17048 fuel gauge: a flat big-endian register file + read pointer. More...

Enumerations

enum  lsm6dso_const_t : uint32_t {
  k_lsm6dso_addr_7b = 0x6BU ,
  k_lsm6dso_reg_who_am_i = 0x0FU ,
  k_lsm6dso_who_am_i_val = 0x6CU ,
  k_lsm6dso_reg_outx_l_g = 0x22U ,
  k_lsm6dso_reg_outz_l_a = 0x2CU ,
  k_lsm6dso_reg_file = 0x100U ,
  k_lsm6dso_seed_accel_z = 0x4000U ,
  k_lsm6dso_seed_gyro_x = 0x0100U
}
 ST LSM6DSO 6-DoF IMU constants (libs/ra8_lsm6dso, DS12140 Rev 4). More...
enum  max17048_const_t : uint32_t {
  k_max17048_addr_7b = 0x36U ,
  k_max17048_reg_vcell = 0x02U ,
  k_max17048_reg_soc = 0x04U ,
  k_max17048_reg_version = 0x08U ,
  k_max17048_reg_crate = 0x16U ,
  k_max17048_reg_file = 0x100U ,
  k_battery_soc_default = 72U ,
  k_battery_soc_max = 100U ,
  k_battery_vcell_default = 0xBE00U ,
  k_battery_version_val = 0x0012U ,
  k_battery_crate_mag = 0x0018U ,
  k_battery_byte_shift = 8U ,
  k_battery_byte_mask = 0xFFU
}
 Maxim MAX17048 fuel-gauge constants (battery state-of-charge over I2C). More...

Functions

static RA8_INTERNAL void internal_lsm6dso_seed16 (lsm6dso_state_t *s, uint8_t reg, uint16_t val)
 Perform lsm6dso seed16 for the board periph I2C devices model.
static RA8_INTERNAL void internal_lsm6dso_reset_regs (lsm6dso_state_t *s)
 Reset the register file: WHO_AM_I + synthetic accel/gyro samples.
static RA8_INTERNAL void internal_lsm6dso_write (void *ctx, uint8_t byte)
 Controller -> LSM6DSO: start register first, then config payload.
static RA8_INTERNAL uint32_t internal_lsm6dso_read (void *ctx, uint8_t *buf, uint32_t max)
 LSM6DSO -> controller: burst from the pointer, auto-incrementing.
static RA8_INTERNAL void internal_lsm6dso_stop (void *ctx)
 STOP / transfer end: re-arm pointer capture for the next transfer.
static RA8_INTERNAL void internal_fuelgauge_put16 (max17048_state_t *s, uint8_t reg, uint16_t val)
 Write a big-endian 16-bit value at register reg of s.
static RA8_INTERNAL void internal_fuelgauge_seed (max17048_state_t *s)
 Lay the MAX17048 register file from the current s_battery state.
static RA8_INTERNAL void internal_fuelgauge_write (void *ctx, uint8_t byte)
 MAX17048 write: first byte is the register pointer, then it advances.
static RA8_INTERNAL uint32_t internal_fuelgauge_read (void *ctx, uint8_t *buf, uint32_t max)
 MAX17048 read: serve consecutive register bytes (auto-increment).
static RA8_INTERNAL void internal_fuelgauge_stop (void *ctx)
 MAX17048 STOP: re-arm the register-pointer capture.
void board_periph_battery_set (uint8_t soc_pct, bool charging)
 Set the emulated battery state surfaced by the MAX17048 fuel gauge.
void board_periph_battery_get (uint8_t *out_soc, bool *out_charging)
 Read back the emulated battery state (for the status overlay).
void priv_board_i2c_imu_fuel_reset (void)
 Implementation of priv_board_i2c_imu_fuel_reset() – re-lay register files.
void priv_board_i2c_imu_fuel_register (void)
 Implementation of priv_board_i2c_imu_fuel_register() – bus attach.
uint32_t priv_board_i2c_imu_reads (void)
 Implementation of priv_board_i2c_imu_reads() – report telemetry.

Variables

static lsm6dso_state_t s_lsm6dso
static battery_state_t s_battery = {.soc_pct = (uint8_t)k_battery_soc_default, .charging = false}
 Battery state + the fuel-gauge device that exposes it over I2C.
static max17048_state_t s_fuelgauge

Detailed Description

LSM6DSO IMU + MAX17048 fuel-gauge device models on the I2C bus.

The two flat-register-file bus devices (the ST LSM6DSO 6-DoF IMU at 0x6B and the MAX17048-class fuel gauge at 0x36, backed by the CLI-settable battery state) – moved verbatim out of board_periph_i2c.c. They register themselves on the modelled bus through the module-internal registry.

Since
0.1.0

Definition in file board_periph_i2c_devices.c.

Enumeration Type Documentation

◆ lsm6dso_const_t

enum lsm6dso_const_t : uint32_t

ST LSM6DSO 6-DoF IMU constants (libs/ra8_lsm6dso, DS12140 Rev 4).

The MikroE 6DOF IMU 12 Click ties SA0 high, so the part answers at 7-bit 0x6B. Unlike the GT911, the LSM6DSO uses an 8-bit register pointer that auto-increments across a burst read, so the model is a flat register file: the driver writes the start register, then reads N consecutive bytes (WHO_AM_I, then 6-byte gyro / accel bursts). WHO_AM_I (0x0F) must read back 0x6C or ra8_lsm6dso_init() rejects the part.

Enumerator
k_lsm6dso_addr_7b 

SA0-high 7-bit I2C address.

k_lsm6dso_reg_who_am_i 

WHO_AM_I register.

k_lsm6dso_who_am_i_val 

Expected WHO_AM_I value.

k_lsm6dso_reg_outx_l_g 

First gyro output byte (OUTX_L_G).

k_lsm6dso_reg_outz_l_a 

Accel Z low byte (OUTZ_L_A).

k_lsm6dso_reg_file 

Flat register-file size (bytes).

k_lsm6dso_seed_accel_z 

Synthetic accel Z (~+1 g at +-2 g FS).

k_lsm6dso_seed_gyro_x 

Synthetic gyro X (small steady rate).

Definition at line 32 of file board_periph_i2c_devices.c.

◆ max17048_const_t

enum max17048_const_t : uint32_t

Maxim MAX17048 fuel-gauge constants (battery state-of-charge over I2C).

The carrier's MAX17048-class fuel gauge answers at 7-bit 0x36 with a flat register file of big-endian 16-bit values. The firmware writes a register pointer then reads two bytes: VCELL (cell voltage, 78.125 uV/LSB), SOC (state-of-charge, high byte = integer percent), VERSION, and CRATE (charge rate, signed – positive while charging). The emulator drives SOC + the CRATE sign from the user-settable battery state (--battery / --charge).

Enumerator
k_max17048_addr_7b 

MAX17048 7-bit I2C address.

k_max17048_reg_vcell 

VCELL: cell voltage (78.125 uV/LSB).

k_max17048_reg_soc 

SOC: state-of-charge (1/256 %/LSB).

k_max17048_reg_version 

VERSION register.

k_max17048_reg_crate 

CRATE: charge rate (signed, 0.208%/hr).

k_max17048_reg_file 

Flat register-file size (bytes).

k_battery_soc_default 

Default state-of-charge percent.

k_battery_soc_max 

SOC clamp ceiling.

k_battery_vcell_default 

~3.80 V at 78.125 uV/LSB.

k_battery_version_val 

Reported VERSION.

k_battery_crate_mag 

|CRATE| ~5 %/hr (sign = charging).

k_battery_byte_shift 

High-byte shift for a 16-bit register.

k_battery_byte_mask 

Low-byte mask.

Definition at line 54 of file board_periph_i2c_devices.c.

Function Documentation

◆ board_periph_battery_get()

void board_periph_battery_get ( uint8_t * out_soc,
bool * out_charging )

Read back the emulated battery state (for the status overlay).

Parameters
[out]out_socReceives the state-of-charge percent (NULL ok).
[out]out_chargingReceives the charging flag (NULL ok).
Since
0.1.0

Read back the emulated battery state (for the status overlay); this step is contained within the board periph model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for board periph battery get.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 334 of file board_periph_i2c_devices.c.

References s_battery.

Referenced by apply_battery_click(), internal_fill_status_hw(), and internal_main_apply_button_battery().

◆ board_periph_battery_set()

void board_periph_battery_set ( uint8_t soc_pct,
bool charging )

Set the emulated battery state surfaced by the MAX17048 fuel gauge.

The firmware reads state-of-charge + charge direction from a MAX17048-class fuel gauge at I2C 0x36; this drives that device's register file. Set from the CLI (--battery <pct> / --charge) before the run. soc_pct is clamped to 0..100.

Parameters
[in]soc_pctState-of-charge percent (clamped to [0, 100]).
[in]chargingtrue marks the charger attached (CRATE reads positive).
Since
0.1.0
Precondition
Arguments satisfy the ranges documented for board periph battery set.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.

Definition at line 327 of file board_periph_i2c_devices.c.

References internal_fuelgauge_seed(), k_battery_soc_max, s_battery, and s_fuelgauge.

Referenced by apply_battery_click(), and internal_main_apply_button_battery().

◆ internal_fuelgauge_put16()

RA8_INTERNAL void internal_fuelgauge_put16 ( max17048_state_t * s,
uint8_t reg,
uint16_t val )
static

Write a big-endian 16-bit value at register reg of s.

Write a big-endian 16-bit value at register reg of s; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]sModule state instance processed by the operation.
[in]regRegister index or value selected by the operation.
[in]valRegister or payload value processed by the operation.
Precondition
Arguments satisfy the ranges documented for fuelgauge put16.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 231 of file board_periph_i2c_devices.c.

References k_battery_byte_mask, k_battery_byte_shift, RA8_INTERNAL, and max17048_state_t::regs.

Referenced by internal_fuelgauge_seed().

◆ internal_fuelgauge_read()

RA8_INTERNAL uint32_t internal_fuelgauge_read ( void * ctx,
uint8_t * buf,
uint32_t max )
static

MAX17048 read: serve consecutive register bytes (auto-increment).

Max17048 read: serve consecutive register bytes (auto-increment); this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
[in,out]bufBounded byte buffer read or updated by the operation.
[in]maxCapacity of the destination or operation in elements.
Returns
The fuelgauge read result produced by the board periph I2C devices model.
Return values
valueThe operation-specific fuelgauge read value.
Precondition
Arguments satisfy the ranges documented for fuelgauge read.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 301 of file board_periph_i2c_devices.c.

References k_max17048_reg_file, RA8_INTERNAL, max17048_state_t::reads, max17048_state_t::reg_ptr, and max17048_state_t::regs.

Referenced by priv_board_i2c_imu_fuel_register().

◆ internal_fuelgauge_seed()

RA8_INTERNAL void internal_fuelgauge_seed ( max17048_state_t * s)
static

Lay the MAX17048 register file from the current s_battery state.

Lay the max17048 register file from the current s_battery state; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]sModule state instance processed by the operation.
Precondition
Arguments satisfy the ranges documented for fuelgauge seed.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 246 of file board_periph_i2c_devices.c.

References internal_fuelgauge_put16(), k_battery_byte_shift, k_battery_crate_mag, k_battery_vcell_default, k_battery_version_val, k_max17048_reg_crate, k_max17048_reg_file, k_max17048_reg_soc, k_max17048_reg_vcell, k_max17048_reg_version, RA8_INTERNAL, max17048_state_t::regs, and s_battery.

Referenced by board_periph_battery_set(), and priv_board_i2c_imu_fuel_reset().

◆ internal_fuelgauge_stop()

RA8_INTERNAL void internal_fuelgauge_stop ( void * ctx)
static

MAX17048 STOP: re-arm the register-pointer capture.

Max17048 stop: re-arm the register-pointer capture; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
Precondition
Arguments satisfy the ranges documented for fuelgauge stop.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 321 of file board_periph_i2c_devices.c.

References max17048_state_t::ptr_set, and RA8_INTERNAL.

Referenced by priv_board_i2c_imu_fuel_register().

◆ internal_fuelgauge_write()

RA8_INTERNAL void internal_fuelgauge_write ( void * ctx,
uint8_t byte )
static

MAX17048 write: first byte is the register pointer, then it advances.

Max17048 write: first byte is the register pointer, then it advances; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
[in]byteOne data byte received from or sent to the emulated interface.
Precondition
Arguments satisfy the ranges documented for fuelgauge write.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 274 of file board_periph_i2c_devices.c.

References k_max17048_reg_file, max17048_state_t::ptr_set, RA8_INTERNAL, max17048_state_t::reg_ptr, and max17048_state_t::regs.

Referenced by priv_board_i2c_imu_fuel_register().

◆ internal_lsm6dso_read()

RA8_INTERNAL uint32_t internal_lsm6dso_read ( void * ctx,
uint8_t * buf,
uint32_t max )
static

LSM6DSO -> controller: burst from the pointer, auto-incrementing.

Lsm6dso -> controller: burst from the pointer, auto-incrementing; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
[in,out]bufBounded byte buffer read or updated by the operation.
[in]maxCapacity of the destination or operation in elements.
Returns
The lsm6dso read result produced by the board periph I2C devices model.
Return values
valueThe operation-specific lsm6dso read value.
Precondition
Arguments satisfy the ranges documented for lsm6dso read.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 194 of file board_periph_i2c_devices.c.

References k_lsm6dso_reg_file, RA8_INTERNAL, lsm6dso_state_t::reads, lsm6dso_state_t::reg_ptr, and lsm6dso_state_t::regs.

Referenced by priv_board_i2c_imu_fuel_register().

◆ internal_lsm6dso_reset_regs()

RA8_INTERNAL void internal_lsm6dso_reset_regs ( lsm6dso_state_t * s)
static

Reset the register file: WHO_AM_I + synthetic accel/gyro samples.

Reset the register file: who_am_i + synthetic accel/gyro samples; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]sModule state instance processed by the operation.
Precondition
Arguments satisfy the ranges documented for lsm6dso reset regs.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 144 of file board_periph_i2c_devices.c.

References internal_lsm6dso_seed16(), k_lsm6dso_reg_file, k_lsm6dso_reg_outx_l_g, k_lsm6dso_reg_outz_l_a, k_lsm6dso_reg_who_am_i, k_lsm6dso_seed_accel_z, k_lsm6dso_seed_gyro_x, k_lsm6dso_who_am_i_val, lsm6dso_state_t::ptr_set, RA8_INTERNAL, lsm6dso_state_t::reads, lsm6dso_state_t::reg_ptr, and lsm6dso_state_t::regs.

Referenced by priv_board_i2c_imu_fuel_reset().

◆ internal_lsm6dso_seed16()

RA8_INTERNAL void internal_lsm6dso_seed16 ( lsm6dso_state_t * s,
uint8_t reg,
uint16_t val )
static

Perform lsm6dso seed16 for the board periph I2C devices model.

Perform lsm6dso seed16 for the board periph i2c devices model; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]sModule state instance processed by the operation.
[in]regRegister index or value selected by the operation.
[in]valRegister or payload value processed by the operation.
Precondition
Arguments satisfy the ranges documented for lsm6dso seed16.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 129 of file board_periph_i2c_devices.c.

References k_i3c_dev_byte_mask, RA8_INTERNAL, and lsm6dso_state_t::regs.

Referenced by internal_lsm6dso_reset_regs().

◆ internal_lsm6dso_stop()

RA8_INTERNAL void internal_lsm6dso_stop ( void * ctx)
static

STOP / transfer end: re-arm pointer capture for the next transfer.

Stop / transfer end: re-arm pointer capture for the next transfer; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
Precondition
Arguments satisfy the ranges documented for lsm6dso stop.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 214 of file board_periph_i2c_devices.c.

References lsm6dso_state_t::ptr_set, and RA8_INTERNAL.

Referenced by priv_board_i2c_imu_fuel_register().

◆ internal_lsm6dso_write()

RA8_INTERNAL void internal_lsm6dso_write ( void * ctx,
uint8_t byte )
static

Controller -> LSM6DSO: start register first, then config payload.

Controller -> lsm6dso: start register first, then config payload; this step is contained within the board periph I2C devices model and uses bounded caller or module-owned storage.

Parameters
[in,out]ctxOpaque callback context identifying module-owned device state.
[in]byteOne data byte received from or sent to the emulated interface.
Precondition
Arguments satisfy the ranges documented for lsm6dso write.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board periph I2C devices model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 167 of file board_periph_i2c_devices.c.

References k_lsm6dso_reg_file, lsm6dso_state_t::ptr_set, RA8_INTERNAL, lsm6dso_state_t::reg_ptr, and lsm6dso_state_t::regs.

Referenced by priv_board_i2c_imu_fuel_register().

◆ priv_board_i2c_imu_fuel_register()

void priv_board_i2c_imu_fuel_register ( void )

◆ priv_board_i2c_imu_fuel_reset()

void priv_board_i2c_imu_fuel_reset ( void )

Implementation of priv_board_i2c_imu_fuel_reset() – re-lay register files.

Re-lay the IMU + fuel-gauge register files (block reset path).

Definition at line 345 of file board_periph_i2c_devices.c.

References internal_fuelgauge_seed(), internal_lsm6dso_reset_regs(), s_fuelgauge, and s_lsm6dso.

Referenced by internal_i3c_reset().

◆ priv_board_i2c_imu_reads()

uint32_t priv_board_i2c_imu_reads ( void )

Implementation of priv_board_i2c_imu_reads() – report telemetry.

IMU register reads answered this run (report telemetry).

Definition at line 370 of file board_periph_i2c_devices.c.

References s_lsm6dso.

Referenced by internal_i3c_report().

Variable Documentation

◆ s_battery

battery_state_t s_battery = {.soc_pct = (uint8_t)k_battery_soc_default, .charging = false}
static

Battery state + the fuel-gauge device that exposes it over I2C.

Definition at line 115 of file board_periph_i2c_devices.c.

Referenced by board_periph_battery_get(), board_periph_battery_set(), and internal_fuelgauge_seed().

◆ s_fuelgauge

◆ s_lsm6dso