libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
pulse_width_reader.test.h
Go to the documentation of this file.
1 #include "unity.h"
3 #include "c++-alloc.h"
4 
5 #include "libpropeller/board/board_unit_tester.h"
6 #include "libpropeller/pin/pin.h"
7 
8 
9 //TODO(SRLM): Add tests for startup conditions: reading right after start, reading after a single transition
10 //TODO(SRLM): Add test for reading a pin that has never changed while other pins have.
11 
13 
14 const int kPulseMask = (1 << Board::kPinSqw)
15 | (1 << Board::kPinTie1b)
16 | (1 << Board::kPinTie2b);
17 
18 
19 Pin sqwOutputPin1(Board::kPinTie1a);
20 Pin sqwOutputPin2(Board::kPinTie2a);
21 
22 const int stacksize = sizeof (_thread_state_t) + sizeof (int) * 30;
24 
25 
26 const int kSqwIndex = 0;
27 const int k1Index = 1;
28 const int k2Index = 2;
29 
30 const int kDelta = 700;
31 
32 volatile bool runCog = false;
35 
36 class UnityTests {
37 public:
38 
39  static void setUp(void) {
42 
43  sut = new PulseWidthReader();
44  sut->Start(kPulseMask);
45  waitcnt(CLKFREQ / 100 + CNT);
46  }
47 
48  static void tearDown(void) {
49  sut->Stop();
50  delete sut;
51  sut = NULL;
52 
53  }
54 
55 
56  static void cog_RunSquareWaveOutput(void * ignored) {
57  unsigned int nextCNT = CNT;
58  while (runCog) {
60  nextCNT += highDuration;
61  waitcnt(nextCNT);
62 
63 
65  nextCNT += lowDuration;
66  waitcnt(nextCNT);
67  }
68 
69  cogstop(cogid());
70  }
71 
72  static void run_SquareWaveTest(const int highCycles, const int lowCycles) {
73  runCog = true;
74  highDuration = highCycles;
75  lowDuration = lowCycles;
76 
77  cogstart(cog_RunSquareWaveOutput, NULL, stack, stacksize);
78  waitcnt(CLKFREQ / 10 + CNT);
79 
80  TEST_ASSERT_INT_WITHIN(kDelta, highDuration, sut->getHighTime(k1Index));
81  TEST_ASSERT_INT_WITHIN(kDelta, lowDuration, sut->getLowTime(k1Index));
82 
83  runCog = false;
84  waitcnt(CLKFREQ / 10 + CNT);
85  }
86 
87  //------------------------------------------------------------------------------
88 
89  static void test_EvenSquareWave(void) {
90  run_SquareWaveTest(CLKFREQ / 1000, CLKFREQ / 1000);
91  }
92 
93  static void test_MostlyHighSquareWave(void) {
94  run_SquareWaveTest(CLKFREQ / 100, CLKFREQ / 1000);
95  }
96 
97  static void test_MostlyLowSquareWave(void) {
98  run_SquareWaveTest(CLKFREQ / 1000, CLKFREQ / 100);
99  }
100 
101  static void test_Something(void) {
102  run_SquareWaveTest(CLKFREQ / 393, CLKFREQ / 484);
103  }
104 
105 
106  //------------------------------------------------------------------------------
107 
108  static void test_SingleHighPulse(void) {
109  const int cycles = CLKFREQ / 1000;
110 
111  sqwOutputPin1.low();
112  waitcnt(CLKFREQ / 100 + CNT);
114  waitcnt(cycles + CNT);
115  sqwOutputPin1.low();
116  waitcnt(CLKFREQ / 100 + CNT);
117 
118  TEST_ASSERT_INT_WITHIN(kDelta, cycles, sut->getHighTime(k1Index));
119  }
120 
121  static void test_SingleLowPulse(void) {
122  const int cycles = CLKFREQ / 1000;
123 
125  waitcnt(CLKFREQ / 100 + CNT);
126  sqwOutputPin1.low();
127  waitcnt(cycles + CNT);
129  waitcnt(CLKFREQ / 100 + CNT);
130 
131  TEST_ASSERT_INT_WITHIN(kDelta, cycles, sut->getLowTime(k1Index));
132  }
133 
134  //------------------------------------------------------------------------------
135 
136  static void test_32768SquareWave(void) {
137  const int kClockFrequency = 32768;
138  TEST_ASSERT_INT_WITHIN(130, CLKFREQ / (2 * kClockFrequency), sut->getHighTime(kSqwIndex));
139  TEST_ASSERT_INT_WITHIN(130, CLKFREQ / (2 * kClockFrequency), sut->getLowTime(kSqwIndex));
140  }
141 
142 
143 
144 
145 
146 };