backlight_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "quantum.h"
  2. #include "backlight.h"
  3. #include "backlight_driver_common.h"
  4. #include "debug.h"
  5. #ifndef BACKLIGHT_GPT_DRIVER
  6. # define BACKLIGHT_GPT_DRIVER GPTD15
  7. #endif
  8. // Platform specific implementations
  9. static void backlight_timer_configure(bool enable);
  10. static void backlight_timer_set_duty(uint16_t duty);
  11. static uint16_t backlight_timer_get_duty(void);
  12. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  13. static uint16_t cie_lightness(uint16_t v) {
  14. if (v <= 5243) // if below 8% of max
  15. return v / 9; // same as dividing by 900%
  16. else {
  17. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  18. // to get a useful result with integer division, we shift left in the expression above
  19. // and revert what we've done again after squaring.
  20. y = y * y * y >> 8;
  21. if (y > 0xFFFFUL) // prevent overflow
  22. return 0xFFFFU;
  23. else
  24. return (uint16_t)y;
  25. }
  26. }
  27. void backlight_init_ports(void) {
  28. backlight_pins_init();
  29. backlight_set(get_backlight_level());
  30. #ifdef BACKLIGHT_BREATHING
  31. if (is_backlight_breathing()) {
  32. breathing_enable();
  33. }
  34. #endif
  35. }
  36. void backlight_set(uint8_t level) {
  37. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  38. backlight_pins_off();
  39. backlight_timer_set_duty(cie_lightness(0xFFFFU / BACKLIGHT_LEVELS * level));
  40. backlight_timer_configure(level != 0);
  41. }
  42. static void backlight_timer_top(void) {
  43. #ifdef BACKLIGHT_BREATHING
  44. if (is_breathing()) {
  45. breathing_task();
  46. }
  47. #endif
  48. if (backlight_timer_get_duty() > 256) {
  49. backlight_pins_on();
  50. }
  51. }
  52. static void backlight_timer_cmp(void) {
  53. backlight_pins_off();
  54. }
  55. void backlight_task(void) {}
  56. #ifdef BACKLIGHT_BREATHING
  57. # define BREATHING_STEPS 128
  58. static bool breathing = false;
  59. static uint16_t breathing_counter = 0;
  60. /* To generate breathing curve in python:
  61. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  62. */
  63. static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  64. // Use this before the cie_lightness function.
  65. static inline uint16_t scale_backlight(uint16_t v) {
  66. return v / BACKLIGHT_LEVELS * get_backlight_level();
  67. }
  68. void breathing_task(void) {
  69. uint8_t breathing_period = get_breathing_period();
  70. uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
  71. // resetting after one period to prevent ugly reset at overflow.
  72. breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
  73. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  74. // printf("index:%u\n", index);
  75. backlight_timer_set_duty(cie_lightness(scale_backlight((uint16_t)breathing_table[index] * 256)));
  76. }
  77. bool is_breathing(void) {
  78. return breathing;
  79. }
  80. void breathing_enable(void) {
  81. breathing_counter = 0;
  82. breathing = true;
  83. }
  84. void breathing_disable(void) {
  85. breathing = false;
  86. }
  87. void breathing_pulse(void) {
  88. backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS);
  89. wait_ms(10);
  90. backlight_set(is_backlight_enabled() ? get_backlight_level() : 0);
  91. }
  92. #endif
  93. #ifdef PROTOCOL_CHIBIOS
  94. // On Platforms where timers fire every tick and have no capture/top events
  95. // - fake event in the normal timer callback
  96. uint16_t s_duty = 0;
  97. static void timerCallback(void) {
  98. /* Software PWM
  99. * timer:1111 1111 1111 1111
  100. * \______/| \_______/____ count(0-255)
  101. * \ \______________ unused(1)
  102. * \__________________ index of step table(0-127)
  103. */
  104. // this works for cca 65536 irqs/sec
  105. static union {
  106. uint16_t raw;
  107. struct {
  108. uint16_t count : 8;
  109. uint8_t dummy : 1;
  110. uint8_t index : 7;
  111. } pwm;
  112. } timer = {.raw = 0};
  113. timer.raw++;
  114. if (timer.pwm.count == 0) {
  115. // LED on
  116. backlight_timer_top();
  117. } else if (timer.pwm.count == (s_duty / 256)) {
  118. // LED off
  119. backlight_timer_cmp();
  120. }
  121. }
  122. static void backlight_timer_set_duty(uint16_t duty) {
  123. s_duty = duty;
  124. }
  125. static uint16_t backlight_timer_get_duty(void) {
  126. return s_duty;
  127. }
  128. // ChibiOS - Map GPT timer onto Software PWM
  129. static void gptTimerCallback(GPTDriver *gptp) {
  130. (void)gptp;
  131. timerCallback();
  132. }
  133. static void backlight_timer_configure(bool enable) {
  134. static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
  135. static bool s_init = false;
  136. if (!s_init) {
  137. gptStart(&BACKLIGHT_GPT_DRIVER, &gptcfg);
  138. s_init = true;
  139. }
  140. if (enable) {
  141. gptStartContinuous(&BACKLIGHT_GPT_DRIVER, gptcfg.frequency / 0xFFFF);
  142. } else {
  143. gptStopTimer(&BACKLIGHT_GPT_DRIVER);
  144. }
  145. }
  146. #endif