libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
pwm2.h
Go to the documentation of this file.
1 #ifndef libpropeller_pwm2_h__
2 #define libpropeller_pwm2_h__
3 
4 
5 #include <propeller.h>
6 extern char _load_start_pwm2_cog[];
7 
23 class PWM2 {
24 public:
25 
29  void Start(void) {
30  volatile void * asm_reference = NULL;
31  __asm__ volatile ( "mov %[asm_reference], #PWM2_Entry \n\t"
32  : [asm_reference] "+r" (asm_reference));
33 
34  SetDutyX(0);
35  SetDutyY(0);
36  SetPinX(-1);
37  SetPinY(-1);
38 
39  SetFrequency(kDefaultFrequency);
40  Cog = cognew(_load_start_pwm2_cog, &Periodxy) + 1;
41  }
42 
46  void Stop(void) {
47  if (Cog > 0) {
48  SetDutyX(0);
49  SetDutyY(0);
50  waitcnt(Periodxy * 2 + CNT);
51  cogstop(Cog - 1);
52  Cog = 0;
53  }
54 
55  }
56 
63  void SetPinX(const int pinX) {
64  pin_x = pinX;
65  if (pin_x == -1) {
66  Pinxmask = 0;
67  Ctrxval = 0;
68 
69  } else {
70  Pinxmask = (1 << pinX);
71  Ctrxval = ((4 << 26) + pinX);
72  }
73  }
74 
81  void SetPinY(const int pinY) {
82  pin_y = pinY;
83  if (pin_y == -1) {
84  Pinymask = 0;
85  Ctryval = 0;
86  } else {
87  Pinymask = (1 << pinY);
88  Ctryval = ((4 << 26) + pinY);
89  }
90  }
91 
96  void SetDutyX(const int percent) {
97  Percentx = percent;
98  Dutyx = ((percent * Periodxy) / 100);
99  }
100 
107  void SetDutyY(const int percent) {
108  Percenty = percent;
109  Dutyy = ((percent * Periodxy) / 100);
110  }
111 
118  void SetFrequency(const int frequency) {
119  Periodxy = (CLKFREQ / frequency);
120  SetDutyX(Percentx);
121  SetDutyY(Percenty);
122  }
123 private:
124  int Cog;
125  int Percentx, Percenty;
126  int pin_x, pin_y;
127  int volatile Periodxy;
128  int volatile Dutyx, Pinxmask, Ctrxval;
129  int volatile Dutyy, Pinymask, Ctryval;
130 
131  static const int kDefaultFrequency = 12000;
132 };
133 
134 #endif // libpropeller_pwm2_h__