backlight_chibios.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "quantum.h"
  2. #include "backlight.h"
  3. #include <hal.h>
  4. #include "debug.h"
  5. // Maximum duty cycle limit
  6. #ifndef BACKLIGHT_LIMIT_VAL
  7. # define BACKLIGHT_LIMIT_VAL 255
  8. #endif
  9. #ifndef BACKLIGHT_PAL_MODE
  10. # if defined(USE_GPIOV1)
  11. # define BACKLIGHT_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  12. # else
  13. // GPIOV2 && GPIOV3
  14. # define BACKLIGHT_PAL_MODE 5
  15. # endif
  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. // Support for pins which are on TIM1_CH1N - requires STM32_PWM_USE_ADVANCED
  25. #ifdef BACKLIGHT_PWM_COMPLEMENTARY_OUTPUT
  26. # if BACKLIGHT_ON_STATE == 1
  27. # define PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW;
  28. # else
  29. # define PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH;
  30. # endif
  31. #else
  32. # if BACKLIGHT_ON_STATE == 1
  33. # define PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH;
  34. # else
  35. # define PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_LOW;
  36. # endif
  37. #endif
  38. #ifndef BACKLIGHT_PWM_COUNTER_FREQUENCY
  39. # define BACKLIGHT_PWM_COUNTER_FREQUENCY 0xFFFF
  40. #endif
  41. #ifndef BACKLIGHT_PWM_PERIOD
  42. # define BACKLIGHT_PWM_PERIOD 256
  43. #endif
  44. static PWMConfig pwmCFG = {
  45. .frequency = BACKLIGHT_PWM_COUNTER_FREQUENCY, /* PWM clock frequency */
  46. .period = BACKLIGHT_PWM_PERIOD, /* PWM period in counter ticks. e.g. clock frequency is 10KHz, period is 256 ticks then t_period is 25.6ms */
  47. };
  48. #ifdef BACKLIGHT_BREATHING
  49. static virtual_timer_t breathing_vt;
  50. #endif
  51. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  52. static uint16_t cie_lightness(uint16_t v) {
  53. if (v <= 5243) // if below 8% of max
  54. return v / 9; // same as dividing by 900%
  55. else {
  56. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  57. // to get a useful result with integer division, we shift left in the expression above
  58. // and revert what we've done again after squaring.
  59. y = y * y * y >> 8;
  60. if (y > 0xFFFFUL) { // prevent overflow
  61. return 0xFFFFU;
  62. } else {
  63. return (uint16_t)y;
  64. }
  65. }
  66. }
  67. static uint32_t rescale_limit_val(uint32_t val) {
  68. // rescale the supplied backlight value to be in terms of the value limit
  69. return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
  70. }
  71. void backlight_init_ports(void) {
  72. #ifdef USE_GPIOV1
  73. palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), BACKLIGHT_PAL_MODE);
  74. #else
  75. palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_ALTERNATE(BACKLIGHT_PAL_MODE));
  76. #endif
  77. pwmCFG.channels[BACKLIGHT_PWM_CHANNEL - 1].mode = PWM_OUTPUT_MODE;
  78. pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG);
  79. backlight_set(get_backlight_level());
  80. #ifdef BACKLIGHT_BREATHING
  81. chVTObjectInit(&breathing_vt);
  82. if (is_backlight_breathing()) {
  83. breathing_enable();
  84. }
  85. #endif
  86. }
  87. void backlight_set(uint8_t level) {
  88. if (level > BACKLIGHT_LEVELS) {
  89. level = BACKLIGHT_LEVELS;
  90. }
  91. if (level == 0) {
  92. // Turn backlight off
  93. pwmDisableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1);
  94. } else {
  95. // Turn backlight on
  96. uint32_t duty = (uint32_t)(cie_lightness(rescale_limit_val(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS)));
  97. pwmEnableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
  98. }
  99. }
  100. void backlight_task(void) {}
  101. #ifdef BACKLIGHT_BREATHING
  102. # define BREATHING_STEPS 128
  103. /* To generate breathing curve in python:
  104. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  105. */
  106. 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};
  107. static void breathing_callback(virtual_timer_t *vtp, void *p);
  108. bool is_breathing(void) {
  109. return chVTIsArmed(&breathing_vt);
  110. }
  111. void breathing_enable(void) {
  112. /* Update frequency is 256Hz -> 3906us intervals */
  113. chVTSetContinuous(&breathing_vt, TIME_US2I(3906), breathing_callback, NULL);
  114. }
  115. void breathing_disable(void) {
  116. chVTReset(&breathing_vt);
  117. // Restore backlight level
  118. backlight_set(get_backlight_level());
  119. }
  120. // Use this before the cie_lightness function.
  121. static inline uint16_t scale_backlight(uint16_t v) {
  122. return v / BACKLIGHT_LEVELS * get_backlight_level();
  123. }
  124. static void breathing_callback(virtual_timer_t *vtp, void *p) {
  125. uint8_t breathing_period = get_breathing_period();
  126. uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
  127. // resetting after one period to prevent ugly reset at overflow.
  128. static uint16_t breathing_counter = 0;
  129. breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
  130. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  131. uint32_t duty = cie_lightness(rescale_limit_val(scale_backlight(breathing_table[index] * 256)));
  132. chSysLockFromISR();
  133. pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
  134. chSysUnlockFromISR();
  135. }
  136. // TODO: integrate generic pulse solution
  137. void breathing_pulse(void) {
  138. backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS);
  139. wait_ms(10);
  140. backlight_set(is_backlight_enabled() ? get_backlight_level() : 0);
  141. }
  142. #endif