-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Maxime Blanc <[email protected]> Co-Authored-By: SamHadjes <[email protected]>
- Loading branch information
1 parent
9da3fb3
commit ab3eba0
Showing
13 changed files
with
324 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Leka - LekaOS | ||
# Copyright 2024 APF France handicap | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
add_library(CoreDAC STATIC) | ||
|
||
target_include_directories(CoreDAC | ||
PUBLIC | ||
include | ||
) | ||
|
||
target_sources(CoreDAC | ||
PRIVATE | ||
source/CoreSTM32HalBasicTimer.cpp | ||
) | ||
|
||
if(NOT(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")) | ||
target_sources(CoreDAC | ||
PUBLIC | ||
source/HAL_IRQHandlers.cpp | ||
) | ||
endif() | ||
|
||
target_link_libraries(CoreDAC | ||
mbed-os | ||
CoreSTM32Hal | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Leka - LekaOS | ||
// Copyright 2024 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include "interface/drivers/STM32HalBasicTimer.h" | ||
|
||
namespace leka { | ||
|
||
class CoreSTM32HalBasicTimer : public interface::STM32HalBasicTimer | ||
{ | ||
static constexpr float DEFAULT_AUDIO_FILE_SAMPLE_RATE = 44'100; | ||
|
||
public: | ||
CoreSTM32HalBasicTimer(interface::STM32Hal &hal); | ||
|
||
[[nodiscard]] auto getHandle() -> TIM_HandleTypeDef & final; | ||
|
||
void registerCallback(std::function<void()> const &callback); | ||
void linkDACTimer(DAC_ChannelConfTypeDef *config) final; | ||
|
||
void initialize(float frequency = DEFAULT_AUDIO_FILE_SAMPLE_RATE) final; | ||
void terminate() final; | ||
|
||
void start() final; | ||
void stop() final; | ||
|
||
private: | ||
void _registerMspCallbacks(); | ||
|
||
interface::STM32Hal &_hal; | ||
|
||
TIM_HandleTypeDef _htim {}; | ||
|
||
std::function<void()> _callback {}; | ||
}; | ||
|
||
} // namespace leka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Leka - LekaOS | ||
// Copyright 2024 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "CoreSTM32HalBasicTimer.h" | ||
#include <cmath> | ||
|
||
using namespace leka; | ||
|
||
CoreSTM32HalBasicTimer::CoreSTM32HalBasicTimer(interface::STM32Hal &hal) : _hal(hal) | ||
{ | ||
_htim.Instance = TIM6; | ||
} | ||
|
||
auto CoreSTM32HalBasicTimer::getHandle() -> TIM_HandleTypeDef & | ||
{ | ||
return _htim; | ||
} | ||
|
||
void CoreSTM32HalBasicTimer::registerCallback(std::function<void()> const &callback) | ||
{ | ||
_callback = callback; | ||
} | ||
|
||
void CoreSTM32HalBasicTimer::initialize(float frequency) | ||
{ | ||
_registerMspCallbacks(); | ||
|
||
// CK_Timer = CK_INT / ((Prescaler + 1) * (Period + 1)) | ||
const auto CK_INT = float {108'000'000.0}; | ||
auto divider = static_cast<int>(std::round(CK_INT / frequency)); | ||
|
||
_htim.Init.Prescaler = 0; | ||
_htim.Init.Period = divider - 1; // ? min 1 | ||
_htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; | ||
_hal.HAL_TIM_Base_Init(&_htim); | ||
|
||
auto timerMasterConfig = TIM_MasterConfigTypeDef {}; | ||
timerMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; | ||
_hal.HAL_TIMEx_MasterConfigSynchronization(&_htim, &timerMasterConfig); | ||
|
||
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 const auto &self = *this; | ||
|
||
_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, []([[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() | ||
{ | ||
_hal.HAL_TIM_Base_DeInit(&_htim); | ||
} | ||
|
||
void CoreSTM32HalBasicTimer::start() | ||
{ | ||
_hal.HAL_TIM_Base_Start_IT(&_htim); | ||
} | ||
|
||
void CoreSTM32HalBasicTimer::stop() | ||
{ | ||
_hal.HAL_TIM_Base_Stop_IT(&_htim); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "CoreSTM32HalBasicTimer.h" | ||
|
||
extern "C" { | ||
|
||
extern leka::CoreSTM32HalBasicTimer hal_timer; | ||
|
||
void TIM6_DAC_IRQHandler() | ||
{ | ||
HAL_TIM_IRQHandler(&hal_timer.getHandle()); | ||
} | ||
|
||
void TIM7_DAC_IRQHandler() | ||
{ | ||
HAL_TIM_IRQHandler(&hal_timer.getHandle()); | ||
} | ||
|
||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Leka - LekaOS | ||
// Copyright 2024 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "interface/drivers/STM32Hal.h" | ||
|
||
namespace leka::interface { | ||
|
||
class STM32HalBasicTimer | ||
{ | ||
public: | ||
virtual ~STM32HalBasicTimer() = default; | ||
|
||
[[nodiscard]] virtual auto getHandle() -> TIM_HandleTypeDef & = 0; | ||
|
||
virtual void linkDACTimer(DAC_ChannelConfTypeDef *config) = 0; | ||
|
||
virtual void initialize(float frequency) = 0; | ||
virtual void terminate() = 0; | ||
|
||
virtual void start() = 0; | ||
virtual void stop() = 0; | ||
}; | ||
|
||
} // namespace leka::interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.