libyggdrasil  v1.0.0
adc.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 ADCChannel {
45  ADCChannel(const ADCChannel&) = delete;
46  auto operator=(const ADCChannel&) = 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 
74  operator float() const noexcept {
75  switchChannel(); // Switch to the actual channel
76 
77  HAL_ADC_Start(Context); // Start a conversion
78  HAL_ADC_PollForConversion(Context, HAL_MAX_DELAY); // Wait for conversion
79 
80  return std::max(static_cast<float>(HAL_ADC_GetValue(Context)) - Offset, 0.0F) / MaxValue; // Get the value and transform it to 0.0 to 1.0
81  }
82 
83  private:
84  ADCChannel() = default;
85 
91  constexpr static u32 getHALChannel() noexcept {
92  switch (Index) {
93  case 0: return ADC_CHANNEL_0;
94  case 1: return ADC_CHANNEL_1;
95  case 2: return ADC_CHANNEL_2;
96  case 3: return ADC_CHANNEL_3;
97  case 4: return ADC_CHANNEL_4;
98  case 5: return ADC_CHANNEL_5;
99  case 6: return ADC_CHANNEL_6;
100  case 7: return ADC_CHANNEL_7;
101  case 8: return ADC_CHANNEL_8;
102  case 9: return ADC_CHANNEL_9;
103  case 10: return ADC_CHANNEL_10;
104  case 11: return ADC_CHANNEL_11;
105  case 12: return ADC_CHANNEL_12;
106  case 13: return ADC_CHANNEL_13;
107  case 14: return ADC_CHANNEL_14;
108  case 15: return ADC_CHANNEL_15;
109  case 16: return ADC_CHANNEL_16;
110  case 17: return ADC_CHANNEL_17;
111  case 18: return ADC_CHANNEL_18;
112  default: bsp::unreachable();
113  }
114  }
115 
119  void switchChannel() const noexcept {
120  ADC_ChannelConfTypeDef channelConfig = { 0 };
121  constexpr auto HALChannel = getHALChannel(); // Get HAL channel name
122 
123  channelConfig.Channel = HALChannel; // Set channel
124  channelConfig.Rank = ADC_REGULAR_RANK_1; // Set rank to default
125  channelConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; // Set Sample time
126 
127  HAL_ADC_ConfigChannel(Context, &channelConfig); // Switch channel
128  }
129 
133  template<auto, template<auto, u8, u32, u32> typename>
134  friend struct bsp::drv::ADConverter;
135  };
136 
137 }
bsp::mid::drv::ADCChannel
ADC Channel implementation for Asgard.
Definition: adc.hpp:44
bsp::mid::drv::ADCChannel::ReferenceVoltage
constexpr static auto ReferenceVoltage
Definition: adc.hpp:48
bsp::mid::drv::ADCChannel::deinit
static bool deinit()
Deinit function.
Definition: adc.hpp:64
u32
uint32_t u32
Definition: types.h:38
bsp::drv::ADConverter
Base class for ADC abstraction.
Definition: adc.hpp:39
bsp::mid::drv::ADCChannel::ADCChannel
ADCChannel(const ADCChannel &)=delete
bsp::mid::drv::ADCChannel::operator=
auto operator=(const ADCChannel &)=delete
bsp::mid::drv::ADCChannel::init
static bool init()
Init function.
Definition: adc.hpp:55
bsp::mid::drv
Definition: adc.hpp:32
adc.hpp
Frontend for the ADC abstraction.