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
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
#if EFI_PROD_CODE
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "memstreams.h"
|
|
|
|
#include "chprintf.h"
|
2019-04-10 05:43:54 -07:00
|
|
|
#include "drivers/gpio/gpio_ext.h"
|
|
|
|
|
|
|
|
#ifndef BOARD_EXT_PINREPOPINS
|
|
|
|
#define BOARD_EXT_PINREPOPINS 0
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-02-22 19:10:41 -08:00
|
|
|
static ioportid_t ports[] = {GPIOA,
|
2015-07-10 06:01:56 -07:00
|
|
|
GPIOB,
|
|
|
|
GPIOC,
|
|
|
|
GPIOD,
|
2019-03-28 19:46:10 -07:00
|
|
|
#if STM32_HAS_GPIOE
|
2015-07-10 06:01:56 -07:00
|
|
|
GPIOE,
|
2019-03-28 19:46:10 -07:00
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
#endif /* STM32_HAS_GPIOE */
|
|
|
|
#if STM32_HAS_GPIOF
|
2015-07-10 06:01:56 -07:00
|
|
|
GPIOF,
|
2019-03-28 19:46:10 -07:00
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
#endif /* STM32_HAS_GPIOF */
|
|
|
|
#if STM32_HAS_GPIOG
|
2019-02-22 19:10:41 -08:00
|
|
|
GPIOG,
|
2019-03-28 19:46:10 -07:00
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
#endif /* STM32_HAS_GPIOG */
|
|
|
|
#if STM32_HAS_GPIOH
|
2015-07-10 06:01:56 -07:00
|
|
|
GPIOH,
|
2019-03-28 19:46:10 -07:00
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
#endif /* STM32_HAS_GPIOH */
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2019-03-08 04:01:15 -08:00
|
|
|
#define PIN_REPO_SIZE (sizeof(ports) / sizeof(ports[0])) * PORT_SIZE
|
2019-02-22 19:10:41 -08:00
|
|
|
// todo: move this into PinRepository class
|
2019-04-10 05:43:54 -07:00
|
|
|
const char *PIN_USED[PIN_REPO_SIZE + BOARD_EXT_PINREPOPINS];
|
2019-02-22 19:10:41 -08:00
|
|
|
static int initialized = FALSE;
|
|
|
|
|
|
|
|
static LoggingWithStorage logger("pin repos");
|
|
|
|
static int totalPinsUsed = 0;
|
|
|
|
|
2019-04-04 15:53:27 -07:00
|
|
|
static int brainPin_to_index(brain_pin_e brainPin)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (brainPin < GPIOA_0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
index = brainPin - GPIOA_0;
|
|
|
|
|
|
|
|
/* if index outside array boundary */
|
|
|
|
if ((unsigned)index > (sizeof(PIN_USED) / sizeof(PIN_USED[0])))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:10 -07:00
|
|
|
static brain_pin_e index_to_brainPin(int i)
|
|
|
|
{
|
|
|
|
return (brain_pin_e)((int)GPIOA_0 + i);
|
|
|
|
}
|
|
|
|
|
2016-04-03 08:02:57 -07:00
|
|
|
PinRepository::PinRepository() {
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:10:41 -08:00
|
|
|
static PinRepository instance;
|
|
|
|
|
2016-02-04 08:02:13 -08:00
|
|
|
static int getPortIndex(ioportid_t port) {
|
2018-07-25 20:30:00 -07:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, port != NULL, "null port", -1);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (port == GPIOA)
|
|
|
|
return 0;
|
|
|
|
if (port == GPIOB)
|
|
|
|
return 1;
|
|
|
|
if (port == GPIOC)
|
|
|
|
return 2;
|
|
|
|
if (port == GPIOD)
|
|
|
|
return 3;
|
2019-03-28 19:46:10 -07:00
|
|
|
#if STM32_HAS_GPIOE
|
2015-07-10 06:01:56 -07:00
|
|
|
if (port == GPIOE)
|
|
|
|
return 4;
|
2019-03-28 19:46:10 -07:00
|
|
|
#endif /* STM32_HAS_GPIOE */
|
|
|
|
#if STM32_HAS_GPIOF
|
2015-07-10 06:01:56 -07:00
|
|
|
if (port == GPIOF)
|
|
|
|
return 5;
|
2019-03-28 19:46:10 -07:00
|
|
|
#endif /* STM32_HAS_GPIOF */
|
|
|
|
#if STM32_HAS_GPIOG
|
2019-02-22 19:10:41 -08:00
|
|
|
if (port == GPIOG)
|
2015-07-10 06:01:56 -07:00
|
|
|
return 6;
|
2019-03-28 19:46:10 -07:00
|
|
|
#endif /* STM32_HAS_GPIOG */
|
|
|
|
#if STM32_HAS_GPIOH
|
2019-02-22 19:10:41 -08:00
|
|
|
if (port == GPIOH)
|
|
|
|
return 7;
|
2019-03-28 19:46:10 -07:00
|
|
|
#endif /* STM32_HAS_GPIOH */
|
2016-10-10 13:02:39 -07:00
|
|
|
firmwareError(CUSTOM_ERR_UNKNOWN_PORT, "unknown port");
|
2015-07-10 06:01:56 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportPins(void) {
|
2019-04-13 07:59:29 -07:00
|
|
|
for (unsigned int i = 0; i < PIN_REPO_SIZE; i++) {
|
2019-04-10 05:43:54 -07:00
|
|
|
const char *pin_user = PIN_USED[i];
|
|
|
|
|
|
|
|
/* show used pins */
|
|
|
|
if (pin_user != NULL) {
|
|
|
|
int portIndex = i / PORT_SIZE;
|
|
|
|
int pin = i % PORT_SIZE;
|
|
|
|
ioportid_t port = ports[portIndex];
|
|
|
|
|
2019-04-13 07:59:29 -07:00
|
|
|
scheduleMsg(&logger, "pin %s%d: %s", portname(port), pin, pin_user);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:43:54 -07:00
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
2019-04-13 07:59:29 -07:00
|
|
|
for (unsigned int i = PIN_REPO_SIZE ; i < PIN_REPO_SIZE + BOARD_EXT_PINREPOPINS /* gpiochips_get_total_pins()*/ ; i++) {
|
2019-04-10 05:43:54 -07:00
|
|
|
const char *pin_name;
|
|
|
|
const char *pin_user;
|
|
|
|
brain_pin_e brainPin = index_to_brainPin(i);
|
|
|
|
|
|
|
|
pin_name = gpiochips_getPinName(brainPin);
|
|
|
|
pin_user = PIN_USED[i];
|
|
|
|
|
|
|
|
/* here show all pins, unused too */
|
|
|
|
if (pin_name != NULL) {
|
2019-04-13 08:22:40 -07:00
|
|
|
// this probably uses a lot of output buffer!
|
2019-04-13 07:59:29 -07:00
|
|
|
scheduleMsg(&logger, "ext %s: %s",
|
2019-04-10 05:43:54 -07:00
|
|
|
pin_name, pin_user ? pin_user : "free");
|
|
|
|
} else {
|
2019-04-13 07:59:29 -07:00
|
|
|
scheduleMsg(&logger, "ext %s.%d: %s",
|
2019-04-10 05:43:54 -07:00
|
|
|
gpiochips_getChipName(brainPin), gpiochips_getPinOffset(brainPin), pin_user ? pin_user : "free");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&logger, "Total pins count: %d", totalPinsUsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static MemoryStream portNameStream;
|
|
|
|
static char portNameBuffer[20];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse string representation of physical pin into brain_pin_e ordinal.
|
|
|
|
*
|
|
|
|
* @return GPIO_UNASSIGNED for "none", GPIO_INVALID for invalid entry
|
|
|
|
*/
|
|
|
|
brain_pin_e parseBrainPin(const char *str) {
|
|
|
|
if (strEqual(str, "none"))
|
|
|
|
return GPIO_UNASSIGNED;
|
|
|
|
// todo: create method toLowerCase?
|
|
|
|
if (str[0] != 'p' && str[0] != 'P') {
|
|
|
|
return GPIO_INVALID;
|
|
|
|
}
|
|
|
|
char port = str[1];
|
|
|
|
brain_pin_e basePin;
|
|
|
|
if (port >= 'a' && port <= 'z') {
|
|
|
|
basePin = (brain_pin_e) ((int) GPIOA_0 + 16 * (port - 'a'));
|
|
|
|
} else if (port >= 'A' && port <= 'Z') {
|
|
|
|
basePin = (brain_pin_e) ((int) GPIOA_0 + 16 * (port - 'A'));
|
|
|
|
} else {
|
|
|
|
return GPIO_INVALID;
|
|
|
|
}
|
|
|
|
const char *pinStr = str + 2;
|
|
|
|
int pin = atoi(pinStr);
|
|
|
|
return (brain_pin_e)(basePin + pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *hwPortname(brain_pin_e brainPin) {
|
|
|
|
if (brainPin == GPIO_INVALID) {
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
portNameStream.eos = 0; // reset
|
2019-04-10 05:43:54 -07:00
|
|
|
if (brain_pin_is_onchip(brainPin)) {
|
|
|
|
int hwPin;
|
|
|
|
|
|
|
|
ioportid_t hwPort = getHwPort("hostname", brainPin);
|
|
|
|
if (hwPort == GPIO_NULL) {
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
hwPin = getHwPin("hostname", brainPin);
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "%s%d", portname(hwPort), hwPin);
|
|
|
|
}
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else {
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "ext:%s.%d (%s)",
|
|
|
|
gpiochips_getChipName(brainPin), gpiochips_getPinOffset(brainPin), gpiochips_getPinName(brainPin));
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
portNameStream.buffer[portNameStream.eos] = 0; // need to terminate explicitly
|
2019-04-10 05:43:54 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
return portNameBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initPinRepository(void) {
|
|
|
|
/**
|
|
|
|
* this method cannot use console because this method is invoked before console is initialized
|
|
|
|
*/
|
|
|
|
|
|
|
|
msObjectInit(&portNameStream, (uint8_t*) portNameBuffer, sizeof(portNameBuffer), 0);
|
|
|
|
|
2019-02-22 19:10:41 -08:00
|
|
|
memset(PIN_USED, 0, sizeof(PIN_USED));
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
initialized = true;
|
2019-04-10 05:43:54 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
addConsoleAction("pins", reportPins);
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:27:15 -07:00
|
|
|
static int getIndex(ioportid_t port, ioportmask_t pin) {
|
|
|
|
int portIndex = getPortIndex(port);
|
|
|
|
return portIndex * PORT_SIZE + pin;
|
|
|
|
}
|
|
|
|
|
2019-04-04 15:53:27 -07:00
|
|
|
bool brain_pin_is_onchip(brain_pin_e brainPin)
|
|
|
|
{
|
2019-04-09 16:31:10 -07:00
|
|
|
if ((brainPin < GPIOA_0) || (brainPin > BRAIN_PIN_LAST_ONCHIP))
|
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)
|
|
|
|
{
|
|
|
|
if (brainPin > BRAIN_PIN_LAST_ONCHIP)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:02:18 -07:00
|
|
|
/**
|
2019-04-09 16:31:10 -07:00
|
|
|
* See also brain_pin_markUnused()
|
2017-04-21 13:27:15 -07:00
|
|
|
* @return true if this pin was already used, false otherwise
|
2015-07-13 17:02:18 -07:00
|
|
|
*/
|
2019-04-09 16:31:10 -07:00
|
|
|
|
2019-04-04 15:53:27 -07:00
|
|
|
bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = brainPin_to_index(brainPin);
|
|
|
|
if (index < 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (PIN_USED[index] != NULL) {
|
|
|
|
/* TODO: get readable name of brainPin... */
|
|
|
|
/**
|
|
|
|
* 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!
|
|
|
|
*/
|
|
|
|
// warning(OBD_PCM_Processor_Fault, "brain pin %d req by %s used by %s", brainPin, msg, PIN_USED[index]);
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_ALREADY_USED_1, "brain pin %d req by %s used by %s", brainPin, msg, PIN_USED[index]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIN_USED[index] = msg;
|
|
|
|
totalPinsUsed++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:10 -07:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-04-21 13:30:14 -07:00
|
|
|
if (!initialized) {
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-21 13:27:15 -07:00
|
|
|
int index = getIndex(port, pin);
|
|
|
|
|
|
|
|
if (PIN_USED[index] != NULL) {
|
|
|
|
/**
|
|
|
|
* 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!
|
|
|
|
*/
|
|
|
|
// warning(OBD_PCM_Processor_Fault, "%s%d req by %s used by %s", portname(port), pin, msg, PIN_USED[index]);
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_ALREADY_USED_1, "%s%d req by %s used by %s", portname(port), pin, msg, PIN_USED[index]);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
PIN_USED[index] = msg;
|
|
|
|
totalPinsUsed++;
|
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) {
|
2019-03-08 04:01:15 -08:00
|
|
|
if (!initialized) {
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int index = getIndex(port, pin);
|
|
|
|
|
|
|
|
if (PIN_USED[index] != NULL)
|
|
|
|
totalPinsUsed--;
|
|
|
|
PIN_USED[index] = NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
return PIN_USED[index];
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:16:08 -08:00
|
|
|
#endif
|