2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_ibutton.h>
|
2022-03-25 10:33:01 +00:00
|
|
|
#include <furi_hal_interrupt.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_resources.h>
|
2021-09-10 02:19:02 +00:00
|
|
|
|
2022-03-25 10:33:01 +00:00
|
|
|
#include <stm32wbxx_ll_tim.h>
|
2022-03-29 13:01:56 +00:00
|
|
|
#include <stm32wbxx_ll_exti.h>
|
2022-03-25 10:33:01 +00:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#define FURI_HAL_IBUTTON_TIMER TIM1
|
2022-03-29 17:37:23 +00:00
|
|
|
#define FURI_HAL_IBUTTON_TIMER_IRQ FuriHalInterruptIdTim1UpTim16
|
2022-03-25 10:33:01 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
FuriHalIbuttonStateIdle,
|
|
|
|
FuriHalIbuttonStateRunning,
|
|
|
|
} FuriHalIbuttonState;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriHalIbuttonState state;
|
|
|
|
FuriHalIbuttonEmulateCallback callback;
|
|
|
|
void* context;
|
|
|
|
} FuriHalIbutton;
|
|
|
|
|
|
|
|
FuriHalIbutton* furi_hal_ibutton = NULL;
|
|
|
|
|
|
|
|
static void furi_hal_ibutton_emulate_isr() {
|
|
|
|
if(LL_TIM_IsActiveFlag_UPDATE(FURI_HAL_IBUTTON_TIMER)) {
|
|
|
|
LL_TIM_ClearFlag_UPDATE(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
furi_hal_ibutton->callback(furi_hal_ibutton->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_init() {
|
|
|
|
furi_hal_ibutton = malloc(sizeof(FuriHalIbutton));
|
|
|
|
furi_hal_ibutton->state = FuriHalIbuttonStateIdle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_emulate_start(
|
|
|
|
uint32_t period,
|
|
|
|
FuriHalIbuttonEmulateCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(furi_hal_ibutton);
|
|
|
|
furi_assert(furi_hal_ibutton->state == FuriHalIbuttonStateIdle);
|
|
|
|
|
|
|
|
furi_hal_ibutton->state = FuriHalIbuttonStateRunning;
|
|
|
|
furi_hal_ibutton->callback = callback;
|
|
|
|
furi_hal_ibutton->context = context;
|
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
LL_TIM_DeInit(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
2022-03-29 17:37:23 +00:00
|
|
|
furi_hal_interrupt_set_isr(FURI_HAL_IBUTTON_TIMER_IRQ, furi_hal_ibutton_emulate_isr, NULL);
|
2022-03-28 13:42:31 +00:00
|
|
|
|
2022-03-25 10:33:01 +00:00
|
|
|
LL_TIM_SetPrescaler(FURI_HAL_IBUTTON_TIMER, 0);
|
|
|
|
LL_TIM_SetCounterMode(FURI_HAL_IBUTTON_TIMER, LL_TIM_COUNTERMODE_UP);
|
|
|
|
LL_TIM_SetAutoReload(FURI_HAL_IBUTTON_TIMER, period);
|
|
|
|
LL_TIM_DisableARRPreload(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
LL_TIM_SetRepetitionCounter(FURI_HAL_IBUTTON_TIMER, 0);
|
|
|
|
|
|
|
|
LL_TIM_SetClockDivision(FURI_HAL_IBUTTON_TIMER, LL_TIM_CLOCKDIVISION_DIV1);
|
|
|
|
LL_TIM_SetClockSource(FURI_HAL_IBUTTON_TIMER, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_GenerateEvent_UPDATE(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
|
|
|
|
LL_TIM_EnableIT_UPDATE(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
|
|
|
|
LL_TIM_EnableCounter(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_emulate_set_next(uint32_t period) {
|
|
|
|
LL_TIM_SetAutoReload(FURI_HAL_IBUTTON_TIMER, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_emulate_stop() {
|
|
|
|
furi_assert(furi_hal_ibutton);
|
|
|
|
|
|
|
|
if(furi_hal_ibutton->state == FuriHalIbuttonStateRunning) {
|
|
|
|
furi_hal_ibutton->state = FuriHalIbuttonStateIdle;
|
|
|
|
LL_TIM_DisableCounter(FURI_HAL_IBUTTON_TIMER);
|
2022-03-28 13:42:31 +00:00
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
LL_TIM_DeInit(FURI_HAL_IBUTTON_TIMER);
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
2022-03-29 17:37:23 +00:00
|
|
|
furi_hal_interrupt_set_isr(FURI_HAL_IBUTTON_TIMER_IRQ, NULL, NULL);
|
2022-03-25 10:33:01 +00:00
|
|
|
|
|
|
|
furi_hal_ibutton->callback = NULL;
|
|
|
|
furi_hal_ibutton->context = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
void furi_hal_ibutton_start_drive() {
|
2021-09-10 02:19:02 +00:00
|
|
|
furi_hal_ibutton_pin_high();
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&ibutton_gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
void furi_hal_ibutton_start_drive_in_isr() {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&ibutton_gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
2022-03-29 13:01:56 +00:00
|
|
|
LL_EXTI_ClearFlag_0_31(ibutton_gpio.pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_start_interrupt() {
|
|
|
|
furi_hal_ibutton_pin_high();
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&ibutton_gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_start_interrupt_in_isr() {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&ibutton_gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
2022-03-29 13:01:56 +00:00
|
|
|
LL_EXTI_ClearFlag_0_31(ibutton_gpio.pin);
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
void furi_hal_ibutton_stop() {
|
|
|
|
furi_hal_ibutton_pin_high();
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init(&ibutton_gpio, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
void furi_hal_ibutton_add_interrupt(GpioExtiCallback cb, void* context) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_add_int_callback(&ibutton_gpio, cb, context);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_remove_interrupt() {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_remove_int_callback(&ibutton_gpio);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 02:19:02 +00:00
|
|
|
void furi_hal_ibutton_pin_low() {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_write(&ibutton_gpio, false);
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_ibutton_pin_high() {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_write(&ibutton_gpio, true);
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_ibutton_pin_get_level() {
|
2022-03-30 15:23:40 +00:00
|
|
|
return furi_hal_gpio_read(&ibutton_gpio);
|
2021-09-10 02:19:02 +00:00
|
|
|
}
|