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