diff options
Diffstat (limited to 'quantum/backlight/backlight_timer.c')
-rw-r--r-- | quantum/backlight/backlight_timer.c | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/quantum/backlight/backlight_timer.c b/quantum/backlight/backlight_timer.c new file mode 100644 index 000000000..c32c35c15 --- /dev/null +++ b/quantum/backlight/backlight_timer.c | |||
@@ -0,0 +1,167 @@ | |||
1 | #include "quantum.h" | ||
2 | #include "backlight.h" | ||
3 | #include "backlight_driver_common.h" | ||
4 | #include "debug.h" | ||
5 | |||
6 | #ifndef BACKLIGHT_GPT_DRIVER | ||
7 | # define BACKLIGHT_GPT_DRIVER GPTD15 | ||
8 | #endif | ||
9 | |||
10 | // Platform specific implementations | ||
11 | static void backlight_timer_configure(bool enable); | ||
12 | static void backlight_timer_set_duty(uint16_t duty); | ||
13 | static uint16_t backlight_timer_get_duty(void); | ||
14 | |||
15 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
16 | static uint16_t cie_lightness(uint16_t v) { | ||
17 | if (v <= 5243) // if below 8% of max | ||
18 | return v / 9; // same as dividing by 900% | ||
19 | else { | ||
20 | uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
21 | // to get a useful result with integer division, we shift left in the expression above | ||
22 | // and revert what we've done again after squaring. | ||
23 | y = y * y * y >> 8; | ||
24 | if (y > 0xFFFFUL) // prevent overflow | ||
25 | return 0xFFFFU; | ||
26 | else | ||
27 | return (uint16_t)y; | ||
28 | } | ||
29 | } | ||
30 | |||
31 | void backlight_init_ports(void) { | ||
32 | backlight_pins_init(); | ||
33 | |||
34 | backlight_set(get_backlight_level()); | ||
35 | |||
36 | #ifdef BACKLIGHT_BREATHING | ||
37 | if (is_backlight_breathing()) { | ||
38 | breathing_enable(); | ||
39 | } | ||
40 | #endif | ||
41 | } | ||
42 | |||
43 | void backlight_set(uint8_t level) { | ||
44 | if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; | ||
45 | |||
46 | backlight_pins_off(); | ||
47 | |||
48 | backlight_timer_set_duty(cie_lightness(0xFFFFU / BACKLIGHT_LEVELS * level)); | ||
49 | backlight_timer_configure(level != 0); | ||
50 | } | ||
51 | |||
52 | static void backlight_timer_top(void) { | ||
53 | #ifdef BACKLIGHT_BREATHING | ||
54 | if (is_breathing()) { | ||
55 | breathing_task(); | ||
56 | } | ||
57 | #endif | ||
58 | |||
59 | if (backlight_timer_get_duty() > 256) { | ||
60 | backlight_pins_on(); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | static void backlight_timer_cmp(void) { backlight_pins_off(); } | ||
65 | |||
66 | void backlight_task(void) {} | ||
67 | |||
68 | #ifdef BACKLIGHT_BREATHING | ||
69 | # define BREATHING_STEPS 128 | ||
70 | |||
71 | static bool breathing = false; | ||
72 | static uint16_t breathing_counter = 0; | ||
73 | |||
74 | /* To generate breathing curve in python: | ||
75 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
76 | */ | ||
77 | 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}; | ||
78 | |||
79 | // Use this before the cie_lightness function. | ||
80 | static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); } | ||
81 | |||
82 | void breathing_task(void) { | ||
83 | uint8_t breathing_period = get_breathing_period(); | ||
84 | uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS; | ||
85 | // resetting after one period to prevent ugly reset at overflow. | ||
86 | breathing_counter = (breathing_counter + 1) % (breathing_period * 256); | ||
87 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
88 | |||
89 | // printf("index:%u\n", index); | ||
90 | |||
91 | backlight_timer_set_duty(cie_lightness(scale_backlight((uint16_t)breathing_table[index] * 256))); | ||
92 | } | ||
93 | |||
94 | bool is_breathing(void) { return breathing; } | ||
95 | |||
96 | void breathing_enable(void) { | ||
97 | breathing_counter = 0; | ||
98 | breathing = true; | ||
99 | } | ||
100 | void breathing_disable(void) { breathing = false; } | ||
101 | |||
102 | void breathing_pulse(void) { | ||
103 | backlight_set(is_backlight_enabled() ? 0 : BACKLIGHT_LEVELS); | ||
104 | wait_ms(10); | ||
105 | backlight_set(is_backlight_enabled() ? get_backlight_level() : 0); | ||
106 | } | ||
107 | #endif | ||
108 | |||
109 | #ifdef PROTOCOL_CHIBIOS | ||
110 | // On Platforms where timers fire every tick and have no capture/top events | ||
111 | // - fake event in the normal timer callback | ||
112 | uint16_t s_duty = 0; | ||
113 | |||
114 | static void timerCallback(void) { | ||
115 | /* Software PWM | ||
116 | * timer:1111 1111 1111 1111 | ||
117 | * \______/| \_______/____ count(0-255) | ||
118 | * \ \______________ unused(1) | ||
119 | * \__________________ index of step table(0-127) | ||
120 | */ | ||
121 | |||
122 | // this works for cca 65536 irqs/sec | ||
123 | static union { | ||
124 | uint16_t raw; | ||
125 | struct { | ||
126 | uint16_t count : 8; | ||
127 | uint8_t dummy : 1; | ||
128 | uint8_t index : 7; | ||
129 | } pwm; | ||
130 | } timer = {.raw = 0}; | ||
131 | |||
132 | timer.raw++; | ||
133 | |||
134 | if (timer.pwm.count == 0) { | ||
135 | // LED on | ||
136 | backlight_timer_top(); | ||
137 | } else if (timer.pwm.count == (s_duty / 256)) { | ||
138 | // LED off | ||
139 | backlight_timer_cmp(); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | static void backlight_timer_set_duty(uint16_t duty) { s_duty = duty; } | ||
144 | static uint16_t backlight_timer_get_duty(void) { return s_duty; } | ||
145 | |||
146 | // ChibiOS - Map GPT timer onto Software PWM | ||
147 | static void gptTimerCallback(GPTDriver *gptp) { | ||
148 | (void)gptp; | ||
149 | timerCallback(); | ||
150 | } | ||
151 | |||
152 | static void backlight_timer_configure(bool enable) { | ||
153 | static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0}; | ||
154 | |||
155 | static bool s_init = false; | ||
156 | if (!s_init) { | ||
157 | gptStart(&BACKLIGHT_GPT_DRIVER, &gptcfg); | ||
158 | s_init = true; | ||
159 | } | ||
160 | |||
161 | if (enable) { | ||
162 | gptStartContinuous(&BACKLIGHT_GPT_DRIVER, gptcfg.frequency / 0xFFFF); | ||
163 | } else { | ||
164 | gptStopTimer(&BACKLIGHT_GPT_DRIVER); | ||
165 | } | ||
166 | } | ||
167 | #endif | ||