2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file io_pins.cpp
|
|
|
|
* @brief It could be that the main purpose of this file is the status LED blinking
|
|
|
|
*
|
|
|
|
* @date Jan 24, 2013
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
#include "main.h"
|
|
|
|
#include "io_pins.h"
|
|
|
|
#include "efiGpio.h"
|
|
|
|
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "gpio_helper.h"
|
|
|
|
#include "status_loop.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "console_io.h"
|
|
|
|
|
|
|
|
#if EFI_ENGINE_CONTROL || defined(__DOXYGEN__)
|
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|
|
|
|
|
|
|
|
extern board_configuration_s *boardConfiguration;
|
|
|
|
|
|
|
|
static LoggingWithStorage logger("io_pins");
|
|
|
|
|
|
|
|
|
2016-11-03 20:02:58 -07:00
|
|
|
extern EnginePins enginePins;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if defined(STM32F4XX)
|
2016-02-04 09:01:41 -08:00
|
|
|
static ioportid_t PORTS[] = { GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH };
|
2015-07-10 06:01:56 -07:00
|
|
|
#else
|
2016-02-04 09:01:41 -08:00
|
|
|
static ioportid_t PORTS[] = { GPIOA, GPIOB, GPIOC, GPIOD, GPIOF};
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pin_output_mode_e DEFAULT_OUTPUT = OM_DEFAULT;
|
2015-07-12 19:03:34 -07:00
|
|
|
pin_output_mode_e OPENDRAIN_OUTPUT = OM_OPENDRAIN;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-10-31 18:01:32 -07:00
|
|
|
/**
|
|
|
|
* This method is used for digital GPIO pins only, for peripheral pins see mySetPadMode
|
|
|
|
*/
|
2016-02-04 09:01:41 -08:00
|
|
|
static void outputPinRegisterExt(const char *msg, OutputPin *output, ioportid_t port, uint32_t pin,
|
2015-07-10 06:01:56 -07:00
|
|
|
pin_output_mode_e *outputMode) {
|
|
|
|
#if EFI_GPIO
|
|
|
|
if (port == GPIO_NULL) {
|
|
|
|
// that's for GRIO_NONE
|
|
|
|
output->port = port;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertOMode(*outputMode);
|
|
|
|
iomode_t mode = (*outputMode == OM_DEFAULT || *outputMode == OM_INVERTED) ?
|
|
|
|
PAL_MODE_OUTPUT_PUSHPULL :
|
|
|
|
PAL_MODE_OUTPUT_OPENDRAIN;
|
|
|
|
|
|
|
|
initOutputPinExt(msg, output, port, pin, mode);
|
|
|
|
|
|
|
|
output->setDefaultPinState(outputMode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-02-04 09:01:41 -08:00
|
|
|
ioportid_t getHwPort(brain_pin_e brainPin) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (brainPin == GPIO_UNASSIGNED)
|
|
|
|
return GPIO_NULL;
|
|
|
|
if (brainPin > GPIO_UNASSIGNED || brainPin < 0) {
|
2016-10-10 13:02:39 -07:00
|
|
|
firmwareError(CUSTOM_ERR_INVALID_PIN, "Invalid brain_pin_e: %d", brainPin);
|
2015-07-10 06:01:56 -07:00
|
|
|
return GPIO_NULL;
|
|
|
|
}
|
|
|
|
return PORTS[brainPin / PORT_SIZE];
|
|
|
|
}
|
|
|
|
|
|
|
|
ioportmask_t getHwPin(brain_pin_e brainPin) {
|
|
|
|
if (brainPin == GPIO_UNASSIGNED)
|
|
|
|
return EFI_ERROR_CODE;
|
|
|
|
if (brainPin > GPIO_UNASSIGNED || brainPin < 0) {
|
2016-10-10 13:02:39 -07:00
|
|
|
firmwareError(CUSTOM_ERR_INVALID_PIN, "Invalid brain_pin_e: %d", brainPin);
|
2015-07-10 06:01:56 -07:00
|
|
|
return EFI_ERROR_CODE;
|
|
|
|
}
|
|
|
|
return brainPin % PORT_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void outputPinRegisterExt2(const char *msg, OutputPin *output, brain_pin_e brainPin, pin_output_mode_e *outputMode) {
|
|
|
|
if (brainPin == GPIO_UNASSIGNED)
|
|
|
|
return;
|
2016-02-04 09:01:41 -08:00
|
|
|
ioportid_t hwPort = getHwPort(brainPin);
|
2015-07-10 06:01:56 -07:00
|
|
|
int hwPin = getHwPin(brainPin);
|
|
|
|
|
|
|
|
outputPinRegisterExt(msg, output, hwPort, hwPin, outputMode);
|
|
|
|
}
|
|
|
|
|
2016-02-04 09:01:41 -08:00
|
|
|
void outputPinRegister(const char *msg, OutputPin *output, ioportid_t port, uint32_t pin) {
|
2015-07-10 06:01:56 -07:00
|
|
|
outputPinRegisterExt(msg, output, port, pin, &DEFAULT_OUTPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initPrimaryPins(void) {
|
2016-12-27 08:01:26 -08:00
|
|
|
outputPinRegister("led: ERROR status", &enginePins.errorLedPin, LED_ERROR_PORT, LED_ERROR_PIN);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void initOutputPins(void) {
|
|
|
|
/**
|
|
|
|
* want to make sure it's all zeros so that we can compare in initOutputPinExt() method
|
|
|
|
*/
|
|
|
|
// todo: it's too late to clear now? this breaks default status LEDs
|
|
|
|
// todo: fix this?
|
|
|
|
// memset(&outputs, 0, sizeof(outputs));
|
|
|
|
// outputPinRegister("ext led 1", LED_EXT_1, EXTRA_LED_1_PORT, EXTRA_LED_1_PIN);
|
|
|
|
// outputPinRegister("ext led 2", LED_EXT_2, EXTRA_LED_2_PORT, EXTRA_LED_2_PIN);
|
|
|
|
// outputPinRegister("ext led 3", LED_EXT_3, EXTRA_LED_2_PORT, EXTRA_LED_3_PIN);
|
|
|
|
// outputPinRegister("alive1", LED_DEBUG, GPIOD, 6);
|
|
|
|
|
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
2016-09-14 16:03:00 -07:00
|
|
|
outputPinRegisterExt2("spi CS5", &enginePins.sdCsPin, boardConfiguration->sdCardCsPin, &DEFAULT_OUTPUT);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// todo: should we move this code closer to the fuel pump logic?
|
|
|
|
outputPinRegisterExt2("fuel pump relay", &enginePins.fuelPumpRelay, boardConfiguration->fuelPumpPin, &DEFAULT_OUTPUT);
|
|
|
|
|
|
|
|
outputPinRegisterExt2("main relay", &enginePins.mainRelay, boardConfiguration->mainRelayPin, &boardConfiguration->mainRelayPinMode);
|
|
|
|
|
|
|
|
outputPinRegisterExt2("fan relay", &enginePins.fanRelay, boardConfiguration->fanPin, &DEFAULT_OUTPUT);
|
|
|
|
outputPinRegisterExt2("o2 heater", &enginePins.o2heater, boardConfiguration->o2heaterPin, &DEFAULT_OUTPUT);
|
|
|
|
outputPinRegisterExt2("A/C relay", &enginePins.acRelay, boardConfiguration->acRelayPin, &boardConfiguration->acRelayPinMode);
|
|
|
|
|
|
|
|
// digit 1
|
|
|
|
/*
|
|
|
|
ledRegister(LED_HUGE_0, GPIOB, 2);
|
|
|
|
ledRegister(LED_HUGE_1, GPIOE, 7);
|
|
|
|
ledRegister(LED_HUGE_2, GPIOE, 8);
|
|
|
|
ledRegister(LED_HUGE_3, GPIOE, 9);
|
|
|
|
ledRegister(LED_HUGE_4, GPIOE, 10);
|
|
|
|
ledRegister(LED_HUGE_5, GPIOE, 11);
|
|
|
|
ledRegister(LED_HUGE_6, GPIOE, 12);
|
|
|
|
|
|
|
|
// digit 2
|
|
|
|
ledRegister(LED_HUGE_7, GPIOE, 13);
|
|
|
|
ledRegister(LED_HUGE_8, GPIOE, 14);
|
|
|
|
ledRegister(LED_HUGE_9, GPIOE, 15);
|
|
|
|
ledRegister(LED_HUGE_10, GPIOB, 10);
|
|
|
|
ledRegister(LED_HUGE_11, GPIOB, 11);
|
|
|
|
ledRegister(LED_HUGE_12, GPIOB, 12);
|
|
|
|
ledRegister(LED_HUGE_13, GPIOB, 13);
|
|
|
|
|
|
|
|
// digit 3
|
|
|
|
ledRegister(LED_HUGE_14, GPIOE, 0);
|
|
|
|
ledRegister(LED_HUGE_15, GPIOE, 2);
|
|
|
|
ledRegister(LED_HUGE_16, GPIOE, 4);
|
|
|
|
ledRegister(LED_HUGE_17, GPIOE, 6);
|
|
|
|
ledRegister(LED_HUGE_18, GPIOE, 5);
|
|
|
|
ledRegister(LED_HUGE_19, GPIOE, 3);
|
|
|
|
ledRegister(LED_HUGE_20, GPIOE, 1);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_GPIO
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is part of fatal error handling.
|
|
|
|
* Please note that worst case scenario the pins might get re-enabled by some other code :(
|
|
|
|
* The whole method is pretty naive, but that's at least something.
|
|
|
|
*/
|
|
|
|
void turnAllPinsOff(void) {
|
|
|
|
for (int i = 0; i < INJECTION_PIN_COUNT; i++) {
|
|
|
|
enginePins.injectors[i].setValue(false);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < IGNITION_PIN_COUNT; i++) {
|
|
|
|
enginePins.coils[i].setValue(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|