forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_acquisition.h
executable file
·82 lines (70 loc) · 2.3 KB
/
adc_acquisition.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2012 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Staged SPI communication with the ADC. Prevents the 128 cycles lock.
#ifndef EDGES_ADC_ACQUISITION_H_
#define EDGES_ADC_ACQUISITION_H_
#include "avrlibx/avrlibx.h"
#include "edges/hardware_config.h"
namespace edges {
class ADCAcquisition {
public:
ADCAcquisition() { }
~ADCAcquisition() { }
static void Init() {
adc_.Init();
acquisition_stage_ = 0;
active_channel_ = 0;
}
static inline uint8_t Cycle() {
int8_t result = -1;
switch (acquisition_stage_) {
case 0:
rx_.bytes[0] = adc_.ImmediateRead();
channels_[active_channel_] = rx_.value;
adc_.End();
adc_.Begin();
result = active_channel_;
active_channel_ = (active_channel_ + 1) & (kNumAdcChannels - 1);
adc_.Overwrite(0x04 | 0x02 | (active_channel_ >> 2));
acquisition_stage_ = 1;
break;
case 1:
adc_.Overwrite(active_channel_ << 6);
acquisition_stage_ = 2;
break;
case 2:
rx_.bytes[1] = adc_.ImmediateRead() & 0xf;
adc_.Overwrite(0x00); // Dummy write to get next byte.
acquisition_stage_ = 0;
break;
}
return result;
}
static uint16_t channel(uint8_t index) {
return channels_[index];
}
private:
static ADCInterface adc_;
static Word rx_;
static uint8_t active_channel_;
static uint8_t acquisition_stage_;
static uint16_t channels_[kNumAdcChannels];
};
extern ADCAcquisition adc_acquisition;
} // namespace edges
#endif // EDGES_ADC_ACQUISITION_H_