libyggdrasil  v1.0.0
dac.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Midgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <cmath>
31 
32 namespace bsp::mid::drv {
33 
43  template<auto Context, u8 Index, u32 Offset, u32 MaxValue>
44  struct DACChannel {
45  DACChannel(const DACChannel&) = delete;
46  auto operator=(const DACChannel&) = delete;
47 
48  constexpr static auto ReferenceVoltage = 3.3;
49 
55  static bool init() {
56  return true;
57  }
58 
64  static bool deinit() {
65  return true;
66  }
67 
73  auto operator=(float value) const noexcept {
74  constexpr auto Channel = getHALChannel();
75  HAL_DAC_SetValue(Context, Channel, DAC_ALIGN_12B_R, u32(std::max(value * MaxValue - Offset, 0.0F)));
76  HAL_DAC_Start(Context, Channel);
77  }
78 
84  operator float() const noexcept {
85  constexpr auto Channel = getHALChannel();
86 
87  return float(std::max(HAL_DAC_GetValue(Context, Channel) + Offset, 0)) / MaxValue;
88  }
89 
90  private:
91  DACChannel() = default;
92 
98  constexpr static u32 getHALChannel() {
99  switch (Index) {
100  case 1: return DAC_CHANNEL_1;
101  case 2: return DAC_CHANNEL_2;
102  default: bsp::unreachable();
103  }
104  }
105 
109  template<auto, template<auto, u8, u32, u32> typename>
110  friend struct bsp::drv::DAConverter;
111  };
112 
113 }
bsp::mid::drv::DACChannel::init
static bool init()
Init function.
Definition: dac.hpp:55
dac.hpp
Frontend for the DAC abstraction.
bsp::mid::drv::DACChannel::ReferenceVoltage
constexpr static auto ReferenceVoltage
Definition: dac.hpp:48
bsp::mid::drv::DACChannel
DAC Channel implementation for Asgard.
Definition: dac.hpp:44
bsp::mid::drv::DACChannel::deinit
static bool deinit()
Deinit function.
Definition: dac.hpp:64
u32
uint32_t u32
Definition: types.h:38
bsp::mid::drv::DACChannel::operator=
auto operator=(const DACChannel &)=delete
bsp::mid::drv::DACChannel::DACChannel
DACChannel(const DACChannel &)=delete
bsp::mid::drv::DACChannel::operator=
auto operator=(float value) const noexcept
Set the current DAC value.
Definition: dac.hpp:73
bsp::mid::drv
Definition: adc.hpp:32
bsp::drv::DAConverter
Base class for DAC abstraction.
Definition: dac.hpp:40