2018-12-08 15:11:28 -08:00
|
|
|
/*
|
|
|
|
* test_pwm_generator.cpp
|
|
|
|
*
|
|
|
|
* @date Dec 8, 2018
|
|
|
|
* Author: user
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "unit_test_framework.h"
|
|
|
|
#include "event_queue.h"
|
|
|
|
#include "pwm_generator_logic.h"
|
|
|
|
|
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 EventQueue schedulingQueue;
|
|
|
|
extern int timeNowUs;
|
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
static int expectedTimeOfNextEvent;
|
2018-12-08 19:04:55 -08:00
|
|
|
static int pinValue = -1;
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 16:15:24 -08:00
|
|
|
static void testApplyPinState(PwmConfig *state, int stateIndex) {
|
2018-12-25 07:13:00 -08:00
|
|
|
pinValue = state->multiWave.getChannelState(/*channelIndex*/0, stateIndex);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
printf("PWM_test: setPinValue=%d @ timeNow=%d\r\n", pinValue, timeNowUs);
|
2018-12-08 16:15:24 -08:00
|
|
|
}
|
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
static void assertNextEvent(const char *msg, int expectedPinState, TestExecutor *executor) {
|
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-09 13:30:14 -08:00
|
|
|
assertEqualsM("PWM_test: schedulingQueue size", 1, executor->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
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM5(msg, " executed", 1, schedulingQueue.executeAll(timeNowUs), 0);
|
|
|
|
assertEqualsM5(msg, " pin state", expectedPinState, pinValue, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
// assert that we have one new action in queue
|
2019-01-09 13:30:14 -08:00
|
|
|
assertEqualsM("PWM_test: queue.size", 1, executor->size());
|
2018-12-08 15:11:28 -08:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
static void test100dutyCycle() {
|
|
|
|
print("*************************************** test100dutyCycle\r\n");
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2019-01-09 05:50:51 -08:00
|
|
|
TestExecutor executor;
|
|
|
|
SimplePwm pwm("test PWM1");
|
2018-12-08 19:57:00 -08:00
|
|
|
OutputPin pin;
|
2019-01-09 07:36:35 -08:00
|
|
|
executor.clear();
|
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 */,
|
|
|
|
1.0 /* duty cycle */,
|
|
|
|
&testApplyPinState);
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
|
|
|
assertEqualsM2("1@1000/100", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@100", HIGH_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec2@100", HIGH_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec3@100", HIGH_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testSwitchToNanPeriod() {
|
|
|
|
print("*************************************** testSwitchToNanPeriod\r\n");
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2019-01-09 05:50:51 -08:00
|
|
|
TestExecutor executor;
|
|
|
|
SimplePwm pwm("test PWM1");
|
2018-12-08 15:11:28 -08:00
|
|
|
OutputPin pin;
|
2019-01-09 07:36:35 -08:00
|
|
|
executor.clear();
|
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 */,
|
|
|
|
0.60 /* duty cycle */,
|
|
|
|
&testApplyPinState);
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 600;
|
|
|
|
assertEqualsM2("1@1000/70", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@70", LOW_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
assertEqualsM("time1", 600, timeNowUs);
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 400;
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec2@70", HIGH_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
pwm.setFrequency(NAN);
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent += 600;
|
|
|
|
assertEqualsM2("1@1000/NAN", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec2@NAN", LOW_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
|
|
|
|
expectedTimeOfNextEvent += MS2US(NAN_FREQUENCY_SLEEP_PERIOD_MS);
|
|
|
|
assertEqualsM2("2@1000/NAN", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec3@NAN", LOW_VALUE, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testPwmGenerator() {
|
|
|
|
test100dutyCycle();
|
|
|
|
testSwitchToNanPeriod();
|
|
|
|
|
|
|
|
print("*************************************** testPwmGenerator\r\n");
|
|
|
|
|
|
|
|
expectedTimeOfNextEvent = timeNowUs = 0;
|
2019-01-09 05:50:51 -08:00
|
|
|
TestExecutor executor;
|
|
|
|
SimplePwm pwm("test PWM3");
|
2018-12-08 19:57:00 -08:00
|
|
|
OutputPin pin;
|
2019-01-09 07:36:35 -08:00
|
|
|
executor.clear();
|
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 */,
|
2018-12-08 15:11:28 -08:00
|
|
|
0.80 /* duty cycle */,
|
|
|
|
&testApplyPinState);
|
|
|
|
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 800;
|
|
|
|
assertEqualsM2("1@1000/80", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 15:11:28 -08:00
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@0", LOW_VALUE, &executor);
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM("time1", 800, timeNowUs);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 200;
|
|
|
|
assertEqualsM2("2@1000/80", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(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);
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM2("2@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@1", LOW_VALUE, &executor);
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM("time2", 1000, timeNowUs);
|
|
|
|
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM2("3@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@2", LOW_VALUE /* pin value */, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
assertEqualsM("time3", 2000, timeNowUs);
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
|
|
|
assertEqualsM2("4@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@3", LOW_VALUE /* pin value */, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
assertEqualsM("time4", 3000, timeNowUs);
|
|
|
|
expectedTimeOfNextEvent += 1000;
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM2("5@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@4", LOW_VALUE /* pin value */, &executor);
|
2018-12-08 19:04:55 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
|
|
|
assertEqualsM2("6@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@5", LOW_VALUE /* pin value */, &executor);
|
2018-12-08 19:57:00 -08:00
|
|
|
expectedTimeOfNextEvent += 1000;
|
|
|
|
assertEqualsM("time4", 5000, timeNowUs);
|
2018-12-08 19:04:55 -08:00
|
|
|
assertEqualsM2("7@1000/0", expectedTimeOfNextEvent, schedulingQueue.getForUnitText(0)->momentX, 0);
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
|
2019-01-09 13:30:14 -08:00
|
|
|
assertNextEvent("exec@6", LOW_VALUE /* pin value */, &executor);
|
2018-12-08 15:11:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-08 16:15:24 -08:00
|
|
|
|
|
|
|
|