lcd_backlight_hal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  3. Jun Wako <wakojun@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "lcd_backlight.h"
  16. #include "hal.h"
  17. #define RED_PIN 1
  18. #define GREEN_PIN 2
  19. #define BLUE_PIN 3
  20. #define CHANNEL_RED FTM0->CHANNEL[0]
  21. #define CHANNEL_GREEN FTM0->CHANNEL[1]
  22. #define CHANNEL_BLUE FTM0->CHANNEL[2]
  23. #define RGB_PORT PORTC
  24. #define RGB_PORT_GPIO GPIOC
  25. // Base FTM clock selection (72 MHz system clock)
  26. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  27. // Higher pre-scalar will use the most power (also look the best)
  28. // Pre-scalar calculations
  29. // 0 - 72 MHz -> 549 Hz
  30. // 1 - 36 MHz -> 275 Hz
  31. // 2 - 18 MHz -> 137 Hz
  32. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  33. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  34. // 5 - 2 250 kHz -> 17 Hz
  35. // 6 - 1 125 kHz -> 9 Hz
  36. // 7 - 562 500 Hz -> 4 Hz
  37. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  38. // Which will reduce the brightness range
  39. #define PRESCALAR_DEFINE 0
  40. void lcd_backlight_hal_init(void) {
  41. // Setup Backlight
  42. SIM->SCGC6 |= SIM_SCGC6_FTM0;
  43. FTM0->CNT = 0; // Reset counter
  44. // PWM Period
  45. // 16-bit maximum
  46. FTM0->MOD = 0xFFFF;
  47. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  48. #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
  49. CHANNEL_RED.CnSC = CNSC_MODE;
  50. CHANNEL_GREEN.CnSC = CNSC_MODE;
  51. CHANNEL_BLUE.CnSC = CNSC_MODE;
  52. // System clock, /w prescalar setting
  53. FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
  54. CHANNEL_RED.CnV = 0;
  55. CHANNEL_GREEN.CnV = 0;
  56. CHANNEL_BLUE.CnV = 0;
  57. RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
  58. RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
  59. RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
  60. #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
  61. RGB_PORT->PCR[RED_PIN] = RGB_MODE;
  62. RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
  63. RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
  64. }
  65. void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
  66. CHANNEL_RED.CnV = r;
  67. CHANNEL_GREEN.CnV = g;
  68. CHANNEL_BLUE.CnV = b;
  69. }