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
|
2019-07-05 17:03:32 -07:00
|
|
|
#include "os_access.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "memstreams.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 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 */
|
2019-06-02 13:48:38 -07:00
|
|
|
if ((unsigned)index >= getNumBrainPins() + BOARD_EXT_PINREPOPINS)
|
2019-04-04 15:53:27 -07:00
|
|
|
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;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static void reportPins(void) {
|
2019-06-02 13:48:38 -07:00
|
|
|
for (unsigned int i = 0; i < getNumBrainPins(); i++) {
|
|
|
|
const char *pin_user = getBrainUsedPin(i);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
|
|
|
/* show used pins */
|
|
|
|
if (pin_user != NULL) {
|
2019-06-02 13:48:38 -07:00
|
|
|
brain_pin_e brainPin = index_to_brainPin(i);
|
|
|
|
int pin = getBrainPinIndex(brainPin);
|
|
|
|
ioportid_t port = getBrainPort(brainPin);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
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-06-02 13:48:38 -07:00
|
|
|
for (unsigned int i = getNumBrainPins() ; i < getNumBrainPins() + 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);
|
2019-06-02 13:48:38 -07:00
|
|
|
pin_user = getBrainUsedPin(i);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
|
|
|
/* 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];
|
|
|
|
|
|
|
|
const char *hwPortname(brain_pin_e brainPin) {
|
|
|
|
if (brainPin == GPIO_INVALID) {
|
|
|
|
return "INVALID";
|
|
|
|
}
|
2019-07-14 19:47:11 -07:00
|
|
|
if (brainPin == GPIO_UNASSIGNED) {
|
|
|
|
return "NONE";
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
portNameStream.eos = 0; // reset
|
2019-04-10 05:43:54 -07:00
|
|
|
if (brain_pin_is_onchip(brainPin)) {
|
|
|
|
|
|
|
|
ioportid_t hwPort = getHwPort("hostname", brainPin);
|
|
|
|
if (hwPort == GPIO_NULL) {
|
|
|
|
return "NONE";
|
|
|
|
}
|
2019-07-14 19:47:11 -07:00
|
|
|
int hwPin = getHwPin("hostname", brainPin);
|
2019-04-10 05:43:54 -07:00
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "%s%d", portname(hwPort), hwPin);
|
|
|
|
}
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else {
|
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));
|
|
|
|
}
|
2019-04-10 05:43:54 -07:00
|
|
|
}
|
|
|
|
#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-06-02 13:48:38 -07:00
|
|
|
initBrainUsedPins();
|
2019-02-22 19:10:41 -08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-05-15 01:26:41 -07:00
|
|
|
bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg) {
|
2019-04-04 15:53:27 -07:00
|
|
|
if (!initialized) {
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-09 15:09:24 -07:00
|
|
|
#if ! EFI_BOOTLOADER
|
|
|
|
scheduleMsg(&logger, "%s on %s", msg, hwPortname(brainPin));
|
|
|
|
#endif
|
|
|
|
|
2019-05-15 01:26:41 -07:00
|
|
|
int index = brainPin_to_index(brainPin);
|
2019-04-04 15:53:27 -07:00
|
|
|
if (index < 0)
|
|
|
|
return true;
|
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
if (getBrainUsedPin(index) != NULL) {
|
2019-04-04 15:53:27 -07:00
|
|
|
/* 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!
|
|
|
|
*/
|
2019-06-02 13:48:38 -07:00
|
|
|
// warning(OBD_PCM_Processor_Fault, "brain pin %d req by %s used by %s", brainPin, msg, getBrainUsedPin(index));
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_ALREADY_USED_1, "brain pin %s req by %s used by %s", hwPortname(brainPin), msg, getBrainUsedPin(index));
|
2019-04-04 15:53:27 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
getBrainUsedPin(index) = msg;
|
2019-04-04 15:53:27 -07:00
|
|
|
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;
|
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
if (getBrainUsedPin(index) != NULL)
|
2019-04-09 16:31:10 -07:00
|
|
|
totalPinsUsed--;
|
2019-06-02 13:48:38 -07:00
|
|
|
getBrainUsedPin(index) = NULL;
|
2019-04-09 16:31:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2019-06-02 13:48:38 -07:00
|
|
|
int index = getBrainIndex(port, pin);
|
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!
|
|
|
|
*/
|
2019-06-02 13:48:38 -07:00
|
|
|
// warning(OBD_PCM_Processor_Fault, "%s%d req by %s used by %s", portname(port), pin, msg, getBrainUsedPin(index));
|
|
|
|
firmwareError(CUSTOM_ERR_PIN_ALREADY_USED_1, "%s%d req by %s used by %s", portname(port), 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;
|
2015-07-10 06:01:56 -07:00
|
|
|
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;
|
|
|
|
}
|
2019-06-02 13:48:38 -07:00
|
|
|
int index = getBrainIndex(port, pin);
|
2019-03-08 04:01:15 -08:00
|
|
|
|
2019-06-02 13:48:38 -07:00
|
|
|
if (getBrainUsedPin(index) != NULL)
|
2019-03-08 04:01:15 -08:00
|
|
|
totalPinsUsed--;
|
2019-06-02 13:48:38 -07:00
|
|
|
getBrainUsedPin(index) = NULL;
|
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
|
|
|
|
const char *hwPortname(brain_pin_e brainPin) {
|
|
|
|
(void)brainPin;
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|