clock_timer.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * NOTE TO ANYONE USING THIS CODE
  3. *
  4. * The following is verbatim from the flipperzero-game-engine codebase and
  5. * exists with a separate license, GPL-3.0
  6. *
  7. * While the flipper-gblink is BSD-2, this project is always open-source.
  8. *
  9. * If you are using the flipper-gblink library in another project that is not
  10. * open-source, then you should review the GPL-3.0 text to ensure you abide by
  11. * the terms of the license before releasing a binary made with this code and
  12. * not including the source alongside it.
  13. *
  14. */
  15. #include "clock_timer.h"
  16. #include <stdlib.h>
  17. #include <furi_hal_interrupt.h>
  18. #include <furi_hal_bus.h>
  19. #include <stm32wbxx_ll_tim.h>
  20. #define FURI_HAL_CLOCK_TIMER TIM2
  21. #define FURI_HAL_CLOCK_TIMER_BUS FuriHalBusTIM2
  22. #define FURI_HAL_CLOCK_TIMER_IRQ FuriHalInterruptIdTIM2
  23. typedef struct {
  24. ClockTimerCallback callback;
  25. void* context;
  26. } ClockTimer;
  27. static ClockTimer clock_timer = {
  28. .callback = NULL,
  29. .context = NULL,
  30. };
  31. static void clock_timer_isr(void* context) {
  32. if(clock_timer.callback) {
  33. clock_timer.callback(context);
  34. }
  35. LL_TIM_ClearFlag_UPDATE(FURI_HAL_CLOCK_TIMER);
  36. }
  37. void clock_timer_start(ClockTimerCallback callback, void* context, float period) {
  38. clock_timer.callback = callback;
  39. clock_timer.context = context;
  40. furi_hal_bus_enable(FURI_HAL_CLOCK_TIMER_BUS);
  41. // init timer to produce interrupts
  42. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  43. TIM_InitStruct.Autoreload = (SystemCoreClock / period) - 1;
  44. LL_TIM_Init(FURI_HAL_CLOCK_TIMER, &TIM_InitStruct);
  45. furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, clock_timer_isr, clock_timer.context);
  46. LL_TIM_EnableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
  47. LL_TIM_EnableCounter(FURI_HAL_CLOCK_TIMER);
  48. }
  49. void clock_timer_stop(void) {
  50. LL_TIM_DisableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
  51. LL_TIM_DisableCounter(FURI_HAL_CLOCK_TIMER);
  52. furi_hal_bus_disable(FURI_HAL_CLOCK_TIMER_BUS);
  53. furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, NULL, NULL);
  54. }