rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
trigger_scope.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#ifdef TRIGGER_SCOPE
4
5#include "trigger_scope.h"
6#include "trigger_scope_config.h"
7
9
10static bool isRunning = false;
11
12static void completionCallback(ADCDriver* adcp) {
13 if (isRunning && adcp->state == ADC_COMPLETE) {
15 }
16}
17
18static const uint32_t smpr1 =
19 ADC_SMPR1_SMP_AN10(TRIGGER_SCOPE_SAMPLE_TIME) |
20 ADC_SMPR1_SMP_AN11(TRIGGER_SCOPE_SAMPLE_TIME) |
21 ADC_SMPR1_SMP_AN12(TRIGGER_SCOPE_SAMPLE_TIME) |
22 ADC_SMPR1_SMP_AN13(TRIGGER_SCOPE_SAMPLE_TIME) |
23 ADC_SMPR1_SMP_AN14(TRIGGER_SCOPE_SAMPLE_TIME) |
24 ADC_SMPR1_SMP_AN15(TRIGGER_SCOPE_SAMPLE_TIME);
25
26static const uint32_t smpr2 =
27 ADC_SMPR2_SMP_AN0(TRIGGER_SCOPE_SAMPLE_TIME) |
28 ADC_SMPR2_SMP_AN1(TRIGGER_SCOPE_SAMPLE_TIME) |
29 ADC_SMPR2_SMP_AN2(TRIGGER_SCOPE_SAMPLE_TIME) |
30 ADC_SMPR2_SMP_AN3(TRIGGER_SCOPE_SAMPLE_TIME) |
31 ADC_SMPR2_SMP_AN4(TRIGGER_SCOPE_SAMPLE_TIME) |
32 ADC_SMPR2_SMP_AN5(TRIGGER_SCOPE_SAMPLE_TIME) |
33 ADC_SMPR2_SMP_AN6(TRIGGER_SCOPE_SAMPLE_TIME) |
34 ADC_SMPR2_SMP_AN7(TRIGGER_SCOPE_SAMPLE_TIME) |
35 ADC_SMPR2_SMP_AN8(TRIGGER_SCOPE_SAMPLE_TIME) |
36 ADC_SMPR2_SMP_AN9(TRIGGER_SCOPE_SAMPLE_TIME);
37
38static const ADCConversionGroup adcConvGroupCh1 = { FALSE, 2, &completionCallback, nullptr,
39 ADC_CR1_RES_1, // Sample in 8-bit mode
40 ADC_CR2_SWSTART,
41 // sample times for channels 10...18
42 smpr1,
43 // sample times for channels 0...9
44 smpr2,
45
46 0, // htr
47 0, // ltr
48
49 0, // sqr1
50 0, // sqr2
51 ADC_SQR3_SQ1_N(TRIGGER_SCOPE_ADC_CH1) | ADC_SQR3_SQ2_N(TRIGGER_SCOPE_ADC_CH2)
52};
53
54static constexpr size_t sampleCount = BIG_BUFFER_SIZE / (2 * sizeof(uint8_t));
55
56static void startSampling() {
57 chibios_rt::CriticalSectionLocker csl;
58
60 // Cancel if ADC isn't ready
61 if (!((TRIGGER_SCOPE_ADC.state == ADC_READY) ||
62 (TRIGGER_SCOPE_ADC.state == ADC_COMPLETE) ||
63 (TRIGGER_SCOPE_ADC.state == ADC_ERROR))) {
65 return;
66 }
67
68 adcStartConversionI(&TRIGGER_SCOPE_ADC, &adcConvGroupCh1, buffer.get<adcsample_t>(), sampleCount);
69 }
70}
71
72// Enable one buffer's worth of perf tracing, and retrieve the buffer size in bytes
80
82 // we're done with the buffer - let somebody else have it
83 buffer = {};
84
85 isRunning = false;
87}
88
90
91// Retrieve the trace buffer
94
95 // Start the next sample once we've read out this one
96 if (isRunning) {
97 static auto const startSamplingAction{ action_s::make<startSampling>() };
98 engine->scheduler.schedule("trigger scope", &restartTimer, getTimeNowNt() + MS2NT(10), startSamplingAction);
99 }
100
101 return buffer;
102}
103
105 // Trigger scope and knock currently mutually exclusive
107 adcStart(&TRIGGER_SCOPE_ADC, nullptr);
108
109 // Manually set ADC DMA to byte mode, as we'll be taking 8 bit samples
110 // Defaults are to sample at 12 bits, and DMA 16-bit words
111 ADCD3.dmamode &= ~(STM32_DMA_CR_PSIZE_MASK | STM32_DMA_CR_MSIZE_MASK);
112 ADCD3.dmamode |= STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
113
114 efiSetPadMode("trg ch1", TRIGGER_SCOPE_PIN_CH1, PAL_MODE_INPUT_ANALOG);
115#if TRIGGER_SCOPE_HAS_CH2
116 efiSetPadMode("trg ch2", TRIGGER_SCOPE_PIN_CH2, PAL_MODE_INPUT_ANALOG);
117#endif
118 }
119}
120
121#endif // TRIGGER_SCOPE
BigBufferHandle getBigBuffer(BigBufferUser user)
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
const TBuffer * get() const
Definition big_buffer.h:34
SingleTimerExecutor scheduler
Definition engine.h:271
TunerStudioOutputChannels outputChannels
Definition engine.h:109
void schedule(const char *msg, scheduling_s *scheduling, efitick_t timeNt, action_s const &action) override
Schedule an action to be executed in the future.
efitick_t getTimeNowNt()
Definition efitime.cpp:19
static EngineAccessor engine
Definition engine.h:413
static constexpr engine_configuration_s * engineConfiguration
ADCDriver ADCD3
ADC3 driver identifier.
Definition hal_adc_lld.c:54
uint16_t adcsample_t
ADC sample data type.
static constexpr size_t sampleCount
static const uint32_t smpr1
void triggerScopeEnable()
static scheduling_s restartTimer
static bool isRunning
static void completionCallback(ADCDriver *adcp)
static void startSampling()
static const uint32_t smpr2
const BigBufferHandle & triggerScopeGetBuffer()
void initTriggerScope()
static BigBufferHandle buffer
void triggerScopeDisable()
static const ADCConversionGroup adcConvGroupCh1