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