reducing GPIO code duplication

This commit is contained in:
rusefillc 2023-05-24 10:37:02 -04:00
parent 458d8982aa
commit fa57f32a37
4 changed files with 47 additions and 101 deletions

View File

@ -795,4 +795,51 @@ const char *portname(ioportid_t GPIOx) {
return "unknown";
}
static int getPortIndex(ioportid_t port) {
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
if (port == GPIOA)
return 0;
if (port == GPIOB)
return 1;
if (port == GPIOC)
return 2;
if (port == GPIOD)
return 3;
#if defined(GPIOF)
if (port == GPIOE)
return 4;
#endif /* GPIOE */
#if defined(GPIOF)
if (port == GPIOF)
return 5;
#endif /* GPIOF */
#if defined(GPIOG)
if (port == GPIOG)
return 6;
#endif /* GPIOG */
#if defined(GPIOH)
if (port == GPIOH)
return 7;
#endif /* GPIOH */
#if defined(GPIOF)
if (port == GPIOI)
return 8;
#endif /* STM32_HAS_GPIOI */
#if defined(GPIOJ)
if (port == GPIOJ)
return 9;
#endif /* GPIOJ */
#if defined(GPIOK)
if (port == GPIOK)
return 10;
#endif /* GPIOK */
firmwareError(ObdCode::CUSTOM_ERR_UNKNOWN_PORT, "unknown port");
return -1;
}
int getPortPinIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
#endif // EFI_GPIO_HARDWARE

View File

@ -36,39 +36,6 @@ ioportid_t * getGpioPorts() {
return ports;
}
static int getPortIndex(ioportid_t port) {
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
if (port == GPIOA)
return 0;
if (port == GPIOB)
return 1;
if (port == GPIOC)
return 2;
if (port == GPIOD)
return 3;
if (port == GPIOE)
return 4;
if (port == GPIOF)
return 5;
if (port == GPIOG)
return 6;
if (port == GPIOH)
return 7;
if (port == GPIOI)
return 8;
if (port == GPIOJ)
return 9;
if (port == GPIOK)
return 10;
firmwareError(ObdCode::CUSTOM_ERR_UNKNOWN_PORT, "unknown port");
return -1;
}
int getBrainPinIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
/**
* Parse string representation of physical pin into brain_pin_e ordinal.
*

View File

@ -18,31 +18,10 @@ static ioportid_t ports[] = {GPIOA,
GPIOE
};
static int getPortIndex(ioportid_t port) {
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
if (port == GPIOA)
return 0;
if (port == GPIOB)
return 1;
if (port == GPIOC)
return 2;
if (port == GPIOD)
return 3;
if (port == GPIOE)
return 4;
firmwareError(ObdCode::CUSTOM_ERR_UNKNOWN_PORT, "unknown port");
return -1;
}
ioportid_t * getGpioPorts() {
return ports;
}
int getPortPinIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
/**
* Parse string representation of physical pin into Gpio ordinal.
*

View File

@ -57,53 +57,6 @@ ioportid_t * getGpioPorts() {
return ports;
}
static int getPortIndex(ioportid_t port) {
efiAssert(ObdCode::CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
if (port == GPIOA)
return 0;
if (port == GPIOB)
return 1;
if (port == GPIOC)
return 2;
if (port == GPIOD)
return 3;
#if defined(GPIOF)
if (port == GPIOE)
return 4;
#endif /* GPIOE */
#if defined(GPIOF)
if (port == GPIOF)
return 5;
#endif /* GPIOF */
#if defined(GPIOG)
if (port == GPIOG)
return 6;
#endif /* GPIOG */
#if defined(GPIOH)
if (port == GPIOH)
return 7;
#endif /* GPIOH */
#if defined(GPIOF)
if (port == GPIOI)
return 8;
#endif /* STM32_HAS_GPIOI */
#if defined(GPIOJ)
if (port == GPIOJ)
return 9;
#endif /* GPIOJ */
#if defined(GPIOK)
if (port == GPIOK)
return 10;
#endif /* GPIOK */
firmwareError(ObdCode::CUSTOM_ERR_UNKNOWN_PORT, "unknown port");
return -1;
}
int getPortPinIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
/**
* Parse string representation of physical pin into Gpio ordinal.
*