ioexp.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 /*
22 PSU 0-50V/3A I/O expander pinout:
23 
24 CH_BOARD_REVISION_R4B43A
25 
26 Pin 0 In, ADC interrupt/DRDY
27 Pin 1 Out, DP enable (active low)
28 Pin 2 In, CC_ACTIVE
29 Pin 3 In, Temp sensor (V/F)
30 Pin 4 In, Battery NTC sensor temperature presented as frequency
31 Pin 5 In, CV_ACTIVE
32 Pin 6 In, PWRGOOD
33 Pin 7 Out, OUTPUT_ENABLE
34 
35 CH_BOARD_REVISION_R5B6B
36 
37 Pin 0 In, "ADC interrupt/DRDY" or "remote sense reverse polarity detection" on CH_BOARD_REVISION_R5B9 and CH_BOARD_REVISION_R5B10
38 Pin 1 Out, DP enable (active low)
39 Pin 2 In, CC_ACTIVE
40 Pin 3 Out, SET_100% (active low)
41 Pin 4 Out, EXT_PROG
42 Pin 5 In, CV_ACTIVE
43 Pin 6 In, PWRGOOD
44 Pin 7 Out, OUTPUT_ENABLE
45 
46 */
47 
48 namespace eez {
49 namespace psu {
50 
51 class Channel;
52 
54 class IOExpander {
55 public:
56  static const uint8_t IO_BIT_IN_ADC_DRDY = 0;
57  static const uint8_t IO_BIT_IN_RPOL = 0; // remote sense reverse polarity detection
58  static const uint8_t IO_BIT_IN_CC_ACTIVE = 2;
59  static const uint8_t IO_BIT_IN_TEMP_SENSOR = 3;
60  static const uint8_t IO_BIT_IN_CV_ACTIVE = 5;
61  static const uint8_t IO_BIT_IN_PWRGOOD = 6;
62 
63  static const uint8_t IO_BIT_OUT_DP_ENABLE = 1;
64  const uint8_t IO_BIT_OUT_SET_100_PERCENT;
65  const uint8_t IO_BIT_OUT_EXT_PROG;
66  static const uint8_t IO_BIT_OUT_OUTPUT_ENABLE = 7;
67 
68  static const uint8_t IOEXP_READ = 0B01000001;
69  static const uint8_t IOEXP_WRITE = 0B01000000;
70 
71  static const uint8_t REG_IODIR = 0x00;
72  static const uint8_t REG_IPOL = 0x01;
73  static const uint8_t REG_GPINTEN = 0x02;
74  static const uint8_t REG_DEVAL = 0x03;
75  static const uint8_t REG_INTCON = 0x04;
76  static const uint8_t REG_IOCON = 0x05;
77  static const uint8_t REG_GPPU = 0x06;
78  static const uint8_t REG_INTF = 0x07;
79  static const uint8_t REG_INTCAP = 0x08;
80  static const uint8_t REG_GPIO = 0x09;
81  static const uint8_t REG_OLAT = 0x0A;
82 
83  static const size_t NUM_REGISTERS = REG_OLAT + 1;
84 
85  psu::TestResult g_testResult;
86 
87  IOExpander(Channel &channel,
88  uint8_t IO_BIT_OUT_SET_100_PERCENT_,
89  uint8_t IO_BIT_OUT_EXT_PROG_
90  );
91 
92  void init();
93  bool test();
94 
95  void tick(uint32_t tick_usec);
96 
97  uint8_t readGpio();
98 
99  bool testBit(int io_bit);
100  void changeBit(int io_bit, bool set);
101  void disableWrite();
102  void enableWriteAndFlush();
103 
104 private:
105  Channel &channel;
106  uint8_t gpio;
107  bool gpio_changed;
108  bool writeDisabled;
109 
110  uint8_t getRegInitValue(int i);
111  uint8_t reg_read_write(uint8_t opcode, uint8_t reg, uint8_t val);
112  uint8_t reg_read(uint8_t reg);
113  void reg_write(uint8_t reg, uint8_t val);
114 };
115 
116 }
117 } // namespace eez::psu
Namespace for the everything from the EEZ.
Definition: actions.cpp:41
PSU channel.
Definition: channel.h:62
IO Expander HW used by the channel.
Definition: ioexp.h:54