libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
pwm2.test.h
Go to the documentation of this file.
1 #include "unity.h"
4 #include "libpropeller/board/board_unit_tester.h"
5 #include "c++-alloc.h"
6 
8 
9 PWM2 * sut;
10 
11 const int kFrequency = 1000;
12 
13 const int outputPin = Board::kPinTie1a;
14 const int inputPinMask = 1 << Board::kPinTie1b;
15 
16 class UnityTests {
17 public:
18 
19  static int MicrosecondsToClockCycles(const int microseconds) {
20  return (CLKFREQ * microseconds) / 1000000;
21  }
22 
23  static int ClockCyclesToMicroseconds(const int clockCycles) {
24  return clockCycles / (CLKFREQ / 1000000);
25  }
26 
27  static void setUp(void) {
28  sut = new PWM2();
29  sut->Start();
30  sut->SetPinX(outputPin);
32 
33  pwr = new PulseWidthReader();
34  pwr->Start(inputPinMask);
35 
36  waitcnt(CLKFREQ / 10 + CNT);
37 
38  }
39 
40  static void tearDown(void) {
41  sut->Stop();
42  delete sut;
43  sut = NULL;
44 
45  pwr->Stop();
46  delete pwr;
47  pwr = NULL;
48 
49  }
50 
51  static void helper_CheckWidths(const int highMicroseconds, const int lowMicroseconds) {
52  TEST_ASSERT_INT_WITHIN(3, highMicroseconds,
53  ClockCyclesToMicroseconds(pwr->getHighTime(0)));
54 
55  TEST_ASSERT_INT_WITHIN(3, lowMicroseconds,
56  ClockCyclesToMicroseconds(pwr->getLowTime(0)));
57  }
58 
59  static void helper_TestDuty(const int percent) {
60 
61 
62 
63  const int periodUs = 1000000 / kFrequency;
64  sut->SetDutyX(percent);
65  waitcnt(CLKFREQ / 10 + CNT);
66 
67  //while(true){}
68 
69  helper_CheckWidths((periodUs * percent) / 100, (periodUs * (100 - percent)) / 100);
70  }
71 
72  static void test_80PercentDuty(void) {
73  helper_TestDuty(80);
74  }
75 
76  static void test_20PercentDuty(void) {
77  helper_TestDuty(20);
78 
79  }
80 
81  static void test_50PercentDuty(void) {
82  helper_TestDuty(50);
83  }
84 
85 };
86