~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-thefreeman-boost21-mp3431-hdr-dac-argb.c

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// thefreeman boost driver 2.1 output helper functions
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
#include "chan-rgbaux.c"
 
7
 
 
8
void set_level_zero();
 
9
 
 
10
void set_level_main(uint8_t level);
 
11
bool gradual_tick_main(uint8_t gt);
 
12
 
 
13
 
 
14
Channel channels[] = {
 
15
    { // main LEDs
 
16
        .set_level    = set_level_main,
 
17
        .gradual_tick = gradual_tick_main
 
18
    },
 
19
    RGB_AUX_CHANNELS
 
20
};
 
21
 
 
22
 
 
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
 
27
 
 
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);
 
32
 
 
33
    // turn off boost last
 
34
    BST_ENABLE_PORT &= ~(1 << BST_ENABLE_PIN);  // BST off
 
35
}
 
36
 
 
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) {
 
40
    uint8_t noflash = 0;
 
41
 
 
42
    // when turning on from off, use IN_NFET to prevent a flash
 
43
    if ((! actual_level) && (level < HDR_ENABLE_LEVEL_MIN)) {
 
44
        noflash = 1;
 
45
        IN_NFET_ENABLE_PORT |= (1 << IN_NFET_ENABLE_PIN);
 
46
    }
 
47
 
 
48
    // BST on first, to give it a few extra microseconds to spin up
 
49
    BST_ENABLE_PORT |= (1 << BST_ENABLE_PIN);
 
50
 
 
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);
 
54
 
 
55
    // enable HDR on top half of ramp
 
56
    if (level >= (HDR_ENABLE_LEVEL_MIN-1))
 
57
        HDR_ENABLE_PORT |= (1 << HDR_ENABLE_PIN);
 
58
    else
 
59
        HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN);
 
60
 
 
61
    // set these in successive clock cycles to avoid getting out of sync
 
62
    // (minimizes ramp bumps when changing gears)
 
63
    DAC_LVL  = dac_lvl;
 
64
    DAC_VREF = dac_vref;
 
65
 
 
66
    if (noflash) {
 
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);
 
70
    }
 
71
}
 
72
 
 
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
 
77
 
 
78
    PWM_DATATYPE dac_next  = PWM_GET(pwm1_levels, gt);
 
79
    PWM_DATATYPE vref_next = PWM_GET(pwm_tops, gt);
 
80
 
 
81
    // different gear = full adjustment
 
82
    if (vref_next != DAC_VREF) return true;  // let parent set_level() for us
 
83
 
 
84
    // same gear = small adjustment
 
85
    GRADUAL_ADJUST_SIMPLE(dac_next, DAC_LVL);
 
86
    if (dac_next == DAC_LVL) return true;  // done
 
87
 
 
88
    return false;  // not done yet
 
89
}
 
90