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
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "memstreams.h"
|
|
|
|
#include "chprintf.h"
|
|
|
|
#include "rusefi.h"
|
|
|
|
|
|
|
|
#define PIN_REPO_SIZE 7 * PORT_SIZE
|
2016-04-03 08:02:57 -07:00
|
|
|
// todo: move this into PinRepository class
|
2015-07-10 06:01:56 -07:00
|
|
|
const char *PIN_USED[PIN_REPO_SIZE];
|
|
|
|
static int initialized = FALSE;
|
|
|
|
|
|
|
|
static LoggingWithStorage logger("pin repos");
|
|
|
|
static int totalPinsUsed = 0;
|
|
|
|
|
2016-02-04 08:02:13 -08:00
|
|
|
static ioportid_t ports[7] = {GPIOA,
|
2015-07-10 06:01:56 -07:00
|
|
|
GPIOB,
|
|
|
|
GPIOC,
|
|
|
|
GPIOD,
|
|
|
|
GPIOE,
|
|
|
|
GPIOF,
|
|
|
|
GPIOH,
|
|
|
|
};
|
|
|
|
|
2016-04-03 08:02:57 -07:00
|
|
|
PinRepository::PinRepository() {
|
|
|
|
}
|
|
|
|
|
2016-02-04 08:02:13 -08:00
|
|
|
static int getPortIndex(ioportid_t port) {
|
2016-09-13 21:03:14 -07:00
|
|
|
efiAssert(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;
|
|
|
|
#if defined(STM32F4XX)
|
|
|
|
if (port == GPIOE)
|
|
|
|
return 4;
|
|
|
|
#endif /* defined(STM32F4XX) */
|
|
|
|
if (port == GPIOF)
|
|
|
|
return 5;
|
|
|
|
#if defined(STM32F4XX)
|
|
|
|
if (port == GPIOH)
|
|
|
|
return 6;
|
|
|
|
#endif /* defined(STM32F4XX) */
|
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) {
|
|
|
|
for (int i = 0; i < PIN_REPO_SIZE; i++) {
|
|
|
|
const char *name = PIN_USED[i];
|
|
|
|
int portIndex = i / PORT_SIZE;
|
|
|
|
int pin = i % PORT_SIZE;
|
2016-02-04 08:02:13 -08:00
|
|
|
ioportid_t port = ports[portIndex];
|
2015-07-10 06:01:56 -07:00
|
|
|
if (name != NULL) {
|
|
|
|
scheduleMsg(&logger, "pin %s%d: %s", portname(port), pin, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2016-02-04 08:02:13 -08:00
|
|
|
ioportid_t hwPort = getHwPort(brainPin);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (hwPort == GPIO_NULL) {
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
int hwPin = getHwPin(brainPin);
|
|
|
|
portNameStream.eos = 0; // reset
|
|
|
|
chprintf((BaseSequentialStream *) &portNameStream, "%s%d", portname(hwPort), hwPin);
|
|
|
|
portNameStream.buffer[portNameStream.eos] = 0; // need to terminate explicitly
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (int i = 0; i < PIN_REPO_SIZE; i++)
|
|
|
|
PIN_USED[i] = 0;
|
|
|
|
initialized = true;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:02:18 -07:00
|
|
|
/**
|
|
|
|
* See also unmarkPin()
|
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
|
|
|
*/
|
2017-04-21 13:27:15 -07:00
|
|
|
bool 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
|
|
|
}
|
|
|
|
|
|
|
|
const char * getPinFunction(brain_input_pin_e brainPin) {
|
|
|
|
ioportid_t port = getHwPort(brainPin);
|
|
|
|
ioportmask_t pin = getHwPin(brainPin);
|
|
|
|
|
|
|
|
int index = getIndex(port, pin);
|
|
|
|
return PIN_USED[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void unmarkPin(brain_pin_e brainPin) {
|
2016-09-13 22:01:57 -07:00
|
|
|
if (brainPin == GPIO_UNASSIGNED) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
ioportid_t port = getHwPort(brainPin);
|
|
|
|
ioportmask_t pin = getHwPin(brainPin);
|
|
|
|
|
|
|
|
int index = getIndex(port, pin);
|
|
|
|
|
|
|
|
if (PIN_USED[index] != NULL) {
|
|
|
|
PIN_USED[index] = NULL;
|
|
|
|
totalPinsUsed--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|