backlight.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "backlight.h"
  17. #include "bsp/dp32g030/gpio.h"
  18. #include "bsp/dp32g030/pwmplus.h"
  19. #include "bsp/dp32g030/portcon.h"
  20. #include "driver/gpio.h"
  21. #include "settings.h"
  22. // this is decremented once every 500ms
  23. uint16_t gBacklightCountdown_500ms = 0;
  24. bool backlightOn;
  25. void BACKLIGHT_InitHardware()
  26. {
  27. // 48MHz / 94 / 1024 ~ 500Hz
  28. const uint32_t PWM_FREQUENCY_HZ = 1000;
  29. PWM_PLUS0_CLKSRC |= ((48000000 / 1024 / PWM_FREQUENCY_HZ) << 16);
  30. PWM_PLUS0_PERIOD = 1023;
  31. PORTCON_PORTB_SEL0 &= ~(0
  32. // Back light
  33. | PORTCON_PORTB_SEL0_B6_MASK
  34. );
  35. PORTCON_PORTB_SEL0 |= 0
  36. // Back light PWM
  37. | PORTCON_PORTB_SEL0_B6_BITS_PWMP0_CH0
  38. ;
  39. PWM_PLUS0_GEN =
  40. PWMPLUS_GEN_CH0_OE_BITS_ENABLE |
  41. PWMPLUS_GEN_CH0_OUTINV_BITS_ENABLE |
  42. 0;
  43. PWM_PLUS0_CFG =
  44. PWMPLUS_CFG_CNT_REP_BITS_ENABLE |
  45. PWMPLUS_CFG_COUNTER_EN_BITS_ENABLE |
  46. 0;
  47. }
  48. void BACKLIGHT_TurnOn(void)
  49. {
  50. if (gEeprom.BACKLIGHT_TIME == 0) {
  51. BACKLIGHT_TurnOff();
  52. return;
  53. }
  54. backlightOn = true;
  55. BACKLIGHT_SetBrightness(gEeprom.BACKLIGHT_MAX);
  56. switch (gEeprom.BACKLIGHT_TIME) {
  57. default:
  58. case 1: // 5 sec
  59. case 2: // 10 sec
  60. case 3: // 20 sec
  61. gBacklightCountdown_500ms = 1 + (2 << (gEeprom.BACKLIGHT_TIME - 1)) * 5;
  62. break;
  63. case 4: // 1 min
  64. case 5: // 2 min
  65. case 6: // 4 min
  66. gBacklightCountdown_500ms = 1 + (2 << (gEeprom.BACKLIGHT_TIME - 4)) * 60;
  67. break;
  68. case 7: // always on
  69. gBacklightCountdown_500ms = 0;
  70. break;
  71. }
  72. }
  73. void BACKLIGHT_TurnOff()
  74. {
  75. #ifdef ENABLE_BLMIN_TMP_OFF
  76. register uint8_t tmp;
  77. if (gEeprom.BACKLIGHT_MIN_STAT == BLMIN_STAT_ON)
  78. tmp = gEeprom.BACKLIGHT_MIN;
  79. else
  80. tmp = 0;
  81. BACKLIGHT_SetBrightness(tmp);
  82. #else
  83. BACKLIGHT_SetBrightness(gEeprom.BACKLIGHT_MIN);
  84. #endif
  85. gBacklightCountdown_500ms = 0;
  86. backlightOn = false;
  87. }
  88. bool BACKLIGHT_IsOn()
  89. {
  90. return backlightOn;
  91. }
  92. static uint8_t currentBrightness;
  93. void BACKLIGHT_SetBrightness(uint8_t brigtness)
  94. {
  95. currentBrightness = brigtness;
  96. PWM_PLUS0_CH0_COMP = (1 << brigtness) - 1;
  97. //PWM_PLUS0_SWLOAD = 1;
  98. }
  99. uint8_t BACKLIGHT_GetBrightness(void)
  100. {
  101. return currentBrightness;
  102. }