From 51902c12c50a3bdbd97acba2de5c9d43f21d9b65 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 16 Aug 2019 22:56:06 -0400 Subject: [PATCH] TLE8888 should be re-initialized every time we get +12 volts #901 moving SPI initialization to thread - still works on real hardware --- firmware/hw_layer/drivers/gpio/tle8888.c | 38 +++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/firmware/hw_layer/drivers/gpio/tle8888.c b/firmware/hw_layer/drivers/gpio/tle8888.c index b57ac0461d..5bd430361c 100644 --- a/firmware/hw_layer/drivers/gpio/tle8888.c +++ b/firmware/hw_layer/drivers/gpio/tle8888.c @@ -276,16 +276,31 @@ static int tle8888_wake_driver(struct tle8888_priv *chip) /* Driver thread. */ /*==========================================================================*/ -static THD_FUNCTION(tle8888_driver_thread, p) -{ - msg_t msg; +int tle8888SpiStartupExchange(void * data); +static bool needInitialSpi = true; + +float vBattForTle8888 = 14; +static void *poorPointerNeedToDoBetter = NULL; + +static THD_FUNCTION(tle8888_driver_thread, p) { (void)p; chRegSetThreadName(DRIVER_NAME); while (1) { - msg = chSemWaitTimeout(&tle8888_wake, TIME_MS2I(TLE8888_POLL_INTERVAL_MS)); + msg_t msg = chSemWaitTimeout(&tle8888_wake, TIME_MS2I(TLE8888_POLL_INTERVAL_MS)); + + if (vBattForTle8888 < 7) { + // we assume TLE8888 is down and we should not bother with SPI communication + needInitialSpi = true; + } + + if (needInitialSpi) { + needInitialSpi = false; + tle8888SpiStartupExchange(poorPointerNeedToDoBetter); + } + /* should we care about msg == MSG_TIMEOUT? */ (void)msg; @@ -341,7 +356,7 @@ int tle8888_writePad(void *data, brain_pin_e pin, int value) { /** * @return 0 for valid configuration, -1 for invalid configuration */ -static int tle8888SpiStartupExchange(void * data) { +int tle8888SpiStartupExchange(void * data) { struct tle8888_priv *chip = (struct tle8888_priv *)data; const struct tle8888_config *cfg = chip->cfg; @@ -435,7 +450,8 @@ static int tle8888SpiStartupExchange(void * data) { return 0; } -int tle8888_chip_init(void * data) { +static int tle8888_chip_init(void * data) { + poorPointerNeedToDoBetter = data; struct tle8888_priv *chip = (struct tle8888_priv *)data; const struct tle8888_config *cfg = chip->cfg; @@ -454,11 +470,6 @@ int tle8888_chip_init(void * data) { goto err_gpios; } - ret = tle8888SpiStartupExchange(data); - if (ret) { - return ret; - } - err_gpios: /* unmark pins */ @@ -522,7 +533,6 @@ struct gpiochip_ops tle8888_ops = { */ int tle8888_add(unsigned int index, const struct tle8888_config *cfg) { - struct tle8888_priv *chip; efiAssert(OBD_PCM_Processor_Fault, cfg != NULL, "8888CFG", 0) @@ -530,12 +540,12 @@ int tle8888_add(unsigned int index, const struct tle8888_config *cfg) { if ((!cfg) || (!cfg->spi_bus) || (index >= BOARD_TLE8888_COUNT)) return -1; - /* check for valid cs. + /* check for valid chip select. * TODO: remove this check? CS can be driven by SPI */ if (cfg->spi_config.ssport == NULL) return -1; - chip = &chips[index]; + struct tle8888_priv *chip = &chips[index]; /* already initted? */ if (chip->cfg != NULL)