chips.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 #include <time.h>
22 
23 #include "eeprom.h"
24 #include "rtc.h"
25 #include "ioexp.h"
26 #include "adc.h"
27 #include "dac.h"
28 
29 namespace eez {
30 namespace psu {
31 namespace simulator {
33 namespace chips {
34 
42 void select(int pin, int state);
43 
46 uint8_t transfer(uint8_t data);
47 
50 void tick();
51 
53 
55 class Chip {
56 public:
57  virtual void select() = 0;
58  virtual uint8_t transfer(uint8_t data) = 0;
59 };
60 
62 
64 class EepromChip : public Chip {
65  enum State {
66  IDLE,
67  READ_ADDR_MSB,
68  READ_ADDR_LSB,
69  READ,
70  WRITE_ADDR_MSB,
71  WRITE_ADDR_LSB,
72  WRITE,
73  RDSR
74  };
75 
76 public:
77  EepromChip();
78  ~EepromChip();
79 
80  void select();
81  uint8_t transfer(uint8_t data);
82 
83 private:
84  FILE *fp;
85 
86  State state;
87  uint16_t address;
88  uint16_t address_index;
89 
90  uint8_t read_byte();
91  void write_byte(uint8_t);
92 };
93 
95 
97 class RtcChip : public Chip {
98  enum State {
99  IDLE,
100  RD_CONTROL_1,
101  RD_CONTROL_2,
102 
103  WR_CONTROL_1,
104  WR_CONTROL_2,
105 
106  RD_SECONDS,
107  RD_MINUTES,
108  RD_HOURS,
109  RD_DAYS,
110  RD_WEEKDAYS,
111  RD_MONTHS,
112  RD_YEARS,
113 
114  WR_SECONDS,
115  WR_MINUTES,
116  WR_HOURS,
117  WR_DAYS,
118  WR_WEEKDAYS,
119  WR_MONTHS,
120  WR_YEARS
121  };
122 
123 public:
124  RtcChip();
125  ~RtcChip();
126 
127  void select();
128  uint8_t transfer(uint8_t data);
129 
130 private:
131  FILE *fp;
132 
133  time_t offset;
134 
135  tm tm_;
136 
137  State state;
138 
139  uint8_t ctrl1;
140  uint8_t ctrl2;
141 
142  void setOffset(time_t offset_);
143 
144  tm *getTime();
145 
146  uint8_t getSeconds();
147  void setSeconds(uint8_t seconds);
148 
149  uint8_t getMinutes();
150  void setMinutes(uint8_t minutes);
151 
152  uint8_t getHours();
153  void setHours(uint8_t hours);
154 
155  uint8_t getDays();
156  void setDays(uint8_t days);
157 
158  uint8_t getWeekdays();
159  void setWeekdays(uint8_t weekdays);
160 
161  uint8_t getMonths();
162  void setMonths(uint8_t months);
163 
164  uint8_t getYears();
165  void setYears(uint8_t years);
166 };
167 
169 
171 class BPChip : public Chip {
172  enum State {
173  IDLE,
174  READ_MSB
175  };
176 
177 public:
178  BPChip();
179 
180  void select();
181  uint8_t transfer(uint8_t data);
182 
183  uint16_t getValue() { return value; }
184 
185 private:
186  State state;
187  uint16_t value;
188 };
189 
191 
193 class IOExpanderChip : public Chip {
194  friend class AnalogDigitalConverterChip;
195 
196  enum State {
197  IDLE,
198  READ_REGISTER_INDEX,
199  READ_REGISTER_VALUE,
200  WRITE_REGISTER_INDEX,
201  WRITE_REGISTER_VALUE
202  };
203 
204 public:
205  IOExpanderChip();
206 
207  static bool getPwrgood(int pin);
208  static void setPwrgood(int pin, bool on);
209 
210  static bool getRPol(int pin);
211  static void setRPol(int pin, bool on);
212 
213  void select();
214  uint8_t transfer(uint8_t data);
215 
216 private:
217  State state;
218  uint8_t register_index;
219  uint8_t register_values[IOExpander::NUM_REGISTERS];
220  bool pwrgood;
221  bool rpol;
222  bool cc;
223  bool cv;
224 };
225 
227 
230  friend class DigitalAnalogConverterChip;
231 
232  enum State {
233  IDLE,
234  READ_REG,
235  WRITE_REG,
236  WR1S0,
237  RDATA_MSB,
238  RDATA_LSB
239  };
240 
241 public:
242  AnalogDigitalConverterChip(IOExpanderChip &ioexp_chip_, int convend_pin_);
243 
244  void tick();
245 
246  void select();
247  uint8_t transfer(uint8_t data);
248 
249 private:
250  IOExpanderChip &ioexp_chip;
251  int convend_pin;
252  State state;
253  uint8_t register_index;
254  uint8_t register_values[4];
255  uint16_t u_mon;
256  uint16_t i_mon;
257  uint16_t u_set;
258  uint16_t i_set;
259  int tick_counter;
260  bool start;
261 
262  uint16_t getValue();
263  void setDacValue(uint8_t data_buffer, uint16_t value);
264  void updateValues();
265 };
266 
268 
271  enum State {
272  IDLE,
273  DATA_BUFFER_MSB,
274  DATA_BUFFER_LSB,
275  };
276 
277 public:
279 
280  void select();
281  uint8_t transfer(uint8_t data);
282 
283 private:
284  AnalogDigitalConverterChip &adc_chip;
285  State state;
286  uint8_t data_buffer;
287  uint16_t value;
288 };
289 
291 
292 extern BPChip bp_chip;
293 
295 
296 }
297 }
298 }
299 } // namespace eez::psu::simulator::hw
Namespace for the everything from the EEZ.
Definition: actions.cpp:41
Abstract base class for all the chips.
Definition: chips.h:55
MCP23S08 chip simulation.
Definition: chips.h:193
PCA21125 chip simulation.
Definition: chips.h:97
TLC5925 chip simulation.
Definition: chips.h:171
ADS1120 chip simulation.
Definition: chips.h:229
void select(int pin, int state)
Select/deselect chip on SPI bus depending.
Definition: chips.cpp:58
DAC8552 chip simulation.
Definition: chips.h:270
void tick()
This should be called periodically by the simulator main loop.
Definition: chips.cpp:164
uint8_t transfer(uint8_t data)
Transfers data to currently selected chip.
Definition: chips.cpp:160
AT25256B chip simulation.
Definition: chips.h:64