led.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "common.h"
  2. #include "pins.h"
  3. #include "led.h"
  4. #include <driver/ledc.h>
  5. /*
  6. * Note: the Arduino HAL doesn't handle low frequencies, because it
  7. * seems to insist on using a high frequency clock source...
  8. */
  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 + PIN_LED_0,
  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. }