Skip to content

Commit

Permalink
Issue #25 : Add wider API support of RasPi Pico SDK
Browse files Browse the repository at this point in the history
hardware_sync.
  • Loading branch information
suikan4github committed Dec 6, 2024
1 parent 94941c7 commit def588c
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/sdk/mocksdkwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/pico_api_alternate_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 61 additions & 0 deletions src/sdk/pico_sdk_apistub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/sdk/pico_sdk_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@
#if __has_include(<hardware/sync.h>) || __has_include(<gmock/gmock.h>)
#include <hardware/sync.h>
#endif // __has_include(<hardware/sync.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)
#include <hardware/ticks.h>
#endif // __has_include(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)
1 change: 1 addition & 0 deletions src/sdk/scripts/api_dirs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ hardware rtc
hardware sha256
hardware spi
hardware sync
hardware ticks
2 changes: 2 additions & 0 deletions src/sdk/scripts/blocks/pico_api_alternate_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/sdk/sdkwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3575,3 +3575,21 @@ void rpp_driver::SdkWrapper::spin_lock_unclaim(uint lock_num) {
::spin_lock_unclaim(lock_num);
}
#endif // __has_include(<hardware/sync.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)
// --------------------------------------------------
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(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)
6 changes: 6 additions & 0 deletions src/sdk/sdkwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,12 @@ class SdkWrapper {
virtual void spin_lock_unclaim(uint lock_num);
#endif // __has_include(<hardware/sync.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)
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(<hardware/ticks.h>) || __has_include(<gmock/gmock.h>)

}; // class SdkWrapper

#include "mocksdkwrapper.hpp"
Expand Down
45 changes: 44 additions & 1 deletion test/test_sdkwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2071,4 +2072,46 @@ TEST(SdkWrapper, spin_lock_is_claimed) {
index++;
}
RESET_FAKE(spin_lock_is_claimed);
} // TEST(SdkWrapper, irq_is_enabled)
} // 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<tick_gen_num_t> 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 &&param0 : 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 &&param0 : 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)
5 changes: 5 additions & 0 deletions test/todo/test_sdkwrapper.md
Original file line number Diff line number Diff line change
@@ -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 .
Expand Down

0 comments on commit def588c

Please sign in to comment.