Skip to content

Commit

Permalink
feat: move starting the DAC channel to the constructor to avoid a 1ms…
Browse files Browse the repository at this point in the history
… delay loop every time the Dac value is set (#387)
  • Loading branch information
daantimmer committed Sep 2, 2024
1 parent ad1e53a commit 8211b8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 15 additions & 5 deletions hal_st/stm32fxxx/DigitalToAnalogPinStm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,44 @@ namespace hal
: dac{ dac }
, channel{ infra::enum_cast(channel) == 1 ? DAC_CHANNEL_1 : DAC_CHANNEL_2 }
, internallyConnected{ true }
{}
{
Configure();
}

DigitalToAnalogPinImplStm::DigitalToAnalogPinImplStm(hal::GpioPinStm& pin, hal::DacStm& dac, External)
: pin{ infra::inPlace, pin }
, dac{ dac }
, channel{ pin.DacChannel(dac.index + 1) == 1 ? DAC_CHANNEL_1 : DAC_CHANNEL_2 }
, internallyConnected{ false }
{}
{
Configure();
}

DigitalToAnalogPinImplStm::DigitalToAnalogPinImplStm(hal::GpioPinStm& pin, hal::DacStm& dac, Both)
: pin{ infra::inPlace, pin }
, dac{ dac }
, channel{ pin.DacChannel(dac.index + 1) == 1 ? DAC_CHANNEL_1 : DAC_CHANNEL_2 }
, internallyConnected{ true }

{}
{
Configure();
}
#else
DigitalToAnalogPinImplStm::DigitalToAnalogPinImplStm(hal::GpioPinStm& pin, hal::DacStm& dac)
: pin{ infra::inPlace, pin }
, dac(dac)
, channel{ pin.DacChannel(dac.index + 1) == 1 ? DAC_CHANNEL_1 : DAC_CHANNEL_2 }
{
Configure();
}
#endif

void DigitalToAnalogPinImplStm::Set(uint16_t value)
{
dac.Set(channel, DAC_ALIGN_12B_R, std::min(static_cast<uint32_t>(value), (infra::Bit<uint32_t>(12) - 1)));
}

void DigitalToAnalogPinImplStm::Configure()
{
DAC_ChannelConfTypeDef sConfig{ 0 };
sConfig.DAC_OutputBuffer = pin ? DAC_OUTPUTBUFFER_ENABLE : DAC_OUTPUTBUFFER_DISABLE;
Expand All @@ -64,8 +76,6 @@ namespace hal

result = HAL_DAC_Start(&dac.handle, channel);
assert(result == HAL_OK);

dac.Set(channel, DAC_ALIGN_12B_R, std::min(static_cast<uint32_t>(value), (infra::Bit<uint32_t>(12) - 1)));
}

DacStm::DacStm(uint8_t oneBasedIndex)
Expand Down
2 changes: 2 additions & 0 deletions hal_st/stm32fxxx/DigitalToAnalogPinStm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace hal
void Set(uint16_t value) override;

private:
void Configure();

infra::Optional<AnalogPinStm> pin;
DacStm& dac;
uint32_t channel;
Expand Down

0 comments on commit 8211b8c

Please sign in to comment.