PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
dualpwm.h
Go to the documentation of this file.
1 
26 #pragma once
27 
29 #include <PropWare/gpio/pin.h>
30 
31 namespace PropWare {
32 
40 class DualPWM: public Runnable {
41  public:
46  static const unsigned int MAX_DUTY = 4096;
47 
48  public:
61  template<size_t N>
62  DualPWM (const unsigned int frequency, const Pin::Mask mask1, const Pin::Mask mask2,
63  volatile unsigned int *duty1, volatile unsigned int *duty2, const uint32_t (&stack)[N])
64  : Runnable(stack),
65  m_period(CLKFREQ / frequency),
66  m_mask1(mask1),
67  m_mask2(mask2),
68  m_duty1(duty1),
69  m_duty2(duty2) {
70  }
71 
83  DualPWM (const unsigned int frequency, const Pin::Mask mask1, const Pin::Mask mask2,
84  volatile unsigned int *duty1, volatile unsigned int *duty2, const uint32_t *stack,
85  const size_t stackLength)
86  : Runnable(stack, stackLength),
87  m_period(CLKFREQ / frequency),
88  m_mask1(mask1),
89  m_mask2(mask2),
90  m_duty1(duty1),
91  m_duty2(duty2) {
92  }
93 
94  void run () {
95  if (this->m_mask1) {
96  CTRA = 0b00100 << 26 | static_cast<unsigned int>(Pin::from_mask(this->m_mask1));
97  FRQA = 1;
98  DIRA |= this->m_mask1;
99  }
100 
101  if (this->m_mask2) {
102  CTRB = 0b00100 << 26 | static_cast<unsigned int>(Pin::from_mask(this->m_mask2));
103  FRQB = 1;
104  DIRA |= this->m_mask2;
105  }
106 
107  const register unsigned int period = this->m_period;
108 
109  auto timer = CNT + period;
110  while (1) {
111  const auto leftPhase = *this->m_duty1 * period / MAX_DUTY;
112  const auto rightPhase = *this->m_duty2 * period / MAX_DUTY;
113  __asm__ volatile("neg PHSA, %0" : : "r" (leftPhase));
114  __asm__ volatile("neg PHSB, %0" : : "r" (rightPhase));
115  timer = waitcnt2(timer, period);
116  }
117  }
118 
119  private:
120  const unsigned int m_period;
121  const Pin::Mask m_mask1;
122  const Pin::Mask m_mask2;
123  volatile unsigned int *m_duty1;
124  volatile unsigned int *m_duty2;
125 };
126 
127 }
runnable.h
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::DualPWM::MAX_DUTY
static const unsigned int MAX_DUTY
Definition: dualpwm.h:46
PropWare::DualPWM
Dual-pin PWM driver. Requires a cog to run and will use both counter modules in that cog.
Definition: dualpwm.h:40
PropWare::Port::Mask
Mask
Definition: port.h:43
FRQA
#define FRQA
Counter A frequency register.
Definition: propeller1.h:169
PropWare::DualPWM::DualPWM
DualPWM(const unsigned int frequency, const Pin::Mask mask1, const Pin::Mask mask2, volatile unsigned int *duty1, volatile unsigned int *duty2, const uint32_t *stack, const size_t stackLength)
Construct a DualPWM instance.
Definition: dualpwm.h:83
waitcnt2
#define waitcnt2(a, b)
Wait until system counter reaches a value.
Definition: propeller.h:183
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
PropWare::Runnable
Helper class for creating easy parallel applications.
Definition: runnable.h:75
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
CTRB
#define CTRB
Counter B control register.
Definition: propeller1.h:167
PropWare::DualPWM::run
void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: dualpwm.h:94
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare::DualPWM::DualPWM
DualPWM(const unsigned int frequency, const Pin::Mask mask1, const Pin::Mask mask2, volatile unsigned int *duty1, volatile unsigned int *duty2, const uint32_t(&stack)[N])
Construct a DualPWM instance.
Definition: dualpwm.h:62
pin.h
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165