2 #include "libpropeller/pulsewidthreader/pulse_width_reader.h" 5 #include "libpropeller/board/board_unit_tester.h" 6 #include "libpropeller/pin/pin.h" 14 const int kPulseMask = (1 << libpropeller::Board::kPinSqw)
15 | (1 << libpropeller::Board::kPinTie1b)
16 | (1 << libpropeller::Board::kPinTie2b);
22 const int stacksize =
sizeof (_thread_state_t) +
sizeof (
int) * 30;
23 int stack [stacksize];
26 const int kSqwIndex = 0;
27 const int k1Index = 1;
28 const int k2Index = 2;
30 const int kDelta = 700;
32 volatile bool runCog =
false;
39 static void setUp(
void) {
40 sqwOutputPin1.
input();
41 sqwOutputPin2.
input();
44 sut->
Start(kPulseMask);
45 waitcnt(CLKFREQ / 100 + CNT);
48 static void tearDown(
void) {
56 static void cog_RunSquareWaveOutput(
void * ignored) {
57 unsigned int nextCNT = CNT;
60 nextCNT += highDuration;
65 nextCNT += lowDuration;
72 static void run_SquareWaveTest(
const int highCycles,
const int lowCycles) {
74 highDuration = highCycles;
75 lowDuration = lowCycles;
77 cogstart(cog_RunSquareWaveOutput, NULL, stack, stacksize);
78 waitcnt(CLKFREQ / 10 + CNT);
80 TEST_ASSERT_INT_WITHIN(kDelta, highDuration, sut->
getHighTime(k1Index));
81 TEST_ASSERT_INT_WITHIN(kDelta, lowDuration, sut->
getLowTime(k1Index));
84 waitcnt(CLKFREQ / 10 + CNT);
89 static void test_EvenSquareWave(
void) {
90 run_SquareWaveTest(CLKFREQ / 1000, CLKFREQ / 1000);
93 static void test_MostlyHighSquareWave(
void) {
94 run_SquareWaveTest(CLKFREQ / 100, CLKFREQ / 1000);
97 static void test_MostlyLowSquareWave(
void) {
98 run_SquareWaveTest(CLKFREQ / 1000, CLKFREQ / 100);
101 static void test_Something(
void) {
102 run_SquareWaveTest(CLKFREQ / 393, CLKFREQ / 484);
108 static void test_SingleHighPulse(
void) {
109 const int cycles = CLKFREQ / 1000;
112 waitcnt(CLKFREQ / 100 + CNT);
113 sqwOutputPin1.
high();
114 waitcnt(cycles + CNT);
116 waitcnt(CLKFREQ / 100 + CNT);
118 TEST_ASSERT_INT_WITHIN(kDelta, cycles, sut->
getHighTime(k1Index));
121 static void test_SingleLowPulse(
void) {
122 const int cycles = CLKFREQ / 1000;
124 sqwOutputPin1.
high();
125 waitcnt(CLKFREQ / 100 + CNT);
127 waitcnt(cycles + CNT);
128 sqwOutputPin1.
high();
129 waitcnt(CLKFREQ / 100 + CNT);
131 TEST_ASSERT_INT_WITHIN(kDelta, cycles, sut->
getLowTime(k1Index));
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));
void Start(unsigned int Inputmask)
int getHighTime(int index)
int getLowTime(int index)