21 callback_ = std::move(callback);
24 auto callback =
reinterpret_cast<std::function<
void()
> *>(context);
31 std::function<void()> callback_;
34#ifdef HAL_TIM_MODULE_ENABLED
36template <TIM_HandleTypeDef *Handle>
class Tim :
public TimBase {
38 using TimBase::attach_callback;
41 stm32cubemx_helper::set_context<Handle, Tim>(
this);
42 HAL_TIM_RegisterCallback(
43 Handle, HAL_TIM_PERIOD_ELAPSED_CB_ID, [](TIM_HandleTypeDef *) {
44 auto tim = stm32cubemx_helper::get_context<Handle, Tim>();
46 tim->callback_(tim->context_);
52 HAL_TIM_UnRegisterCallback(Handle, HAL_TIM_PERIOD_ELAPSED_CB_ID);
53 stm32cubemx_helper::set_context<Handle, Tim>(
nullptr);
56 bool start()
override {
return HAL_TIM_Base_Start_IT(Handle) == HAL_OK; }
58 bool stop()
override {
return HAL_TIM_Base_Stop_IT(Handle) == HAL_OK; }
60 uint32_t get_counter()
const override {
61 return __HAL_TIM_GET_COUNTER(Handle);
64 void set_counter(uint32_t count)
override {
65 __HAL_TIM_SET_COUNTER(Handle, count);
68 bool attach_callback(
void (*callback)(
void *context),
69 void *context)
override {
78 bool detach_callback()
override {
87 void (*callback_)(
void *context) =
nullptr;
88 void *context_ =
nullptr;
virtual bool attach_callback(void(*callback)(void *context), void *context)=0
virtual ~TimBase()
Definition tim.hpp:11
virtual uint32_t get_counter() const =0
virtual void set_counter(uint32_t count)=0
virtual bool detach_callback()=0
bool attach_callback(std::function< void()> &&callback)
Definition tim.hpp:20