REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
mode.h
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#pragma once
6
7#include <Arduino.h>
8#include <array>
9#include <cstdint>
10
11#include "utils/helpers.h"
12#include <mode/mode.h>
13
14namespace mode {
15
16constexpr uint8_t PIN_MODE_IC = 40; // FlexIO 3:4
17constexpr uint8_t PIN_MODE_OP = 41; // FlexIO 3:5
18constexpr uint8_t PIN_MODE_OVERLOAD = 20; // FlexIO 3:10
19constexpr uint8_t PIN_MODE_EXTHALT = 21; // FlexIO 3:11
20constexpr uint8_t PIN_SYNC_CLK = 22; // FlexIO 3:8
21constexpr uint8_t PIN_SYNC_ID = 23; // FlexIO 3:9
22constexpr uint8_t PIN_QTMR_OP_GATE = 12; // QTimer11
23
25private:
26 static constexpr uint8_t CLK_SEL = 3, CLK_PRED = 0, CLK_PODF = 1;
27 // Hand-tuned assignments of shifters/states
28 static constexpr uint8_t s_idle = 0, s_ic = 1, s_op = 2, s_exthalt = 3, s_end = 4, s_overload = 5,
29 z_sync_match = 7;
30 // Hand-tuned assignments of timers
31 static constexpr uint8_t t_sync_clk = 0, t_sync_trigger = 1, t_ic = 2, t_ic_second = 3, t_op = 4,
32 t_op_second = 5, t_state_check = 6;
33 // Check constraints just to be safe
34 static_assert(t_sync_clk <= 7 and t_sync_trigger <= 7 and t_ic <= 7 and t_op <= 7 and t_op_second <= 7 and
35 t_state_check <= 7,
36 "Timer index out of range.");
37 static_assert(all_unique(std::make_tuple(t_sync_clk, t_sync_trigger, t_ic, t_ic_second, t_op, t_op_second,
38 t_state_check)),
39 "All values must be unique.");
40 static_assert(t_op_second == t_op + 1 and t_op_second % 4,
41 "Chained timers must have consecutive indices, but not 3->4.");
42 static_assert(t_ic_second == t_ic + 1 and t_ic_second % 4,
43 "Chained timers must have consecutive indices, but not 3->4.");
44
45 static constexpr std::array<uint8_t, 6> get_states() {
46 return {s_idle, s_ic, s_op, s_exthalt, s_end, s_overload};
47 }
48
49 static constexpr auto FLEXIO_SHIFTCTL_SMOD_STATE = FLEXIO_SHIFTCTL_SMOD(6);
50
51 static constexpr auto FLEXIO_TIMCTL_TRGSEL_STATE(uint8_t s_) {
52 return FLEXIO_TIMCTL_TRGSEL(4 * s_ + 1) | FLEXIO_TIMCTL_TRGSRC;
53 }
54
55 static constexpr uint32_t FLEXIO_STATE_SHIFTBUF(const uint8_t outputs, const uint8_t next_0,
56 const uint8_t next_1, const uint8_t next_2,
57 const uint8_t next_3, const uint8_t next_4,
58 const uint8_t next_5, const uint8_t next_6,
59 const uint8_t next_7) {
60 // flexio->port().SHIFTBUF[n] is
61 // [8 bit outputs][3 bit next state][3 bit][3 bit][3 bit][3 bit][3 bit][3 bit][3 bit]
62 // e.g. 0b11011111'010'010'000'010'010'010'000'010;
63 // With [3 bit next] according to
64 // FXIO_D[PINSEL+2] FXIO_D[PINSEL+1] FXIO_D[PINSEL] Next State Value
65 // 0 0 0 SHIFTBUFi[2:0]
66 // 0 0 1 SHIFTBUFi[5:3]
67 // 0 1 0 SHIFTBUFi[8:6]
68 // 0 1 1 SHIFTBUFi[11:9]
69 // ... ... ... ...
70 // 1 1 1 SHIFTBUFi[23:21]
71 return (outputs << 24) | ((next_7 & 0b111) << 21) | ((next_6 & 0b111) << 18) | ((next_5 & 0b111) << 15) |
72 ((next_4 & 0b111) << 12) | ((next_3 & 0b111) << 9) | ((next_2 & 0b111) << 6) |
73 ((next_1 & 0b111) << 3) | ((next_0 & 0b111) << 0);
74 }
75
76 static constexpr uint32_t FLEXIO_STATE_SHIFTBUF(const uint8_t outputs, const uint8_t next) {
77 return FLEXIO_STATE_SHIFTBUF(outputs, next, next, next, next, next, next, next, next);
78 }
79
80 // global static bool default to false
81 static bool _is_initialized, _is_enabled;
82
83public:
84 static bool init(unsigned long long ic_time_ns, unsigned long long op_time_ns,
85 mode::OnOverload on_overload = mode::OnOverload::HALT,
86 mode::OnExtHalt on_ext_halt = mode::OnExtHalt::IGNORE, SyncConfig sync_config = {});
87
88 static bool is_initialized() { return _is_initialized; }
89
90 static void disable();
91 static void enable();
92
93 static bool is_enabled() { return _is_enabled; }
94
95 static void reset();
96
97 // QTMR functions
98 static void _init_qtmr_op();
99 static void _reset_qtmr_op();
100 static unsigned long long get_actual_op_time();
101
102 static void force_start();
103 static void to_idle();
104 static void to_ic();
105 static void to_op();
106 static void to_exthalt();
107 static void to_end();
108
109 static bool is_idle();
110 static bool is_op();
111 static bool is_done();
112 static bool is_overloaded();
113 static bool is_exthalt();
114
115 static void delay_till_done();
116};
117
118} // namespace mode
static void to_exthalt()
Definition mode.cpp:390
static void disable()
Definition mode.cpp:358
static void to_end()
Definition mode.cpp:396
static void _reset_qtmr_op()
Definition mode.cpp:417
static bool is_op()
Definition mode.cpp:468
static bool is_idle()
Definition mode.cpp:463
static bool is_initialized()
Definition mode.h:88
static unsigned long long get_actual_op_time()
Definition mode.cpp:458
static bool is_done()
Definition mode.cpp:473
static void reset()
Definition mode.cpp:402
static bool is_enabled()
Definition mode.h:93
static void to_ic()
Definition mode.cpp:378
static void to_op()
Definition mode.cpp:384
static void delay_till_done()
Definition mode.cpp:412
static void force_start()
Definition mode.cpp:370
static void to_idle()
Definition mode.cpp:372
static bool is_overloaded()
Definition mode.cpp:479
static bool init(unsigned long long ic_time_ns, unsigned long long op_time_ns, mode::OnOverload on_overload=mode::OnOverload::HALT, mode::OnExtHalt on_ext_halt=mode::OnExtHalt::IGNORE, SyncConfig sync_config={})
Definition mode.cpp:95
static void enable()
Definition mode.cpp:364
static void _init_qtmr_op()
Definition mode.cpp:422
static bool is_exthalt()
Definition mode.cpp:484
uint32_t
Definition flasher.cpp:195
Definition mode.h:14
constexpr uint8_t PIN_MODE_OP
Definition mode.h:17
constexpr uint8_t PIN_SYNC_CLK
Definition mode.h:20
constexpr uint8_t PIN_MODE_IC
Definition mode.h:16
constexpr uint8_t PIN_QTMR_OP_GATE
Definition mode.h:22
constexpr uint8_t PIN_MODE_EXTHALT
Definition mode.h:19
constexpr uint8_t PIN_MODE_OVERLOAD
Definition mode.h:18
constexpr uint8_t PIN_SYNC_ID
Definition mode.h:21