1
// thefreeman boost driver 2.1 output helper functions
2
// Copyright (C) 2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
6
#include "chan-rgbaux.c"
10
void set_level_main(uint8_t level);
11
bool gradual_tick_main(uint8_t gt);
14
Channel channels[] = {
16
.set_level = set_level_main,
17
.gradual_tick = gradual_tick_main
23
void set_level_zero() {
24
DAC_LVL = 0; // DAC off
25
DAC_VREF = V055; // low Vref
26
HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN); // HDR off
28
// prevent post-off flash
29
IN_NFET_ENABLE_PORT |= (1 << IN_NFET_ENABLE_PIN);
30
delay_4ms(IN_NFET_DELAY_TIME/4);
31
IN_NFET_ENABLE_PORT &= ~(1 << IN_NFET_ENABLE_PIN);
33
// turn off boost last
34
BST_ENABLE_PORT &= ~(1 << BST_ENABLE_PIN); // BST off
37
// single set of LEDs with 1 regulated power channel
38
// and low/high HDR plus low/high Vref as different "gears"
39
void set_level_main(uint8_t level) {
42
// when turning on from off, use IN_NFET to prevent a flash
43
if ((! actual_level) && (level < HDR_ENABLE_LEVEL_MIN)) {
45
IN_NFET_ENABLE_PORT |= (1 << IN_NFET_ENABLE_PIN);
48
// BST on first, to give it a few extra microseconds to spin up
49
BST_ENABLE_PORT |= (1 << BST_ENABLE_PIN);
51
// pre-load ramp data so it can be assigned faster later
52
PWM_DATATYPE dac_lvl = PWM_GET(pwm1_levels, level);
53
PWM_DATATYPE dac_vref = PWM_GET(pwm_tops, level);
55
// enable HDR on top half of ramp
56
if (level >= (HDR_ENABLE_LEVEL_MIN-1))
57
HDR_ENABLE_PORT |= (1 << HDR_ENABLE_PIN);
59
HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN);
61
// set these in successive clock cycles to avoid getting out of sync
62
// (minimizes ramp bumps when changing gears)
67
// wait for flash prevention to finish
68
delay_4ms(IN_NFET_DELAY_TIME/4);
69
IN_NFET_ENABLE_PORT &= ~(1 << IN_NFET_ENABLE_PIN);
73
bool gradual_tick_main(uint8_t gt) {
74
// if HDR and Vref "engine gear" is the same, do a small adjustment...
75
// otherwise, simply jump to the next ramp level
76
// and let set_level() handle any gear changes
78
PWM_DATATYPE dac_next = PWM_GET(pwm1_levels, gt);
79
PWM_DATATYPE vref_next = PWM_GET(pwm_tops, gt);
81
// different gear = full adjustment
82
if (vref_next != DAC_VREF) return true; // let parent set_level() for us
84
// same gear = small adjustment
85
GRADUAL_ADJUST_SIMPLE(dac_next, DAC_LVL);
86
if (dac_next == DAC_LVL) return true; // done
88
return false; // not done yet