arch_avr_adc.h Source File

yasimavr: arch_avr_adc.h Source File
yasimavr
Loading...
Searching...
No Matches
arch_avr_adc.h
Go to the documentation of this file.
1/*
2 * arch_avr_adc.h
3 *
4 * Copyright 2021-2026 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_AVR_ADC_H__
25#define __YASIMAVR_AVR_ADC_H__
26
27#include "arch_avr_globals.h"
28#include "core/sim_interrupt.h"
32
34
35
36//=======================================================================================
37
102
103
121
122public:
123
124 ArchAVR_ADC(int num, const ArchAVR_ADCConfig& config);
125
126 virtual bool init(Device& device) override;
127 virtual void reset(int flags) override;
128 virtual bool ctlreq(ctlreq_id_t req, ctlreq_data_t* data) override;
129 virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value) override;
130 virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t& data) override;
131 virtual void sleep(bool on, SleepMode mode) override;
132
133private:
134
135 //enum defining the various steps of a conversion
136 enum State {
137 ADC_Disabled,
138 ADC_Idle,
139 ADC_PendingConversion,
140 ADC_PendingRaise,
141 };
142
143 const ArchAVR_ADCConfig& m_config;
144
145 //Step of the conversion
146 State m_state;
147 //Boolean defining if a conversion is the first after the ADC being enabled
148 //It has impact on the timing
149 bool m_first;
150
152
153 //Timer to simulate the conversion cycle duration
154 PrescaledTimer m_timer;
156
157 //Simulated device temperature value in deg Celsius
158 double m_temperature;
159
160 //Configuration values for the channel and reference latched at the start of a conversion
161 uint8_t m_latched_ch_mux;
162 uint8_t m_latched_ref_mux;
163
164 //Raw converted value
165 int16_t m_conv_value;
166
167 //Signal raised at various steps of the conversion
168 Signal m_signal;
169
170 //Interrupt flag raised at the completion of a conversion
171 InterruptFlag m_intflag;
172
173 void start_conversion_cycle();
174 void reset_prescaler();
175 void timer_raised(const signal_data_t& sigdata, int hooktag);
176
177 void read_analog_value();
178 void write_digital_value();
179
180};
181
182
184
185#endif //__YASIMAVR_AVR_ADC_H__
#define AVR_ARCHAVR_PUBLIC_API
Definition arch_avr_globals.h:46
Implementation of an ADC for AVR series.
Definition arch_avr_adc.h:120
Definition sim_signal.h:227
Basic AVR device model.
Definition sim_device.h:61
Generic helper to manage a typical Interrupt Flag/Enable in a I/O register.
Definition sim_interrupt.h:324
Abstract class defining a framework for MCU peripherals.
Definition sim_peripheral.h:286
virtual bool init(Device &device)
Definition sim_peripheral.cpp:60
virtual bool ctlreq(ctlreq_id_t req, ctlreq_data_t *data)
Definition sim_peripheral.cpp:79
virtual void sleep(bool on, SleepMode mode)
Definition sim_peripheral.cpp:125
virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t &data) override
Definition sim_peripheral.cpp:117
virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value) override
Definition sim_peripheral.cpp:91
virtual void reset(int flags)
Definition sim_peripheral.cpp:72
Generic model of a Timer with prescaling.
Definition sim_timer.h:63
Signalling framework class.
Definition sim_signal.h:97
Source
Enumation value for the sources of voltage references.
Definition sim_vref.h:77
Representation of a I/O register address, with validity state.
Definition sim_types.h:60
Represents a field in a I/O register with address.
Definition sim_types.h:348
int ctlreq_id_t
Definition sim_peripheral.h:111
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
SleepMode
Definition sim_sleep.h:49
short int_vect_t
Definition sim_types.h:43
Definition arch_avr_adc.h:44
VREF::Source source
Definition arch_avr_adc.h:45
Definition arch_avr_adc.h:54
Trigger trigger
Definition arch_avr_adc.h:55
Configuration structure for ArchAVR_ADC.
Definition arch_avr_adc.h:42
regbit_t rb_chan_mux
Regbit for the channel mux selection.
Definition arch_avr_adc.h:73
reg_addr_t reg_datah
Register address for the result high byte.
Definition arch_avr_adc.h:71
std::vector< unsigned long > clk_ps_factors
List of the prescaler factors for the ADC clock.
Definition arch_avr_adc.h:63
regbit_t rb_ref_mux
Regbit for the reference mux selection.
Definition arch_avr_adc.h:75
reg_addr_t reg_datal
Register address for the result low byte.
Definition arch_avr_adc.h:69
std::vector< trigger_config_t > triggers
List of the autotrigger sources.
Definition arch_avr_adc.h:65
regbit_t rb_start
Regbit for the conversion manual start bit.
Definition arch_avr_adc.h:79
std::vector< ADC::channel_config_t > channels
List of the channels.
Definition arch_avr_adc.h:59
int_vect_t int_vector
Interrupt vector index.
Definition arch_avr_adc.h:95
unsigned int vref_channel
Channel index for the internal voltage reference.
Definition arch_avr_adc.h:67
regbit_t rb_enable
Regbit for the enable bit.
Definition arch_avr_adc.h:77
double temp_cal_coef
Calibration value for the internal temperature sensor - linear coefficient in V/°C.
Definition arch_avr_adc.h:99
regbit_t rb_prescaler
Regbit for the clock prescaler selection.
Definition arch_avr_adc.h:87
regbit_t rb_auto_trig
Regbit for the auto-trigger enable bit.
Definition arch_avr_adc.h:81
regbit_t rb_int_enable
Regbit for the interrupt enable bit.
Definition arch_avr_adc.h:83
regbit_t rb_int_flag
Regbit for the interrupt flag bit.
Definition arch_avr_adc.h:85
double temp_cal_25C
Calibration value for the internal temperature sensor - offset in V at +25°C.
Definition arch_avr_adc.h:97
regbit_t rb_bipolar
Regbit for the bipolar mode.
Definition arch_avr_adc.h:91
regbit_t rb_trig_mux
Regbit for the auto-trigger mux selection.
Definition arch_avr_adc.h:89
std::vector< reference_config_t > references
List of the voltage references.
Definition arch_avr_adc.h:61
Trigger
Definition arch_avr_adc.h:48
@ Trig_FreeRunning
Definition arch_avr_adc.h:50
@ Trig_Manual
Definition arch_avr_adc.h:49
@ Trig_External
Definition arch_avr_adc.h:51
regbit_t rb_left_adj
Regbit for result left adjusting.
Definition arch_avr_adc.h:93
Definition sim_peripheral.h:252
Definition sim_peripheral.h:237
Definition sim_ioreg.h:39
Definition sim_signal.h:39