2018-12-08 15:11:28 -08:00
|
|
|
/*
|
|
|
|
* test_pwm_generator.cpp
|
|
|
|
*
|
|
|
|
* @date Dec 8, 2018
|
2022-01-05 17:12:38 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2018-12-08 15:11:28 -08:00
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 20:12:37 -08:00
|
|
|
#define LOW_VALUE 0
|
|
|
|
#define HIGH_VALUE 1
|
|
|
|
|
2018-12-08 15:11:28 -08:00
|
|
|
extern int timeNowUs;
|
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
static int expectedTimeOfNextEvent;
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor *executor, OutputPin& pin) {
|
2018-12-08 19:04:55 -08:00
|
|
|
printf("PWM_test: Asserting event [%s]\r\n", msg);
|
2018-12-08 16:15:24 -08:00
|
|
|
// only one action expected in queue
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 1, executor->size()) << "PWM_test: schedulingQueue size";
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
// move time to next event timestamp
|
2018-12-08 19:04:55 -08:00
|
|
|
timeNowUs = expectedTimeOfNextEvent;
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
// execute pending actions and assert that only one action was executed
|
2019-07-24 21:28:04 -07:00
|
|
|
ASSERT_NEAR(1, executor->executeAll(timeNowUs), 0) << msg << " executed";
|
2021-05-07 06:38:41 -07:00
|
|
|
ASSERT_NEAR(expectedPinState, pin.currentLogicValue, 0) << msg << " pin state";
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
// assert that we have one new action in queue
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 1, executor->size()) << "PWM_test: queue.size";
|
2018-12-08 15:11:28 -08:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
static void test100dutyCycle() {
|
2021-03-26 14:23:26 -07:00
|
|
|
printf("*************************************** test100dutyCycle\r\n");
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2021-09-05 02:56:59 -07:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
OutputPin pin;
|
2021-09-05 02:56:59 -07:00
|
|
|
SimplePwm pwm("test PWM1");
|
|
|
|
TestExecutor executor;
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
startSimplePwm(&pwm, "unit_test",
|
2019-01-09 05:50:51 -08:00
|
|
|
&executor,
|
2018-12-08 19:57:00 -08:00
|
|
|
&pin,
|
|
|
|
1000 /* frequency */,
|
2021-05-07 06:38:41 -07:00
|
|
|
1.0 /* duty cycle */);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("1@1000/100", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@100", HIGH_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec2@100", HIGH_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec3@100", HIGH_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testSwitchToNanPeriod() {
|
2021-03-26 14:23:26 -07:00
|
|
|
printf("*************************************** testSwitchToNanPeriod\r\n");
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2021-09-05 02:56:59 -07:00
|
|
|
|
2018-12-08 15:11:28 -08:00
|
|
|
OutputPin pin;
|
2021-09-05 02:56:59 -07:00
|
|
|
SimplePwm pwm("test PWM1");
|
|
|
|
TestExecutor executor;
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
startSimplePwm(&pwm, "unit_test",
|
2019-01-09 05:50:51 -08:00
|
|
|
&executor,
|
2018-12-08 19:57:00 -08:00
|
|
|
&pin,
|
|
|
|
1000 /* frequency */,
|
2021-05-07 06:38:41 -07:00
|
|
|
0.60 /* duty cycle */);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 600;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("1@1000/70", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@70", LOW_VALUE, &executor, pin);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 600, timeNowUs) << "time1";
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 400;
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec2@70", HIGH_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
pwm.setFrequency(NAN);
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 600;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("1@1000/NAN", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec2@NAN", LOW_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += MS2US(NAN_FREQUENCY_SLEEP_PERIOD_MS);
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("2@1000/NAN", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec3@NAN", LOW_VALUE, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
}
|
|
|
|
|
2022-02-02 13:36:35 -08:00
|
|
|
TEST(PWM, testPwmGenerator) {
|
2018-12-08 19:57:00 -08:00
|
|
|
test100dutyCycle();
|
|
|
|
testSwitchToNanPeriod();
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2021-09-05 02:56:59 -07:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
OutputPin pin;
|
2021-09-05 02:56:59 -07:00
|
|
|
SimplePwm pwm("test PWM3");
|
|
|
|
TestExecutor executor;
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2019-01-09 05:50:51 -08:00
|
|
|
startSimplePwm(&pwm,
|
|
|
|
"unit_test",
|
|
|
|
&executor,
|
2018-12-08 15:11:28 -08:00
|
|
|
&pin,
|
2018-12-08 16:15:24 -08:00
|
|
|
1000 /* frequency */,
|
2021-05-07 06:38:41 -07:00
|
|
|
0.80 /* duty cycle */);
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 800;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("1@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@0", LOW_VALUE, &executor, pin);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 800, timeNowUs) << "time1";
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 200;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("2@1000/80", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
// above we had vanilla duty cycle, now let's handle a special case
|
2018-12-08 16:15:24 -08:00
|
|
|
pwm.setSimplePwmDutyCycle(0);
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("2@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 19:04:55 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@1", LOW_VALUE, &executor, pin);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 1000, timeNowUs) << "time2";
|
2018-12-08 19:04:55 -08:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("3@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 19:04:55 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@2", LOW_VALUE /* pin value */, &executor, pin);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 2000, timeNowUs) << "time3";
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("4@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@3", LOW_VALUE /* pin value */, &executor, pin);
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 3000, timeNowUs) << "time4";
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("5@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@4", LOW_VALUE /* pin value */, &executor, pin);
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("6@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@5", LOW_VALUE /* pin value */, &executor, pin);
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-14 15:38:20 -08:00
|
|
|
ASSERT_EQ( 5000, timeNowUs) << "time4";
|
2019-01-09 13:42:34 -08:00
|
|
|
assertEqualsM2("7@1000/0", expectedTimeOfNextEvent, executor.getForUnitTest(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2021-05-07 06:38:41 -07:00
|
|
|
assertNextEvent("exec@6", LOW_VALUE /* pin value */, &executor, pin);
|
2018-12-08 15:11:28 -08:00
|
|
|
}
|