2
0

led.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "common.h"
  2. #include "led.h"
  3. #include <driver/ledc.h>
  4. /*
  5. * Note: the Arduino HAL doesn't handle low frequencies, because it
  6. * seems to insist on using a high frequency clock source...
  7. */
  8. #define LED_PIN_BASE 2
  9. void led_set(int led, enum led_mode mode)
  10. {
  11. static const struct {
  12. uint32_t freq;
  13. uint32_t duty;
  14. } led_modes[] = {
  15. { 1, 0 }, /* Off */
  16. { 1, 1 << 12 }, /* On */
  17. { 2, 1 << 11 }, /* Medium blink (2 Hz) */
  18. { 1, 1 << 11 }, /* Slow blink (1 Hz) */
  19. { 4, 1 << 11 } /* Fast blink (4 Hz) */
  20. };
  21. ledc_channel_config_t channel_config = {
  22. .speed_mode = LEDC_LOW_SPEED_MODE,
  23. .hpoint = 0,
  24. .flags.output_invert = 0,
  25. .intr_type = LEDC_INTR_DISABLE,
  26. .channel = led,
  27. .timer_sel = led,
  28. .gpio_num = led + LED_PIN_BASE,
  29. .duty = led_modes[mode].duty
  30. };
  31. ledc_timer_config_t timer_config = {
  32. .speed_mode = LEDC_LOW_SPEED_MODE,
  33. .duty_resolution = 12,
  34. .clk_cfg = LEDC_USE_REF_TICK,
  35. .timer_num = led,
  36. .freq_hz = led_modes[mode].freq
  37. };
  38. ledc_timer_config(&timer_config);
  39. ledc_channel_config(&channel_config);
  40. }
  41. void led_init(void)
  42. {
  43. for (int led = 0; led < 3; led++)
  44. led_set(led, LED_OFF);
  45. }