furi_hal_ibutton.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <furi_hal_ibutton.h>
  2. #include <furi_hal_interrupt.h>
  3. #include <furi_hal_resources.h>
  4. #include <stm32wbxx_ll_tim.h>
  5. #include <stm32wbxx_ll_exti.h>
  6. #include <furi.h>
  7. #define FURI_HAL_IBUTTON_TIMER TIM1
  8. #define FURI_HAL_IBUTTON_TIMER_IRQ FuriHalInterruptIdTim1UpTim16
  9. typedef enum {
  10. FuriHalIbuttonStateIdle,
  11. FuriHalIbuttonStateRunning,
  12. } FuriHalIbuttonState;
  13. typedef struct {
  14. FuriHalIbuttonState state;
  15. FuriHalIbuttonEmulateCallback callback;
  16. void* context;
  17. } FuriHalIbutton;
  18. FuriHalIbutton* furi_hal_ibutton = NULL;
  19. static void furi_hal_ibutton_emulate_isr() {
  20. if(LL_TIM_IsActiveFlag_UPDATE(FURI_HAL_IBUTTON_TIMER)) {
  21. LL_TIM_ClearFlag_UPDATE(FURI_HAL_IBUTTON_TIMER);
  22. furi_hal_ibutton->callback(furi_hal_ibutton->context);
  23. }
  24. }
  25. void furi_hal_ibutton_init() {
  26. furi_hal_ibutton = malloc(sizeof(FuriHalIbutton));
  27. furi_hal_ibutton->state = FuriHalIbuttonStateIdle;
  28. }
  29. void furi_hal_ibutton_emulate_start(
  30. uint32_t period,
  31. FuriHalIbuttonEmulateCallback callback,
  32. void* context) {
  33. furi_assert(furi_hal_ibutton);
  34. furi_assert(furi_hal_ibutton->state == FuriHalIbuttonStateIdle);
  35. furi_hal_ibutton->state = FuriHalIbuttonStateRunning;
  36. furi_hal_ibutton->callback = callback;
  37. furi_hal_ibutton->context = context;
  38. FURI_CRITICAL_ENTER();
  39. LL_TIM_DeInit(FURI_HAL_IBUTTON_TIMER);
  40. FURI_CRITICAL_EXIT();
  41. furi_hal_interrupt_set_isr(FURI_HAL_IBUTTON_TIMER_IRQ, furi_hal_ibutton_emulate_isr, NULL);
  42. LL_TIM_SetPrescaler(FURI_HAL_IBUTTON_TIMER, 0);
  43. LL_TIM_SetCounterMode(FURI_HAL_IBUTTON_TIMER, LL_TIM_COUNTERMODE_UP);
  44. LL_TIM_SetAutoReload(FURI_HAL_IBUTTON_TIMER, period);
  45. LL_TIM_DisableARRPreload(FURI_HAL_IBUTTON_TIMER);
  46. LL_TIM_SetRepetitionCounter(FURI_HAL_IBUTTON_TIMER, 0);
  47. LL_TIM_SetClockDivision(FURI_HAL_IBUTTON_TIMER, LL_TIM_CLOCKDIVISION_DIV1);
  48. LL_TIM_SetClockSource(FURI_HAL_IBUTTON_TIMER, LL_TIM_CLOCKSOURCE_INTERNAL);
  49. LL_TIM_GenerateEvent_UPDATE(FURI_HAL_IBUTTON_TIMER);
  50. LL_TIM_EnableIT_UPDATE(FURI_HAL_IBUTTON_TIMER);
  51. LL_TIM_EnableCounter(FURI_HAL_IBUTTON_TIMER);
  52. }
  53. void furi_hal_ibutton_emulate_set_next(uint32_t period) {
  54. LL_TIM_SetAutoReload(FURI_HAL_IBUTTON_TIMER, period);
  55. }
  56. void furi_hal_ibutton_emulate_stop() {
  57. furi_assert(furi_hal_ibutton);
  58. if(furi_hal_ibutton->state == FuriHalIbuttonStateRunning) {
  59. furi_hal_ibutton->state = FuriHalIbuttonStateIdle;
  60. LL_TIM_DisableCounter(FURI_HAL_IBUTTON_TIMER);
  61. FURI_CRITICAL_ENTER();
  62. LL_TIM_DeInit(FURI_HAL_IBUTTON_TIMER);
  63. FURI_CRITICAL_EXIT();
  64. furi_hal_interrupt_set_isr(FURI_HAL_IBUTTON_TIMER_IRQ, NULL, NULL);
  65. furi_hal_ibutton->callback = NULL;
  66. furi_hal_ibutton->context = NULL;
  67. }
  68. }
  69. void furi_hal_ibutton_start_drive() {
  70. furi_hal_ibutton_pin_high();
  71. furi_hal_gpio_init(&ibutton_gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  72. }
  73. void furi_hal_ibutton_start_drive_in_isr() {
  74. furi_hal_gpio_init(&ibutton_gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  75. LL_EXTI_ClearFlag_0_31(ibutton_gpio.pin);
  76. }
  77. void furi_hal_ibutton_start_interrupt() {
  78. furi_hal_ibutton_pin_high();
  79. furi_hal_gpio_init(&ibutton_gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  80. }
  81. void furi_hal_ibutton_start_interrupt_in_isr() {
  82. furi_hal_gpio_init(&ibutton_gpio, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  83. LL_EXTI_ClearFlag_0_31(ibutton_gpio.pin);
  84. }
  85. void furi_hal_ibutton_stop() {
  86. furi_hal_ibutton_pin_high();
  87. furi_hal_gpio_init(&ibutton_gpio, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  88. }
  89. void furi_hal_ibutton_add_interrupt(GpioExtiCallback cb, void* context) {
  90. furi_hal_gpio_add_int_callback(&ibutton_gpio, cb, context);
  91. }
  92. void furi_hal_ibutton_remove_interrupt() {
  93. furi_hal_gpio_remove_int_callback(&ibutton_gpio);
  94. }
  95. void furi_hal_ibutton_pin_low() {
  96. furi_hal_gpio_write(&ibutton_gpio, false);
  97. }
  98. void furi_hal_ibutton_pin_high() {
  99. furi_hal_gpio_write(&ibutton_gpio, true);
  100. }
  101. bool furi_hal_ibutton_pin_get_level() {
  102. return furi_hal_gpio_read(&ibutton_gpio);
  103. }