ClearCore Library
AdcManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
30 #ifndef __ADCMANAGER_H__
31 #define __ADCMANAGER_H__
32 
33 #include <stdint.h>
34 #include "IirFilter.h"
35 
36 namespace ClearCore {
37 
48 class AdcManager {
49 public:
57  typedef enum {
66  ADC_CHANNEL_COUNT // Keep at end
67  } AdcChannels;
68 
75  typedef enum {
82  } FilterUnits;
83 
87  static const uint8_t ADC_RESOLUTION_DEFAULT = 12;
91  static const uint8_t ADC_TIMEOUT_DEFAULT = 3;
95  static const uint32_t ADC_IIR_FILTER_TC_MS = 2;
96 
97 #ifndef HIDE_FROM_DOXYGEN
98  // Max voltage that a channel can read.
99  // This will probably need a scale rework once populated
100  // Calculated by multiplying the ratio by the ADC supply which is
101  // 3.3v
102  static constexpr float ADC_CHANNEL_MAX_FLOAT[ADC_CHANNEL_COUNT] = {
103  80.85, // ADC_VSUPPLY_MON
104  10.0, // ADC_AIN12
105  6.6, // ADC_5VOB_MON
106  10.0, // ADC_AIN11
107  10.0, // ADC_AIN10
108  10.0, // ADC_AIN09
109  10.0, // ADC_SDRVR3_IMON
110  10.0 // ADC_SDRVR2_IMON
111  };
112 
120  void Initialize();
121 
131  void Update();
132 #endif
133 
158  bool AdcResolution(uint8_t resolution);
159 
170  volatile const uint8_t &AdcResolution() {
171  return m_AdcResolution;
172  }
173 
187  volatile const uint16_t &FilteredResult(AdcChannels adcChannel) {
188  return m_AdcResultsConvertedFiltered[adcChannel];
189  }
190 
204  volatile const uint16_t &ConvertedResult(AdcChannels adcChannel) {
205  return m_AdcResultsConverted[adcChannel];
206  }
207 
224  bool FilterTc(AdcChannels adcChannel, uint16_t tc, FilterUnits theUnits);
225 
242  uint16_t FilterTc(AdcChannels adcChannel, FilterUnits theUnits);
243 
256  bool FilterReset(AdcChannels adcChannel, uint16_t newSetting) {
257  if (adcChannel >= ADC_CHANNEL_COUNT) {
258  return false;
259  }
260  m_analogFilter[adcChannel].Reset(newSetting);
261  m_AdcResultsConvertedFiltered[adcChannel] =
262  m_analogFilter[adcChannel].LastOutput();
263  return true;
264  }
265 
280  void AdcTimeoutLimit(uint8_t timeout) {
281  m_AdcTimeoutLimit = timeout;
282  }
283 
295  volatile const bool &AdcTimeout() {
296  return m_AdcTimeout;
297  }
298 
312  float AnalogVoltage(AdcChannels adcChannel) {
313  uint16_t maxReading = INT16_MAX & ~(INT16_MAX >> m_AdcResolution);
314  float voltage = ADC_CHANNEL_MAX_FLOAT[adcChannel] *
315  m_AdcResultsConvertedFiltered[adcChannel] / maxReading;
316  return voltage;
317  }
318 
319 #ifndef HIDE_FROM_DOXYGEN
320 
323  static AdcManager &Instance();
324 
331  volatile const uint32_t &ShiftRegSnapshot() {
332  return m_shiftRegSnapshot;
333  }
334 #endif
335 private:
336 
342  static constexpr float ADC_INITIAL_FILTER_VALUE_V[ADC_CHANNEL_COUNT] = {
343  24.0, // ADC_VSUPPLY_MON
344  0.0, // ADC_AIN12
345  5.0, // ADC_5VOB_MON
346  0.0, // ADC_AIN11
347  0.0, // ADC_AIN10
348  0.0, // ADC_AIN09
349  0.0, // ADC_SDRVR3_IMON
350  0.0, // ADC_SDRVR2_IMON
351  };
352 
353  // ADC state holders in Q15. ADC logic has already been performed
354  volatile uint16_t m_AdcResultsConverted[ADC_CHANNEL_COUNT] = {0};
355  volatile uint16_t m_AdcResultsConvertedFiltered[ADC_CHANNEL_COUNT] = {0};
356  Iir16 m_analogFilter[ADC_CHANNEL_COUNT];
357 
358  bool m_initialized;
359 
360  bool m_AdcTimeout;
361  uint32_t m_shiftRegSnapshot;
362  uint32_t m_shiftRegPending;
363 
365  uint8_t m_AdcResolution;
366  uint8_t m_AdcResPending;
367 
370  uint8_t m_AdcTimeoutLimit;
371 
373  uint32_t m_AdcBusyCount;
374 
382  AdcManager();
383 
384  void DmaInit();
385  void DmaUpdate();
386 
395  bool AdcResChange();
396 
397 }; // AdcManager
398 
399 } // ClearCore namespace
400 
401 #endif // __ADCMANAGER_H__
volatile const bool & AdcTimeout()
Determine whether the ADC has timed out.
Definition: AdcManager.h:295
static const uint32_t ADC_IIR_FILTER_TC_MS
Definition: AdcManager.h:95
volatile const uint16_t & ConvertedResult(AdcChannels adcChannel)
Returns the converted ADC result of a specific channel.
Definition: AdcManager.h:204
AdcChannels
Definition: AdcManager.h:57
Analog input A-11.
Definition: AdcManager.h:61
Analog input A-10.
Definition: AdcManager.h:62
volatile const uint16_t & FilteredResult(AdcChannels adcChannel)
Returns the filtered ADC result of a specific channel.
Definition: AdcManager.h:187
static const uint8_t ADC_TIMEOUT_DEFAULT
Definition: AdcManager.h:91
Analog input A-9.
Definition: AdcManager.h:63
Definition: AdcManager.h:79
bool FilterReset(AdcChannels adcChannel, uint16_t newSetting)
Resets the filter for an ADC Channel.
Definition: AdcManager.h:256
void AdcTimeoutLimit(uint8_t timeout)
Configure the ADC conversion timeout.
Definition: AdcManager.h:280
Screwdriver M-2 current monitor.
Definition: AdcManager.h:65
bool FilterTc(AdcChannels adcChannel, uint16_t tc, FilterUnits theUnits)
Sets the IIR filter time constant for an ADC channel.
Definition: AdcManager.h:77
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
Screwdriver M-3 current monitor.
Definition: AdcManager.h:64
ADC Peripheral Manager for the ClearCore Board.
Definition: AdcManager.h:48
Supply voltage monitor.
Definition: AdcManager.h:58
5V off-board monitor
Definition: AdcManager.h:60
FilterUnits
Units for the filter time constant.
Definition: AdcManager.h:75
static const uint8_t ADC_RESOLUTION_DEFAULT
Definition: AdcManager.h:87
float AnalogVoltage(AdcChannels adcChannel)
Returns the filtered ADC result of a specific channel in volts.
Definition: AdcManager.h:312
volatile const uint8_t & AdcResolution()
Returns the resolution of the ADC.
Definition: AdcManager.h:170
Analog input A-12.
Definition: AdcManager.h:59