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_flash.
  • Loading branch information
suikan4github committed Nov 5, 2024
1 parent 761df00 commit ede330c
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/sdk/mocksdkwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ class MockSdkWrapper : public SdkWrapper {
exception_handler_t handler));
MOCK_METHOD2(exception_set_priority,
bool(uint num, uint8_t hardware_priority));
MOCK_METHOD3(flash_do_cmd,
void(const uint8_t* txbuf, uint8_t* rxbuf, size_t count));
MOCK_METHOD0(flash_flush_cache, void());
MOCK_METHOD1(flash_get_unique_id, void(uint8_t* id_out));
MOCK_METHOD2(flash_range_erase, void(uint32_t flash_offs, size_t count));
MOCK_METHOD3(flash_range_program,
void(uint32_t flash_offs, const uint8_t* data, size_t count));
MOCK_METHOD1(i2c_deinit, void(i2c_inst_t* i2c));
MOCK_METHOD2(i2c_get_dreq, uint(i2c_inst_t* i2c, bool is_tx));
MOCK_METHOD1(i2c_get_hw, i2c_hw_t*(i2c_inst_t* i2c));
Expand Down
115 changes: 115 additions & 0 deletions src/sdk/pico_sdk_apistub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,121 @@ extern "C" bool _weak_exception_set_priority(uint num,
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf,
size_t count);
__attribute__((weak)) void flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf,
size_t count)
#elif defined(_MSC_VER) // Microsoft Visual C
extern "C" void _weak_flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf,
size_t count)
#else // Other compilers are not supported
#error "Unknown compiler."
#endif // Compiler detection
{
assert(false &&
"Error : The hardware_flash 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:_flash_do_cmd=__weak_flash_do_cmd")
#elif defined(_M_AMD64) // for AMD64
#pragma comment(linker, "/alternatename:flash_do_cmd=_weak_flash_do_cmd")
#endif // x86 or amd64
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void flash_flush_cache(void);
__attribute__((weak)) void flash_flush_cache(void)
#elif defined(_MSC_VER) // Microsoft Visual C
extern "C" void _weak_flash_flush_cache(void)
#else // Other compilers are not supported
#error "Unknown compiler."
#endif // Compiler detection
{
assert(false &&
"Error : The hardware_flash 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:_flash_flush_cache=__weak_flash_flush_cache")
#elif defined(_M_AMD64) // for AMD64
#pragma comment(linker, \
"/alternatename:flash_flush_cache=_weak_flash_flush_cache")
#endif // x86 or amd64
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void flash_get_unique_id(uint8_t* id_out);
__attribute__((weak)) void flash_get_unique_id(uint8_t* id_out)
#elif defined(_MSC_VER) // Microsoft Visual C
extern "C" void _weak_flash_get_unique_id(uint8_t* id_out)
#else // Other compilers are not supported
#error "Unknown compiler."
#endif // Compiler detection
{
assert(false &&
"Error : The hardware_flash 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:_flash_get_unique_id=__weak_flash_get_unique_id")
#elif defined(_M_AMD64) // for AMD64
#pragma comment( \
linker, "/alternatename:flash_get_unique_id=_weak_flash_get_unique_id")
#endif // x86 or amd64
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void flash_range_erase(uint32_t flash_offs, size_t count);
__attribute__((weak)) void flash_range_erase(uint32_t flash_offs, size_t count)
#elif defined(_MSC_VER) // Microsoft Visual C
extern "C" void _weak_flash_range_erase(uint32_t flash_offs, size_t count)
#else // Other compilers are not supported
#error "Unknown compiler."
#endif // Compiler detection
{
assert(false &&
"Error : The hardware_flash 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:_flash_range_erase=__weak_flash_range_erase")
#elif defined(_M_AMD64) // for AMD64
#pragma comment(linker, \
"/alternatename:flash_range_erase=_weak_flash_range_erase")
#endif // x86 or amd64
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void flash_range_program(uint32_t flash_offs, const uint8_t* data,
size_t count);
__attribute__((weak)) void flash_range_program(uint32_t flash_offs,
const uint8_t* data,
size_t count)
#elif defined(_MSC_VER) // Microsoft Visual C
extern "C" void _weak_flash_range_program(uint32_t flash_offs,
const uint8_t* data, size_t count)
#else // Other compilers are not supported
#error "Unknown compiler."
#endif // Compiler detection
{
assert(false &&
"Error : The hardware_flash 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:_flash_range_program=__weak_flash_range_program")
#elif defined(_M_AMD64) // for AMD64
#pragma comment( \
linker, "/alternatename:flash_range_program=_weak_flash_range_program")
#endif // x86 or amd64
#endif // _MSC_VER
// --------------------------------------------------
#if defined(__GNUC__) || defined(__clang__) // Compiler detection
extern "C" void i2c_deinit(i2c_inst_t* i2c);
__attribute__((weak)) void i2c_deinit(i2c_inst_t* i2c)
#elif defined(_MSC_VER) // Microsoft Visual C
Expand Down
4 changes: 4 additions & 0 deletions src/sdk/pico_sdk_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#endif // __has_include(<hardware/exception.h>) ||
// __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)
#include <hardware/flash.h>
#endif // __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>)
#include <hardware/i2c.h>
#endif // __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>)
Expand Down
1 change: 1 addition & 0 deletions src/sdk/scripts/api_dirs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ hardware adc
hardware divider
hardware dma
hardware exception
hardware flash
hardware i2c
hardware pio
hardware gpio
Expand Down
32 changes: 32 additions & 0 deletions src/sdk/sdkwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,38 @@ bool rpp_driver::SdkWrapper::exception_set_priority(uint num,
#endif // __has_include(<hardware/exception.h>) ||
// __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)
// --------------------------------------------------
extern "C" void flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf,
size_t count);
void rpp_driver::SdkWrapper::flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf,
size_t count) {
::flash_do_cmd(txbuf, rxbuf, count);
}
// --------------------------------------------------
extern "C" void flash_flush_cache(void);
void rpp_driver::SdkWrapper::flash_flush_cache(void) { ::flash_flush_cache(); }
// --------------------------------------------------
extern "C" void flash_get_unique_id(uint8_t* id_out);
void rpp_driver::SdkWrapper::flash_get_unique_id(uint8_t* id_out) {
::flash_get_unique_id(id_out);
}
// --------------------------------------------------
extern "C" void flash_range_erase(uint32_t flash_offs, size_t count);
void rpp_driver::SdkWrapper::flash_range_erase(uint32_t flash_offs,
size_t count) {
::flash_range_erase(flash_offs, count);
}
// --------------------------------------------------
extern "C" void flash_range_program(uint32_t flash_offs, const uint8_t* data,
size_t count);
void rpp_driver::SdkWrapper::flash_range_program(uint32_t flash_offs,
const uint8_t* data,
size_t count) {
::flash_range_program(flash_offs, data, count);
}
#endif // __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>)
// --------------------------------------------------
extern "C" void i2c_deinit(i2c_inst_t* i2c);
Expand Down
9 changes: 9 additions & 0 deletions src/sdk/sdkwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ virtual void adc_fifo_drain ( void );
#endif // __has_include(<hardware/exception.h>) ||
// __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)
virtual void flash_do_cmd(const uint8_t* txbuf, uint8_t* rxbuf, size_t count);
virtual void flash_flush_cache(void);
virtual void flash_get_unique_id(uint8_t* id_out);
virtual void flash_range_erase(uint32_t flash_offs, size_t count);
virtual void flash_range_program(uint32_t flash_offs, const uint8_t* data,
size_t count);
#endif // __has_include(<hardware/flash.h>) || __has_include(<gmock/gmock.h>)

#if __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>)
virtual void i2c_deinit(i2c_inst_t* i2c);
virtual uint i2c_get_dreq(i2c_inst_t* i2c, bool is_tx);
Expand Down
48 changes: 26 additions & 22 deletions test/test_sdkwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ FAKE_VALUE_FUNC(uint16_t, adc_read);
FAKE_VALUE_FUNC(int32_t, hw_divider_quotient_s32, int32_t, int32_t);
FAKE_VALUE_FUNC(dma_channel_config, dma_channel_get_default_config, uint);
FAKE_VALUE_FUNC(uint, exception_get_priority, uint);
FAKE_VOID_FUNC(flash_range_erase, uint32_t, size_t);
}
// The cpp file of the library to test.
#include "../src/sdk/sdkwrapper.cpp"
Expand Down Expand Up @@ -1577,43 +1578,46 @@ TEST(SdkWrapper, dma_channel_get_default_config) {
}
RESET_FAKE(dma_channel_get_default_config);
} // TEST(SdkWrapper, dma_channel_get_default_config)

// -----------------------------------------------------------
//
// hardware_exception
// hardware_flash
// virtual void flash_range_erase(uint32_t flash_offs, size_t count);
//
// -----------------------------------------------------------

TEST(SdkWrapper, exception_get_priority) {
TEST(SdkWrapper, flash_range_erase) {
std::random_device rng;
::rpp_driver::SdkWrapper pico;
// uniform distribution
std::uniform_int_distribution<uint> dist(0, UINT_MAX);
uint param_array0[] = {dist(rng), dist(rng)};
uint retval_array[std::size(param_array0)] = {dist(rng), dist(rng)};
std::uniform_int_distribution<uint32_t> dist0(0, UINT32_MAX);
std::uniform_int_distribution<size_t> dist1(0, SIZE_MAX);
uint32_t param_array0[] = {dist0(rng), dist0(rng)};
size_t param_array1[] = {dist1(rng), dist1(rng)};

FFF_RESET_HISTORY();
RESET_FAKE(exception_get_priority);

SET_RETURN_SEQ(exception_get_priority, retval_array, std::size(retval_array));
RESET_FAKE(flash_range_erase);

// Check whether return values are correctly passed to wrapper.
int index = 0;
for (auto &&param0 : param_array0) {
ASSERT_EQ(pico.exception_get_priority(param0), retval_array[index]);
index++;
for (auto &&param1 : param_array1) {
pico.flash_range_erase(param0, param1);
}
}

// Check the data from test spy. How many time called?
ASSERT_EQ(exception_get_priority_fake.call_count, std::size(retval_array));
ASSERT_EQ(flash_range_erase_fake.call_count,
std::size(param_array0) * std::size(param_array1));

// Check whether parameters were correctly passed from wrapper.
index = 0;
int index = 0;
for (auto &&param0 : param_array0) {
// Check the data from test spy. Call order.
ASSERT_EQ(fff.call_history[index], (void *)exception_get_priority);
// Check the data from test spy. : Parameters.
ASSERT_EQ(exception_get_priority_fake.arg0_history[index], param0);
index++;
for (auto &&param1 : param_array1) {
// Check the data from test spy. Call order.
ASSERT_EQ(fff.call_history[index], (void *)flash_range_erase);
// Check the data from test spy. : Parameters.
ASSERT_EQ(flash_range_erase_fake.arg0_history[index], param0);
ASSERT_EQ(flash_range_erase_fake.arg1_history[index], param1);
index++;
}
}
RESET_FAKE(exception_get_priority);
} // TEST(SdkWrapper, exception_get_priority)
RESET_FAKE(flash_range_erase);
} // TEST(SdkWrapper, flash_range_erase)
8 changes: 8 additions & 0 deletions test/todo/test_sdkwrapper.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# test_SdkWrapper TDD


## SdkWrapper::flash_range_erase()
- [x] Implement member function .
- [x] Create test case to fail.
- [x] Make it success.
- [x] Remove the duplication.


## SdkWrapper::dma_channel_get_default_config()
- [x] Implement member function .
- [x] Create test case to fail.
Expand Down

0 comments on commit ede330c

Please sign in to comment.