]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/backlight/backlight_arm.c
3f94ccef8ecd05157fab74d6d9521061bfc242a7
[qmk_firmware.git] / quantum / backlight / backlight_arm.c
1 #include "quantum.h"
2 #include "backlight.h"
3 #include <hal.h>
4 #include "debug.h"
5
6 // TODO: remove short term bodge when refactoring BACKLIGHT_CUSTOM_DRIVER out
7 #ifdef BACKLIGHT_PIN
8
9 #    if defined(STM32F0XX) || defined(STM32F0xx)
10 #        error "Backlight support for STMF072 is not available. Please disable."
11 #    endif
12
13 #    if defined(STM32F1XX) || defined(STM32F1xx)
14 #        define USE_GPIOV1
15 #    endif
16
17 // GPIOV2 && GPIOV3
18 #    ifndef BACKLIGHT_PAL_MODE
19 #        define BACKLIGHT_PAL_MODE 2
20 #    endif
21
22 // GENERIC
23 #    ifndef BACKLIGHT_PWM_DRIVER
24 #        define BACKLIGHT_PWM_DRIVER PWMD4
25 #    endif
26 #    ifndef BACKLIGHT_PWM_CHANNEL
27 #        define BACKLIGHT_PWM_CHANNEL 3
28 #    endif
29
30 static void breathing_callback(PWMDriver *pwmp);
31
32 static PWMConfig pwmCFG = {0xFFFF, /* PWM clock frequency  */
33                            256,    /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
34                            NULL,   /* No Callback */
35                            {       /* Default all channels to disabled - Channels will be configured durring init */
36                             {PWM_OUTPUT_DISABLED, NULL},
37                             {PWM_OUTPUT_DISABLED, NULL},
38                             {PWM_OUTPUT_DISABLED, NULL},
39                             {PWM_OUTPUT_DISABLED, NULL}},
40                            0, /* HW dependent part.*/
41                            0};
42
43 static PWMConfig pwmCFG_breathing = {0xFFFF,             /** PWM clock frequency  */
44                                      256,                /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
45                                      breathing_callback, /* Breathing Callback */
46                                      {                   /* Default all channels to disabled - Channels will be configured durring init */
47                                       {PWM_OUTPUT_DISABLED, NULL},
48                                       {PWM_OUTPUT_DISABLED, NULL},
49                                       {PWM_OUTPUT_DISABLED, NULL},
50                                       {PWM_OUTPUT_DISABLED, NULL}},
51                                      0, /* HW dependent part.*/
52                                      0};
53
54 // See http://jared.geek.nz/2013/feb/linear-led-pwm
55 static uint16_t cie_lightness(uint16_t v) {
56     if (v <= 5243)     // if below 8% of max
57         return v / 9;  // same as dividing by 900%
58     else {
59         uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL);  // add 16% of max and compare
60         // to get a useful result with integer division, we shift left in the expression above
61         // and revert what we've done again after squaring.
62         y = y * y * y >> 8;
63         if (y > 0xFFFFUL)  // prevent overflow
64             return 0xFFFFU;
65         else
66             return (uint16_t)y;
67     }
68 }
69
70 void backlight_init_ports(void) {
71     // printf("backlight_init_ports()\n");
72
73 #    ifdef USE_GPIOV1
74     palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
75 #    else
76     palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_ALTERNATE(BACKLIGHT_PAL_MODE));
77 #    endif
78
79     pwmCFG.channels[BACKLIGHT_PWM_CHANNEL - 1].mode           = PWM_OUTPUT_ACTIVE_HIGH;
80     pwmCFG_breathing.channels[BACKLIGHT_PWM_CHANNEL - 1].mode = PWM_OUTPUT_ACTIVE_HIGH;
81     pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG);
82
83     backlight_set(get_backlight_level());
84     if (is_backlight_breathing()) {
85         breathing_enable();
86     }
87 }
88
89 void backlight_set(uint8_t level) {
90     // printf("backlight_set(%d)\n", level);
91     if (level == 0) {
92         // Turn backlight off
93         pwmDisableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1);
94     } else {
95         // Turn backlight on
96         if (!is_breathing()) {
97             uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS));
98             // printf("duty: (%d)\n", duty);
99             pwmEnableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
100         }
101     }
102 }
103
104 uint8_t backlight_tick = 0;
105
106 void backlight_task(void) {}
107
108 #    define BREATHING_NO_HALT 0
109 #    define BREATHING_HALT_OFF 1
110 #    define BREATHING_HALT_ON 2
111 #    define BREATHING_STEPS 128
112
113 static uint8_t  breathing_period  = BREATHING_PERIOD;
114 static uint8_t  breathing_halt    = BREATHING_NO_HALT;
115 static uint16_t breathing_counter = 0;
116
117 bool is_breathing(void) { return BACKLIGHT_PWM_DRIVER.config == &pwmCFG_breathing; }
118
119 static inline void breathing_min(void) { breathing_counter = 0; }
120
121 static inline void breathing_max(void) { breathing_counter = breathing_period * 256 / 2; }
122
123 void breathing_interrupt_enable(void) {
124     pwmStop(&BACKLIGHT_PWM_DRIVER);
125     pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG_breathing);
126     chSysLockFromISR();
127     pwmEnablePeriodicNotification(&BACKLIGHT_PWM_DRIVER);
128     pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, 0xFFFF));
129     chSysUnlockFromISR();
130 }
131
132 void breathing_interrupt_disable(void) {
133     pwmStop(&BACKLIGHT_PWM_DRIVER);
134     pwmStart(&BACKLIGHT_PWM_DRIVER, &pwmCFG);
135 }
136
137 void breathing_enable(void) {
138     breathing_counter = 0;
139     breathing_halt    = BREATHING_NO_HALT;
140     breathing_interrupt_enable();
141 }
142
143 void breathing_pulse(void) {
144     if (get_backlight_level() == 0)
145         breathing_min();
146     else
147         breathing_max();
148     breathing_halt = BREATHING_HALT_ON;
149     breathing_interrupt_enable();
150 }
151
152 void breathing_disable(void) {
153     // printf("breathing_disable()\n");
154     breathing_interrupt_disable();
155     // Restore backlight level
156     backlight_set(get_backlight_level());
157 }
158
159 void breathing_self_disable(void) {
160     if (get_backlight_level() == 0)
161         breathing_halt = BREATHING_HALT_OFF;
162     else
163         breathing_halt = BREATHING_HALT_ON;
164 }
165
166 void breathing_toggle(void) {
167     if (is_breathing())
168         breathing_disable();
169     else
170         breathing_enable();
171 }
172
173 void breathing_period_set(uint8_t value) {
174     if (!value) value = 1;
175     breathing_period = value;
176 }
177
178 void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }
179
180 void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }
181
182 void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }
183
184 /* To generate breathing curve in python:
185  * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
186  */
187 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};
188
189 // Use this before the cie_lightness function.
190 static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
191
192 static void breathing_callback(PWMDriver *pwmp) {
193     (void)pwmp;
194     uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS;
195     // resetting after one period to prevent ugly reset at overflow.
196     breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
197     uint8_t index     = breathing_counter / interval % BREATHING_STEPS;
198
199     if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
200         breathing_interrupt_disable();
201     }
202
203     uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
204
205     chSysLockFromISR();
206     pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
207     chSysUnlockFromISR();
208 }
209
210 #else
211
212 __attribute__((weak)) void backlight_init_ports(void) {}
213
214 __attribute__((weak)) void backlight_set(uint8_t level) {}
215
216 __attribute__((weak)) void backlight_task(void) {}
217
218 #endif