PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
port.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/PropWare.h>
29 
30 namespace PropWare {
31 
38 class Port {
39  public:
43  typedef enum { P0 = BIT_0, P1 = BIT_1, P2 = BIT_2, P3 = BIT_3, P4 = BIT_4, P5 = BIT_5, P6 = BIT_6, P7 = BIT_7, P8 = BIT_8, P9 = BIT_9, P10 = BIT_10, P11 = BIT_11, P12 = BIT_12, P13 = BIT_13, P14 = BIT_14, P15 = BIT_15, P16 = BIT_16, P17 = BIT_17, P18 = BIT_18, P19 = BIT_19, P20 = BIT_20, P21 = BIT_21, P22 = BIT_22, P23 = BIT_23, P24 = BIT_24, P25 = BIT_25, P26 = BIT_26, P27 = BIT_27, P28 = BIT_28, P29 = BIT_29, P30 = BIT_30, P31 = BIT_31,NULL_PIN = 0
77  } Mask;
78 
82  enum class Dir { IN = 0,OUT = -1
85  };
86 
87  public:
98  static uint8_t from_mask (const Mask mask) {
99  uint8_t retVal = 0;
100  uint32_t maskInt = mask;
101 
102  while (maskInt >>= 1)
103  ++retVal;
104 
105  return retVal;
106  }
107 
115  static Mask to_mask (const uint8_t pinNum) {
116  if (31 < pinNum)
117  return Mask::NULL_PIN;
118  else
119  return static_cast<Mask>(1 << pinNum);
120  }
121 
129  static void flash_port (const uint32_t pinMask, unsigned int iterations = 10) {
130  const Port port(pinMask, Dir::OUT);
131 
132  const unsigned int delay = MILLISECOND << 7; // MILLISECOND * 64
133  unsigned int timer = delay + CNT;
134  iterations <<= 1;
135  for (uint32_t i = 0; i < iterations; ++i) {
136  port.toggle();
137  timer = waitcnt2(timer, delay);
138  }
139  }
140 
141  public:
145  Port (const uint32_t portMask = Mask::NULL_PIN) {
146  this->m_mask = portMask;
147  }
148 
153  Port (const uint32_t portMask, const PropWare::Port::Dir direction) {
154  this->m_mask = portMask;
155  this->set_dir(direction);
156  }
157 
163  void set_mask (const uint32_t mask) {
164  this->m_mask = mask;
165  }
166 
172  uint32_t get_mask () const {
173  return this->m_mask;
174  }
175 
181  void add_pins (const uint32_t mask) {
182  this->m_mask |= mask;
183  }
184 
191  void set_dir (const PropWare::Port::Dir direction) const {
192  DIRA = (DIRA & ~(this->m_mask)) | (this->m_mask & static_cast<uint32_t>(direction));
193  }
194 
201  if (DIRA & this->m_mask)
202  return Dir::OUT;
203  else
204  return Dir::IN;
205  }
206 
210  inline void set_dir_out () const {
211  __asm__ volatile ("or dira, %0" : : "r" (this->m_mask));
212  }
213 
217  inline void set_dir_in () const {
218  __asm__ volatile ("andn dira, %0" : : "r" (this->m_mask));
219  }
220 
226  void set () const {
227  __asm__ volatile ("or outa, %0" : : "r" (this->m_mask));
228  }
229 
233  void high () const {
234  this->set();
235  }
236 
240  void on () const {
241  this->set();
242  }
243 
249  void clear () const {
250  __asm__ volatile ("andn outa, %0" : : "r" (this->m_mask));
251  }
252 
256  void low () const {
257  this->clear();
258  }
259 
263  void off () const {
264  this->clear();
265  }
266 
272  void toggle () const {
273  __asm__ volatile ("xor outa, %0" : : "r" (this->m_mask));
274  }
275 
282  void write_fast (const uint32_t value) const {
283  OUTA = ((OUTA & ~(this->m_mask)) | (value & this->m_mask));
284  }
285 
291  uint32_t read_fast () const {
292  return INA & this->m_mask;
293  }
294 
295  protected:
296  uint32_t m_mask;
297 };
298 
299 }
PropWare::Port::P20
@ P20
Definition: port.h:64
PropWare::Port::set_dir_out
void set_dir_out() const
Set the port for output.
Definition: port.h:210
PropWare::Port::NULL_PIN
@ NULL_PIN
Definition: port.h:76
PropWare::Port::from_mask
static uint8_t from_mask(const Mask mask)
Determine which pin number based on a pin mask.
Definition: port.h:98
PropWare::Port::P3
@ P3
Definition: port.h:47
OUTA
#define OUTA
Use to set output pin states when corresponding DIRA bits are 1.
Definition: propeller1.h:157
PropWare::Port::read_fast
uint32_t read_fast() const
Allow easy reading of only selected pins from a port.
Definition: port.h:291
PropWare::Port::Dir::IN
@ IN
INA
#define INA
Use to read the pins when corresponding DIRA bits are 0.
Definition: propeller1.h:153
PropWare::Port::P7
@ P7
Definition: port.h:51
PropWare::Port::P24
@ P24
Definition: port.h:68
PropWare::Port::high
void high() const
Definition: port.h:233
PropWare::Port::on
void on() const
Definition: port.h:240
PropWare::Port::add_pins
void add_pins(const uint32_t mask)
Add pins to the current mask.
Definition: port.h:181
PropWare::Port::P29
@ P29
Definition: port.h:73
PropWare::Port::P17
@ P17
Definition: port.h:61
PropWare::Port::P23
@ P23
Definition: port.h:67
PropWare::Port::P11
@ P11
Definition: port.h:55
PropWare::Port::Mask
Mask
Definition: port.h:43
PropWare::Port::P15
@ P15
Definition: port.h:59
PropWare::Port::P2
@ P2
Definition: port.h:46
PropWare::Port::P26
@ P26
Definition: port.h:70
PropWare::Port::flash_port
static void flash_port(const uint32_t pinMask, unsigned int iterations=10)
Great for quick debugging to ensure a line of code is executed, this will quickly flash a given set o...
Definition: port.h:129
PropWare::Port::P8
@ P8
Definition: port.h:52
PropWare::Port::P27
@ P27
Definition: port.h:71
PropWare::Port::P4
@ P4
Definition: port.h:48
PropWare::Port::set_dir_in
void set_dir_in() const
Set the port for input.
Definition: port.h:217
PropWare::Port::set_dir
void set_dir(const PropWare::Port::Dir direction) const
Set port as either input or output.
Definition: port.h:191
waitcnt2
#define waitcnt2(a, b)
Wait until system counter reaches a value.
Definition: propeller.h:183
PropWare::Port::Dir
Dir
Definition: port.h:82
PropWare::Port::Port
Port(const uint32_t portMask, const PropWare::Port::Dir direction)
Definition: port.h:153
PropWare::Port::P16
@ P16
Definition: port.h:60
PropWare::Port::Dir::OUT
@ OUT
PropWare::Port::write_fast
void write_fast(const uint32_t value) const
Allow easy writing to a port w/o destroying data elsewhere in the port; No shift is performed to alig...
Definition: port.h:282
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
PropWare::Port::P22
@ P22
Definition: port.h:66
PropWare::Port::P5
@ P5
Definition: port.h:49
PropWare.h
PropWare::Port::P0
@ P0
Definition: port.h:44
PropWare::Port::P12
@ P12
Definition: port.h:56
PropWare::Port::P25
@ P25
Definition: port.h:69
PropWare::Port::P21
@ P21
Definition: port.h:65
PropWare::Port::P31
@ P31
Definition: port.h:75
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::Port::Port
Port(const uint32_t portMask=Mask::NULL_PIN)
Definition: port.h:145
PropWare::Port::P10
@ P10
Definition: port.h:54
PropWare::Port::P28
@ P28
Definition: port.h:72
PropWare::Port::set_mask
void set_mask(const uint32_t mask)
Set the mask for this port.
Definition: port.h:163
PropWare::Port::P19
@ P19
Definition: port.h:63
PropWare::Port::P6
@ P6
Definition: port.h:50
PropWare::Port::P9
@ P9
Definition: port.h:53
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare::Port::low
void low() const
Definition: port.h:256
PropWare::Port::P18
@ P18
Definition: port.h:62
PropWare::Port::get_mask
uint32_t get_mask() const
Return the full pin mask of all pins in the port.
Definition: port.h:172
PropWare::Port::toggle
void toggle() const
Toggle the output value of a port.
Definition: port.h:272
PropWare::Port::get_dir
PropWare::Port::Dir get_dir() const
Determine the direction this port is currently set to.
Definition: port.h:200
PropWare::Port::P13
@ P13
Definition: port.h:57
PropWare::Port::P30
@ P30
Definition: port.h:74
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
PropWare::Port::to_mask
static Mask to_mask(const uint8_t pinNum)
Return a Mask type based on a pin number.
Definition: port.h:115
PropWare::Port::P1
@ P1
Definition: port.h:45
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::P14
@ P14
Definition: port.h:58
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249
PropWare::Port::off
void off() const
Definition: port.h:263