2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file io_pins.cpp
|
2017-04-21 10:36:51 -07:00
|
|
|
* @brief his file is about general input/output utility methods, not much EFI-specifics
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* @date Jan 24, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2019-09-19 19:10:39 -07:00
|
|
|
#include "io_pins.h"
|
2020-09-10 19:16:20 -07:00
|
|
|
#include "efi_gpio.h"
|
|
|
|
#include "engine.h"
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
2019-01-03 21:16:08 -08:00
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
2020-09-10 19:16:20 -07:00
|
|
|
|
2020-03-07 07:28:18 -08:00
|
|
|
#include "os_access.h"
|
2019-04-10 05:43:54 -07:00
|
|
|
#include "drivers/gpio/gpio_ext.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "status_loop.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "console_io.h"
|
|
|
|
|
2017-04-21 09:01:44 -07:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|
|
|
|
|
2017-05-15 02:03:40 -07:00
|
|
|
bool efiReadPin(brain_pin_e pin) {
|
2021-01-08 17:01:26 -08:00
|
|
|
if (!isBrainPinValid(pin))
|
|
|
|
return false;
|
2019-04-10 05:43:54 -07:00
|
|
|
if (brain_pin_is_onchip(pin))
|
|
|
|
return palReadPad(getHwPort("readPin", pin), getHwPin("readPin", pin));
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else if (brain_pin_is_ext(pin))
|
|
|
|
return (gpiochips_readPad(pin) > 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* incorrect pin */
|
|
|
|
return false;
|
2017-05-15 02:03:40 -07:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:20:57 -08:00
|
|
|
|
|
|
|
void efiSetPadModeWithoutOwnershipAcquisition(const char *msg, brain_pin_e brainPin, iomode_t mode)
|
|
|
|
{
|
|
|
|
/*check if on-chip pin or external */
|
|
|
|
if (brain_pin_is_onchip(brainPin)) {
|
|
|
|
/* on-chip */
|
|
|
|
ioportid_t port = getHwPort(msg, brainPin);
|
|
|
|
ioportmask_t pin = getHwPin(msg, brainPin);
|
|
|
|
/* paranoid */
|
|
|
|
if (port == GPIO_NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
palSetPadMode(port, pin, mode);
|
|
|
|
}
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else {
|
|
|
|
gpiochips_setPadMode(brainPin, mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:30:14 -07:00
|
|
|
/**
|
|
|
|
* This method would set an error condition if pin is already used
|
|
|
|
*/
|
2019-04-10 05:43:54 -07:00
|
|
|
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
|
|
|
|
{
|
2021-01-08 17:01:26 -08:00
|
|
|
if (!isBrainPinValid(brainPin)) {
|
2020-09-14 00:54:25 -07:00
|
|
|
// No pin configured, nothing to do here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-10 14:51:12 -07:00
|
|
|
bool wasUsed = brain_pin_markUsed(brainPin, msg);
|
2019-04-10 05:43:54 -07:00
|
|
|
|
|
|
|
if (!wasUsed) {
|
2020-12-22 18:20:57 -08:00
|
|
|
efiSetPadModeWithoutOwnershipAcquisition(msg, brainPin, mode);
|
2017-04-21 13:30:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:28:49 -07:00
|
|
|
void efiSetPadUnused(brain_pin_e brainPin)
|
|
|
|
{
|
|
|
|
/* input with pull up, is it safe? */
|
|
|
|
iomode_t mode = PAL_STM32_MODE_INPUT | PAL_STM32_PUPDR_PULLUP;
|
|
|
|
|
|
|
|
if (brain_pin_is_onchip(brainPin)) {
|
|
|
|
ioportid_t port = getHwPort("unused", brainPin);
|
|
|
|
ioportmask_t pin = getHwPin("unused", brainPin);
|
|
|
|
|
2020-11-25 21:16:59 -08:00
|
|
|
/* input with pull up, is it safe?
|
|
|
|
* todo: shall we reuse 'default state' constants with board.h?
|
|
|
|
* */
|
2019-04-21 06:28:49 -07:00
|
|
|
palSetPadMode(port, pin, mode);
|
2020-11-25 21:24:15 -08:00
|
|
|
palWritePad(port, pin, 0);
|
2019-04-21 06:28:49 -07:00
|
|
|
}
|
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
else {
|
|
|
|
gpiochips_setPadMode(brainPin, mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
brain_pin_markUnused(brainPin);
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:30:14 -07:00
|
|
|
iomode_t getInputMode(pin_input_mode_e mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case PI_PULLUP:
|
|
|
|
return PAL_MODE_INPUT_PULLUP;
|
|
|
|
case PI_PULLDOWN:
|
|
|
|
return PAL_MODE_INPUT_PULLDOWN;
|
|
|
|
case PI_DEFAULT:
|
|
|
|
default:
|
|
|
|
return PAL_MODE_INPUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if HAL_USE_ICU
|
2020-03-07 07:28:18 -08:00
|
|
|
static char icuError[30];
|
|
|
|
|
2019-04-07 15:25:46 -07:00
|
|
|
void efiIcuStart(const char *msg, ICUDriver *icup, const ICUConfig *config) {
|
|
|
|
if (icup->state != ICU_STOP && icup->state != ICU_READY) {
|
2020-03-07 07:28:18 -08:00
|
|
|
chsnprintf(icuError, sizeof(icuError), "ICU already used %s", msg);
|
2019-04-07 15:25:46 -07:00
|
|
|
firmwareError(CUSTOM_ERR_6679, icuError);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 13:30:14 -07:00
|
|
|
icuStart(icup, config);
|
|
|
|
}
|
2017-05-30 12:46:02 -07:00
|
|
|
#endif /* HAL_USE_ICU */
|
2019-09-19 19:10:39 -07:00
|
|
|
|
2021-07-13 16:28:03 -07:00
|
|
|
void writePad(const char *msg, brain_pin_e pin, int bit) {
|
|
|
|
palWritePad(getHwPort(msg, pin), getHwPin(msg, pin), bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* EFI_PROD_CODE */
|
2020-12-09 21:10:58 -08:00
|
|
|
|
|
|
|
// This has been made global so we don't need to worry about efiReadPin having access the object
|
|
|
|
// we store it in, every time we need to use efiReadPin.
|
|
|
|
bool mockPinStates[BRAIN_PIN_COUNT];
|
2020-09-10 19:16:20 -07:00
|
|
|
|
2019-09-19 19:10:39 -07:00
|
|
|
bool efiReadPin(brain_pin_e pin) {
|
2020-09-10 19:16:20 -07:00
|
|
|
return mockPinStates[static_cast<int>(pin)];
|
2019-09-19 19:10:39 -07:00
|
|
|
}
|
2020-12-18 14:19:02 -08:00
|
|
|
|
|
|
|
void setMockState(brain_pin_e pin, bool state) {
|
|
|
|
mockPinStates[static_cast<int>(pin)] = state;
|
|
|
|
}
|
|
|
|
|
2019-09-19 19:10:39 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|