diff --git a/Peripherals/Inc/peripherals_hal_st.hpp b/Peripherals/Inc/peripherals_hal_st.hpp index fc08092..2e3033b 100644 --- a/Peripherals/Inc/peripherals_hal_st.hpp +++ b/Peripherals/Inc/peripherals_hal_st.hpp @@ -10,8 +10,6 @@ #include -#include "peripherals_utility.hpp" - #include #define USE_DELAY_US @@ -95,10 +93,16 @@ struct Gpio bool read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); } }; -#define CREATE_GPIO(name) \ - Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, false } -#define CREATE_INV_GPIO(name) \ - Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, true } +#define CREATE_GPIO(name) \ + Peripherals::Hal::St::Gpio \ + { \ + name##_GPIO_Port, name##_Pin, false \ + } +#define CREATE_INV_GPIO(name) \ + Peripherals::Hal::St::Gpio \ + { \ + name##_GPIO_Port, name##_Pin, true \ + } #endif// HAL_GPIO_MODULE_ENABLED @@ -270,17 +274,17 @@ namespace _Details * are const-correct. The others remain unconst. So these wrappers will exist until we're no longer partially * const-correct. */ -inline HAL_StatusTypeDef uartTransmit(UART_HandleTypeDef *huart, std::uint8_t *data, const std::uint16_t size, const std::uint32_t timeout) +inline HAL_StatusTypeDef uartTransmit(UART_HandleTypeDef* huart, std::uint8_t* data, const std::uint16_t size, const std::uint32_t timeout) { return HAL_UART_Transmit(huart, data, size, timeout); } -inline HAL_StatusTypeDef uartTransmitDma(UART_HandleTypeDef *huart, std::uint8_t *data, const std::uint16_t size) +inline HAL_StatusTypeDef uartTransmitDma(UART_HandleTypeDef* huart, std::uint8_t* data, const std::uint16_t size) { return HAL_UART_Transmit_DMA(huart, data, size); } -} +}// namespace _Details using UartInterface = SerialInterface; diff --git a/Peripherals/Inc/peripherals_imu_icm.hpp b/Peripherals/Inc/peripherals_imu_icm.hpp index c8418a7..ed7f684 100644 --- a/Peripherals/Inc/peripherals_imu_icm.hpp +++ b/Peripherals/Inc/peripherals_imu_icm.hpp @@ -12,7 +12,6 @@ #include #include "peripherals_imu.hpp" -#include "peripherals_utility.hpp" namespace Peripherals { diff --git a/Peripherals/Inc/peripherals_utility.hpp b/Peripherals/Inc/peripherals_utility.hpp deleted file mode 100644 index 0ab29af..0000000 --- a/Peripherals/Inc/peripherals_utility.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * peripherals_utility.hpp - * - * Created on: Feb 24, 2021 - * Author: erki - */ - -#ifndef SKULLC_PERIPHERALS_UTILITY_HPP_ -#define SKULLC_PERIPHERALS_UTILITY_HPP_ - -#include -#include -#include - -namespace Peripherals -{ - -template -constexpr const T& clamp(const T& v, const T& lo, const T& hi) -{ - if (v > hi) - return hi; - else if (v < lo) - return lo; - else - return v; -} - -template -T byteToTypeBE(const std::uint8_t a[N]) -{ - T t(0); - - for (std::size_t i = 0; i < N; i++) - { - t |= a[i] << (i * 8); - } - - return t; -} - -template -T byteToTypeLE(const std::uint8_t a[N]) -{ - T t(0); - - for (std::size_t i = N; i >= 0; i--) - { - t |= a[i] << ((i - 1) * 8); - } - - return t; -} - -template -constexpr T zeroInitialized() -{ - static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); - - T t; - std::memset(&t, 0, sizeof(T)); - - return t; -} - -}// namespace Peripherals - -#endif /* SKULLC_PERIPHERALS_UTILITY_HPP_ */ diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 53eedd7..745b313 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable(tests assert_ndebug.cpp assert.cpp enum_helpers.cpp + bytes.cpp ) target_link_libraries(tests diff --git a/Tests/bytes.cpp b/Tests/bytes.cpp new file mode 100644 index 0000000..f7c78d2 --- /dev/null +++ b/Tests/bytes.cpp @@ -0,0 +1,68 @@ +// +// Created by erki on 10/12/22. +// + +#include + +#include "utility_bytes.hpp" + +TEST_CASE("zeroInitialized creates an appropriate struct.") +{ + struct TestStruct + { + int a; + char b; + int c; + }; + + SECTION("Return by value variant returns a struct where all members are 0.") + { + const TestStruct test = Utility::zeroInitialized(); + REQUIRE(test.a == 0); + REQUIRE(test.b == 0); + REQUIRE(test.c == 0); + } + + SECTION("Pass by reference variant initializes the struct to all 0's.") + { + TestStruct test; + Utility::zeroInitialized(test); + + REQUIRE(test.a == 0); + REQUIRE(test.b == 0); + REQUIRE(test.c == 0); + } + + SECTION("Pass by pointer variant initializes the struct to all 0's.") + { + TestStruct test; + Utility::zeroInitialized(&test); + + REQUIRE(test.a == 0); + REQUIRE(test.b == 0); + REQUIRE(test.c == 0); + } +} + +TEST_CASE("arrayToType constructs a type appropriately.") +{ + const std::uint32_t initial_value = 0x12345678; + + SECTION("Using C-arrays the operation works appropriately.") + { + std::uint8_t raw_data[4] = {0}; + std::memcpy(raw_data, &initial_value, 4); + + const std::uint32_t output = Utility::arrayToType(raw_data); + REQUIRE(output == initial_value); + } + + SECTION("Using C-arrays the operation works appropriately.") + { + std::array raw_data = {0, 0, 0, 0}; + std::memcpy(raw_data.data(), &initial_value, 4); + + const std::uint32_t output = Utility::arrayToType(raw_data); + REQUIRE(output == initial_value); + } +} \ No newline at end of file diff --git a/Threads/Inc/threads_primitivethread.hpp b/Threads/Inc/threads_primitivethread.hpp index 6862d51..775476b 100644 --- a/Threads/Inc/threads_primitivethread.hpp +++ b/Threads/Inc/threads_primitivethread.hpp @@ -11,9 +11,9 @@ #include #include +#include #include #include -#include namespace Threads { diff --git a/Threads/Src/threads_primitivethread.cpp b/Threads/Src/threads_primitivethread.cpp index 52005e8..a4d5342 100644 --- a/Threads/Src/threads_primitivethread.cpp +++ b/Threads/Src/threads_primitivethread.cpp @@ -7,7 +7,7 @@ #include "threads_primitivethread.hpp" -#include "peripherals_utility.hpp" +#include "utility_bytes.hpp" #include @@ -108,7 +108,7 @@ PrimitiveThread::PrimitiveThread(TaskHandle_t threadHandle) osThreadAttr_t PrimitiveThread::constructThreadAttrs_(const char* name, const osPriority_t priority, const std::uint32_t stack_size) { - auto attrs = Peripherals::zeroInitialized(); + auto attrs = Utility::zeroInitialized(); attrs.name = name; attrs.priority = priority; diff --git a/Utility/Inc/utility_bytes.hpp b/Utility/Inc/utility_bytes.hpp new file mode 100644 index 0000000..e9ccc95 --- /dev/null +++ b/Utility/Inc/utility_bytes.hpp @@ -0,0 +1,69 @@ +// +// Created by erki on 10/12/22. +// + +#ifndef SKULLC_UTILITY_BYTES_HPP_ +#define SKULLC_UTILITY_BYTES_HPP_ + +#include +#include +#include +#include + +namespace Utility +{ + +template +T arrayToType(const std::uint8_t raw[N]) +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + static_assert(sizeof(T) == N, "The raw data array is not the same size as T."); + + T t; + std::memcpy(&t, raw, sizeof(T)); + + return t; +} + +template +T arrayToType(const std::array raw) +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + static_assert(sizeof(T) == N, "The raw data array is not the same size as T."); + + T t; + std::memcpy(&t, raw.data(), sizeof(T)); + + return t; +} + +template +constexpr T zeroInitialized() +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + + T t; + std::memset(&t, 0, sizeof(T)); + + return t; +} + +template +void zeroInitialized(T& t) +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + + std::memset(&t, 0, sizeof(T)); +} + +template +void zeroInitialized(T* t) +{ + static_assert(std::is_trivially_default_constructible::value, "Struct is not trivially default constructible."); + + std::memset(t, 0, sizeof(T)); +} + +}// namespace Utility + +#endif//SKULLC_UTILITY_BYTES_HPP_