diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index 2617bf8a9a..68e2433781 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -30,7 +30,6 @@ void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockMafVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockIatVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); void setMockCltVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX); -void setMockState(brain_pin_e pin, bool state); void printCurrentState(Logging *logging, int seconds, const char *engineTypeName, const char *firmwareBuildId); diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index 394e94b354..3c926a8626 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -70,16 +70,6 @@ void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { void setMockVBattVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) { setMockVoltage(engineConfiguration->vbattAdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX); } - -void setMockState(brain_pin_e pin, bool state) { -#if EFI_UNIT_TEST - mockPinStates[static_cast(pin)] = state; -#else - UNUSED(pin); - UNUSED(state); -#endif -} - #endif /* EFI_ENABLE_MOCK_ADC */ #if EFI_PROD_CODE diff --git a/firmware/hw_layer/io_pins.cpp b/firmware/hw_layer/io_pins.cpp index a5f95a2d5a..dd4f373202 100644 --- a/firmware/hw_layer/io_pins.cpp +++ b/firmware/hw_layer/io_pins.cpp @@ -133,4 +133,9 @@ bool mockPinStates[BRAIN_PIN_COUNT]; bool efiReadPin(brain_pin_e pin) { return mockPinStates[static_cast(pin)]; } + +void setMockState(brain_pin_e pin, bool state) { + mockPinStates[static_cast(pin)] = state; +} + #endif /* EFI_PROD_CODE */ diff --git a/firmware/hw_layer/io_pins.h b/firmware/hw_layer/io_pins.h index db2aeae11b..978f6c5b8f 100644 --- a/firmware/hw_layer/io_pins.h +++ b/firmware/hw_layer/io_pins.h @@ -35,4 +35,6 @@ EXTERNC void efiIcuStart(const char *msg, ICUDriver *icup, const ICUConfig *conf #if ! EFI_PROD_CODE #define BRAIN_PIN_COUNT (1 << 8 * sizeof(brain_pin_e)) extern bool mockPinStates[BRAIN_PIN_COUNT]; + +void setMockState(brain_pin_e pin, bool state); #endif diff --git a/unit_tests/tests/test_gpio.cpp b/unit_tests/tests/test_gpio.cpp new file mode 100644 index 0000000000..dc269ffa30 --- /dev/null +++ b/unit_tests/tests/test_gpio.cpp @@ -0,0 +1,97 @@ +#include "efi_gpio.h" +#include "engine_test_helper.h" +#include "unit_test_framework.h" +#include + +TEST(gpio, testPinInitNonInverted) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + OutputPin dut; + + // Set the "hardware" pin to on initially + setMockState(GPIOA_6, true); + ASSERT_TRUE(efiReadPin(GPIOA_6)); + + // this should turn it off + pin_output_mode_e mode = OM_DEFAULT; + dut.initPin("test", GPIOA_6, &mode); + + EXPECT_FALSE(efiReadPin(GPIOA_6)); +} + +TEST(gpio, testPinInitInverted) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + OutputPin dut; + + // Set the "hardware" pin to off initially + setMockState(GPIOA_6, false); + ASSERT_FALSE(efiReadPin(GPIOA_6)); + + // this should turn it off + pin_output_mode_e mode = OM_INVERTED; + dut.initPin("test", GPIOA_6, &mode); + + EXPECT_TRUE(efiReadPin(GPIOA_6)); +} + +TEST(gpio, multipleInit) { + OutputPin dut; + + // Initial setup should be ok + EXPECT_NO_FATAL_ERROR(dut.initPin("testPin", GPIOA_6)); + + // Reinit with the same pin should be ok + EXPECT_NO_FATAL_ERROR(dut.initPin("testPin", GPIOA_6)); + + // Reinit with DIFFERENT pin should fail + EXPECT_FATAL_ERROR(dut.initPin("testPin", GPIOB_5)); +} + +TEST(gpio, deInit) { + OutputPin dut; + + // Initial setup should be ok + EXPECT_NO_FATAL_ERROR(dut.initPin("testPin", GPIOA_6)); + + dut.deInit(); + + // Reinit with DIFFERENT pin should work after deinit + EXPECT_NO_FATAL_ERROR(dut.initPin("testPin", GPIOB_5)); +} + +TEST(gpio, pinSetNotInverted) { + OutputPin dut; + + pin_output_mode_e mode = OM_DEFAULT; + dut.initPin("test", GPIOA_6, &mode); + + // Initial state should be logic 0 + EXPECT_FALSE(efiReadPin(GPIOA_6)); + + dut.setValue(true); + EXPECT_TRUE(efiReadPin(GPIOA_6)); + EXPECT_TRUE(dut.getLogicValue()); + + dut.setValue(false); + EXPECT_FALSE(efiReadPin(GPIOA_6)); + EXPECT_FALSE(dut.getLogicValue()); +} + +TEST(gpio, pinSetInverted) { + OutputPin dut; + + pin_output_mode_e mode = OM_INVERTED; + dut.initPin("test", GPIOA_6, &mode); + + // Initial state should be logic 0 + EXPECT_TRUE(efiReadPin(GPIOA_6)); + + dut.setValue(true); + EXPECT_FALSE(efiReadPin(GPIOA_6)); + EXPECT_TRUE(dut.getLogicValue()); + + dut.setValue(false); + EXPECT_TRUE(efiReadPin(GPIOA_6)); + EXPECT_FALSE(dut.getLogicValue()); +} diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index ff33fcfb53..8200b2bb72 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -63,4 +63,5 @@ TESTS_SRC_CPP = \ tests/test_fuel_math.cpp \ tests/test_binary_log.cpp \ tests/test_dynoview.cpp \ + tests/test_gpio.cpp \