adc.h
1 /*
2  * EEZ PSU Firmware
3  * Copyright (C) 2015-present, Envox d.o.o.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 namespace eez {
22 namespace psu {
23 
26 public:
27  static const uint16_t ADC_MIN = 0;
28  static const uint16_t ADC_MAX = (1L << ADC_RES) - 1;
29 
30  static const uint8_t ADC_RESET = 0B00000110;
31  static const uint8_t ADC_RDATA = 0B00010000;
32  static const uint8_t ADC_START = 0B00001000;
33 
34  static const uint8_t ADC_WR3S1 = 0B01000110;
35  static const uint8_t ADC_RD3S1 = 0B00100110;
36  static const uint8_t ADC_WR1S0 = 0B01000000;
37  static const uint8_t ADC_WR4S0 = 0B01000011;
38 
39  static const uint8_t ADC_REG0_READ_U_MON = 0x91; // B10010001: [7:4] AINP = AIN1, AINN = AVSS, [3:1] Gain = 1, [0] PGA disabled and bypassed
40  static const uint8_t ADC_REG0_READ_I_MON = 0xA1; // B10100001: [7:4] AINP = AIN2, AINN = AVSS, [3:1] Gain = 1, [0] PGA disabled and bypassed
41 
42  static const uint8_t ADC_REG0_READ_U_SET = 0x81; // B10000001: [7:4] AINP = AIN0, AINN = AVSS, [3:1] Gain = 1, [0] PGA disabled and bypassed
43  static const uint8_t ADC_REG0_READ_I_SET = 0xB1; // B10110001: [7:4] AINP = AIN3, AINN = AVSS, [3:1] Gain = 1, [0] PGA disabled and bypassed
44 
45  psu::TestResult g_testResult;
46  uint8_t start_reg0;
47 
49 
50  void init();
51  bool test();
52 
53  void tick(uint32_t tick_usec);
54 
55  void start(uint8_t reg0);
56  int16_t read();
57 
58 #if ADC_USE_INTERRUPTS
59  void onInterrupt();
60 #endif
61 
62 private:
63  Channel &channel;
64 #if ADC_USE_INTERRUPTS
65  uint8_t current_sps;
66 #endif
67 
68  uint32_t start_time;
69 
70  uint8_t adc_timeout_recovery_attempts_counter;
71 
72  uint8_t getReg1Val();
73 };
74 
75 }
76 } // namespace eez::psu
#define ADC_RES
ADC chip resolution in number of bits.
Definition: conf_advanced.h:53
Namespace for the everything from the EEZ.
Definition: actions.cpp:41
Analog to digital converter HW used by the channel.
Definition: adc.h:25
PSU channel.
Definition: channel.h:62