lcd_backlight_hal.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "lcd_backlight.h"
  21. #include "hal.h"
  22. #define RED_PIN 1
  23. #define GREEN_PIN 2
  24. #define BLUE_PIN 3
  25. #define CHANNEL_RED FTM0->CHANNEL[0]
  26. #define CHANNEL_GREEN FTM0->CHANNEL[1]
  27. #define CHANNEL_BLUE FTM0->CHANNEL[2]
  28. #define RGB_PORT PORTC
  29. #define RGB_PORT_GPIO GPIOC
  30. // Base FTM clock selection (72 MHz system clock)
  31. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  32. // Higher pre-scalar will use the most power (also look the best)
  33. // Pre-scalar calculations
  34. // 0 - 72 MHz -> 549 Hz
  35. // 1 - 36 MHz -> 275 Hz
  36. // 2 - 18 MHz -> 137 Hz
  37. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  38. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  39. // 5 - 2 250 kHz -> 17 Hz
  40. // 6 - 1 125 kHz -> 9 Hz
  41. // 7 - 562 500 Hz -> 4 Hz
  42. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  43. // Which will reduce the brightness range
  44. #define PRESCALAR_DEFINE 0
  45. void lcd_backlight_hal_init(void) {
  46. // Setup Backlight
  47. SIM->SCGC6 |= SIM_SCGC6_FTM0;
  48. FTM0->CNT = 0; // Reset counter
  49. // PWM Period
  50. // 16-bit maximum
  51. FTM0->MOD = 0xFFFF;
  52. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  53. #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
  54. CHANNEL_RED.CnSC = CNSC_MODE;
  55. CHANNEL_GREEN.CnSC = CNSC_MODE;
  56. CHANNEL_BLUE.CnSC = CNSC_MODE;
  57. // System clock, /w prescalar setting
  58. FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
  59. CHANNEL_RED.CnV = 0;
  60. CHANNEL_GREEN.CnV = 0;
  61. CHANNEL_BLUE.CnV = 0;
  62. RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
  63. RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
  64. RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
  65. #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
  66. RGB_PORT->PCR[RED_PIN] = RGB_MODE;
  67. RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
  68. RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
  69. }
  70. void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
  71. CHANNEL_RED.CnV = r;
  72. CHANNEL_GREEN.CnV = g;
  73. CHANNEL_BLUE.CnV = b;
  74. }