refactoring - reducing GPIO complexity

This commit is contained in:
rusefi 2017-04-21 16:27:15 -04:00
parent e7e7458729
commit b193e17b7d
3 changed files with 26 additions and 26 deletions

View File

@ -22,7 +22,6 @@
#include "advance_map.h" #include "advance_map.h"
#include "interpolation.h" #include "interpolation.h"
#include "efilib2.h" #include "efilib2.h"
#include "engine_configuration.h"
#include "engine_math.h" #include "engine_math.h"
EXTERN_ENGINE; EXTERN_ENGINE;

View File

@ -154,12 +154,30 @@ void initPinRepository(void) {
addConsoleAction("pins", reportPins); addConsoleAction("pins", reportPins);
} }
static int getIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
/** /**
* See also unmarkPin() * See also unmarkPin()
* @return true if this pin was already used, false otherwise
*/ */
static inline void markUsed(int index, const char *msg) { bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg) {
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;
}
PIN_USED[index] = msg; PIN_USED[index] = msg;
totalPinsUsed++; totalPinsUsed++;
return false;
} }
void mySetPadMode2(const char *msg, brain_pin_e pin, iomode_t mode) { void mySetPadMode2(const char *msg, brain_pin_e pin, iomode_t mode) {
@ -178,11 +196,6 @@ iomode_t getInputMode(pin_input_mode_e mode) {
} }
} }
static int getIndex(ioportid_t port, ioportmask_t pin) {
int portIndex = getPortIndex(port);
return portIndex * PORT_SIZE + pin;
}
const char * getPinFunction(brain_input_pin_e brainPin) { const char * getPinFunction(brain_input_pin_e brainPin) {
ioportid_t port = getHwPort(brainPin); ioportid_t port = getHwPort(brainPin);
ioportmask_t pin = getHwPin(brainPin); ioportmask_t pin = getHwPin(brainPin);
@ -199,23 +212,16 @@ void mySetPadMode(const char *msg, ioportid_t port, ioportmask_t pin, iomode_t m
firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized"); firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized");
return; return;
} }
if (port == GPIO_NULL) if (port == GPIO_NULL) {
return; return;
}
scheduleMsg(&logger, "%s on %s%d", msg, portname(port), pin); scheduleMsg(&logger, "%s on %s%d", msg, portname(port), pin);
int index = getIndex(port, pin); bool wasUsed = markUsed(port, pin, msg);
if (wasUsed) {
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; return;
} }
markUsed(index, msg);
palSetPadMode(port, pin, mode); palSetPadMode(port, pin, mode);
} }
@ -241,15 +247,10 @@ void unmarkPin(brain_pin_e brainPin) {
void registedFundamentralIoPin(char *msg, ioportid_t port, ioportmask_t pin, iomode_t mode) { void registedFundamentralIoPin(char *msg, ioportid_t port, ioportmask_t pin, iomode_t mode) {
efiAssertVoid(initialized, "repo not initialized"); efiAssertVoid(initialized, "repo not initialized");
int index = getIndex(port, pin); bool wasUsed = markUsed(port, pin, msg);
if (wasUsed) {
if (PIN_USED[index] != NULL) {
print("!!!!!!!!!!!!! Already used [%s] %d\r\n", msg, pin);
print("!!!!!!!!!!!!! Already used by [%s]\r\n", PIN_USED[index]);
firmwareError(CUSTOM_ERR_PIN_ALREADY_USED_2, "pin already used");
return; return;
} }
markUsed(index, msg);
palSetPadMode(port, pin, mode); palSetPadMode(port, pin, mode);
} }

View File

@ -29,7 +29,7 @@ class PinRepository {
#define PORT_SIZE 16 #define PORT_SIZE 16
void initPinRepository(void); void initPinRepository(void);
void markUsed(int index, const char *msg); bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg);
const char * getPinFunction(brain_input_pin_e brainPin); const char * getPinFunction(brain_input_pin_e brainPin);
void unmarkPin(brain_pin_e brainPin); void unmarkPin(brain_pin_e brainPin);