analog_digital_converter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Richard Henrichsen
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef ADC_H
33 #define ADC_H
34 
35 #include "system.h"
36 
43 {
44 public:
49  void init(const ADCHardwareStruct *adc_def);
58  uint8_t add_channel(uint8_t channel);
59  // Returns a value between 0 and RAW_READING_MAX. 0 represents 0V, and RAW_READING_MAX
60  // represents REFERENCE_VOLTAGE
61  // The parameter, index, is the return value from add_channel when the channel was added
70  uint16_t read(uint8_t index) const;
75  bool is_initialized() const;
80  uint8_t get_current_channel_count() const;
81 
86  static constexpr double REFERENCE_VOLTAGE{3.3};
90  static constexpr uint16_t RAW_READING_MAX{0xFFF};
95  static constexpr uint16_t NO_READING{0xFFFF};
99  static constexpr uint8_t CHANNEL_COUNT{16};
100 
101 private:
103  bool is_initialized_{false};
105 
106  // These two constants are used for adjusting the ADC configuration when a channel is added
107  static constexpr uint32_t SQR1_CHANNEL_COUNT_MASK{~0xFF0FFFFF};
108  static constexpr uint8_t SQR1_CHANNEL_COUNT_OFFSET{20};
109 
110  volatile uint32_t buffer[CHANNEL_COUNT];
111 
112  void init_dma();
113  void start_dma();
114 };
115 
116 #endif // ADC_H
static constexpr uint16_t NO_READING
static constexpr uint8_t SQR1_CHANNEL_COUNT_OFFSET
static constexpr uint16_t RAW_READING_MAX
A driver for the analog-digital converters available on the microcontroller.
uint16_t read(uint8_t index) const
Reads a single channel.
static constexpr uint8_t CHANNEL_COUNT
uint8_t get_current_channel_count() const
Checks the number of used channels. If this exceeds CHANNEL_COUNT, stuff will break.
const ADCHardwareStruct * adc_def_
void init(const ADCHardwareStruct *adc_def)
Initializes the ADC according to the definition struct provided.
bool is_initialized() const
Checks if the adc has been initialized, i.e. init has been called.
uint8_t add_channel(uint8_t channel)
Adds a channel to the list that is scanned.
volatile uint32_t buffer[CHANNEL_COUNT]
static constexpr uint32_t SQR1_CHANNEL_COUNT_MASK
static constexpr double REFERENCE_VOLTAGE


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:46