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