summaryrefslogtreecommitdiff
path: root/src/peripherals/avrdx-vref.ts
blob: 009709663d6527fb5757d3431f6e136d05ea7e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// AVR-Dx VREF peripheral
// Voltage reference selection for DAC0 and ADC0

import { CPU } from 'avr8js/cpu/cpu';

const ADC0REF = 0;
const DAC0REF = 2;

const VREF_VOLTAGES = [1.024, 2.048, 2.500, 4.096] as const;

export class AVRDxVREF {
  constructor(private cpu: CPU, private base: number) {
  }

  /** Get DAC Vref selection (raw register value) */
  get dacRef(): number {
    return this.cpu.data[this.base + DAC0REF] & 0x07;
  }

  get dacRefVolts(): number {
    return VREF_VOLTAGES[this.dacRef] ?? 0;
  }

  /** Get ADC Vref selection (raw register value) */
  get adcRef(): number {
    return this.cpu.data[this.base + ADC0REF] & 0x07;
  }

  get adcRefVolts(): number {
    return VREF_VOLTAGES[this.adcRef] ?? 0;
  }
}