diff --git a/firmware/controllers/algo/io_pins.h b/firmware/controllers/algo/io_pins.h index bc0c65d029..5742892e2c 100644 --- a/firmware/controllers/algo/io_pins.h +++ b/firmware/controllers/algo/io_pins.h @@ -16,6 +16,7 @@ * Logical pins. See brain_pin_e for physical pins. */ typedef enum { + IO_INVALID, LED_WARNING, // Orange on-board LED LED_RUNNING, // Green on-board LED LED_ERROR, // Red on-board LED @@ -128,6 +129,7 @@ extern "C" void initPrimaryPins(void); void initOutputPins(void); const char *getPinName(io_pin_e io_pin); +io_pin_e getPinByName(const char *name); void turnOutputPinOn(io_pin_e pin); void turnOutputPinOff(io_pin_e pin); void setOutputPinValue(io_pin_e pin, int logicValue); diff --git a/firmware/controllers/algo/signal_executor.c b/firmware/controllers/algo/signal_executor.c index 1b3af42582..4afa4840ac 100644 --- a/firmware/controllers/algo/signal_executor.c +++ b/firmware/controllers/algo/signal_executor.c @@ -117,6 +117,14 @@ void scheduleOutput(OutputSignal *signal, float delayMs, float durationMs) { scheduleTask("out down", sDown, (int)MS2US(delayMs + durationMs), (schfunc_t) &turnPinLow, (void*) signal->io_pin); } +io_pin_e getPinByName(const char *name) { + if(startsWith(name, "spa")) { + int index = atoi(name + 3); + return (io_pin_e)((int)SPARKOUT_1_OUTPUT - 1 + index); + } + return IO_INVALID; +} + const char *getPinName(io_pin_e io_pin) { switch (io_pin) { // todo: refactor this hell - introduce arrays & checks? diff --git a/firmware/hw_layer/io_pins.c b/firmware/hw_layer/io_pins.c index e09bda4b60..b1c8deb12a 100644 --- a/firmware/hw_layer/io_pins.c +++ b/firmware/hw_layer/io_pins.c @@ -23,6 +23,8 @@ extern board_configuration_s *boardConfiguration; +static Logging logger; + static pin_output_mode_e *pinDefaultState[IO_PIN_COUNT]; static OutputPin outputs[IO_PIN_COUNT]; static io_pin_e leds[] = { LED_WARNING, LED_RUNNING, LED_ERROR, LED_COMMUNICATION_1, LED_DEBUG, LED_EXT_1, @@ -59,7 +61,7 @@ inline static void assertOMode(pin_output_mode_e mode) { * @brief Sets the value according to current electrical settings */ void setOutputPinValue(io_pin_e pin, int logicValue) { - if (outputs[pin].port == GPIO_NULL ) + if (outputs[pin].port == GPIO_NULL) return; efiAssertVoid(pinDefaultState[pin]!=NULL, "pin mode not initialized"); pin_output_mode_e mode = *pinDefaultState[pin]; @@ -78,7 +80,7 @@ void setDefaultPinState(io_pin_e pin, pin_output_mode_e *outputMode) { } static void comBlinkingThread(void *arg) { - (void)arg; + (void) arg; chRegSetThreadName("communication blinking"); while (TRUE) { int delay; @@ -106,7 +108,7 @@ static void comBlinkingThread(void *arg) { int isTriggerDecoderError(void); static void errBlinkingThread(void *arg) { - (void)arg; + (void) arg; chRegSetThreadName("err blinking"); #if EFI_ENGINE_CONTROL while (TRUE) { @@ -123,16 +125,16 @@ static void errBlinkingThread(void *arg) { static void outputPinRegisterExt(const char *msg, io_pin_e ioPin, GPIO_TypeDef *port, uint32_t pin, pin_output_mode_e *outputMode) { efiAssertVoid((int)ioPin < IO_PIN_COUNT, "io pin out of range"); - if (port == GPIO_NULL ) { + if (port == GPIO_NULL) { // that's for GRIO_NONE outputs[ioPin].port = port; return; } assertOMode(*outputMode); - iomode_t mode = - (*outputMode == OM_DEFAULT || *outputMode == OM_INVERTED) ? - PAL_MODE_OUTPUT_PUSHPULL : PAL_MODE_OUTPUT_OPENDRAIN; + iomode_t mode = (*outputMode == OM_DEFAULT || *outputMode == OM_INVERTED) ? + PAL_MODE_OUTPUT_PUSHPULL : + PAL_MODE_OUTPUT_OPENDRAIN; initOutputPinExt(msg, &outputs[ioPin], port, pin, mode); @@ -141,10 +143,10 @@ static void outputPinRegisterExt(const char *msg, io_pin_e ioPin, GPIO_TypeDef * GPIO_TypeDef * getHwPort(brain_pin_e brainPin) { if (brainPin == GPIO_NONE) - return GPIO_NULL ; + return GPIO_NULL; if (brainPin > GPIO_NONE || brainPin < 0) { firmwareError("Invalid brain_pin_e: %d", brainPin); - return GPIO_NULL ; + return GPIO_NULL; } return PORTS[brainPin / 16]; } @@ -188,7 +190,19 @@ void initPrimaryPins(void) { outputPinRegister("LED_ERROR", LED_ERROR, LED_ERROR_PORT, LED_ERROR_PIN); } +static void getPinValue(const char *name) { + io_pin_e pin = getPinByName(name); + if (pin == IO_INVALID) { + return; + } + OutputPin * outputPin = &outputs[pin]; + int value = getLogicPinValue(outputPin); + scheduleMsg(&logger, "pin_value %s %d", name, value); +} + void initOutputPins(void) { + initLogging(&logger, "io_pins"); + outputPinRegister("warning", LED_WARNING, LED_WARNING_PORT, LED_WARNING_PIN); outputPinRegister("is running status", LED_RUNNING, LED_RUNNING_STATUS_PORT, LED_RUNNING_STATUS_PIN); outputPinRegister("communication status 1", LED_COMMUNICATION_1, LED_COMMUNICATION_PORT, LED_COMMUNICATION_PIN); @@ -254,6 +268,8 @@ void initOutputPins(void) { ledRegister(LED_HUGE_20, GPIOE, 1); */ - chThdCreateStatic(comBlinkingStack, sizeof(comBlinkingStack), NORMALPRIO, (tfunc_t) comBlinkingThread, NULL ); - chThdCreateStatic(errBlinkingStack, sizeof(errBlinkingStack), NORMALPRIO, (tfunc_t) errBlinkingThread, NULL ); + chThdCreateStatic(comBlinkingStack, sizeof(comBlinkingStack), NORMALPRIO, (tfunc_t) comBlinkingThread, NULL); + chThdCreateStatic(errBlinkingStack, sizeof(errBlinkingStack), NORMALPRIO, (tfunc_t) errBlinkingThread, NULL); + + addConsoleActionS("get_pin_value", getPinValue); } diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index 3f048dca9b..c961399f7e 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -53,8 +53,22 @@ uint32_t efiStrlen(const char *param) { return strlen(param); } +bool startsWith(const char *line, const char *prefix) { + int len = efiStrlen(prefix); + if(efiStrlen(line) < len) { + return false; + } + for(int i =0;i stack; assertEquals(0, stack.size()); - - +} + +void testMisc(void) { + assertEquals(true, strEqual("spa3", getPinName(SPARKOUT_3_OUTPUT))); + assertEquals(SPARKOUT_12_OUTPUT, getPinByName("spa12")); } diff --git a/unit_tests/test_util.h b/unit_tests/test_util.h index 72d24a8ff9..05983ef114 100644 --- a/unit_tests/test_util.h +++ b/unit_tests/test_util.h @@ -21,6 +21,7 @@ void testMalfunctionCentral(void); void testConsoleLogic(void); void testGpsParser(void); void testFLStack(void); +void testMisc(void); #ifdef __cplusplus }