|
| 1 | +//! Analog-to-Digital Converter |
| 2 | +
|
| 3 | +use crate::port; |
| 4 | +pub use avr_hal_generic::adc::{AdcChannel, AdcOps, ClockDivider}; |
| 5 | + |
| 6 | +/// Select the voltage reference for the ADC peripheral |
| 7 | +/// |
| 8 | +/// The internal voltage reference options may not be used if an external reference voltage is |
| 9 | +/// being applied to the AREF pin. |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | +#[repr(u8)] |
| 12 | +pub enum ReferenceVoltage { |
| 13 | + /// Voltage applied to AREF pin. |
| 14 | + #[cfg(any( |
| 15 | + feature = "attiny85", |
| 16 | + feature = "attiny167", |
| 17 | + ))] |
| 18 | + Aref, |
| 19 | + /// Default reference voltage (default). |
| 20 | + AVcc, |
| 21 | + /// Internal 1.1V reference. |
| 22 | + Internal1_1, |
| 23 | + /// Internal 2.56V reference. |
| 24 | + #[cfg(any( |
| 25 | + feature = "attiny85", |
| 26 | + feature = "attiny167", |
| 27 | + ))] |
| 28 | + Internal2_56, |
| 29 | +} |
| 30 | + |
| 31 | +impl Default for ReferenceVoltage { |
| 32 | + fn default() -> Self { |
| 33 | + Self::AVcc |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Configuration for the ADC peripheral. |
| 38 | +#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] |
| 39 | +pub struct AdcSettings { |
| 40 | + pub clock_divider: ClockDivider, |
| 41 | + pub ref_voltage: ReferenceVoltage, |
| 42 | +} |
| 43 | + |
| 44 | +/// Check the [`avr_hal_generic::adc::Adc`] documentation. |
| 45 | +pub type Adc<CLOCK> = avr_hal_generic::adc::Adc<crate::Attiny, crate::pac::ADC, CLOCK>; |
| 46 | + |
| 47 | +/// Check the [`avr_hal_generic::adc::Channel`] documentation. |
| 48 | +pub type Channel = avr_hal_generic::adc::Channel<crate::Attiny, crate::pac::ADC>; |
| 49 | + |
| 50 | +/// Additional channels |
| 51 | +/// |
| 52 | +/// Some channels are not directly connected to pins. This module provides types which can be used |
| 53 | +/// to access them. |
| 54 | +/// |
| 55 | +/// # Example |
| 56 | +/// ``` |
| 57 | +/// let dp = atmega_hal::Peripherals::take().unwrap(); |
| 58 | +/// let mut adc = atmega_hal::Adc::new(dp.ADC, Default::default()); |
| 59 | +/// |
| 60 | +/// let value = adc.read_blocking(&channel::Vbg); |
| 61 | +/// ``` |
| 62 | +pub mod channel { |
| 63 | + #[cfg(feature = "attiny167")] |
| 64 | + pub struct AVcc_4; |
| 65 | + pub struct Vbg; |
| 66 | + pub struct Gnd; |
| 67 | + pub struct Temperature; |
| 68 | +} |
| 69 | + |
| 70 | +fn apply_clock(peripheral: &crate::pac::ADC, settings: AdcSettings) { |
| 71 | + peripheral.adcsra.write(|w| { |
| 72 | + w.aden().set_bit(); |
| 73 | + match settings.clock_divider { |
| 74 | + ClockDivider::Factor2 => w.adps().prescaler_2(), |
| 75 | + ClockDivider::Factor4 => w.adps().prescaler_4(), |
| 76 | + ClockDivider::Factor8 => w.adps().prescaler_8(), |
| 77 | + ClockDivider::Factor16 => w.adps().prescaler_16(), |
| 78 | + ClockDivider::Factor32 => w.adps().prescaler_32(), |
| 79 | + ClockDivider::Factor64 => w.adps().prescaler_64(), |
| 80 | + ClockDivider::Factor128 => w.adps().prescaler_128(), |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +#[cfg(feature = "attiny85")] |
| 87 | +avr_hal_generic::impl_adc! { |
| 88 | + hal: crate::Attiny, |
| 89 | + peripheral: crate::pac::ADC, |
| 90 | + settings: AdcSettings, |
| 91 | + apply_settings: |peripheral, settings| { |
| 92 | + apply_clock(peripheral, settings); |
| 93 | + peripheral.admux.write(|w| match settings.ref_voltage { |
| 94 | + ReferenceVoltage::Aref => w.refs().aref(), |
| 95 | + ReferenceVoltage::AVcc => w.refs().vcc(), |
| 96 | + ReferenceVoltage::Internal1_1 => w.refs().internal().refs2().clear_bit(), |
| 97 | + ReferenceVoltage::Internal2_56 => w.refs().internal().refs2().set_bit(), |
| 98 | + }); |
| 99 | + }, |
| 100 | + channel_id: crate::pac::adc::admux::MUX_A, |
| 101 | + set_channel: |peripheral, id| { |
| 102 | + peripheral.admux.modify(|_, w| w.mux().variant(id)); |
| 103 | + }, |
| 104 | + pins: { |
| 105 | + port::PB5: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), |
| 106 | + port::PB2: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), |
| 107 | + port::PB4: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), |
| 108 | + port::PB3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), |
| 109 | + }, |
| 110 | + channels: { |
| 111 | + channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, |
| 112 | + channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, |
| 113 | + channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, |
| 114 | + }, |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +#[cfg(feature = "attiny88")] |
| 119 | +avr_hal_generic::impl_adc! { |
| 120 | + hal: crate::Attiny, |
| 121 | + peripheral: crate::pac::ADC, |
| 122 | + settings: AdcSettings, |
| 123 | + apply_settings: |peripheral, settings| { |
| 124 | + apply_clock(peripheral, settings); |
| 125 | + peripheral.admux.write(|w| match settings.ref_voltage { |
| 126 | + ReferenceVoltage::AVcc => w.refs0().avcc(), |
| 127 | + ReferenceVoltage::Internal1_1 => w.refs0().internal(), |
| 128 | + }); |
| 129 | + }, |
| 130 | + channel_id: crate::pac::adc::admux::MUX_A, |
| 131 | + set_channel: |peripheral, id| { |
| 132 | + peripheral.admux.modify(|_, w| w.mux().variant(id)); |
| 133 | + }, |
| 134 | + pins: { |
| 135 | + port::PC0: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), |
| 136 | + port::PC1: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), |
| 137 | + port::PC2: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), |
| 138 | + port::PC3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), |
| 139 | + port::PC4: (crate::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), |
| 140 | + port::PC5: (crate::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), |
| 141 | + port::PA0: (crate::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), |
| 142 | + port::PA1: (crate::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), |
| 143 | + }, |
| 144 | + channels: { |
| 145 | + channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, |
| 146 | + channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, |
| 147 | + channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, |
| 148 | + }, |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +#[cfg(feature = "attiny167")] |
| 153 | +avr_hal_generic::impl_adc! { |
| 154 | + hal: crate::Attiny, |
| 155 | + peripheral: crate::pac::ADC, |
| 156 | + settings: AdcSettings, |
| 157 | + apply_settings: |peripheral, settings| { |
| 158 | + apply_clock(peripheral, settings); |
| 159 | + peripheral.amiscr.write(|w| match settings.ref_voltage { |
| 160 | + ReferenceVoltage::Aref => w.arefen().set_bit(), |
| 161 | + _ => w.arefen().clear_bit(), |
| 162 | + }); |
| 163 | + peripheral.admux.write(|w| match settings.ref_voltage { |
| 164 | + ReferenceVoltage::Aref => w.refs().avcc(), |
| 165 | + ReferenceVoltage::AVcc => w.refs().avcc(), |
| 166 | + ReferenceVoltage::Internal1_1 => w.refs().internal_11(), |
| 167 | + ReferenceVoltage::Internal2_56 => w.refs().internal_256(), |
| 168 | + }); |
| 169 | + }, |
| 170 | + channel_id: crate::pac::adc::admux::MUX_A, |
| 171 | + set_channel: |peripheral, id| { |
| 172 | + peripheral.admux.modify(|_, w| w.mux().variant(id)); |
| 173 | + }, |
| 174 | + pins: { |
| 175 | + port::PA0: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), |
| 176 | + port::PA1: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), |
| 177 | + port::PA2: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), |
| 178 | + port::PA3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), |
| 179 | + port::PA4: (crate::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), |
| 180 | + port::PA5: (crate::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), |
| 181 | + port::PA6: (crate::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), |
| 182 | + port::PA7: (crate::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), |
| 183 | + port::PB5: (crate::pac::adc::admux::MUX_A::ADC8, didr1::adc8d), |
| 184 | + port::PB6: (crate::pac::adc::admux::MUX_A::ADC9, didr1::adc9d), |
| 185 | + port::PB7: (crate::pac::adc::admux::MUX_A::ADC10, didr1::adc10d), |
| 186 | + }, |
| 187 | + channels: { |
| 188 | + channel::AVcc_4: crate::pac::adc::admux::MUX_A::ADC_AVCC_4, |
| 189 | + channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, |
| 190 | + channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, |
| 191 | + channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, |
| 192 | + }, |
| 193 | +} |
0 commit comments