Peripherals, Utility: refactor out peripherals_utility.hpp into utility_bytes.hpp
This commit is contained in:
parent
8fbb0efd5d
commit
500c2704bb
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
|
|
||||||
#include "peripherals_utility.hpp"
|
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#define USE_DELAY_US
|
#define USE_DELAY_US
|
||||||
@ -96,9 +94,15 @@ struct Gpio
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define CREATE_GPIO(name) \
|
#define CREATE_GPIO(name) \
|
||||||
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, false }
|
Peripherals::Hal::St::Gpio \
|
||||||
|
{ \
|
||||||
|
name##_GPIO_Port, name##_Pin, false \
|
||||||
|
}
|
||||||
#define CREATE_INV_GPIO(name) \
|
#define CREATE_INV_GPIO(name) \
|
||||||
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, true }
|
Peripherals::Hal::St::Gpio \
|
||||||
|
{ \
|
||||||
|
name##_GPIO_Port, name##_Pin, true \
|
||||||
|
}
|
||||||
|
|
||||||
#endif// HAL_GPIO_MODULE_ENABLED
|
#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
|
* are const-correct. The others remain unconst. So these wrappers will exist until we're no longer partially
|
||||||
* const-correct.
|
* 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);
|
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);
|
return HAL_UART_Transmit_DMA(huart, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}// namespace _Details
|
||||||
|
|
||||||
using UartInterface =
|
using UartInterface =
|
||||||
SerialInterface<UART_HandleTypeDef, _Details::uartTransmit, HAL_UART_Receive>;
|
SerialInterface<UART_HandleTypeDef, _Details::uartTransmit, HAL_UART_Receive>;
|
||||||
|
|||||||
@ -12,7 +12,6 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#include "peripherals_imu.hpp"
|
#include "peripherals_imu.hpp"
|
||||||
#include "peripherals_utility.hpp"
|
|
||||||
|
|
||||||
namespace Peripherals
|
namespace Peripherals
|
||||||
{
|
{
|
||||||
|
|||||||
@ -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 <cstdint>
|
|
||||||
#include <cstring>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace Peripherals
|
|
||||||
{
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
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<typename T, std::size_t N>
|
|
||||||
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<typename T, std::size_t N>
|
|
||||||
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<typename T>
|
|
||||||
constexpr T zeroInitialized()
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
|
|
||||||
T t;
|
|
||||||
std::memset(&t, 0, sizeof(T));
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
}// namespace Peripherals
|
|
||||||
|
|
||||||
#endif /* SKULLC_PERIPHERALS_UTILITY_HPP_ */
|
|
||||||
@ -29,6 +29,7 @@ add_executable(tests
|
|||||||
assert_ndebug.cpp
|
assert_ndebug.cpp
|
||||||
assert.cpp
|
assert.cpp
|
||||||
enum_helpers.cpp
|
enum_helpers.cpp
|
||||||
|
bytes.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(tests
|
target_link_libraries(tests
|
||||||
|
|||||||
68
Tests/bytes.cpp
Normal file
68
Tests/bytes.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 10/12/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#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<TestStruct>();
|
||||||
|
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<std::uint32_t, 4>(raw_data);
|
||||||
|
REQUIRE(output == initial_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Using C-arrays the operation works appropriately.")
|
||||||
|
{
|
||||||
|
std::array<std::uint8_t, 4> raw_data = {0, 0, 0, 0};
|
||||||
|
std::memcpy(raw_data.data(), &initial_value, 4);
|
||||||
|
|
||||||
|
const std::uint32_t output = Utility::arrayToType<std::uint32_t>(raw_data);
|
||||||
|
REQUIRE(output == initial_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,9 +11,9 @@
|
|||||||
#include <cmsis_os.h>
|
#include <cmsis_os.h>
|
||||||
#include <freertos_os2.h>
|
#include <freertos_os2.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
namespace Threads
|
namespace Threads
|
||||||
{
|
{
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "threads_primitivethread.hpp"
|
#include "threads_primitivethread.hpp"
|
||||||
|
|
||||||
#include "peripherals_utility.hpp"
|
#include "utility_bytes.hpp"
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
@ -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)
|
osThreadAttr_t PrimitiveThread::constructThreadAttrs_(const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
||||||
{
|
{
|
||||||
auto attrs = Peripherals::zeroInitialized<osThreadAttr_t>();
|
auto attrs = Utility::zeroInitialized<osThreadAttr_t>();
|
||||||
|
|
||||||
attrs.name = name;
|
attrs.name = name;
|
||||||
attrs.priority = priority;
|
attrs.priority = priority;
|
||||||
|
|||||||
69
Utility/Inc/utility_bytes.hpp
Normal file
69
Utility/Inc/utility_bytes.hpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 10/12/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SKULLC_UTILITY_BYTES_HPP_
|
||||||
|
#define SKULLC_UTILITY_BYTES_HPP_
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace Utility
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T, std::size_t N>
|
||||||
|
T arrayToType(const std::uint8_t raw[N])
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::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<typename T, std::size_t N>
|
||||||
|
T arrayToType(const std::array<std::uint8_t, N> raw)
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::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<typename T>
|
||||||
|
constexpr T zeroInitialized()
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
||||||
|
|
||||||
|
T t;
|
||||||
|
std::memset(&t, 0, sizeof(T));
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void zeroInitialized(T& t)
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
||||||
|
|
||||||
|
std::memset(&t, 0, sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void zeroInitialized(T* t)
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
||||||
|
|
||||||
|
std::memset(t, 0, sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
}// namespace Utility
|
||||||
|
|
||||||
|
#endif//SKULLC_UTILITY_BYTES_HPP_
|
||||||
Loading…
x
Reference in New Issue
Block a user