NetBurner 3.5.0
PDF Version
 
cortex-m7/cpu/SAME70/include/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
32class PinIO {
33public:
34 volatile Pio &pio;
35 uint32_t mask;
36
48
53 PinIO() : pio(*((volatile Pio*)PIOA)), mask(0) {}
65 constexpr PinIO(uint32_t port, uint32_t pin) : pio(*((volatile Pio*)PIOA+port)), mask(1 << pin) {}
70 constexpr PinIO(const PinIO &rhs): pio(rhs.pio), mask(rhs.mask) {}
71
76 void setFn(pin_fn_t fn) const {
77 if (fn > PIN_FN_OUT) {
78 uint8_t bits = ((uint8_t)fn)-2;
79 if (bits & 0x1) { pio.PIO_ABCDSR[0] |= mask; }
80 else { pio.PIO_ABCDSR[0] &= ~mask; }
81 if (bits & 0x2) { pio.PIO_ABCDSR[1] |= mask; }
82 else { pio.PIO_ABCDSR[1] &= ~mask; }
83
84 pio.PIO_PDR = mask;
85 }
86 else {
87 if (fn) { pio.PIO_OER = mask; }
88 else { pio.PIO_ODR = mask; }
89 pio.PIO_PER = mask;
90 }
91 }
92
97 int8_t getFn()
98 {
99 // TODO: check if PIO_LOCKSR is set. if it's set, a peripheral has locked the I/O line
100 // The only way to unlock it is to apply a hardware reset to the PIO controller
101 if(mask == 0) { return -1; } // pin is not valid
102
103 if(pio.PIO_PSR & mask) // if PIO_PSR bit is set, PIO is active on corresponding I/O line (peripheral is inactive)
104 {
105 // Determine if I/O line is configured for an input or output
106 // Read output status register (PIO_OSR). If 0, I/O is an input. If 1, I/O is an output
107 return ((pio.PIO_OSR & mask) ? PIN_FN_OUT : PIN_FN_IN);
108 }
109 else // if PIO_PSR bit is clear, PIO is inactive on corresponding I/O line (peripheral is active)
110 {
111 // Determine which peripheral function is set
112 uint32_t abcdsr0 = (pio.PIO_ABCDSR[0] & mask);
113 uint32_t abcdsr1 = (pio.PIO_ABCDSR[1] & mask);
114
115 if (abcdsr0 & mask)
116 {
117 if (abcdsr1 & mask) { return PIN_FN_D; }
118 else { return PIN_FN_B; }
119 }
120 else
121 {
122 if (abcdsr1 & mask) { return PIN_FN_C; }
123 else { return PIN_FN_A; }
124 }
125 }
126 }
127
132 void function(pin_fn_t fn) const { setFn(fn); }
136 void hiz() const { setFn(PIN_FN_IN); }
140 void drive() const { setFn(PIN_FN_OUT); }
144 inline void set() const { pio.PIO_SODR = mask; }
148 inline void clr() const { pio.PIO_CODR = mask; }
153 inline bool tgl() const {bool val = pio.PIO_ODSR & mask;(&(pio.PIO_SODR))[val] = mask;return val;}
158 inline bool toggle() const { return tgl(); }
164 inline bool readBack() const { return (pio.PIO_PDSR & mask); }
169 inline bool read() const { hiz(); return readBack(); }
175 inline bool operator=( bool val ) { (&(pio.PIO_SODR))[!val] = mask; return val;}
182 inline PinIO& operator=(const PinIO& rhs)
183 {
184 bool val = rhs;
185 (&(pio.PIO_SODR))[!val] = mask; return *this;
186 }
187
192 inline operator bool() const { return (pio.PIO_PDSR & mask); }
193
198 inline bool operator!() const { return ((pio.PIO_ODR & mask) == 0); }
199
206 void multidrv(bool enable) const { (&(pio.PIO_MDER))[!enable] = mask; }
207
214 void setHighStrength( bool bHighDrive )
215 {
216 if (bHighDrive) { pio.PIO_DRIVER |= mask; }
217 else { pio.PIO_DRIVER &= ~mask;}
218 }
219
226 void PullUp(bool enable) const {if (enable) {pio.PIO_PUER =mask;} else {pio.PIO_PUDR =mask;}};
233 void PullDown(bool enable) const {if (enable) {pio.PIO_PPDER =mask;} else {pio.PIO_PPDDR =mask;}};
234
240 uint16_t analogRead() const;
241
242 //friend class PinIOArray2;
243
244};
245
246
260protected:
261 struct pinCfg {
262 uint8_t port : 3;
263 uint8_t num : 5;
264 };
265 const uint8_t len;
266 union {
267 pinCfg _pins[4];
268 pinCfg *const pinArr;
269 };
270 // _PinVector constructors are protected to prevent the construction of the
271 // base class, as the base class has no allocated storage for the pin
272 // confuguration settings.
273 _PinVector(uint8_t _len, pinCfg *arr);
274 _PinVector(uint8_t len, pinCfg *arr,
275 PinIO *initpins, uint32_t pinCount);
276public:
282 uint32_t operator=(uint32_t val);
289
296 void config(uint32_t idx, PinIO cfg);
304 void config(PinIO *pinCfgs, uint32_t count);
305
310 operator uint32_t() const;
311};
312
313
323template<uint8_t n> class PinVector : public _PinVector {
324 _PinVector::pinCfg pinStore[n];
325public:
330 inline PinVector(): _PinVector(n, pinStore) {}
331
338 inline PinVector(PinIO *initpins, uint32_t pinCount)
339 : _PinVector(n, pinStore, initpins, pinCount)
340 { }
346 inline uint32_t operator=(uint32_t val) {return (*(_PinVector*)this) = val; }
347 inline uint32_t operator=(int val) {return (*(_PinVector*)this) = (uint32_t)val; }
348};
349
350// TinyPinVectors are a template specialization made for vector lengths where
351// the entire configuration list can fit within the memory space of the
352// storage pointer, thereby reducing the storage requirements to an absolute
353// minimum, while preventing the duplication of code.
354#define TinyPinVector(n) \
355template<> class PinVector<n> : public _PinVector { \
356public: \
357 inline PinVector(): _PinVector((n), nullptr) {} \
358 \
359 inline PinVector(PinIO *initpins, uint32_t pinCount) \
360 : _PinVector((n), nullptr, initpins, pinCount) \
361 { } \
362 inline uint32_t operator=(uint32_t val) {return (*(_PinVector*)this) = val; } \
363 inline uint32_t operator=(int val) {return (*(_PinVector*)this) = (uint32_t)val; } \
364};
365
366TinyPinVector(1);
367TinyPinVector(2);
368TinyPinVector(3);
369TinyPinVector(4);
370
371
372
373
374 // end of groupGPIO
375#endif /* ----- #ifndef __CPU_PINS_H ----- */
GPIO Pin Vector Base Class.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:259
void config(PinIO *pinCfgs, uint32_t count)
Configure the _PinVector based on an array of PinIOs. The index of the PinIO in the configuration arr...
uint32_t operator=(uint32_t val)
Assign a value to the _PinVector Bus.
PinIO operator[](int idx)
Access the PinIO for a specific bit position in the _PinVector.
void config(uint32_t idx, PinIO cfg)
Set the PinIO that will be used for a given bit position in the _PinVector.
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:14
bool operator=(bool val)
Assign a driven value to the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:175
void setFn(pin_fn_t fn) const
Set the pin function for the managed pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:76
bool operator!() const
Return the opposite of the driven value of the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:198
void PullDown(bool enable) const
Configure the pad Pull Down resistor for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:233
void set() const
Drive the pin(s) High.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:144
constexpr PinIO(uint32_t port, uint32_t pin)
Construct a PinIO for a specific cpu pin.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:65
bool toggle() const
Toggle the driven value for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:158
void setHighStrength(bool bHighDrive)
Configure the drive strength of the output driver for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:214
PinIO & operator=(const PinIO &rhs)
Assign a driven value to the pin(s) based on the line state of another pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:182
int8_t getFn()
Get the pin function for the managed pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:97
bool readBack() const
Read the state of the pin(s) line state without changing the pin function or direction.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:164
void drive() const
Configure the pin(s) to Output, without modifying the driven value.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:140
void PullUp(bool enable) const
Configure the pad Pull Up resistor for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:226
uint16_t analogRead() const
Read an analog voltage on the given Pin. Only available for pins connected to the ADC.
bool read() const
Configure the pin as an input then return the line state.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:169
pin_fn_t
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:45
@ PIN_FN_D
Peripheral D.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:46
@ PIN_FN_A
Peripheral A.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:43
@ PIN_FN_OUT
Output.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:51
@ PIN_FN_C
Peripheral C.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:45
@ PIN_FN_IN
Input.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:50
@ PIN_FN_B
Peripheral B.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:44
void hiz() const
Configure the pin(s) to Input.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:136
void function(pin_fn_t fn) const
Set the pin function for the managed pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:132
bool tgl() const
Toggle the driven value for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:153
PinIO()
Construct an empty PinIO. Exists for bootstrap compatibility; not intended for general use.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:53
constexpr PinIO(const PinIO &rhs)
Construct a copy of another PinIO.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:70
void clr() const
Drive the pin(s) Low.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:148
void multidrv(bool enable) const
Configure the multidrive/open-drain driver for the pin(s).
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:206
GPIO Pin Vector Class PinVector is a template instantiation of the _PinVector class,...
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:323
uint32_t operator=(uint32_t val)
Assign a value to the PinVector Bus.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:346
PinVector(PinIO *initpins, uint32_t pinCount)
PinVector Constructor, where pin configurations will be made at the time of construction.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:338
PinVector()
Bare constructor for a PinVector, where the bit configurations will be made later.
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:330
volatile Pio & pio
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:34
uint32_t mask
Definition cortex-m7/cpu/SAME70/include/cpu_pins.h:35
#define PIOA
(PIOA ) Base Address
Definition same70q21_sim.h:253