backlight_arm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "quantum.h"
  2. #include "backlight.h"
  3. #include <hal.h>
  4. #include "debug.h"
  5. // TODO: remove short term bodge when refactoring BACKLIGHT_CUSTOM_DRIVER out
  6. #ifdef BACKLIGHT_PIN
  7. # if defined(STM32F0XX) || defined(STM32F0xx)
  8. # error "Backlight support for STMF072 is not available. Please disable."
  9. # endif
  10. # if defined(STM32F1XX) || defined(STM32F1xx)
  11. # define USE_GPIOV1
  12. # endif
  13. // GPIOV2 && GPIOV3
  14. # ifndef BACKLIGHT_PAL_MODE
  15. # define BACKLIGHT_PAL_MODE 2
  16. # endif
  17. // GENERIC
  18. # ifndef BACKLIGHT_PWM_DRIVER
  19. # define BACKLIGHT_PWM_DRIVER PWMD4
  20. # endif
  21. # ifndef BACKLIGHT_PWM_CHANNEL
  22. # define BACKLIGHT_PWM_CHANNEL 3
  23. # endif
  24. static void breathing_callback(PWMDriver *pwmp);
  25. static PWMConfig pwmCFG = {0xFFFF, /* PWM clock frequency */
  26. 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
  27. NULL, /* No Callback */
  28. { /* Default all channels to disabled - Channels will be configured durring init */
  29. {PWM_OUTPUT_DISABLED, NULL},
  30. {PWM_OUTPUT_DISABLED, NULL},
  31. {PWM_OUTPUT_DISABLED, NULL},
  32. {PWM_OUTPUT_DISABLED, NULL}},
  33. 0, /* HW dependent part.*/
  34. 0};
  35. static PWMConfig pwmCFG_breathing = {0xFFFF, /** PWM clock frequency */
  36. 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
  37. breathing_callback, /* Breathing Callback */
  38. { /* Default all channels to disabled - Channels will be configured durring init */
  39. {PWM_OUTPUT_DISABLED, NULL},
  40. {PWM_OUTPUT_DISABLED, NULL},
  41. {PWM_OUTPUT_DISABLED, NULL},
  42. {PWM_OUTPUT_DISABLED, NULL}},
  43. 0, /* HW dependent part.*/
  44. 0};
  45. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  46. static uint16_t cie_lightness(uint16_t v) {
  47. if (v <= 5243) // if below 8% of max
  48. return v / 9; // same as dividing by 900%
  49. else {
  50. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  51. // to get a useful result with integer division, we shift left in the expression above
  52. // and revert what we've done again after squaring.
  53. y = y * y * y >> 8;
  54. if (y > 0xFFFFUL) // prevent overflow
  55. return 0xFFFFU;
  56. else
  57. return (uint16_t)y;
  58. }
  59. }
  60. void backlight_init_ports(void) {
  61. // printf("backlight_init_ports()\n");
  62. # ifdef USE_GPIOV1
  63. palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  64. # else
  65. palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_ALTERNATE(BACKLIGHT_PAL_MODE));
  66. # endif
  67. pwmCFG.channels[BACKLIGHT_PWM_CHANNEL - 1].mode = PWM_OUTPUT_ACTIVE_HIGH;
  68. pwmCFG_breathing.channels[BACKLIGHT_PWM_CHANNEL - 1].mode = PWM_OUTPUT_ACTIVE_HIGH;
  69. pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG);
  70. backlight_set(get_backlight_level());
  71. if (is_backlight_breathing()) {
  72. breathing_enable();
  73. }
  74. }
  75. void backlight_set(uint8_t level) {
  76. // printf("backlight_set(%d)\n", level);
  77. if (level == 0) {
  78. // Turn backlight off
  79. pwmDisableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1);
  80. } else {
  81. // Turn backlight on
  82. if (!is_breathing()) {
  83. uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS));
  84. // printf("duty: (%d)\n", duty);
  85. pwmEnableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
  86. }
  87. }
  88. }
  89. uint8_t backlight_tick = 0;
  90. void backlight_task(void) {}
  91. # define BREATHING_NO_HALT 0
  92. # define BREATHING_HALT_OFF 1
  93. # define BREATHING_HALT_ON 2
  94. # define BREATHING_STEPS 128
  95. static uint8_t breathing_period = BREATHING_PERIOD;
  96. static uint8_t breathing_halt = BREATHING_NO_HALT;
  97. static uint16_t breathing_counter = 0;
  98. bool is_breathing(void) { return BACKLIGHT_PWM_DRIVER.config == &pwmCFG_breathing; }
  99. static inline void breathing_min(void) { breathing_counter = 0; }
  100. static inline void breathing_max(void) { breathing_counter = breathing_period * 256 / 2; }
  101. void breathing_interrupt_enable(void) {
  102. pwmStop(&BACKLIGHT_PWM_DRIVER);
  103. pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG_breathing);
  104. chSysLockFromISR();
  105. pwmEnablePeriodicNotification(&BACKLIGHT_PWM_DRIVER);
  106. pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, 0xFFFF));
  107. chSysUnlockFromISR();
  108. }
  109. void breathing_interrupt_disable(void) {
  110. pwmStop(&BACKLIGHT_PWM_DRIVER);
  111. pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG);
  112. }
  113. void breathing_enable(void) {
  114. breathing_counter = 0;
  115. breathing_halt = BREATHING_NO_HALT;
  116. breathing_interrupt_enable();
  117. }
  118. void breathing_pulse(void) {
  119. if (get_backlight_level() == 0)
  120. breathing_min();
  121. else
  122. breathing_max();
  123. breathing_halt = BREATHING_HALT_ON;
  124. breathing_interrupt_enable();
  125. }
  126. void breathing_disable(void) {
  127. // printf("breathing_disable()\n");
  128. breathing_interrupt_disable();
  129. // Restore backlight level
  130. backlight_set(get_backlight_level());
  131. }
  132. void breathing_self_disable(void) {
  133. if (get_backlight_level() == 0)
  134. breathing_halt = BREATHING_HALT_OFF;
  135. else
  136. breathing_halt = BREATHING_HALT_ON;
  137. }
  138. void breathing_toggle(void) {
  139. if (is_breathing())
  140. breathing_disable();
  141. else
  142. breathing_enable();
  143. }
  144. void breathing_period_set(uint8_t value) {
  145. if (!value) value = 1;
  146. breathing_period = value;
  147. }
  148. void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }
  149. void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }
  150. void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }
  151. /* To generate breathing curve in python:
  152. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  153. */
  154. 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};
  155. // Use this before the cie_lightness function.
  156. static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
  157. static void breathing_callback(PWMDriver *pwmp) {
  158. (void)pwmp;
  159. uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
  160. // resetting after one period to prevent ugly reset at overflow.
  161. breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
  162. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  163. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  164. breathing_interrupt_disable();
  165. }
  166. uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
  167. chSysLockFromISR();
  168. pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
  169. chSysUnlockFromISR();
  170. }
  171. #else
  172. __attribute__((weak)) void backlight_init_ports(void) {}
  173. __attribute__((weak)) void backlight_set(uint8_t level) {}
  174. __attribute__((weak)) void backlight_task(void) {}
  175. #endif