libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
pwm32.test.h
Go to the documentation of this file.
1 #include "unity.h"
2 #include "propeller.h"
3 
4 #include "c++-alloc.h"
5 
8 
9 #include "libpropeller/board/board_unit_tester.h"
10 
11 
12 
14 
16 
17 
18 
19 
20 const int outputPin = Board::kPinTie1a;
21 const int inputPinMask = 1 << Board::kPinTie1b;
22 
23 class UnityTests {
24 public:
25 
26  static int MicrosecondsToClockCycles(const int microseconds) {
27  return (CLKFREQ * microseconds) / 1000000;
28  }
29 
30  static int ClockCyclesToMicroseconds(const int clockCycles) {
31  return clockCycles / (CLKFREQ / 1000000);
32  }
33 
34  static void setUp(void) {
35  sut = new PWM32();
36  sut->Start();
37 
38  pwr = new PulseWidthReader();
39  pwr->Start(inputPinMask);
40 
41  waitcnt(CLKFREQ / 10 + CNT);
42 
43  }
44 
45  static void tearDown(void) {
46  sut->Stop();
47  delete sut;
48  sut = NULL;
49 
50  pwr->Stop();
51  delete pwr;
52  pwr = NULL;
53 
54  }
55 
56  static void helper_CheckWidths(const int highMicroseconds, const int lowMicroseconds) {
57  TEST_ASSERT_INT_WITHIN(PWM32::Resolution * 2 / 1000, highMicroseconds,
58  ClockCyclesToMicroseconds(pwr->getHighTime(0)));
59 
60  TEST_ASSERT_INT_WITHIN(PWM32::Resolution * 2 / 1000, lowMicroseconds,
61  ClockCyclesToMicroseconds(pwr->getLowTime(0)));
62  }
63 
64  static void helper_TestDuty(const int percent, const int period) {
65  sut->Duty(outputPin, percent, period);
66  waitcnt(CLKFREQ / 10 + CNT);
67 
68  //while(true){}
69 
70  helper_CheckWidths((period * percent) / 100, (period * (100 - percent)) / 100);
71  }
72 
73  static void helper_TestServo(const int pulsewidth) {
74  sut->Servo(outputPin, pulsewidth);
75  waitcnt(CLKFREQ / 10 + CNT);
76 
77  //while(true){}
78 
79  helper_CheckWidths(pulsewidth, 20000 - pulsewidth);
80  }
81 
82  static void helper_TestPWM(const int high, const int low) {
83  sut->PWM(outputPin, high, low);
84  waitcnt(CLKFREQ / 10 + CNT);
85 
86  //while(true){}
87 
88  helper_CheckWidths(high, low);
89  }
90 
91  static void test_50PercentDuty(void) {
92  helper_TestDuty(50, 1000);
93  }
94 
95  static void test_20PercentDuty(void) {
96  helper_TestDuty(20, 1000);
97  }
98 
99  static void test_80PercentDuty(void) {
100  helper_TestDuty(80, 1000);
101  }
102 
103  static void test_ServoMiddle(void) {
104  helper_TestServo(1500);
105  }
106 
107  static void test_ServoLow(void) {
108  helper_TestServo(1000);
109  }
110 
111  static void test_ServoHigh(void) {
112  helper_TestServo(2000);
113  }
114 
115  static void test_ServoVeryLow(void) {
116  helper_TestServo(500);
117  }
118 
119  static void test_ServoVeryHigh(void) {
120  helper_TestServo(2500);
121  }
122 
123  static void test_pwmEven(void) {
124  helper_TestPWM(1000, 1000);
125  }
126 
127  static void test_pwmLongHigh(void) {
128  helper_TestPWM(2000, 100);
129  }
130 
131  static void test_pwmLongLow(void) {
132  helper_TestPWM(300, 1800);
133  }
134 
135  static void test_pwmPrimeTimes(void) {
136  helper_TestPWM(773, 907);
137 
138  }
139 
140 };
141 
142 
143