From def588cbcc8657fb4c594b0df72450dbd43bff60 Mon Sep 17 00:00:00 2001 From: Suikan <26223147+suikan4github@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:22:50 +0900 Subject: [PATCH] Issue #25 : Add wider API support of RasPi Pico SDK hardware_sync. --- src/sdk/mocksdkwrapper.hpp | 3 + src/sdk/pico_api_alternate_defs.hpp | 2 + src/sdk/pico_sdk_apistub.cpp | 61 +++++++++++++++++++ src/sdk/pico_sdk_headers.h | 4 ++ src/sdk/scripts/api_dirs.txt | 1 + .../blocks/pico_api_alternate_defs.hpp | 2 + src/sdk/sdkwrapper.cpp | 18 ++++++ src/sdk/sdkwrapper.hpp | 6 ++ test/test_sdkwrapper.cpp | 45 +++++++++++++- test/todo/test_sdkwrapper.md | 5 ++ 10 files changed, 146 insertions(+), 1 deletion(-) diff --git a/src/sdk/mocksdkwrapper.hpp b/src/sdk/mocksdkwrapper.hpp index 871bc0f..b978b6c 100644 --- a/src/sdk/mocksdkwrapper.hpp +++ b/src/sdk/mocksdkwrapper.hpp @@ -864,6 +864,9 @@ class MockSdkWrapper : public SdkWrapper { MOCK_METHOD1(spin_lock_claim_unused, int(bool required)); MOCK_METHOD1(spin_lock_is_claimed, bool(uint lock_num)); MOCK_METHOD1(spin_lock_unclaim, void(uint lock_num)); + MOCK_METHOD1(tick_is_running, bool(tick_gen_num_t tick)); + MOCK_METHOD2(tick_start, void(tick_gen_num_t tick, uint cycles)); + MOCK_METHOD1(tick_stop, void(tick_gen_num_t tick)); } // class MockSdkWrapper : public SdkWrapper ; // GCOVR_EXCL_STOP diff --git a/src/sdk/pico_api_alternate_defs.hpp b/src/sdk/pico_api_alternate_defs.hpp index bd0fc53..0c3fd2a 100644 --- a/src/sdk/pico_api_alternate_defs.hpp +++ b/src/sdk/pico_api_alternate_defs.hpp @@ -39,6 +39,8 @@ enum pwm_clkdiv_mode { q, r }; /// @brief Alternate definition for Google Test build. enum sha256_endianness { s, t }; +//// @brief Alternate definition for Google Test build. +typedef int tick_gen_num_t; //// @brief Alternate definition for Google Test build. typedef int spi_inst_t; //// @brief Alternate definition for Google Test build. diff --git a/src/sdk/pico_sdk_apistub.cpp b/src/sdk/pico_sdk_apistub.cpp index 8a7f5c2..0e1913f 100644 --- a/src/sdk/pico_sdk_apistub.cpp +++ b/src/sdk/pico_sdk_apistub.cpp @@ -14444,3 +14444,64 @@ extern "C" void _weak_spin_lock_unclaim(uint lock_num) "/alternatename:spin_lock_unclaim=_weak_spin_lock_unclaim") #endif // x86 or amd64 #endif // _MSC_VER +// -------------------------------------------------- +#if defined(__GNUC__) || defined(__clang__) // Compiler detection +extern "C" bool tick_is_running(tick_gen_num_t tick); +__attribute__((weak)) bool tick_is_running(tick_gen_num_t tick) +#elif defined(_MSC_VER) // Microsoft Visual C +extern "C" bool _weak_tick_is_running(tick_gen_num_t tick) +#else // Other compilers are not supported +#error "Unknown compiler." +#endif // Compiler detection +{ + assert(false && + "Error : The hardware_ticks library is missing in the link phase."); +} +#if defined(_MSC_VER) // weak binding in MSVC must be after definition +#if defined(_M_IX86) // for x86 +#pragma comment(linker, \ + "/alternatename:_tick_is_running=__weak_tick_is_running") +#elif defined(_M_AMD64) // for AMD64 +#pragma comment(linker, "/alternatename:tick_is_running=_weak_tick_is_running") +#endif // x86 or amd64 +#endif // _MSC_VER +// -------------------------------------------------- +#if defined(__GNUC__) || defined(__clang__) // Compiler detection +extern "C" void tick_start(tick_gen_num_t tick, uint cycles); +__attribute__((weak)) void tick_start(tick_gen_num_t tick, uint cycles) +#elif defined(_MSC_VER) // Microsoft Visual C +extern "C" void _weak_tick_start(tick_gen_num_t tick, uint cycles) +#else // Other compilers are not supported +#error "Unknown compiler." +#endif // Compiler detection +{ + assert(false && + "Error : The hardware_ticks library is missing in the link phase."); +} +#if defined(_MSC_VER) // weak binding in MSVC must be after definition +#if defined(_M_IX86) // for x86 +#pragma comment(linker, "/alternatename:_tick_start=__weak_tick_start") +#elif defined(_M_AMD64) // for AMD64 +#pragma comment(linker, "/alternatename:tick_start=_weak_tick_start") +#endif // x86 or amd64 +#endif // _MSC_VER +// -------------------------------------------------- +#if defined(__GNUC__) || defined(__clang__) // Compiler detection +extern "C" void tick_stop(tick_gen_num_t tick); +__attribute__((weak)) void tick_stop(tick_gen_num_t tick) +#elif defined(_MSC_VER) // Microsoft Visual C +extern "C" void _weak_tick_stop(tick_gen_num_t tick) +#else // Other compilers are not supported +#error "Unknown compiler." +#endif // Compiler detection +{ + assert(false && + "Error : The hardware_ticks library is missing in the link phase."); +} +#if defined(_MSC_VER) // weak binding in MSVC must be after definition +#if defined(_M_IX86) // for x86 +#pragma comment(linker, "/alternatename:_tick_stop=__weak_tick_stop") +#elif defined(_M_AMD64) // for AMD64 +#pragma comment(linker, "/alternatename:tick_stop=_weak_tick_stop") +#endif // x86 or amd64 +#endif // _MSC_VER diff --git a/src/sdk/pico_sdk_headers.h b/src/sdk/pico_sdk_headers.h index e6d85d3..a1230a6 100644 --- a/src/sdk/pico_sdk_headers.h +++ b/src/sdk/pico_sdk_headers.h @@ -95,3 +95,7 @@ #if __has_include() || __has_include() #include #endif // __has_include() || __has_include() + +#if __has_include() || __has_include() +#include +#endif // __has_include() || __has_include() diff --git a/src/sdk/scripts/api_dirs.txt b/src/sdk/scripts/api_dirs.txt index acfb0f7..fb8c5bd 100644 --- a/src/sdk/scripts/api_dirs.txt +++ b/src/sdk/scripts/api_dirs.txt @@ -20,3 +20,4 @@ hardware rtc hardware sha256 hardware spi hardware sync +hardware ticks diff --git a/src/sdk/scripts/blocks/pico_api_alternate_defs.hpp b/src/sdk/scripts/blocks/pico_api_alternate_defs.hpp index bd0fc53..0c3fd2a 100644 --- a/src/sdk/scripts/blocks/pico_api_alternate_defs.hpp +++ b/src/sdk/scripts/blocks/pico_api_alternate_defs.hpp @@ -39,6 +39,8 @@ enum pwm_clkdiv_mode { q, r }; /// @brief Alternate definition for Google Test build. enum sha256_endianness { s, t }; +//// @brief Alternate definition for Google Test build. +typedef int tick_gen_num_t; //// @brief Alternate definition for Google Test build. typedef int spi_inst_t; //// @brief Alternate definition for Google Test build. diff --git a/src/sdk/sdkwrapper.cpp b/src/sdk/sdkwrapper.cpp index 86a3445..85a513f 100644 --- a/src/sdk/sdkwrapper.cpp +++ b/src/sdk/sdkwrapper.cpp @@ -3575,3 +3575,21 @@ void rpp_driver::SdkWrapper::spin_lock_unclaim(uint lock_num) { ::spin_lock_unclaim(lock_num); } #endif // __has_include() || __has_include() + +#if __has_include() || __has_include() +// -------------------------------------------------- +extern "C" bool tick_is_running(tick_gen_num_t tick); +bool rpp_driver::SdkWrapper::tick_is_running(tick_gen_num_t tick) { + return ::tick_is_running(tick); +} +// -------------------------------------------------- +extern "C" void tick_start(tick_gen_num_t tick, uint cycles); +void rpp_driver::SdkWrapper::tick_start(tick_gen_num_t tick, uint cycles) { + ::tick_start(tick, cycles); +} +// -------------------------------------------------- +extern "C" void tick_stop(tick_gen_num_t tick); +void rpp_driver::SdkWrapper::tick_stop(tick_gen_num_t tick) { + ::tick_stop(tick); +} +#endif // __has_include() || __has_include() diff --git a/src/sdk/sdkwrapper.hpp b/src/sdk/sdkwrapper.hpp index 06b8c3b..f0b6e41 100644 --- a/src/sdk/sdkwrapper.hpp +++ b/src/sdk/sdkwrapper.hpp @@ -935,6 +935,12 @@ class SdkWrapper { virtual void spin_lock_unclaim(uint lock_num); #endif // __has_include() || __has_include() +#if __has_include() || __has_include() + virtual bool tick_is_running(tick_gen_num_t tick); + virtual void tick_start(tick_gen_num_t tick, uint cycles); + virtual void tick_stop(tick_gen_num_t tick); +#endif // __has_include() || __has_include() + }; // class SdkWrapper #include "mocksdkwrapper.hpp" diff --git a/test/test_sdkwrapper.cpp b/test/test_sdkwrapper.cpp index 2942e0a..efe4e88 100644 --- a/test/test_sdkwrapper.cpp +++ b/test/test_sdkwrapper.cpp @@ -82,6 +82,7 @@ FAKE_VOID_FUNC(rtc_init); FAKE_VOID_FUNC(sha256_set_dma_size, uint); FAKE_VALUE_FUNC(bool, spi_is_readable, const spi_inst_t *); FAKE_VALUE_FUNC(bool, spin_lock_is_claimed, uint); +FAKE_VALUE_FUNC(bool, tick_is_running, tick_gen_num_t); } // The cpp file of the library to test. #include "../src/sdk/sdkwrapper.cpp" @@ -2071,4 +2072,46 @@ TEST(SdkWrapper, spin_lock_is_claimed) { index++; } RESET_FAKE(spin_lock_is_claimed); -} // TEST(SdkWrapper, irq_is_enabled) \ No newline at end of file +} // TEST(SdkWrapper, irq_is_enabled) + +// ----------------------------------------------------------- +// +// hardware_tick +// virtual bool tick_is_running(tick_gen_num_t tick); +// +// ----------------------------------------------------------- + +TEST(SdkWrapper, tick_is_running) { + std::random_device rng; + ::rpp_driver::SdkWrapper pico; + // uniform distribution + std::uniform_int_distribution param_dist(0, INT_MAX); + tick_gen_num_t param_array0[] = {param_dist(rng), param_dist(rng)}; + bool retval_array[std::size(param_array0)] = {true, false}; + + FFF_RESET_HISTORY(); + RESET_FAKE(tick_is_running); + + SET_RETURN_SEQ(tick_is_running, retval_array, std::size(retval_array)); + + // Check whether return values are correctly passed to wrapper. + int index = 0; + for (auto &¶m0 : param_array0) { + ASSERT_EQ(pico.tick_is_running(param0), retval_array[index]); + index++; + } + + // Check the data from test spy. How many time called? + ASSERT_EQ(tick_is_running_fake.call_count, std::size(retval_array)); + + // Check whether parameters were correctly passed from wrapper. + index = 0; + for (auto &¶m0 : param_array0) { + // Check the data from test spy. Call order. + ASSERT_EQ(fff.call_history[index], (void *)tick_is_running); + // Check the data from test spy. : Parameters. + ASSERT_EQ(tick_is_running_fake.arg0_history[index], param0); + index++; + } + RESET_FAKE(tick_is_running); +} // TEST(SdkWrapper, tick_is_running) \ No newline at end of file diff --git a/test/todo/test_sdkwrapper.md b/test/todo/test_sdkwrapper.md index ad55a5f..c6a1c76 100644 --- a/test/todo/test_sdkwrapper.md +++ b/test/todo/test_sdkwrapper.md @@ -1,4 +1,9 @@ # test_SdkWrapper TDD +## SdkWrapper::tick_is_running() +- [x] Implement member function . +- [x] Create test case to fail. +- [x] Make it success. +- [x] Remove the duplication. ## SdkWrapper::spin_lock_is_claimed() - [x] Implement member function .