mp_flipper_modflipperzero_pwm.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <furi_hal.h>
  2. #include <furi_hal_pwm.h>
  3. #include <mp_flipper_modflipperzero.h>
  4. #include <mp_flipper_runtime.h>
  5. #include "mp_flipper_context.h"
  6. #define NO_VALUE (-1)
  7. inline static const FuriHalPwmOutputId decode_pin_to_output_id(uint8_t pin) {
  8. switch(pin) {
  9. case MP_FLIPPER_GPIO_PIN_PA4:
  10. return FuriHalPwmOutputIdLptim2PA4;
  11. case MP_FLIPPER_GPIO_PIN_PA7:
  12. return FuriHalPwmOutputIdTim1PA7;
  13. default:
  14. return NO_VALUE;
  15. }
  16. }
  17. inline bool mp_flipper_pwm_start(uint8_t raw_pin, uint32_t frequency, uint8_t duty) {
  18. const mp_flipper_context_t* ctx = mp_flipper_context;
  19. FuriHalPwmOutputId channel = decode_pin_to_output_id(raw_pin);
  20. if(channel == NO_VALUE) {
  21. return false;
  22. }
  23. if(ctx->gpio_pins[raw_pin] != MP_FLIPPER_GPIO_PIN_OFF &&
  24. ctx->gpio_pins[raw_pin] != MP_FLIPPER_GPIO_PIN_PWM) {
  25. return false;
  26. }
  27. if(ctx->gpio_pins[raw_pin] == MP_FLIPPER_GPIO_PIN_OFF) {
  28. furi_hal_pwm_start(channel, frequency, duty);
  29. } else {
  30. furi_hal_pwm_set_params(channel, frequency, duty);
  31. }
  32. ctx->gpio_pins[raw_pin] = MP_FLIPPER_GPIO_PIN_PWM;
  33. return true;
  34. }
  35. inline void mp_flipper_pwm_stop(uint8_t raw_pin) {
  36. const mp_flipper_context_t* ctx = mp_flipper_context;
  37. FuriHalPwmOutputId channel = decode_pin_to_output_id(raw_pin);
  38. if(channel == NO_VALUE) {
  39. return;
  40. }
  41. if(ctx->gpio_pins[raw_pin] != MP_FLIPPER_GPIO_PIN_PWM) {
  42. return;
  43. }
  44. furi_hal_pwm_stop(channel);
  45. ctx->gpio_pins[raw_pin] = MP_FLIPPER_GPIO_PIN_OFF;
  46. }
  47. inline bool mp_flipper_pwm_is_running(uint8_t raw_pin) {
  48. const mp_flipper_context_t* ctx = mp_flipper_context;
  49. FuriHalPwmOutputId channel = decode_pin_to_output_id(raw_pin);
  50. if(channel == NO_VALUE) {
  51. return false;
  52. }
  53. if(ctx->gpio_pins[raw_pin] != MP_FLIPPER_GPIO_PIN_PWM) {
  54. return false;
  55. }
  56. return furi_hal_pwm_is_running(channel);
  57. }