NetBurner 3.1
cpu_pins.h
Go to the documentation of this file.
1 #ifndef __CPU_PINS_H
2 #define __CPU_PINS_H
3 /*NB_REVISION*/
4 
5 /*NB_COPYRIGHT*/
6 
17 #include <sim.h>
18 #include <basictypes.h>
19 
20 
32 class PinIO {
33 public:
34  volatile Pio &pio;
35  uint32_t mask;
36 
41  typedef enum {
42  PIN_FN_IN = 0,
48  } pin_fn_t;
49 
54  PinIO() : pio(*((volatile Pio*)PIOA)), mask(0) {}
66  constexpr PinIO(uint32_t port, uint32_t pin) : pio(*((volatile Pio*)PIOA+port)), mask(1 << pin) {}
71  constexpr PinIO(const PinIO &rhs): pio(rhs.pio), mask(rhs.mask) {}
72 
77  void setFn(pin_fn_t fn) const {
78  if (fn > PIN_FN_OUT) {
79  uint8_t bits = ((uint8_t)fn)-2;
80  if (bits & 0x1) { pio.PIO_ABCDSR[0] |= mask; }
81  else { pio.PIO_ABCDSR[0] &= ~mask; }
82  if (bits & 0x2) { pio.PIO_ABCDSR[1] |= mask; }
83  else { pio.PIO_ABCDSR[1] &= ~mask; }
84 
85  pio.PIO_PDR = mask;
86  }
87  else {
88  if (fn) { pio.PIO_OER = mask; }
89  else { pio.PIO_ODR = mask; }
90  pio.PIO_PER = mask;
91  }
92  }
97  void function(pin_fn_t fn) const { setFn(fn); }
101  void hiz() const { setFn(PIN_FN_IN); }
105  void drive() const { setFn(PIN_FN_OUT); }
109  inline void set() const { pio.PIO_SODR = mask; }
113  inline void clr() const { pio.PIO_CODR = mask; }
118  inline bool tgl() const {bool val = pio.PIO_ODSR & mask;(&(pio.PIO_SODR))[val] = mask;return val;}
123  inline bool toggle() const { return tgl(); }
129  inline bool readBack() const { return (pio.PIO_PDSR & mask); }
134  inline bool read() const { hiz(); return readBack(); }
140  inline bool operator=( bool val ) { (&(pio.PIO_SODR))[!val] = mask; return val;}
147  inline PinIO& operator=(const PinIO& rhs)
148  {
149  bool val = rhs;
150  (&(pio.PIO_SODR))[!val] = mask; return *this;
151  }
152 
157  inline operator bool() const { return (pio.PIO_PDSR & mask); }
158 
163  inline bool operator!() const { return ((pio.PIO_ODR & mask) == 0); }
164 
171  void multidrv(bool enable) const { (&(pio.PIO_MDER))[!enable] = mask; }
172 
179  void setHighStrength( bool bHighDrive )
180  {
181  if (bHighDrive) { pio.PIO_DRIVER |= mask; }
182  else { pio.PIO_DRIVER &= ~mask;}
183  }
184 
191  void PullUp(bool enable) const {if (enable) {pio.PIO_PUER =mask;} else {pio.PIO_PUDR =mask;}};
198  void PullDown(bool enable) const {if (enable) {pio.PIO_PPDER =mask;} else {pio.PIO_PPDDR =mask;}};
199 
205  uint16_t analogRead() const;
206 
207  //friend class PinIOArray2;
208 
209 };
210 
211 
224 class _PinVector {
225 protected:
226  struct pinCfg {
227  uint8_t port : 3;
228  uint8_t num : 5;
229  };
230  const uint8_t len;
231  union {
232  pinCfg _pins[4];
233  pinCfg *const pinArr;
234  };
235  // _PinVector constructors are protected to prevent the construction of the
236  // base class, as the base class has no allocated storage for the pin
237  // confuguration settings.
238  _PinVector(uint8_t _len, pinCfg *arr);
239  _PinVector(uint8_t len, pinCfg *arr,
240  PinIO *initpins, uint32_t pinCount);
241 public:
247  uint32_t operator=(uint32_t val);
253  PinIO operator[](int idx);
254 
261  void config(uint32_t idx, PinIO cfg);
269  void config(PinIO *pinCfgs, uint32_t count);
270 
275  operator uint32_t() const;
276 };
277 
278 
289 template<uint8_t n> class PinVector : public _PinVector {
290  _PinVector::pinCfg pinStore[n];
291 public:
296  inline PinVector(): _PinVector(n, pinStore) {}
297 
304  inline PinVector(PinIO *initpins, uint32_t pinCount)
305  : _PinVector(n, pinStore, initpins, pinCount)
306  { }
312  inline uint32_t operator=(uint32_t val) {return (*(_PinVector*)this) = val; }
313 };
314 
315 // TinyPinVectors are a template specialization made for vector lengths where
316 // the entire configuration list can fit within the memory space of the
317 // storage pointer, thereby reducing the storage requirements to an absolute
318 // minimum, while preventing the duplication of code.
319 #define TinyPinVector(n) \
320 template<> class PinVector<n> : public _PinVector { \
321 public: \
322  inline PinVector(): _PinVector((n), nullptr) {} \
323  \
324  inline PinVector(PinIO *initpins, uint32_t pinCount) \
325  : _PinVector((n), nullptr, initpins, pinCount) \
326  { } \
327 };
328 
329 TinyPinVector(1);
330 TinyPinVector(2);
331 TinyPinVector(3);
332 TinyPinVector(4);
333 
334 
335 
336  // end of groupGPIO
338 #endif /* ----- #ifndef __CPU_PINS_H ----- */
bool operator!() const
Return the oposite of the driven value of the pin(s).
Definition: cpu_pins.h:163
bool readBack() const
Read the state of the pin(s) line state without changing the pin function or direction.
Definition: cpu_pins.h:129
PinVector(PinIO *initpins, uint32_t pinCount)
PinVector Constructor, where pin configurations will be made at the time of construction.
Definition: cpu_pins.h:304
PinIO()
Construct an empty PinIO. Exists for bootstrap compatibility; not intended for general use...
Definition: cpu_pins.h:54
Output.
Definition: cpu_pins.h:43
void multidrv(bool enable) const
Configure the multidrive/open-drain driver for the pin(s).
Definition: cpu_pins.h:171
GPIO Pin Vector Base Class.
Definition: cpu_pins.h:224
constexpr PinIO(uint32_t port, uint32_t pin)
Construct a PinIO for a specific cpu pin.
Definition: cpu_pins.h:66
Peripheral A.
Definition: cpu_pins.h:44
bool operator=(bool val)
Assign a driven value to the pin(s).
Definition: cpu_pins.h:140
bool read() const
Configure the pin as an input then return the line state.
Definition: cpu_pins.h:134
PinIO & operator=(const PinIO &rhs)
Assign a driven value to the pin(s) based on the line state of another pin(s).
Definition: cpu_pins.h:147
PinIO operator[](int idx)
Access the PinIO for a specific bit position in the _PinVector.
uint32_t operator=(uint32_t val)
Assign a value to the _PinVector Bus.
void PullUp(bool enable) const
Configure the pad Pull Up resistor for the pin(s).
Definition: cpu_pins.h:191
void clr() const
Drive the pin(s) Low.
Definition: cpu_pins.h:113
void PullDown(bool enable) const
Configure the pad Pull Down resistor for the pin(s).
Definition: cpu_pins.h:198
Peripheral D.
Definition: cpu_pins.h:47
bool toggle() const
Toggle the driven value for the pin(s).
Definition: cpu_pins.h:123
uint32_t operator=(uint32_t val)
Assign a value to the PinVector Bus.
Definition: cpu_pins.h:312
volatile Pio & pio
Definition: cpu_pins.h:34
void setHighStrength(bool bHighDrive)
Configure the drive strength of the output driver for the pin(s).
Definition: cpu_pins.h:179
void hiz() const
Configure the pin(s) to Input.
Definition: cpu_pins.h:101
void setFn(pin_fn_t fn) const
Set the pin function for the managed pin(s).
Definition: cpu_pins.h:77
void drive() const
Configure the pin(s) to Output, without modifying the driven value.
Definition: cpu_pins.h:105
constexpr PinIO(const PinIO &rhs)
Construct a copy of another PinIO.
Definition: cpu_pins.h:71
Input.
Definition: cpu_pins.h:42
uint16_t analogRead() const
Read an analog voltage on the given Pin. Only available for pins connected to the ADC...
void config(uint32_t idx, PinIO cfg)
Set the PinIO that will be used for a given bit position in the _PinVector.
Peripheral B.
Definition: cpu_pins.h:45
GPIO Pin Class.
Definition: cpu_pins.h:32
Peripheral C.
Definition: cpu_pins.h:46
pin_fn_t
Definition: cpu_pins.h:41
GPIO Pin Vector Class PinVector is a template instantiation of the _PinVector class, allowing for minimal storage requirements for potentially large vectors, without heavy code duplication due to template copies.
Definition: cpu_pins.h:289
PinVector()
Bare constructor for a PinVector, where the bit configurations will be made later.
Definition: cpu_pins.h:296
bool tgl() const
Toggle the driven value for the pin(s).
Definition: cpu_pins.h:118
uint32_t mask
Definition: cpu_pins.h:35