[FL-1167] Rework GPIO and EXTI with LL lib (#424)

* api-hal-gpio: rework gpio on ll
* one_wire_slave: rework gpio initialization
* interrupts: add attribute weak to hal exti interrupts handlers
* api-hal-gpio: add exti interrupt handlers
* input: rework with api-hal-gpio interrupts
* one_wire_slave: rework with api-hal-gpio interrupts
* api-hal-gpio: fix incorrect exti line config
* api-hal-gpio: add doxygen documentation
* api-hal-gpio: add enable / disable interrupts
* api-hal-gpio: add get_rfid_level
* core: remove api-gpio
* applications: rework gpio with api-hal-gpio
* lib: rework gpio with api-hal-gpio
* rfal: disable exti interrupt when rfal is inactive
* rfal: add interrupt gpio reinitialization
* api-hal-gpio: hide setting speed and pull mode LL implementation
* stm32wbxx_it: remove unused EXTI handlers
* api-hal-gpio: guard set, enable, disable and remove interrupt
* Drop F4 target
* Accessor: update gpio api usage

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-04-29 11:51:48 +03:00
committed by GitHub
parent c3350990c2
commit b405a22cd1
147 changed files with 482 additions and 18140 deletions

View File

@@ -8,12 +8,11 @@ static osThreadAttr_t platform_irq_thread_attr;
static volatile osThreadId_t platform_irq_thread_id = NULL;
static volatile PlatformIrqCallback platform_irq_callback = NULL;
static ApiHalSpiDevice* platform_st25r3916 = NULL;
static const GpioPin pin = {ST25R_INT_PORT, ST25R_INT_PIN};
void nfc_isr(void* _pin, void* _ctx) {
uint32_t pin = (uint32_t)_pin;
if(pin == NFC_IRQ_Pin
&& platform_irq_callback
&& platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
void nfc_isr(void* _ctx) {
if(platform_irq_callback
&& platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
osThreadFlagsSet(platform_irq_thread_id, 0x1);
}
}
@@ -27,13 +26,26 @@ void platformIrqWorker() {
}
}
void platformEnableIrqCallback() {
hal_gpio_init(&pin, GpioModeInterruptRise, GpioPullNo, GpioSpeedLow);
hal_gpio_enable_int_callback(&pin);
}
void platformDisableIrqCallback() {
hal_gpio_init(&pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
hal_gpio_disable_int_callback(&pin);
}
void platformSetIrqCallback(PlatformIrqCallback callback) {
platform_irq_callback = callback;
platform_irq_thread_attr.name = "rfal_irq_worker";
platform_irq_thread_attr.stack_size = 1024;
platform_irq_thread_attr.priority = osPriorityISR;
platform_irq_thread_id = osThreadNew(platformIrqWorker, NULL, &platform_irq_thread_attr);
api_interrupt_add(nfc_isr, InterruptTypeExternalInterrupt, NULL);
hal_gpio_add_int_callback(&pin, nfc_isr, NULL);
// Disable interrupt callback as the pin is shared between 2 apps
// It is enabled in rfalLowPowerModeStop()
hal_gpio_disable_int_callback(&pin);
}
HAL_StatusTypeDef platformSpiTxRx(const uint8_t *txBuf, uint8_t *rxBuf, uint16_t len) {

View File

@@ -14,6 +14,8 @@
typedef void (*PlatformIrqCallback)();
void platformSetIrqCallback(PlatformIrqCallback cb);
void platformEnableIrqCallback();
void platformDisableIrqCallback();
HAL_StatusTypeDef platformSpiTxRx(const uint8_t *txBuf, uint8_t *rxBuf, uint16_t len);
void platformProtectST25RComm();

View File

@@ -4559,6 +4559,8 @@ ReturnCode rfalLowPowerModeStart( void )
gRFAL.state = RFAL_STATE_IDLE;
gRFAL.lpm.isRunning = true;
platformDisableIrqCallback();
return ERR_NONE;
}
@@ -4569,6 +4571,8 @@ ReturnCode rfalLowPowerModeStop( void )
{
ReturnCode ret;
platformEnableIrqCallback();
/* Check if RFAL is on right state */
if( !gRFAL.lpm.isRunning )
{

View File

@@ -47,16 +47,16 @@ void CyfralEmulator::send_byte(uint8_t data) {
void CyfralEmulator::send_bit(bool bit) {
if(!bit) {
gpio_write(&ibutton_gpio, false);
hal_gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
gpio_write(&ibutton_gpio, true);
hal_gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ZERO_HIGH);
gpio_write(&ibutton_gpio, false);
hal_gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
} else {
gpio_write(&ibutton_gpio, true);
hal_gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ONE_HIGH);
gpio_write(&ibutton_gpio, false);
hal_gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ONE_LOW);
}
}
@@ -87,10 +87,10 @@ void CyfralEmulator::send(uint8_t* data, uint8_t count, uint8_t repeat) {
}
void CyfralEmulator::start(void) {
gpio_init(emulate_pin_record, GpioModeOutputOpenDrain);
gpio_write(emulate_pin_record, false);
hal_gpio_init(emulate_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
hal_gpio_write(emulate_pin_record, false);
}
void CyfralEmulator::stop(void) {
gpio_init(emulate_pin_record, GpioModeAnalog);
hal_gpio_init(emulate_pin_record, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}

View File

@@ -231,13 +231,13 @@ void CyfralReaderComp::start(void) {
// pulldown lf-rfid pins to prevent interference
// TODO open record
GpioPin rfid_pull_pin = {.port = RFID_PULL_GPIO_Port, .pin = RFID_PULL_Pin};
gpio_init((GpioPin*)&rfid_pull_pin, GpioModeOutputOpenDrain);
gpio_write((GpioPin*)&rfid_pull_pin, false);
hal_gpio_init((GpioPin*)&rfid_pull_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
hal_gpio_write((GpioPin*)&rfid_pull_pin, false);
// TODO open record
GpioPin rfid_out_pin = {.port = RFID_OUT_GPIO_Port, .pin = RFID_OUT_Pin};
gpio_init((GpioPin*)&rfid_out_pin, GpioModeOutputOpenDrain);
gpio_write((GpioPin*)&rfid_out_pin, false);
hal_gpio_init((GpioPin*)&rfid_out_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
hal_gpio_write((GpioPin*)&rfid_out_pin, false);
// connect comparator callback
void* comp_ctx = this;

View File

@@ -43,7 +43,7 @@ public:
#include <api-hal.h>
void BlanksWriter::onewire_release(void) {
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
}
void BlanksWriter::onewire_write_one_bit(bool value, uint32_t delay = 10000) {

View File

@@ -11,11 +11,11 @@ OneWireMaster::~OneWireMaster() {
}
void OneWireMaster::start(void) {
gpio_init(gpio, GpioModeOutputOpenDrain);
hal_gpio_init(gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
}
void OneWireMaster::stop(void) {
gpio_init(gpio, GpioModeAnalog);
hal_gpio_init(gpio, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}
void OneWireMaster::reset_search() {
@@ -154,25 +154,25 @@ bool OneWireMaster::reset(void) {
uint8_t retries = 125;
// wait until the gpio is high
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
do {
if(--retries == 0) return 0;
delay_us(2);
} while(!gpio_read(gpio));
} while(!hal_gpio_read(gpio));
// pre delay
delay_us(OneWireTiming::RESET_DELAY_PRE);
// drive low
gpio_write(gpio, false);
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::RESET_DRIVE);
// release
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::RESET_RELEASE);
// read and post delay
r = !gpio_read(gpio);
r = !hal_gpio_read(gpio);
delay_us(OneWireTiming::RESET_DELAY_POST);
return r;
@@ -182,15 +182,15 @@ bool OneWireMaster::read_bit(void) {
bool result;
// drive low
gpio_write(gpio, false);
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::READ_DRIVE);
// release
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::READ_RELEASE);
// read and post delay
result = gpio_read(gpio);
result = hal_gpio_read(gpio);
delay_us(OneWireTiming::READ_DELAY_POST);
return result;
@@ -199,19 +199,19 @@ bool OneWireMaster::read_bit(void) {
void OneWireMaster::write_bit(bool value) {
if(value) {
// drive low
gpio_write(gpio, false);
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_1_DRIVE);
// release
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_1_RELEASE);
} else {
// drive low
gpio_write(gpio, false);
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_0_DRIVE);
// release
gpio_write(gpio, true);
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_0_RELEASE);
}
}

View File

@@ -7,10 +7,10 @@
void OneWireSlave::start(void) {
// add exti interrupt
api_interrupt_add(exti_cb, InterruptTypeExternalInterrupt, this);
hal_gpio_add_int_callback(one_wire_pin_record, exti_cb, this);
// init gpio
gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall);
hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
pin_set_float();
// init instructions per us count
@@ -19,15 +19,9 @@ void OneWireSlave::start(void) {
void OneWireSlave::stop(void) {
// deinit gpio
gpio_init_ex(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
// TODO change after gpio rework
// Clear EXTI registers
LL_EXTI_DisableRisingTrig_0_31(LL_EXTI_LINE_14);
LL_EXTI_DisableFallingTrig_0_31(LL_EXTI_LINE_14);
LL_EXTI_DisableIT_0_31(LL_EXTI_LINE_14);
hal_gpio_init(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
// remove exti interrupt
api_interrupt_remove(exti_cb, InterruptTypeExternalInterrupt);
hal_gpio_remove_int_callback(one_wire_pin_record);
// deattach devices
deattach();
@@ -60,11 +54,11 @@ void OneWireSlave::set_result_callback(OneWireSlaveResultCallback result_cb, voi
}
void OneWireSlave::pin_set_float() {
gpio_write(one_wire_pin_record, true);
hal_gpio_write(one_wire_pin_record, true);
}
void OneWireSlave::pin_set_low() {
gpio_write(one_wire_pin_record, false);
hal_gpio_write(one_wire_pin_record, false);
}
void OneWireSlave::pin_init_interrupt_in_isr_ctx(void) {
@@ -84,7 +78,7 @@ OneWiteTimeType OneWireSlave::wait_while_gpio_is(OneWiteTimeType time, const boo
do {
time_captured = DWT->CYCCNT;
if(gpio_read(one_wire_pin_record) != pin_value) {
if(hal_gpio_read(one_wire_pin_record) != pin_value) {
OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
remaining_time /= __instructions_per_us;
return remaining_time;
@@ -286,33 +280,29 @@ bool OneWireSlave::bus_start(void) {
return result;
}
void OneWireSlave::exti_callback(void* _pin, void* _ctx) {
// interrupt manager get us pin constant, so...
uint32_t pin = (uint32_t)_pin;
void OneWireSlave::exti_callback(void* _ctx) {
OneWireSlave* _this = static_cast<OneWireSlave*>(_ctx);
if(pin == _this->one_wire_pin_record->pin) {
volatile bool input_state = gpio_read(_this->one_wire_pin_record);
static uint32_t pulse_start = 0;
volatile bool input_state = hal_gpio_read(_this->one_wire_pin_record);
static uint32_t pulse_start = 0;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / __instructions_per_us;
if(pulse_length >= OWET::RESET_MIN) {
if(pulse_length <= OWET::RESET_MAX) {
// reset cycle ok
bool result = _this->bus_start();
if(result && _this->result_cb != nullptr) {
_this->result_cb(result, _this->result_cb_ctx);
}
} else {
error = OneWireSlaveError::VERY_LONG_RESET;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / __instructions_per_us;
if(pulse_length >= OWET::RESET_MIN) {
if(pulse_length <= OWET::RESET_MAX) {
// reset cycle ok
bool result = _this->bus_start();
if(result && _this->result_cb != nullptr) {
_this->result_cb(result, _this->result_cb_ctx);
}
} else {
error = OneWireSlaveError::VERY_SHORT_RESET;
error = OneWireSlaveError::VERY_LONG_RESET;
}
} else {
//FALL event
pulse_start = DWT->CYCCNT;
error = OneWireSlaveError::VERY_SHORT_RESET;
}
} else {
//FALL event
pulse_start = DWT->CYCCNT;
}
}

View File

@@ -30,8 +30,8 @@ private:
const GpioPin* one_wire_pin_record;
// exti callback and its pointer
void exti_callback(void* _pin, void* _ctx);
void (*exti_cb)(void* _pin, void* _ctx);
void exti_callback(void* _ctx);
void (*exti_cb)(void* _ctx);
uint32_t __instructions_per_us;