2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file pin_repository.cpp
|
|
|
|
* @brief I/O pin registry code
|
|
|
|
*
|
|
|
|
* This job of this class is to make sure that we are not using same hardware pin for two
|
|
|
|
* different purposes.
|
|
|
|
*
|
|
|
|
* @date Jan 15, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2021-07-14 21:03:31 -07:00
|
|
|
|
2024-09-04 10:33:07 -07:00
|
|
|
PinRepository pinRepository CCM_OPTIONAL;
|
|
|
|
|
2023-08-21 20:09:30 -07:00
|
|
|
static size_t getBrainPinTotalNum() {
|
2021-07-14 21:03:31 -07:00
|
|
|
return BRAIN_PIN_TOTAL_PINS;
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:09:30 -07:00
|
|
|
const char* & getBrainUsedPin(size_t index) {
|
2024-09-04 10:33:07 -07:00
|
|
|
return pinRepository.getBrainUsedPin(index);
|
2021-07-14 21:03:31 -07:00
|
|
|
}
|
2021-01-08 17:01:26 -08:00
|
|
|
|
|
|
|
/* Common for firmware and unit tests */
|
2022-04-28 14:32:39 -07:00
|
|
|
bool isBrainPinValid(brain_pin_e brainPin) {
|
|
|
|
if ((brainPin == Gpio::Unassigned) || (brainPin == Gpio::Invalid))
|
2021-01-08 17:01:26 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (brainPin > BRAIN_PIN_LAST)
|
|
|
|
/* something terribly wrong */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-21 20:09:30 -07:00
|
|
|
int brainPin_to_index(Gpio brainPin) {
|
2022-04-28 14:32:39 -07:00
|
|
|
if (brainPin < Gpio::A0)
|
2019-04-04 15:53:27 -07:00
|
|
|
return -1;
|
|
|
|
|
2023-08-21 20:09:30 -07:00
|
|
|
size_t i = brainPin - Gpio::A0;
|
2020-02-12 23:55:09 -08:00
|
|
|
|
2021-01-06 15:29:47 -08:00
|
|
|
if (i >= getBrainPinTotalNum())
|
2019-04-04 15:53:27 -07:00
|
|
|
return -1;
|
|
|
|
|
2021-01-06 15:29:47 -08:00
|
|
|
return i;
|
2019-04-04 15:53:27 -07:00
|
|
|
}
|
|
|
|
|
2021-07-14 22:28:44 -07:00
|
|
|
/**
|
|
|
|
* See also brain_pin_markUnused()
|
|
|
|
* @return true if this pin was already used, false otherwise
|
|
|
|
*/
|
|
|
|
|
2023-08-21 20:09:30 -07:00
|
|
|
bool brain_pin_markUsed(Gpio brainPin, const char *msg) {
|
2024-05-23 15:23:52 -07:00
|
|
|
#ifndef EFI_BOOTLOADER
|
2022-06-19 08:03:26 -07:00
|
|
|
efiPrintf("pin_markUsed: %s on %s", msg, hwPortname(brainPin));
|
2021-07-14 22:28:44 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int index = brainPin_to_index(brainPin);
|
|
|
|
if (index < 0)
|
|
|
|
return true;
|
|
|
|
|
2024-09-04 10:33:07 -07:00
|
|
|
if (pinRepository.getBrainUsedPin(index) != nullptr) {
|
2023-09-17 07:40:08 -07:00
|
|
|
// hwPortname and share a buffer behind the scenes, even while they probably never use it for different
|
|
|
|
// values here let's have an explicit second buffer to make this more reliable
|
|
|
|
char physicalPinName[32];
|
|
|
|
strncpy(physicalPinName, hwPhysicalPinName(brainPin), sizeof(physicalPinName) - 1);
|
2023-11-24 11:35:56 -08:00
|
|
|
criticalError("Pin \"%s\" (%s) required by \"%s\" but is used by \"%s\"",
|
2021-07-14 22:28:44 -07:00
|
|
|
hwPortname(brainPin),
|
2023-09-17 07:40:08 -07:00
|
|
|
physicalPinName,
|
2021-07-14 22:28:44 -07:00
|
|
|
msg,
|
2023-08-21 20:09:30 -07:00
|
|
|
getBrainUsedPin(index));
|
2021-07-14 22:28:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
getBrainUsedPin(index) = msg;
|
2021-07-14 22:28:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:03:31 -07:00
|
|
|
/**
|
|
|
|
* See also brain_pin_markUsed()
|
|
|
|
*/
|
2020-02-12 23:55:09 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void brain_pin_markUnused(brain_pin_e brainPin) {
|
2024-05-23 15:23:52 -07:00
|
|
|
#ifndef EFI_BOOTLOADER
|
2022-06-19 08:03:26 -07:00
|
|
|
efiPrintf("pin_markUnused: %s", hwPortname(brainPin));
|
|
|
|
#endif
|
2021-07-14 21:03:31 -07:00
|
|
|
int index = brainPin_to_index(brainPin);
|
|
|
|
if (index < 0)
|
|
|
|
return;
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
getBrainUsedPin(index) = nullptr;
|
2019-04-09 16:31:10 -07:00
|
|
|
}
|
|
|
|
|
2021-07-14 21:03:31 -07:00
|
|
|
#if EFI_PROD_CODE
|
|
|
|
#include "memstreams.h"
|
2020-05-02 20:15:48 -07:00
|
|
|
static MemoryStream portNameStream;
|
|
|
|
static char portNameBuffer[20];
|
2021-07-14 21:03:31 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2020-05-02 20:15:48 -07:00
|
|
|
|
2016-04-03 08:02:57 -07:00
|
|
|
PinRepository::PinRepository() {
|
2021-07-14 21:03:31 -07:00
|
|
|
#if EFI_PROD_CODE
|
2020-05-02 20:15:48 -07:00
|
|
|
msObjectInit(&portNameStream, (uint8_t*) portNameBuffer, sizeof(portNameBuffer), 0);
|
2021-07-14 21:03:31 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2016-04-03 08:02:57 -07:00
|
|
|
|
2021-07-14 23:00:48 -07:00
|
|
|
memset(PIN_USED, 0, sizeof(PIN_USED));
|
2020-05-02 20:15:48 -07:00
|
|
|
}
|
2019-02-22 19:10:41 -08:00
|
|
|
|
2021-07-14 21:03:31 -07:00
|
|
|
#if EFI_PROD_CODE
|
2022-09-07 12:56:45 -07:00
|
|
|
|
2021-07-14 21:03:31 -07:00
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "drivers/gpio/gpio_ext.h"
|
|
|
|
#include "smart_gpio.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
|
2022-01-03 11:21:54 -08:00
|
|
|
void pinDiag2string(char *buffer, size_t size, brain_pin_diag_e pin_diag) {
|
|
|
|
/* use autogeneraged helpers here? */
|
|
|
|
if (pin_diag == PIN_OK) {
|
|
|
|
chsnprintf(buffer, size, "Ok");
|
2024-01-28 07:35:14 -08:00
|
|
|
} else if (pin_diag != PIN_UNKNOWN) {
|
2022-01-03 17:33:35 -08:00
|
|
|
chsnprintf(buffer, size, "%s%s%s%s%s%s",
|
|
|
|
pin_diag & PIN_DRIVER_OFF ? "driver_off " : "",
|
2022-01-03 11:21:54 -08:00
|
|
|
pin_diag & PIN_OPEN ? "open_load " : "",
|
|
|
|
pin_diag & PIN_SHORT_TO_GND ? "short_to_gnd " : "",
|
|
|
|
pin_diag & PIN_SHORT_TO_BAT ? "short_to_bat " : "",
|
|
|
|
pin_diag & PIN_OVERLOAD ? "overload " : "",
|
|
|
|
pin_diag & PIN_DRIVER_OVERTEMP ? "overtemp": "");
|
|
|
|
} else {
|
|
|
|
chsnprintf(buffer, size, "INVALID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:03:31 -07:00
|
|
|
static brain_pin_e index_to_brainPin(unsigned int i)
|
|
|
|
{
|
|
|
|
if (i < getBrainPinTotalNum())
|
2022-04-28 14:32:39 -07:00
|
|
|
return Gpio::A0 + i;
|
2021-07-14 21:03:31 -07:00
|
|
|
|
2022-04-28 14:32:39 -07:00
|
|
|
return Gpio::Invalid;
|
2021-07-14 21:03:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
static void reportPins() {
|
2022-11-03 15:26:08 -07:00
|
|
|
int totalPinsUsed = 0;
|
|
|
|
|
2021-01-06 15:29:47 -08:00
|
|
|
for (unsigned int i = 0; i < getBrainPinOnchipNum(); i++) {
|
2019-06-02 13:48:38 -07:00
|
|
|
const char *pin_user = getBrainUsedPin(i);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
|
|
|
/* show used pins */
|
2024-09-04 17:00:39 -07:00
|
|
|
if (pin_user) {
|
2024-05-12 10:07:02 -07:00
|
|
|
static char pin_state[64];
|
2019-06-02 13:48:38 -07:00
|
|
|
brain_pin_e brainPin = index_to_brainPin(i);
|
|
|
|
int pin = getBrainPinIndex(brainPin);
|
2021-01-06 15:29:47 -08:00
|
|
|
ioportid_t port = getBrainPinPort(brainPin);
|
2024-05-12 10:07:02 -07:00
|
|
|
debugBrainPin(pin_state, sizeof(pin_state), brainPin);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
2023-07-05 19:18:22 -07:00
|
|
|
const char *boardPinName = getBoardSpecificPinName(brainPin);
|
2024-05-12 10:07:02 -07:00
|
|
|
efiPrintf("pin %s%d (%s): %s %s", portname(port), pin, boardPinName, pin_user, pin_state);
|
2022-11-03 15:26:08 -07:00
|
|
|
totalPinsUsed++;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:43:54 -07:00
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
2021-01-06 15:29:47 -08:00
|
|
|
for (unsigned int i = getBrainPinOnchipNum() ; i < getBrainPinTotalNum(); i++) {
|
2020-01-12 07:20:10 -08:00
|
|
|
static char pin_error[64];
|
2019-04-10 05:43:54 -07:00
|
|
|
brain_pin_e brainPin = index_to_brainPin(i);
|
|
|
|
|
2022-01-03 11:21:54 -08:00
|
|
|
const char *pin_name = gpiochips_getPinName(brainPin);
|
|
|
|
const char *pin_user = getBrainUsedPin(i);
|
|
|
|
brain_pin_diag_e pin_diag = gpiochips_getDiag(brainPin);
|
|
|
|
|
|
|
|
pinDiag2string(pin_error, sizeof(pin_error), pin_diag);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
|
|
|
/* here show all pins, unused too */
|
2024-09-04 17:00:39 -07:00
|
|
|
if (pin_name) {
|
2019-04-13 08:22:40 -07:00
|
|
|
// this probably uses a lot of output buffer!
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("ext %s: %s diagnostic: %s",
|
2020-01-12 07:20:10 -08:00
|
|
|
pin_name, pin_user ? pin_user : "free", pin_error);
|
2019-04-10 05:43:54 -07:00
|
|
|
} else {
|
2020-01-07 04:55:50 -08:00
|
|
|
const char *chip_name = gpiochips_getChipName(brainPin);
|
|
|
|
/* if chip exist */
|
2024-09-04 17:00:39 -07:00
|
|
|
if (chip_name) {
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("ext %s.%d: %s diagnostic: %s",
|
2020-01-12 07:20:10 -08:00
|
|
|
chip_name, gpiochips_getPinOffset(brainPin), pin_user ? pin_user : "free", pin_error);
|
2020-01-07 04:55:50 -08:00
|
|
|
}
|
2019-04-10 05:43:54 -07:00
|
|
|
}
|
2024-09-04 17:00:39 -07:00
|
|
|
if (pin_user) {
|
2022-11-03 15:26:08 -07:00
|
|
|
totalPinsUsed++;
|
|
|
|
}
|
2019-04-10 05:43:54 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-03 15:26:08 -07:00
|
|
|
efiPrintf("Total pins used: %d", totalPinsUsed);
|
2024-06-23 14:36:13 -07:00
|
|
|
|
|
|
|
gpiochips_debug();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2022-03-22 13:53:24 -07:00
|
|
|
__attribute__((weak)) const char * getBoardSpecificPinName(brain_pin_e /*brainPin*/) {
|
2022-02-03 09:45:26 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-08-11 15:27:14 -07:00
|
|
|
const char *hwOnChipPhysicalPinName(ioportid_t hwPort, int hwPin) {
|
2024-07-20 19:37:41 -07:00
|
|
|
portNameStream.eos = 0; // reset
|
|
|
|
if (hwPort == GPIO_NULL) {
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "%s%d", portname(hwPort), hwPin);
|
|
|
|
portNameStream.buffer[portNameStream.eos] = 0; // need to terminate explicitly
|
|
|
|
return portNameBuffer;
|
|
|
|
}
|
|
|
|
|
2023-09-17 07:40:08 -07:00
|
|
|
const char *hwPhysicalPinName(Gpio brainPin) {
|
2022-04-28 14:32:39 -07:00
|
|
|
if (brainPin == Gpio::Invalid) {
|
2015-07-10 06:01:56 -07:00
|
|
|
return "INVALID";
|
|
|
|
}
|
2022-04-28 14:32:39 -07:00
|
|
|
if (brainPin == Gpio::Unassigned) {
|
2019-07-14 19:47:11 -07:00
|
|
|
return "NONE";
|
|
|
|
}
|
2022-02-03 09:45:26 -08:00
|
|
|
|
2019-04-10 05:43:54 -07:00
|
|
|
if (brain_pin_is_onchip(brainPin)) {
|
|
|
|
ioportid_t hwPort = getHwPort("hostname", brainPin);
|
2019-07-14 19:47:11 -07:00
|
|
|
int hwPin = getHwPin("hostname", brainPin);
|
2024-07-20 19:37:41 -07:00
|
|
|
return hwOnChipPhysicalPinName(hwPort, hwPin);
|
2019-04-10 05:43:54 -07:00
|
|
|
}
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else {
|
2024-07-20 19:37:41 -07:00
|
|
|
portNameStream.eos = 0; // reset
|
2019-05-09 15:09:24 -07:00
|
|
|
const char *pin_name = gpiochips_getPinName(brainPin);
|
|
|
|
|
|
|
|
if (pin_name) {
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "ext:%s",
|
|
|
|
pin_name);
|
|
|
|
} else {
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "ext:%s.%d",
|
|
|
|
gpiochips_getChipName(brainPin), gpiochips_getPinOffset(brainPin));
|
|
|
|
}
|
2024-07-20 19:37:41 -07:00
|
|
|
portNameStream.buffer[portNameStream.eos] = 0; // need to terminate explicitly
|
|
|
|
return portNameBuffer;
|
2019-04-10 05:43:54 -07:00
|
|
|
}
|
|
|
|
#endif
|
2024-07-20 19:37:41 -07:00
|
|
|
return "unexpected";
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2023-09-17 07:40:08 -07:00
|
|
|
const char *hwPortname(brain_pin_e brainPin) {
|
|
|
|
const char * boardSpecificPinName = getBoardSpecificPinName(brainPin);
|
|
|
|
if (boardSpecificPinName != nullptr) {
|
|
|
|
return boardSpecificPinName;
|
|
|
|
}
|
|
|
|
return hwPhysicalPinName(brainPin);
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initPinRepository(void) {
|
|
|
|
/**
|
|
|
|
* this method cannot use console because this method is invoked before console is initialized
|
|
|
|
*/
|
|
|
|
|
2019-11-29 18:57:59 -08:00
|
|
|
addConsoleAction(CMD_PINS, reportPins);
|
2020-02-26 15:16:35 -08:00
|
|
|
|
|
|
|
#if (BOARD_TLE8888_COUNT > 0)
|
2020-02-11 14:49:14 -08:00
|
|
|
addConsoleAction("tle8888", tle8888_dump_regs);
|
2020-10-23 09:25:30 -07:00
|
|
|
addConsoleAction("tle8888init", tle8888_req_init);
|
2020-02-26 15:16:35 -08:00
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-04 15:53:27 -07:00
|
|
|
bool brain_pin_is_onchip(brain_pin_e brainPin)
|
|
|
|
{
|
2022-04-28 14:32:39 -07:00
|
|
|
if ((brainPin < Gpio::A0) || (brainPin > BRAIN_PIN_ONCHIP_LAST))
|
2019-04-04 15:53:27 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:10 -07:00
|
|
|
bool brain_pin_is_ext(brain_pin_e brainPin)
|
|
|
|
{
|
2021-01-06 15:29:47 -08:00
|
|
|
if (brainPin > BRAIN_PIN_ONCHIP_LAST)
|
2019-04-09 16:31:10 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2021-01-06 15:29:47 -08:00
|
|
|
int index = getPortPinIndex(port, pin);
|
2024-07-20 19:37:41 -07:00
|
|
|
#ifndef EFI_BOOTLOADER
|
|
|
|
efiPrintf("pin_markUsed: %s on %s", msg, hwOnChipPhysicalPinName(port, pin));
|
|
|
|
#endif
|
2017-04-21 13:27:15 -07:00
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
if (getBrainUsedPin(index) != NULL) {
|
2017-04-21 13:27:15 -07:00
|
|
|
/**
|
|
|
|
* todo: the problem is that this warning happens before the console is even
|
|
|
|
* connected, so the warning is never displayed on the console and that's quite a problem!
|
|
|
|
*/
|
2023-04-11 17:01:34 -07:00
|
|
|
// warning(ObdCode::OBD_PCM_Processor_Fault, "%s%d req by %s used by %s", portname(port), pin, msg, getBrainUsedPin(index));
|
2024-06-10 16:21:35 -07:00
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_PIN_ALREADY_USED_1, "%s%d req by %s used by %s", portname(port), (int)pin, msg, getBrainUsedPin(index));
|
2017-04-21 13:27:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-02 13:48:38 -07:00
|
|
|
getBrainUsedPin(index) = msg;
|
2017-04-21 13:27:15 -07:00
|
|
|
return false;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-03-08 04:01:15 -08:00
|
|
|
/**
|
2019-04-09 16:31:10 -07:00
|
|
|
* Marks on-chip gpio port-pin as UNused. Works only for on-chip gpios
|
|
|
|
* To be replaced with brain_pin_markUnused later
|
2019-03-08 04:01:15 -08:00
|
|
|
*/
|
2019-04-04 15:53:27 -07:00
|
|
|
|
2019-04-09 16:31:10 -07:00
|
|
|
void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin) {
|
2021-01-06 15:29:47 -08:00
|
|
|
int index = getPortPinIndex(port, pin);
|
2019-03-08 04:01:15 -08:00
|
|
|
|
2019-09-22 05:22:35 -07:00
|
|
|
getBrainUsedPin(index) = nullptr;
|
2019-03-08 04:01:15 -08:00
|
|
|
}
|
|
|
|
|
2019-04-04 15:53:27 -07:00
|
|
|
const char *getPinFunction(brain_input_pin_e brainPin) {
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = brainPin_to_index(brainPin);
|
|
|
|
if (index < 0)
|
|
|
|
return NULL;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
return getBrainUsedPin(index);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
2019-09-19 18:41:52 -07:00
|
|
|
#else
|
2023-09-17 07:40:08 -07:00
|
|
|
const char *hwPhysicalPinName(Gpio brainPin) {
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
const char *hwPortname(Gpio brainPin) {
|
2019-09-19 18:41:52 -07:00
|
|
|
(void)brainPin;
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|