Skip to content

store unix timestamp in Omi's Storage - #4391

Merged
beastoin merged 15 commits into
BasedHardware:mainfrom
TuEmb:TuEmb/unix_timestamp_save
Jan 30, 2026
Merged

beastoin merged 15 commits into
BasedHardware:mainfrom
TuEmb:TuEmb/unix_timestamp_save

Conversation

@TuEmb

@TuEmb TuEmb commented Jan 25, 2026 •

Copy link
Copy Markdown
Collaborator

Related #3976
This PR adds:

  • A time-sync GATT service for syncing timestamps from the phone app to Omi devices.
  • Timestamp storage in flash memory.
  • RTC retention while the system is off (up to 29 hours).

Time Sync Service: 19B10030-E8F2-537E-4F6C-D104768A1214

1. Time Sync Write

UUID: 19B10031-E8F2-537E-4F6C-D104768A1214
Write 4 bytes (uint32_t epoch_s) to sync time. Example: edf67669

2. Time Sync Read

UUID: 19B10032-E8F2-537E-4F6C-D104768A1214
Read 4 bytes (uint32_t epoch_s) current device time

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to maintain time across system power-off cycles by using the LSM6DSO IMU's internal timestamp counter. It adds new modules for RTC management (rtc.c) and for interacting with the IMU's timestamp feature (lsm6dso_time.c). The changes also include updates to device tree files to properly configure the I2C bus and the IMU, and modifications to the settings and main application logic to support saving and restoring time.

The overall approach is solid, but I've found a critical issue with a hardcoded timestamp that breaks the time persistence logic. I've also identified an opportunity to improve code maintainability in the new lsm6dso_time.c file by refactoring duplicated error-handling logic. Please see my detailed comments.

I am having trouble creating individual review comments. Click here to see my feedback.

omi/firmware/omi/src/main.c (228)

critical

The RTC is initialized with a hardcoded future timestamp. This will reset the system time on every boot, overriding any time persisted in settings or adjusted from the IMU's timer. This defeats the purpose of the time-persistence feature. To allow the system to load the time from settings, you should call init_rtc with 0.

    init_rtc(0);

omi/firmware/omi/src/lsm6dso_time.c (166-196)

high

There is significant code duplication in the I2C communication logic. The pattern of an I2C operation followed by an error check with a specific recovery and retry for -EIO is repeated in lsm6dso_timestamp_enable, lsm6dso_timestamp_disable, lsm6dso_timestamp_reset_by_datasheet, lsm6dso_timestamp_set_resolution_6p4ms, and lsm6dso_timestamp_read. This makes the code harder to read and maintain.

Consider refactoring this logic into static helper functions. For example, you could create wrappers for I2C read and write operations that include the recovery and retry mechanism.

Here's an example for a read operation:

static int i2c_read_with_retry(const struct i2c_dt_spec *spec, uint8_t reg, uint8_t *val, const char *op_name)
{
	int err = i2c_reg_read_byte_dt(spec, reg, val);
	if (err == -EIO) {
		lsm6dso_try_recover_i2c_eio(op_name);
		err = i2c_reg_read_byte_dt(spec, reg, val);
		if (err) {
			LOG_WRN("Retry read %s failed (err %d)", op_name, err);
			lsm6dso_i2c_scan_bus_once();
		}
	}
	return err;
}

Using such helpers would greatly simplify the main functions and improve code quality.

@TuEmb

TuEmb commented Jan 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

the observed time drift over ~2 hours is 2 minutes

image image

@TuEmb

TuEmb commented Jan 25, 2026

Copy link
Copy Markdown
Collaborator Author

Power consumption while system off (+8uA) - from 19uA to 27uA

image

@TuEmb
TuEmb marked this pull request as ready for review January 26, 2026 05:09
@TuEmb
TuEmb requested a review from Copilot January 26, 2026 05:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a proper UTC timekeeping layer, BLE time synchronization, and IMU‑based time retention across system_off, plus the necessary board and settings wiring to persist timestamps.

Changes:

  • Add an RTC abstraction (rtc.c/.h) that maintains UTC using a persisted epoch plus monotonic uptime, with helpers for formatted UTC strings.
  • Extend settings and main firmware to persist RTC epoch / IMU timestamp bases and to use the IMU’s hardware timestamp counter to estimate elapsed time across system_off (imu.c/.h, button.c, main.c).
  • Expose a BLE GATT “Time Sync” service for setting/reading device time and update board DTS / pinctrl / CMake to support the new RTC and LSM6DS3TR‑C configuration.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
omi/src/settings.c Adds persisted rtc_timestamp, rtc_epoch (with legacy u32 compatibility), and lsm6dsl_time_base structures plus getters/setters and enhanced initialization logging to support the new timekeeping flow.
omi/src/rtc.h Declares the RTC API (init, get/set UTC in s/ms, validity check, and UTC datetime formatting helpers) used by the rest of the application.
omi/src/rtc.c Implements the monotonic UTC timebase backed by persisted epoch and uptime, and adds UTC datetime formatting utilities; also contains a mutex-protected core for time access and updates.
omi/src/main.c Initializes RTC early in boot, applies IMU-based RTC adjustment on startup, and periodically logs the current UTC time for observability.
omi/src/lib/evt/imu.c Adjusts IMU enable GPIO handling macros to cope with different DT property names, but still references outdated DT node labels, which no longer match the updated board DTS.
omi/src/lib/core/transport.c Introduces a BLE GATT Time Sync service and handlers to set UTC from the phone app and read the current UTC epoch, and registers the service alongside existing audio/settings/features services.
omi/src/lib/core/settings.h Extends the settings API with RTC timestamp/epoch and IMU time-base accessors, and pulls in the RTC driver type needed for struct rtc_time.
omi/src/lib/core/button.c Invokes IMU time preparation before entering system_off so the IMU timestamp base is persisted, ensuring better time reconstruction on wake.
omi/src/imu.h Declares IMU time utility functions for persisting and applying an IMU-based time offset across system_off cycles.
omi/src/imu.c Implements low-level LSM6DS3TR‑C timestamp configuration/readout and the logic to store an IMU/epoch base before system_off and adjust the RTC on boot using the elapsed IMU ticks.
omi/CMakeLists.txt Adds rtc.c and imu.c to the app sources so the new RTC/IMU time logic is built into the firmware.
boards/omi/omi_nrf5340_cpuapp.dts Reworks the IMU node to lsm6ds3tr_c (with lsm6dsl alias), converts the IMU enable pin to a regulator-fixed node, enables rtc0, and tunes I²C2 configuration to match the new IMU wiring.
boards/omi/omi-pinctrl.dtsi Adds pull-ups to the I²C2 SDA/SCL lines in both default and sleep pinctrl groups to improve bus reliability with the IMU.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread omi/firmware/omi/src/main.c Resolved
Comment thread omi/firmware/omi/src/rtc.c Resolved
Comment thread omi/firmware/omi/src/lib/evt/imu.c Resolved
@TuEmb

TuEmb commented Jan 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

@mdmohsin7 I need your help to send the UTC timestamp (in second) from Phone app to the Omi device. Everytime we connect to Omi device successfully, we need to send UTC timestamp (in second) to it.

https://www.epochconverter.com/clock
image

@mdmohsin7

Copy link
Copy Markdown
Member

@mdmohsin7 I need your help to send the UTC timestamp (in second) from Phone app to the Omi device. Everytime we connect to Omi device successfully, we need to send UTC timestamp (in second) to it.

Got it, will do @TuEmb

@TuEmb
TuEmb force-pushed the TuEmb/unix_timestamp_save branch from ad93783 to a19c5ee Compare January 29, 2026 06:49
@TuEmb
TuEmb requested a review from beastoin January 29, 2026 07:03
@mdmohsin7

Copy link
Copy Markdown
Member

#4445

@beastoin
beastoin merged commit b29ff57 into BasedHardware:main Jan 30, 2026
@beastoin

Copy link
Copy Markdown
Collaborator

lgtm @TuEmb

@TuEmb
TuEmb deleted the TuEmb/unix_timestamp_save branch January 30, 2026 10:39
Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
Related BasedHardware#3976
This PR adds:
- A time-sync GATT service for syncing timestamps from the phone app to
Omi devices.
 - Timestamp storage in flash memory.
 - RTC retention while the system is off (up to 29 hours).
 
### Time Sync Service: `19B10030-E8F2-537E-4F6C-D104768A1214`

### 1. Time Sync Write
UUID: `19B10031-E8F2-537E-4F6C-D104768A1214`
Write 4 bytes (uint32_t epoch_s) to sync time. Example: `edf67669`

### 2. Time Sync Read
UUID: `19B10032-E8F2-537E-4F6C-D104768A1214`
Read 4 bytes (uint32_t epoch_s) current device time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants