rusEFI
The most advanced open source ECU
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 
10 static bool isRunning = false;
11 
12 static void completionCallback(ADCDriver* adcp) {
13  if (isRunning && adcp->state == ADC_COMPLETE) {
15  }
16 }
17 
18 static 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 
26 static 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 
38 static 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 
54 static constexpr size_t sampleCount = BIG_BUFFER_SIZE / (2 * sizeof(uint8_t));
55 
56 static void startSampling(void* = nullptr) {
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
75 
76  isRunning = true;
77 
78  startSampling();
79 }
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  engine->executor.scheduleByTimestampNt("trigger scope", &restartTimer, getTimeNowNt() + MS2NT(10), startSampling);
98  }
99 
100  return buffer;
101 }
102 
104  // Trigger scope and knock currently mutually exclusive
106  adcStart(&TRIGGER_SCOPE_ADC, nullptr);
107 
108  // Manually set ADC DMA to byte mode, as we'll be taking 8 bit samples
109  // Defaults are to sample at 12 bits, and DMA 16-bit words
110  ADCD3.dmamode &= ~(STM32_DMA_CR_PSIZE_MASK | STM32_DMA_CR_MSIZE_MASK);
111  ADCD3.dmamode |= STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
112 
113  efiSetPadMode("trg ch1", TRIGGER_SCOPE_PIN_CH1, PAL_MODE_INPUT_ANALOG);
114 #if TRIGGER_SCOPE_HAS_CH2
115  efiSetPadMode("trg ch2", TRIGGER_SCOPE_PIN_CH2, PAL_MODE_INPUT_ANALOG);
116 #endif
117  }
118 }
119 
120 #endif // TRIGGER_SCOPE
BigBufferHandle getBigBuffer(BigBufferUser user)
Definition: big_buffer.cpp:61
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
const TBuffer * get() const
Definition: big_buffer.h:32
SingleTimerExecutor executor
Definition: engine.h:240
TunerStudioOutputChannels outputChannels
Definition: engine.h:96
void scheduleByTimestampNt(const char *msg, scheduling_s *scheduling, efitick_t timeNt, action_s action) override
efitick_t getTimeNowNt()
Definition: efitime.cpp:19
Engine * engine
ADCDriver ADCD3
ADC3 driver identifier.
Definition: hal_adc_lld.c:54
uint16_t adcsample_t
ADC sample data type.
Definition: hal_adc_lld.h:190
engine_configuration_s * engineConfiguration
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 const uint32_t smpr2
const BigBufferHandle & triggerScopeGetBuffer()
void initTriggerScope()
static BigBufferHandle buffer
void triggerScopeDisable()
static void startSampling(void *=nullptr)
static const ADCConversionGroup adcConvGroupCh1