simulavr  1.1.0
pin.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 PIN
27 #define PIN
28 #include <cstddef>
29 #include <vector>
30 
31 #include "pinnotify.h"
32 
33 class Net;
34 class HWPort;
35 template<typename T> class IOReg;
36 
37 #define REL_FLOATING_POTENTIAL 0.55
38 
41  public:
42  virtual ~AnalogSignalChange() {}
43 
44  virtual void NotifySignalChanged(void)=0;
45 };
46 
48 
54 class AnalogValue {
55 
56  private:
57  int dState;
58  float aValue;
59 
60  public:
61  enum {
65  ST_ANALOG
66  };
67 
69  AnalogValue(void) { dState = ST_FLOATING; aValue = 0.0; }
71  AnalogValue(float val) { dState = ST_ANALOG; aValue = val; }
73  AnalogValue(int dig) { dState = dig; aValue = 0.0; }
74 #ifndef SWIG
75  AnalogValue &operator= (const AnalogValue& a) { dState = a.dState; aValue = a.aValue; return *this; }
77 #endif
78  void setD(int dig) { dState = dig; aValue = 0.0; }
80  int getD(void) const { return dState; }
82  void setA(float val) { dState = ST_ANALOG; aValue = val; }
84  float getA(float vcc);
86  float getRaw(void) const { return aValue; }
88  bool analogValid(void) const { return dState == ST_ANALOG; }
89 };
90 
92 
98 class Pin {
99 
100  protected:
101  unsigned char *pinOfPort;
103  unsigned char mask;
105 
107 
108  public:
109 
111 
115  typedef enum {
123  ANALOG_SHORTED
124  } T_Pinstate;
125 
126  T_Pinstate outState;
127  std::vector<HasPinNotifyFunction*> notifyList;
128 
129  Pin(void);
130  Pin(const Pin& p);
131  Pin(T_Pinstate ps);
132  Pin(unsigned char *parentPin, unsigned char mask);
133  Pin(float analog);
134  virtual ~Pin();
135 
136 #ifndef SWIG
137  operator char() const;
138  virtual Pin &operator= (char);
139  virtual operator bool() const;
140  virtual Pin operator+ (const Pin& p);
141  virtual Pin operator+= (const Pin& p);
142 #endif
143 
144  virtual void SetInState(const Pin &p);
145  virtual void RegisterNet(Net *n);
146  virtual void UnRegisterNet(Net *n);
147  virtual Pin GetPin(void) { return *this;}
148  int GetAnalog(void);
149  float GetRawAnalog(void) const { return analogVal.getRaw(); }
150  float GetAnalogValue(float vcc) { return analogVal.getA(vcc); }
151  Pin& SetAnalogValue(float value);
152  void SetRawAnalog(float value) { analogVal.setA(value); }
153  void RegisterCallback(HasPinNotifyFunction *);
154 
157  bool CalcPin(void);
158 
159  bool isPortPin(void) { return pinOfPort != nullptr; }
160  bool isConnected(void) { return connectedTo != nullptr; }
161  bool hasListener(void) { return notifyList.size() != 0; }
162 
163  friend class HWPort;
164  friend class Net;
165 
166 };
167 
169 class PortPin: public Pin {
170 
171  private:
172  unsigned int regCount;
173 
174  protected:
175  unsigned char DDOE;
176  unsigned char DDOV;
177  unsigned char PVOE;
178  unsigned char PVOV;
179  unsigned char PVOEwDDR;
180  unsigned char PUOE;
181  unsigned char PUOV;
182 
183  public:
184  PortPin(void);
185  virtual ~PortPin();
186  void ResetOverride(void);
187 
188  // override interface, index is the registered index for multiple alternate pin functions
189  void SetDDOV(bool val, int index = 0);
190  void SetDDOE(bool val, int index = 0);
191  void SetPVOV(bool val, int index = 0);
192  void SetPVOE(bool val, int index = 0);
193  void SetPVOE_WithDDR(bool val, int index = 0);
194  void SetPUOV(bool val, int index = 0);
195  void SetPUOE(bool val, int index = 0);
196 
197  // register interface for PinAtPort to support multiple alternate functionality
198  int RegisterAlternateUse(void);
199 
200  // calculate outState with override
201  bool CalcPinOverride(bool ddr, bool port, bool pud);
202 
203  friend class HWPort;
204  friend class Net;
205 };
206 
208 class OpenDrain: public Pin {
209  protected:
210  Pin *pin; // the connected pin, which control input
211 
212  public:
213  OpenDrain(Pin *p);
214  virtual Pin GetPin();
215 };
216 
217 #endif
Net * connectedTo
the connection to other pins (nullptr, if not connected)
Definition: pin.h:106
unsigned char PVOV
Definition: pin.h:178
unsigned char * pinOfPort
points to HWPort::pin or nullptr
Definition: pin.h:101
float getA(float vcc)
calculate real voltage potential, needs value of Vcc potential
Definition: pin.cpp:33
Pin class, handles input and output to external parts.
Definition: pin.h:98
digital state, Vcc potential
Definition: pin.h:64
AnalogValue(void)
standard constructor, status is floating
Definition: pin.h:69
virtual Pin GetPin(void)
"cast method" to get back a Pin instance
Definition: pin.h:147
Defines a Port, e.g. a hardware device for GPIO.
Definition: hwport.h:43
Pin * pin
Definition: pin.h:210
float aValue
analog value from setA method or constructor (not checked to valid range!)
Definition: pin.h:58
unsigned char DDOV
Definition: pin.h:176
AnalogValue(int dig)
digital value constructor, set a digital state
Definition: pin.h:73
Open drain Pin class, a special pin with open drain behavior.
Definition: pin.h:208
int dState
digital state and validity of aValue
Definition: pin.h:57
T_Pinstate
Possible PIN states.
Definition: pin.h:115
digital state, ground potential
Definition: pin.h:62
float getRaw(void) const
get raw analog value (no calculation, just content of aValue
Definition: pin.h:86
Definition: pin.h:116
unsigned char mask
byte mask for HWPort::pin
Definition: pin.h:103
void setA(float val)
set analog value, no check to value range between ground and vcc
Definition: pin.h:82
std::vector< HasPinNotifyFunction * > notifyList
listeners for change of input value
Definition: pin.h:127
T_Pinstate outState
discrete value of output stage
Definition: pin.h:126
bool isConnected(void)
True, if it&#39;s connected to other pins.
Definition: pin.h:160
void SetRawAnalog(float value)
Definition: pin.h:152
IO register to be specialized for a certain class/hardware.
Definition: pin.h:35
Pin class for HWPort, a special pin with override functionality for output stage. ...
Definition: pin.h:169
unsigned char PUOV
Definition: pin.h:181
bool hasListener(void)
True, if there change listeners.
Definition: pin.h:161
unsigned int regCount
register counter
Definition: pin.h:172
unsigned char PVOE
Definition: pin.h:177
virtual void NotifySignalChanged(void)=0
int getD(void) const
Definition: pin.h:80
unsigned char DDOE
Definition: pin.h:175
floating potential, not connected or tristate, assumed as FLOATING_POTENTIAL
Definition: pin.h:63
IOReg< HWPort > * pinRegOfPort
points to PIN io register of port or nullptr
Definition: pin.h:102
AnalogValue(float val)
analog value constructor, set real analog value
Definition: pin.h:71
bool isPortPin(void)
True, if it&#39;s a port pin.
Definition: pin.h:159
Implements "real" analog value as float.
Definition: pin.h:54
unsigned char PUOE
Definition: pin.h:180
Definition: pin.h:117
unsigned char PVOEwDDR
Definition: pin.h:179
float GetRawAnalog(void) const
get back raw analog value (just variable content!)
Definition: pin.h:149
float GetAnalogValue(float vcc)
Returns real analog input value of pin.
Definition: pin.h:150
virtual ~AnalogSignalChange()
Definition: pin.h:42
bool analogValid(void) const
test, if real analog value is available
Definition: pin.h:88
AnalogValue analogVal
"real" analog voltage value
Definition: pin.h:104
Connect Pins to each other and transfers a output change from a pin to input values for all pins...
Definition: net.h:34