Skip to content

Commit

Permalink
New code
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Mar 4, 2024
1 parent e2afa58 commit 6973048
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 55 deletions.
1 change: 1 addition & 0 deletions drivers/CoreDAC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ target_link_libraries(CoreDAC

if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
leka_unit_tests_sources(
tests/CoreSTM32HalBasicTimer_test.cpp
tests/CoreDAC_test.cpp
)
endif()
4 changes: 2 additions & 2 deletions drivers/CoreDAC/include/CoreDAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CoreDAC : public interface::DACBase
DMA_HandleTypeDef _hdma {};

std::span<uint16_t> _data;
std::function<void()> _on_half_transfer = [] {};
std::function<void()> _on_complete_transfer = [] {};
std::function<void()> _on_half_transfer {};
std::function<void()> _on_complete_transfer {};
};

} // namespace leka
5 changes: 3 additions & 2 deletions drivers/CoreDAC/include/CoreSTM32HalBasicTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class CoreSTM32HalBasicTimer : public interface::STM32HalBasicTimer
[[nodiscard]] auto getHandle() -> TIM_HandleTypeDef & final;

void registerCallback(std::function<void()> const &callback);
void linkDACTimer(DAC_ChannelConfTypeDef *config) final;

void initialize(uint32_t frequency) final;
void initialize(uint32_t frequency = 44'100) final;
void terminate() final;

void start() final;
Expand All @@ -32,7 +33,7 @@ class CoreSTM32HalBasicTimer : public interface::STM32HalBasicTimer

TIM_HandleTypeDef _htim {};

std::function<void()> _callback = [] {};
std::function<void()> _callback {};
};

} // namespace leka
2 changes: 2 additions & 0 deletions drivers/CoreDAC/include/interface/STM32HalBasicTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class STM32HalBasicTimer

[[nodiscard]] virtual auto getHandle() -> TIM_HandleTypeDef & = 0;

virtual void linkDACTimer(DAC_ChannelConfTypeDef *config) = 0;

virtual void initialize(uint32_t frequency) = 0;
virtual void terminate() = 0;

Expand Down
23 changes: 15 additions & 8 deletions drivers/CoreDAC/source/CoreDAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ void CoreDAC::initialize()
_hal.HAL_DAC_Init(&_hdac);

DAC_ChannelConfTypeDef config = {};
config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; // necessary to reach the full voltage range in DAC output
config.DAC_Trigger = DAC_TRIGGER_T6_TRGO; // configure the DAC to be triggered by TIM6 through TRGO signal
config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
_hal_timer.linkDACTimer(&config);
_hal.HAL_DAC_ConfigChannel(&_hdac, &config, DAC_CHANNEL_1);

static auto &self = *this;
static const auto &self = *this;
_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_CH1_HALF_COMPLETE_CB_ID,
[](DAC_HandleTypeDef *hdac) { self._on_half_transfer(); });
_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_CH1_COMPLETE_CB_ID,
[](DAC_HandleTypeDef *hdac) { self._on_complete_transfer(); });
[]([[maybe_unused]] DAC_HandleTypeDef *hdac) {
if (self._on_half_transfer != nullptr) {
self._on_half_transfer();
}
});
_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_CH1_COMPLETE_CB_ID, []([[maybe_unused]] DAC_HandleTypeDef *hdac) {
if (self._on_complete_transfer != nullptr) {
self._on_complete_transfer();
}
});
}

void CoreDAC::terminate()
Expand Down Expand Up @@ -67,14 +74,14 @@ void CoreDAC::_registerMspCallbacks()
{
static auto &self = *this;

_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_MSPINIT_CB_ID, [](DAC_HandleTypeDef *hdac) {
_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_MSPINIT_CB_ID, []([[maybe_unused]] DAC_HandleTypeDef *hdac) {
__HAL_LINKDMA(&self._hdac, DMA_Handle1, self._hdma);
self._initializeDMA();

self._hal.HAL_RCC_DAC_CLK_ENABLE();
});

_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_MSPDEINIT_CB_ID, [](DAC_HandleTypeDef *hdac) {
_hal.HAL_DAC_RegisterCallback(&_hdac, HAL_DAC_MSPDEINIT_CB_ID, []([[maybe_unused]] DAC_HandleTypeDef *hdac) {
self._hal.HAL_DMA_DeInit(&self._hdma);

self._hal.HAL_RCC_DAC_CLK_DISABLE();
Expand Down
37 changes: 22 additions & 15 deletions drivers/CoreDAC/source/CoreSTM32HalBasicTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,44 @@ void CoreSTM32HalBasicTimer::initialize(uint32_t frequency)
// CK_Timer = CK_Int / ((Prescaler + 1) * (Period + 1))
auto divider = 54'000'000 / frequency;

_htim.Init.Prescaler = 2;
_htim.Init.Period = 100; // ? min 1
_htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; // Disable shadow write
// _htim.Init.CounterMode; // NOT AVAILABLE for BasicTimer
// _htim.Init.ClockDivision; // NOT AVAILABLE for BasicTimer
// _htim.Init.RepetitionCounter; // NOT AVAILABLE for BasicTimer
_htim.Init.Prescaler = divider / 100;
_htim.Init.Period = 100; // ? min 1
_htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
_hal.HAL_TIM_Base_Init(&_htim);

auto timerMasterConfig = TIM_MasterConfigTypeDef {};
timerMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; // Use hardware trigger
// timerMasterConfig.MasterSlaveMode; // NOT AVAILABLE for BasicTimer
timerMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
_hal.HAL_TIMEx_MasterConfigSynchronization(&_htim, &timerMasterConfig);

static auto &self = *this;
_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_PERIOD_ELAPSED_CB_ID,
[](TIM_HandleTypeDef *htim) { self._callback(); });
static const auto &self = *this;
_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_PERIOD_ELAPSED_CB_ID, []([[maybe_unused]] TIM_HandleTypeDef *htim) {
if (self._callback != nullptr) {
self._callback();
}
});
}

void CoreSTM32HalBasicTimer::_registerMspCallbacks()
{
static auto &self = *this;
static const auto &self = *this;

_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_BASE_MSPINIT_CB_ID, [](TIM_HandleTypeDef *htim) {
_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_BASE_MSPINIT_CB_ID, []([[maybe_unused]] TIM_HandleTypeDef *htim) {
self._hal.HAL_RCC_TIM6_CLK_ENABLE();

self._hal.HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 0x00, 0x00);
self._hal.HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
});

_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_BASE_MSPDEINIT_CB_ID,
[](TIM_HandleTypeDef *htim) { self._hal.HAL_RCC_TIM6_CLK_DISABLE(); });
_hal.HAL_TIM_RegisterCallback(&_htim, HAL_TIM_BASE_MSPDEINIT_CB_ID, []([[maybe_unused]] TIM_HandleTypeDef *htim) {
self._hal.HAL_RCC_TIM6_CLK_DISABLE();
});
}

void CoreSTM32HalBasicTimer::linkDACTimer(DAC_ChannelConfTypeDef *config)
{
if (config != nullptr) {
config->DAC_Trigger = DAC_TRIGGER_T6_TRGO;
}
}

void CoreSTM32HalBasicTimer::terminate()
Expand Down
5 changes: 5 additions & 0 deletions drivers/CoreDAC/source/HAL_IRQHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ void TIM6_DAC_IRQHandler()
HAL_TIM_IRQHandler(&hal_timer.getHandle());
}

void TIM7_DAC_IRQHandler()
{
HAL_TIM_IRQHandler(&hal_timer.getHandle());
}

void DMA1_Stream5_IRQHandler()
{
HAL_DMA_IRQHandler(coredac.getHandle().DMA_Handle1);
Expand Down
30 changes: 13 additions & 17 deletions drivers/CoreDAC/tests/CoreDAC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ using namespace leka;
using testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Matcher;
using ::testing::MockFunction;
using ::testing::Return;
using ::testing::SaveArg;
Expand All @@ -38,6 +37,7 @@ class CoreDACTest : public ::testing::Test
.WillOnce(DoAll(SaveArg<2>(&dma_complete_transfer_callback), Return(HAL_StatusTypeDef::HAL_OK)));

EXPECT_CALL(halmock, HAL_DAC_Init);
EXPECT_CALL(haltimermock, linkDACTimer);
EXPECT_CALL(halmock, HAL_DAC_ConfigChannel)
.WillOnce(DoAll(SaveArgPointee<1>(&dac_config), Return(HAL_StatusTypeDef::HAL_OK)));

Expand Down Expand Up @@ -74,8 +74,6 @@ TEST_F(CoreDACTest, initializationDefault)

TEST_F(CoreDACTest, initialize)
{
dac.registerDMACallbacks(on_half_transfer_callback.AsStdFunction(), on_complete_transfer_callback.AsStdFunction());

{
InSequence seq;

Expand All @@ -86,6 +84,7 @@ TEST_F(CoreDACTest, initialize)

{
EXPECT_CALL(halmock, HAL_DAC_Init);
EXPECT_CALL(haltimermock, linkDACTimer);
EXPECT_CALL(halmock, HAL_DAC_ConfigChannel);
} // DAC config

Expand All @@ -107,7 +106,14 @@ TEST_F(CoreDACTest, initializeMspInit)

EXPECT_CALL(halmock, HAL_RCC_DAC_CLK_ENABLE);

mspinit_callback(nullptr);
mspinit_callback(&dac.getHandle());
}

TEST_F(CoreDACTest, initializeMspDeinit)
{
EXPECT_CALL(halmock, HAL_DMA_DeInit);
EXPECT_CALL(halmock, HAL_RCC_DAC_CLK_DISABLE);
mspdeinit_callback(&dac.getHandle());
}

TEST_F(CoreDACTest, initializeMspInitDMAConfig)
Expand All @@ -122,7 +128,7 @@ TEST_F(CoreDACTest, initializeMspInitDMAConfig)

EXPECT_CALL(halmock, HAL_RCC_DAC_CLK_ENABLE);

mspinit_callback(nullptr);
mspinit_callback(&dac.getHandle());

EXPECT_NE(&dma_handle, nullptr);
EXPECT_EQ(dma_handle.Instance, DMA1_Stream5);
Expand All @@ -131,32 +137,22 @@ TEST_F(CoreDACTest, initializeMspInitDMAConfig)
EXPECT_EQ(dma_handle.Init.Direction, DMA_MEMORY_TO_PERIPH);
}

TEST_F(CoreDACTest, initializeMspDeinit)
{
EXPECT_CALL(halmock, HAL_DMA_DeInit);
EXPECT_CALL(halmock, HAL_RCC_DAC_CLK_DISABLE);
mspdeinit_callback(nullptr);
}

TEST_F(CoreDACTest, initializeConfig)
{
// necessary to reach the full voltage range in DAC output
EXPECT_EQ(dac_config.DAC_OutputBuffer, DAC_OUTPUTBUFFER_ENABLE);

// configure the DAC to be triggered by TIM6 through TRGO signal
EXPECT_EQ(dac_config.DAC_Trigger, DAC_TRIGGER_T6_TRGO);
}

TEST_F(CoreDACTest, initializeDmaHalfTransferCallback)
{
EXPECT_CALL(on_half_transfer_callback, Call);
dma_half_transfer_callback(nullptr);
dma_half_transfer_callback(&dac.getHandle());
}

TEST_F(CoreDACTest, initializeDmaCompleteTransferCallback)
{
EXPECT_CALL(on_complete_transfer_callback, Call);
dma_complete_transfer_callback(nullptr);
dma_complete_transfer_callback(&dac.getHandle());
}

TEST_F(CoreDACTest, terminate)
Expand Down
Loading

0 comments on commit 6973048

Please sign in to comment.