simulavr  1.1.0
hwad.h
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #ifndef HWAD
27 #define HWAD
28 
29 #include "hardware.h"
30 #include "avrdevice.h"
31 #include "rwmem.h"
32 #include "traceval.h"
33 
35 class HWARef {
36 
37  protected:
39 
40  public:
41  HWARef(AvrDevice *_core): core(_core) { }
42  virtual ~HWARef() { }
43 
44  virtual float GetRefValue(int select, float vcc) = 0;
45 };
46 
48 class HWARefPin: public HWARef {
49 
50  protected:
52 
53  public:
54  HWARefPin(AvrDevice *_core);
55 
56  virtual float GetRefValue(int select, float vcc);
57 };
58 
60 class HWARef4: public HWARefPin {
61 
62  protected:
63  int refType;
64 
65  public:
66  enum {
69  REFTYPE_BG4
70  };
71 
72  HWARef4(AvrDevice *_core, int _type);
73 
74  virtual float GetRefValue(int select, float vcc);
75 };
76 
78 class HWARef8: public HWARef {
79 
80  protected:
82 
83  public:
84  HWARef8(AvrDevice *_core, Pin* _refpin): HWARef(_core), aref_pin(_refpin) { }
85 
86  virtual float GetRefValue(int select, float vcc);
87 };
88 
91 
92  protected:
93  Pin* ad[16]; // 4 to 16 pins selectable from the mux
95  int muxSelect;
96  int numPins;
98 
99  public:
100  HWAdmux(AvrDevice* _core, int _pins):
101  notifyClient(0),
102  muxSelect(0),
103  numPins(_pins),
104  core(_core)
105  { }
106  virtual ~HWAdmux() { }
107 
108  // select bit 5 = MUX5 = use upper channels (if supported, see datasheet table 26-4)
109  virtual float GetValue(int select, float vcc) = 0;
110  virtual float GetValueAComp(int select, float vcc) { return 0.0; }
111  virtual bool IsDifferenceChannel(int select) { return false; }
112  void SetMuxSelect(int select);
113  void PinStateHasChanged(Pin*);
114  void RegisterNotifyClient(AnalogSignalChange *client) { notifyClient = client; }
115  void UnregisterNotifyClient(void) { notifyClient = 0; }
116 };
117 
118 class HWAdmux6: public HWAdmux {
119 
120  public:
121  HWAdmux6(AvrDevice* c, Pin* _ad0,
122  Pin* _ad1,
123  Pin* _ad2,
124  Pin* _ad3,
125  Pin* _ad4,
126  Pin* _ad5);
127 
128  virtual float GetValue(int select, float vcc);
129 };
130 
131 class HWAdmuxM8: public HWAdmux {
132 
133  protected:
134  HWAdmuxM8(AvrDevice* c, Pin* _ad0,
135  Pin* _ad1,
136  Pin* _ad2,
137  Pin* _ad3);
138 
139  public:
140  HWAdmuxM8(AvrDevice* c, Pin* _ad0,
141  Pin* _ad1,
142  Pin* _ad2,
143  Pin* _ad3,
144  Pin* _ad4,
145  Pin* _ad5,
146  Pin* _ad6,
147  Pin* _ad7);
148  virtual float GetValue(int select, float vcc);
149  virtual float GetValueAComp(int select, float vcc);
150 };
151 
152 class HWAdmuxM16: public HWAdmuxM8 {
153 
154  public:
155  HWAdmuxM16(AvrDevice* c, Pin* _ad0,
156  Pin* _ad1,
157  Pin* _ad2,
158  Pin* _ad3,
159  Pin* _ad4,
160  Pin* _ad5,
161  Pin* _ad6,
162  Pin* _ad7);
163 
164  virtual float GetValue(int select, float vcc);
165  virtual bool IsDifferenceChannel(int select);
166 };
167 
168 class HWAdmuxT25: public HWAdmuxM8 {
169 
170  public:
171  HWAdmuxT25(AvrDevice* c, Pin* _ad0,
172  Pin* _ad1,
173  Pin* _ad2,
174  Pin* _ad3);
175 
176  virtual float GetValue(int select, float vcc);
177  virtual bool IsDifferenceChannel(int select);
178 };
179 
180 class HWAdmuxM2560: public HWAdmux {
181 
182  public:
183  HWAdmuxM2560(AvrDevice* c, Pin* _ad0,
184  Pin* _ad1,
185  Pin* _ad2,
186  Pin* _ad3,
187  Pin* _ad4,
188  Pin* _ad5,
189  Pin* _ad6,
190  Pin* _ad7,
191  Pin* _ad8,
192  Pin* _ad9,
193  Pin* _ad10,
194  Pin* _ad11,
195  Pin* _ad12,
196  Pin* _ad13,
197  Pin* _ad14,
198  Pin* _ad15);
199 
200  virtual float GetValue(int select, float vcc);
201 };
202 
204 class HWAd: public Hardware, public TraceValueRegister, public AnalogSignalChange {
205 
206  protected:
207  int adType;
208  unsigned char adch;
209  unsigned char adcl;
210  unsigned char adcsra;
211  unsigned char adcsrb;
212  unsigned char admux;
217  unsigned int irqVec;
218 
220  int adSample;
227 
228  enum T_State {
232  } state;
233 
234  enum {
235  ADEN = 0x80,
236  BIN = 0x80,
237  ADSC = 0x40,
238  ACME = 0x40,
239  ADFR = 0x20,
240  ADATE = 0x20,
241  ADLAR = 0x20,
242  IPR = 0x20,
243  ADIF = 0x10,
244  ADIE = 0x08,
245  MUX5 = 0x08, // for selecting high ADC channels in ADCSRB
246  ADPS = 0x07,
247  ADTS = 0x07
248  };
249 
250  bool IsPrescalerClock(void);
251  bool IsFreeRunning(void);
252  virtual int GetTriggerSource(void);
253  int ConversionBipolar(float value, float ref);
254  int ConversionUnipolar(float value, float ref);
255 
256  public:
257  enum {
266  AD_M2560
267  };
268 
269  IOReg<HWAd> adch_reg,
270  adcl_reg,
271  adcsra_reg,
272  adcsrb_reg,
273  admux_reg;
274 
275  HWAd(AvrDevice *c, int _typ, HWIrqSystem *i, unsigned int iv, HWAdmux *a, HWARef *r);
276  virtual ~HWAd() { mux->UnregisterNotifyClient(); }
277 
278  unsigned int CpuCycle();
279 
280  unsigned char GetAdch(void);
281  unsigned char GetAdcl(void);
282  unsigned char GetAdcsrA(void) { return adcsra; }
283  unsigned char GetAdcsrB(void) { return adcsrb; }
284  unsigned char GetAdmux(void) { return admux; }
285  void SetAdcsrA(unsigned char);
286  void SetAdcsrB(unsigned char);
287  void SetAdmux(unsigned char val);
288  void Reset(void);
289  void ClearIrqFlag(unsigned int vec);
290 
291  // interface for notify signal change in multiplexer
292  void NotifySignalChanged(void);
293 
294  // interface for analog comparator
296  bool IsADEnabled(void) { return (adcsra & ADEN) == ADEN; }
298  bool IsSetACME(void) { return (adcsrb & ACME) == ACME; }
300  float GetADMuxValue(float vcc) { return mux->GetValueAComp(admux, vcc); }
302  void RegisterNotifyClient(AnalogSignalChange *client) { notifyClient = client; }
304  void UnregisterNotifyClient(void) { notifyClient = 0; }
305 };
306 
308 class HWAd_SFIOR: public HWAd, public IOSpecialRegClient {
309 
310  protected:
312  int adts;
313 
314  public:
315  HWAd_SFIOR(AvrDevice *c, int _typ, HWIrqSystem *i, unsigned int iv, HWAdmux *a, HWARef *r, IOSpecialReg *s);
316 
317  void Reset(void) { HWAd::Reset(); adts = 0; }
318 
319  unsigned char set_from_reg(const IOSpecialReg* reg, unsigned char nv);
320  unsigned char get_from_client(const IOSpecialReg* reg, unsigned char v) { return v; }
321 
322  virtual int GetTriggerSource(void) { return adts; }
323 };
324 
325 #endif
unsigned char GetAdmux(void)
Definition: hwad.h:284
ADC reference is selected on 3 or 4 different sources: Vcc, aref pin, bandgap or 2.56V reference.
Definition: hwad.h:60
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
0:aref, 1:vcc, 2:bg, 3:2.56V
Definition: hwad.h:68
0:aref, 1:vcc, 2:-, 3:2.56V
Definition: hwad.h:67
unsigned int irqVec
Definition: hwad.h:217
int refType
Definition: hwad.h:63
int adType
Definition: hwad.h:207
void RegisterNotifyClient(AnalogSignalChange *client)
Register analog comparator for notification of multiplexer signal change.
Definition: hwad.h:302
HWARef8(AvrDevice *_core, Pin *_refpin)
Definition: hwad.h:84
virtual float GetRefValue(int select, float vcc)=0
Definition: hwad.h:90
unsigned char GetAdcsrA(void)
Definition: hwad.h:282
void UnregisterNotifyClient(void)
Unregister client for signal change notification.
Definition: hwad.h:304
unsigned char GetAdcsrB(void)
Definition: hwad.h:283
Pin class, handles input and output to external parts.
Definition: pin.h:98
int adSample
Definition: hwad.h:220
IOReg< HWAd > admux_reg
Definition: hwad.h:269
AnalogSignalChange * notifyClient
Definition: hwad.h:94
ADC reference is selected on 4 diff. sources: Vcc, aref pin, bandgap or 2.56V reference.
Definition: hwad.h:78
ADC reference is taken from special ADREF pin (no port pin)
Definition: hwad.h:48
virtual ~HWAdmux()
Definition: hwad.h:106
unsigned char admux
Definition: hwad.h:212
int prescaler
Definition: hwad.h:222
HWAdmux * mux
Definition: hwad.h:214
Interface class to connect hardware units to control registers.
Definition: rwmem.h:398
void RegisterNotifyClient(AnalogSignalChange *client)
Definition: hwad.h:114
ADC type M16: ADC on atmega16 and atmega32.
Definition: hwad.h:260
int prescalerSelect
Definition: hwad.h:223
int numPins
Multiplexer channel, can&#39;t be used for ADC sampling because of buffering on conversion start! ...
Definition: hwad.h:96
HWIrqSystem * irqSystem
Definition: hwad.h:216
void Reset(void)
Definition: hwad.cpp:426
Build a register for TraceValue&#39;s.
Definition: traceval.h:442
ADC type M8: ADC on atmega8.
Definition: hwad.h:259
AvrDevice * core
Definition: hwad.h:38
int conversionState
Definition: hwad.h:224
ADC type M64: ADC on atmega64.
Definition: hwad.h:261
bool adchLocked
Definition: hwad.h:219
void Reset(void)
Definition: hwad.h:317
Reference source for ADC (base class)
Definition: hwad.h:35
AvrDevice * core
Definition: hwad.h:97
T_State
Definition: hwad.h:228
unsigned char adcl
Definition: hwad.h:209
HWAdmux(AvrDevice *_core, int _pins)
Definition: hwad.h:100
Pin aref_pin
Definition: hwad.h:51
ADC type M164: ADC on atmega164/324/644/1284 and at90can32/64/128.
Definition: hwad.h:264
unsigned char adcsrb
Definition: hwad.h:211
Pin * aref_pin
Definition: hwad.h:81
virtual ~HWARef()
Definition: hwad.h:42
IOSpecialReg * sfior_reg
Definition: hwad.h:311
Definition: hwad.h:118
ADC type 4433: ADC on at90s/l4433.
Definition: hwad.h:258
int muxSelect
Definition: hwad.h:95
unsigned char adcsra
Definition: hwad.h:210
ADC type T25: ADC on attiny25/45/85.
Definition: hwad.h:265
bool IsADEnabled(void)
Check, if ADC is enabled.
Definition: hwad.h:296
virtual float GetValueAComp(int select, float vcc)
Definition: hwad.h:110
ADC type M128: ADC on atmega128.
Definition: hwad.h:262
AnalogSignalChange * notifyClient
Definition: hwad.h:226
float GetADMuxValue(float vcc)
Get analog value from ADC multiplexer.
Definition: hwad.h:300
HWARef * aref
Definition: hwad.h:215
int adMuxConfig
Definition: hwad.h:221
AvrDevice * core
Definition: hwad.h:213
HWARef(AvrDevice *_core)
Definition: hwad.h:41
bool IsSetACME(void)
Check, if ACME bit is set, return false, if not available.
Definition: hwad.h:298
int adts
Definition: hwad.h:312
Definition: hwad.h:204
virtual ~HWAd()
Definition: hwad.h:276
void UnregisterNotifyClient(void)
Definition: hwad.h:115
virtual bool IsDifferenceChannel(int select)
Definition: hwad.h:111
ADC type M48: ADC on atmega48/88/168/328.
Definition: hwad.h:263
unsigned char get_from_client(const IOSpecialReg *reg, unsigned char v)
Definition: hwad.h:320
bool firstConversion
Definition: hwad.h:225
unsigned char adch
Definition: hwad.h:208
virtual int GetTriggerSource(void)
Definition: hwad.h:322