Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ if (BUILD_EXECUTABLE STREQUAL "unitTest")
add_subdirectory(libs/bsw/platform/test)
add_subdirectory(libs/bsw/runtime/test)
add_subdirectory(libs/bsw/storage/test)
add_subdirectory(libs/bsw/time/test)
add_subdirectory(libs/bsw/timer/test)
add_subdirectory(libs/bsw/transport/test)
add_subdirectory(libs/bsw/transportRouterSimple/test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ uint32_t getSystemTicks32Bit(void) { return static_cast<uint32_t>(updateTicks())

uint64_t getSystemTimeNs(void) { return updateTicks() * 1000U / TICK_FREQ_MHZ; }

uint64_t getSystemTimeUs(void) { return updateTicks() / TICK_FREQ_MHZ; }
uint64_t getSystemTimeUs64Bit(void) { return updateTicks() / TICK_FREQ_MHZ; }

uint32_t getSystemTimeUs32Bit(void) { return static_cast<uint32_t>(updateTicks() / TICK_FREQ_MHZ); }

Expand All @@ -112,8 +112,10 @@ uint32_t getFastTicksPerSecond(void)

void sysDelayUs(uint32_t const delay)
{
uint64_t const start = getSystemTimeUs();
while (getSystemTimeUs() < start + delay) {}
uint32_t const start = getSystemTimeUs32Bit();
// Use unsigned wraparound: (now - start) yields the elapsed microseconds modulo 2^32, which is
// correct as long as the real elapsed time is < 2^32 us (~71.6 min) and we poll often enough.
while ((getSystemTimeUs32Bit() - start) < delay) {}
}

} // extern "C"
1 change: 1 addition & 0 deletions libs/bsw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_subdirectory(lifecycle)
add_subdirectory(logger)
add_subdirectory(platform)
add_subdirectory(stdioConsoleInput)
add_subdirectory(time)
add_subdirectory(timer)
add_subdirectory(util)

Expand Down
2 changes: 1 addition & 1 deletion libs/bsw/bsp/include/bsp/timer/SystemTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ uint64_t getSystemTimeNs(void);
* Returns the Time in Us since startup of the CPU.
* \return Systemtime in Us.
*/
uint64_t getSystemTimeUs(void);
uint64_t getSystemTimeUs64Bit(void);

/**
* Returns the Time in Ms since startup of the CPU.
Expand Down
4 changes: 2 additions & 2 deletions libs/bsw/bsp/include/bsp/timer/isEqualAfterTimeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace bsp
template<typename T>
bool isEqualAfterTimeout(T const* const ptr, T const mask, T const value, uint32_t const timeout)
{
uint64_t const endTime = getSystemTimeUs() + timeout;
uint64_t const endTime = getSystemTimeUs64Bit() + timeout;

while (((*ptr & mask) == (value & mask)) && (getSystemTimeUs() <= endTime)) {}
while (((*ptr & mask) == (value & mask)) && (getSystemTimeUs64Bit() <= endTime)) {}
return (*ptr & mask) == (value & mask);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/bsw/bsp/mock/include/bsp/timer/SystemTimerMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class SystemTimerMock : public ::etl::singleton_base<SystemTimerMock>
MOCK_METHOD(uint64_t, getSystemTimeNs, ());

/**
* \see getSystemTimeUs(void)
* \see getSystemTimeUs64Bit(void)
*/
MOCK_METHOD(uint64_t, getSystemTimeUs, ());
MOCK_METHOD(uint64_t, getSystemTimeUs64Bit, ());

/**
* \see getSystemTimeMs(void)
Expand Down
2 changes: 1 addition & 1 deletion libs/bsw/bsp/mock/src/bsp/timer/SystemTimerMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ uint32_t getSystemTicks32Bit(void) { return SystemTimerMock::instance().getSyste

uint64_t getSystemTimeNs(void) { return SystemTimerMock::instance().getSystemTimeNs(); }

uint64_t getSystemTimeUs(void) { return SystemTimerMock::instance().getSystemTimeUs(); }
uint64_t getSystemTimeUs64Bit(void) { return SystemTimerMock::instance().getSystemTimeUs64Bit(); }

uint64_t getSystemTimeMs(void) { return SystemTimerMock::instance().getSystemTimeMs(); }

Expand Down
4 changes: 2 additions & 2 deletions libs/bsw/bsp/test/src/bsp/timer/IsEqualAfterTimeoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ TEST(IsEqualAfterTimeoutTest, Check)
{
StrictMock<SystemTimerMock> timer;

EXPECT_CALL(timer, getSystemTimeUs()).Times(1).WillOnce(Return(11U));
EXPECT_CALL(timer, getSystemTimeUs64Bit()).Times(1).WillOnce(Return(11U));

uint32_t address = 0x000000FFU;
EXPECT_FALSE(::bsp::isEqualAfterTimeout<uint32_t>(&address, 0xFFFFFF00U, 0x0000FFFFU, 10U));

EXPECT_CALL(timer, getSystemTimeUs())
EXPECT_CALL(timer, getSystemTimeUs64Bit())
.Times(3)
.WillOnce(Return(12U))
.WillOnce(Return(20U))
Expand Down
26 changes: 26 additions & 0 deletions libs/bsw/time/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# *******************************************************************************
# Copyright (c) 2026 BMW AG
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

load("@rules_cc//cc:cc_library.bzl", "cc_library")

cc_library(
name = "time",
srcs = [
"src/time/TimestampProvider.cpp",
],
hdrs = [
"include/time/TimestampProvider.h",
],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
deps = [
"//libs/bsw/bsp",
],
)
5 changes: 5 additions & 0 deletions libs/bsw/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(time src/time/TimestampProvider.cpp)

target_include_directories(time PUBLIC include)

target_link_libraries(time PUBLIC bsp)
49 changes: 49 additions & 0 deletions libs/bsw/time/include/time/TimestampProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/********************************************************************************
* Copyright (c) 2026 BMW AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#pragma once

#include <cstdint>

namespace bsw
{
namespace time
{

/**
* A class for providing timestamps.
*
* Uses underlying BSP / BSW functionality.
*/
class TimestampProvider
{
public:
/**
* Provides a 32 bit CPU local and relative timestamp in micro seconds resolution.
*
* Will overflow after about 71 minutes. Therefore only suitable for short term measurements.
*
* A 32 bit timestamp is less expensive compared to a 64 bit timestamp, at least on 32 bit
* platforms. Therefore prefer 32 bit timestamp if feasible.
*
* \return 32 bit CPU local timestamp micro seconds.
*/
static uint32_t getTimestampUs32Bit();

/**
* Provides a 64 bit CPU local and relative timestamp in micro seconds resolution.
*
* \return 64 bit CPU local timestamp micro seconds.
*/
static uint64_t getTimestampUs64Bit();
};

} // namespace time
} // namespace bsw
1 change: 1 addition & 0 deletions libs/bsw/time/module.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oss: true
25 changes: 25 additions & 0 deletions libs/bsw/time/src/time/TimestampProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/********************************************************************************
* Copyright (c) 2026 BMW AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include "time/TimestampProvider.h"

#include <bsp/SystemTime.h>

namespace bsw
{
namespace time
{

uint32_t TimestampProvider::getTimestampUs32Bit() { return getSystemTimeUs32Bit(); }

uint64_t TimestampProvider::getTimestampUs64Bit() { return getSystemTimeUs64Bit(); }

} // namespace time
} // namespace bsw
5 changes: 5 additions & 0 deletions libs/bsw/time/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(timeTest src/time/TimestampProviderTest.cpp)

target_link_libraries(timeTest PRIVATE time bspMock gmock_main)

gtest_discover_tests(timeTest PROPERTIES LABELS "timeTest")
35 changes: 35 additions & 0 deletions libs/bsw/time/test/src/time/TimestampProviderTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/********************************************************************************
* Copyright (c) 2026 BMW AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include "time/TimestampProvider.h"

#include "bsp/timer/SystemTimerMock.h"

#include <gtest/gtest.h>

using namespace ::testing;

TEST(TimestampProviderTest, getTimestampUs32Bit_returns_system_time)
{
StrictMock<SystemTimerMock> timer;

EXPECT_CALL(timer, getSystemTimeUs32Bit()).WillOnce(Return(42U));

EXPECT_EQ(42U, ::bsw::time::TimestampProvider::getTimestampUs32Bit());
}

TEST(TimestampProviderTest, getTimestampUs64Bit_returns_system_time)
{
StrictMock<SystemTimerMock> timer;

EXPECT_CALL(timer, getSystemTimeUs64Bit()).WillOnce(Return(0x1'0000'0042ULL));

EXPECT_EQ(0x1'0000'0042ULL, ::bsw::time::TimestampProvider::getTimestampUs64Bit());
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ uint64_t getSystemTimeNs()
return duration_cast<nanoseconds>(steady_clock::now() - startTime).count();
}

uint64_t getSystemTimeUs()
uint64_t getSystemTimeUs64Bit()
{
return duration_cast<microseconds>(steady_clock::now() - startTime).count();
}
Expand Down
2 changes: 1 addition & 1 deletion platforms/s32k1xx/etlImpl/src/clocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ etl::chrono::high_resolution_clock::rep etl_get_high_resolution_clock()

etl::chrono::system_clock::rep etl_get_system_clock()
{
return etl::chrono::system_clock::rep(static_cast<int64_t>(getSystemTimeUs()));
return etl::chrono::system_clock::rep(static_cast<int64_t>(getSystemTimeUs64Bit()));
}

etl::chrono::steady_clock::rep etl_get_steady_clock()
Expand Down
Loading