gpios rework (#739)

* Some more include fixes for linux

* Pass cross-compile path through CROSS_COMPILE

* Lower-upper case conversion fixes for linux

* Rename unmarkPin to brain_pin_markUnused

This is part of external gpiochip integration

* Add gpio_pin prefix to markUsed and markUnused

To distinguish from similar functions using brain_pin

* pin_repository: add helpers

this is part of gpio chips integration

* efi_gpio: simplify getHwPin using brain_pin_is_onchip
This commit is contained in:
dron0gus 2019-04-10 02:31:10 +03:00 committed by rusefi
parent 572d04a799
commit c6a1d0cbe0
14 changed files with 90 additions and 78 deletions

View File

@ -130,7 +130,7 @@ void startAuxPins(void) {
void stopAuxPins(void) {
for (int i = 0;i < AUX_PID_COUNT;i++) {
unmarkPin(activeConfiguration.auxPidPins[i]);
brain_pin_markUnused(activeConfiguration.auxPidPins[i]);
}
}

View File

@ -358,10 +358,10 @@ bool isETBRestartNeeded(void) {
}
void stopETBPins(void) {
unmarkPin(activeConfiguration.bc.etb1.controlPin1);
unmarkPin(activeConfiguration.bc.etb1.controlPin2);
unmarkPin(activeConfiguration.bc.etb1.directionPin1);
unmarkPin(activeConfiguration.bc.etb1.directionPin2);
brain_pin_markUnused(activeConfiguration.bc.etb1.controlPin1);
brain_pin_markUnused(activeConfiguration.bc.etb1.controlPin2);
brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin1);
brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin2);
}
void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) {

View File

@ -790,6 +790,9 @@ typedef enum __attribute__ ((__packed__)) {
} brain_pin_e;
/* Plase keep updating this define */
#define BRAIN_PIN_LAST_ONCHIP GPIOH_15
/**
* https://rusefi.com//wiki/index.php?title=Manual:Debug_fields
*/

View File

@ -237,7 +237,7 @@ void OutputPin::unregisterOutput(brain_pin_e oldPin, brain_pin_e newPin) {
if (oldPin != GPIO_UNASSIGNED && oldPin != newPin) {
scheduleMsg(logger, "unregistering %s", hwPortname(oldPin));
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
unmarkPin(oldPin);
brain_pin_markUnused(oldPin);
port = NULL;
#endif /* EFI_GPIO_HARDWARE */
}

View File

@ -474,14 +474,13 @@ const char *portname(ioportid_t GPIOx) {
/**
* this method returns the numeric part of pin name. For instance, for PC13 this would return '13'
*/
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin) {
if (brainPin == GPIO_UNASSIGNED || brainPin == GPIO_INVALID)
return EFI_ERROR_CODE;
if (brainPin < GPIOA_0 || brainPin > GPIOH_15) {
firmwareError(CUSTOM_ERR_INVALID_PIN, "%s: Invalid brain_pin_e: %d", msg, brainPin);
return EFI_ERROR_CODE;
}
return (brainPin - GPIOA_0) % PORT_SIZE;
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
{
if (brain_pin_is_onchip(brainPin))
return (brainPin - GPIOA_0) % PORT_SIZE;
firmwareError(CUSTOM_ERR_INVALID_PIN, "%s: Invalid on-chip brain_pin_e: %d", msg, brainPin);
return EFI_ERROR_CODE;
}
#else /* EFI_GPIO_HARDWARE */

View File

@ -303,8 +303,8 @@ void enableFrankensoCan(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
}
void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
unmarkPin(activeConfiguration.bc.canTxPin);
unmarkPin(activeConfiguration.bc.canRxPin);
brain_pin_markUnused(activeConfiguration.bc.canTxPin);
brain_pin_markUnused(activeConfiguration.bc.canRxPin);
}
void startCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {

View File

@ -232,7 +232,7 @@ void removeWaveAnalyzerDriver(const char *msg, brain_pin_e brainPin) {
if (brainPin == GPIO_UNASSIGNED) {
return;
}
unmarkPin(brainPin);
brain_pin_markUnused(brainPin);
ICUDriver *driver = getInputCaptureDriver(msg, brainPin);
if (driver == NULL) {

View File

@ -200,12 +200,12 @@ static int tle6240_chip_init(struct tle6240_priv *chip)
const struct tle6240_config *cfg = chip->cfg;
/* mark pins used */
ret = markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS");
ret = gpio_pin_markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS");
if (cfg->reset.port != NULL)
ret |= markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST");
ret |= gpio_pin_markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST");
for (n = 0; n < TLE6240_DIRECT_OUTPUTS; n++)
if (cfg->direct_io[n].port)
ret |= markUsed(cfg->direct_io[n].port, cfg->direct_io[n].pad, DRIVER_NAME " DIRECT IO");
ret |= gpio_pin_markUsed(cfg->direct_io[n].port, cfg->direct_io[n].pad, DRIVER_NAME " DIRECT IO");
if (ret) {
ret = -1;
@ -289,12 +289,12 @@ static int tle6240_chip_init(struct tle6240_priv *chip)
err_gpios:
/* unmark pins */
markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad);
gpio_pin_markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad);
if (cfg->reset.port != NULL)
markUnused(cfg->reset.port, cfg->reset.pad);
gpio_pin_markUnused(cfg->reset.port, cfg->reset.pad);
for (n = 0; n < TLE6240_DIRECT_OUTPUTS; n++)
if (cfg->direct_io[n].port)
markUnused(cfg->direct_io[n].port, cfg->direct_io[n].pad);
gpio_pin_markUnused(cfg->direct_io[n].port, cfg->direct_io[n].pad);
return ret;
}

View File

@ -166,12 +166,12 @@ int tle8888_chip_init(void * data)
int ret = 0;
/* mark pins used */
// we do not initialize CS pin so we should not be marking it used
// ret = markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS");
// ret = gpio_pin_markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS");
if (cfg->reset.port != NULL)
ret |= markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST");
ret |= gpio_pin_markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST");
for (i = 0; i < TLE8888_DIRECT_MISC; i++)
if (cfg->direct_io[i].port)
ret |= markUsed(cfg->direct_io[i].port, cfg->direct_io[i].pad, DRIVER_NAME " DIRECT IO");
ret |= gpio_pin_markUsed(cfg->direct_io[i].port, cfg->direct_io[i].pad, DRIVER_NAME " DIRECT IO");
if (ret) {
ret = -1;
@ -236,12 +236,12 @@ int tle8888_chip_init(void * data)
err_gpios:
/* unmark pins */
//markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad);
//gpio_pin_markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad);
if (cfg->reset.port != NULL)
markUnused(cfg->reset.port, cfg->reset.pad);
gpio_pin_markUnused(cfg->reset.port, cfg->reset.pad);
for (i = 0; i < TLE8888_DIRECT_MISC; i++)
if (cfg->direct_io[i].port)
markUnused(cfg->direct_io[i].port, cfg->direct_io[i].pad);
gpio_pin_markUnused(cfg->direct_io[i].port, cfg->direct_io[i].pad);
return ret;
}

View File

@ -244,7 +244,7 @@ void turnOnHardware(Logging *sharedLogger) {
static void unregisterPin(brain_pin_e currentPin, brain_pin_e prevPin) {
if (currentPin != prevPin) {
unmarkPin(prevPin);
brain_pin_markUnused(prevPin);
}
}
@ -253,9 +253,9 @@ void stopSpi(spi_device_e device) {
if (!isSpiInitialized[device])
return; // not turned on
isSpiInitialized[device] = false;
unmarkPin(getSckPin(device));
unmarkPin(getMisoPin(device));
unmarkPin(getMosiPin(device));
brain_pin_markUnused(getSckPin(device));
brain_pin_markUnused(getMisoPin(device));
brain_pin_markUnused(getMosiPin(device));
#endif /* HAL_USE_SPI */
}
@ -420,7 +420,7 @@ void initHardware(Logging *l) {
isBoardTestMode_b = (!efiReadPin(CONFIGB(boardTestModeJumperPin)));
// we can now relese this pin, it is actually used as output sometimes
unmarkPin(CONFIGB(boardTestModeJumperPin));
brain_pin_markUnused(CONFIGB(boardTestModeJumperPin));
} else {
isBoardTestMode_b = false;
}

View File

@ -57,7 +57,7 @@ void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode) {
scheduleMsg(&logger, "%s on %s%d", msg, portname(port), pin);
bool wasUsed = markUsed(port, pin, msg);
bool wasUsed = gpio_pin_markUsed(port, pin, msg);
if (wasUsed) {
return;
}

View File

@ -66,6 +66,11 @@ static int brainPin_to_index(brain_pin_e brainPin)
return index;
}
static brain_pin_e index_to_brainPin(int i)
{
return (brain_pin_e)((int)GPIOA_0 + i);
}
PinRepository::PinRepository() {
}
@ -181,16 +186,25 @@ static int getIndex(ioportid_t port, ioportmask_t pin) {
bool brain_pin_is_onchip(brain_pin_e brainPin)
{
if ((brainPin < GPIOA_0) || (brainPin > GPIOH_15))
if ((brainPin < GPIOA_0) || (brainPin > BRAIN_PIN_LAST_ONCHIP))
return false;
return true;
}
bool brain_pin_is_ext(brain_pin_e brainPin)
{
if (brainPin > BRAIN_PIN_LAST_ONCHIP)
return true;
return false;
}
/**
* See also unmarkPin()
* See also brain_pin_markUnused()
* @return true if this pin was already used, false otherwise
*/
bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg)
{
int index;
@ -220,7 +234,34 @@ bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg)
return false;
}
bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg) {
/**
* See also brain_pin_markUsed()
*/
void brain_pin_markUnused(brain_pin_e brainPin)
{
int index;
if (!initialized) {
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
return;
}
index = brainPin_to_index(brainPin);
if (index < 0)
return;
if (PIN_USED[index] != NULL)
totalPinsUsed--;
PIN_USED[index] = NULL;
}
/**
* Marks on-chip gpio port-pin as used. Works only for on-chip gpios
* To be replaced with brain_pin_markUsed later
*/
bool gpio_pin_markUsed(ioportid_t port, ioportmask_t pin, const char *msg) {
if (!initialized) {
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
return false;
@ -242,28 +283,11 @@ bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg) {
}
/**
* See also markUsed()
* Marks on-chip gpio port-pin as UNused. Works only for on-chip gpios
* To be replaced with brain_pin_markUnused later
*/
void brain_pin_markUnused(brain_pin_e brainPin)
{
int index;
if (!initialized) {
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
return;
}
index = brainPin_to_index(brainPin);
if (index < 0)
return;
if (PIN_USED[index] != NULL)
totalPinsUsed--;
PIN_USED[index] = NULL;
}
void markUnused(ioportid_t port, ioportmask_t pin) {
void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin) {
if (!initialized) {
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
return;
@ -285,20 +309,4 @@ const char *getPinFunction(brain_input_pin_e brainPin) {
return PIN_USED[index];
}
void unmarkPin(brain_pin_e brainPin) {
if (brainPin == GPIO_UNASSIGNED) {
return;
}
ioportid_t port = getHwPort("unmark", brainPin);
ioportmask_t pin = getHwPin("unmark", brainPin);
int index = getIndex(port, pin);
if (PIN_USED[index] != NULL) {
PIN_USED[index] = NULL;
totalPinsUsed--;
}
}
#endif

View File

@ -29,11 +29,13 @@ class PinRepository {
void initPinRepository(void);
EXTERNC bool brain_pin_is_onchip(brain_pin_e brainPin);
EXTERNC bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg);
EXTERNC bool brain_pin_is_ext(brain_pin_e brainPin);
EXTERNC bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg);
EXTERNC void markUnused(ioportid_t port, ioportmask_t pin);
EXTERNC void brain_pin_markUnused(brain_pin_e brainPin);
const char * getPinFunction(brain_input_pin_e brainPin);
void unmarkPin(brain_pin_e brainPin);
/* For on-chip gpios only */
EXTERNC bool gpio_pin_markUsed(ioportid_t port, ioportmask_t pin, const char *msg);
EXTERNC void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin);
#endif /* PIN_REPOSITORY_H_ */

View File

@ -146,7 +146,7 @@ static void turnOffTriggerInputPin(brain_pin_e hwPin) {
icuStopCapture(driver);
icuStop(driver);
scheduleMsg(logger, "turnOffTriggerInputPin %s", hwPortname(hwPin));
unmarkPin(hwPin);
brain_pin_markUnused(hwPin);
}
}