2 #ifdef ARDUINO_ARCH_RP2040
7 #include "hardware/pio.h"
8 #include "hardware/dma.h"
9 #include "hardware/structs/bus_ctrl.h"
12 #include "logic_analyzer.h"
14 namespace logic_analyzer {
37 log(
"Number of samples: %d", n_samples);
38 log(
"Time in us: %lu", run_time_us);
47 pio_sm_set_enabled(pio, sm,
false);
48 dma_channel_abort(dma_chan);
66 float measured_freq = run_time_us == 0 ? 0 : 1000000.0 * n_samples / run_time_us;
70 float maxFrequency(
int warmup=1,
int repeat=2) {
71 log(
"determine maxFrequency");
72 if (max_frequecy_value<=0.0) {
79 for (
int j=0;j<warmup;j++){
81 dma_channel_wait_for_finish_blocking(dma_chan);
86 for (
int j=0;j<repeat;j++){
88 dma_channel_wait_for_finish_blocking(dma_chan);
89 run_time_us = micros() - start_time;
92 max_frequecy_value = freqTotal / repeat;
93 log(
"maxFrequency: %f", max_frequecy_value);
95 return max_frequecy_value;
111 uint32_t n_transfers;
112 size_t capture_size_words;
116 uint64_t frequecy_value;
117 float max_frequecy_value = -1.0;
119 unsigned long start_time;
120 unsigned long run_time_us;
121 float test_duty_cycle;
128 if (
logicAnalyzer().captureFrequency() > (1.5 * maxFrequency())){
132 log(
"The frequency %u is not supported!",
logicAnalyzer().captureFrequency () );
150 float result =
static_cast<float>(maxFrequency()) /
static_cast<float>(frequecy_value_hz) ;
151 log(
"divider: %f", result);
152 return result < 1.0 ? 1.0 : result;
159 log(
"- Init trigger");
164 bus_ctrl_hw->priority = BUSCTRL_BUS_PRIORITY_DMA_W_BITS | BUSCTRL_BUS_PRIORITY_DMA_R_BITS;
168 uint16_t capture_prog_instr = pio_encode_in(pio_pins, pin_count);
169 struct pio_program capture_prog = {
170 .instructions = &capture_prog_instr,
175 uint offset = pio_add_program(pio, &capture_prog);
179 pio_sm_config c = pio_get_default_sm_config();
180 sm_config_set_in_pins(&c, pin_base);
181 sm_config_set_wrap(&c, offset, offset);
182 sm_config_set_clkdiv(&c, divider_value);
186 sm_config_set_in_shift(&c,
true,
true, 32);
187 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX);
188 pio_sm_init(pio, sm, offset, &c);
191 log(
"- Arming trigger");
192 pio_sm_set_enabled(pio, sm,
false);
195 pio_sm_clear_fifos(pio, sm);
196 pio_sm_restart(pio, sm);
198 dma_channel_config dma_config = dma_channel_get_default_config(dma_chan);
199 channel_config_set_read_increment(&dma_config,
false);
200 channel_config_set_write_increment(&dma_config,
true);
201 channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_32);
202 channel_config_set_dreq(&dma_config, pio_get_dreq(pio, sm,
false));
204 n_transfers = n_samples /4 *
sizeof(PinBitArray);
205 dma_channel_configure(dma_chan, &dma_config,
216 start_time = micros();
217 pio_sm_set_enabled(pio, sm,
true);
249 log(
"dump() - ended with %u records", count);
253 log(
"dump() - aborted");
259 log(
"waitForResult()");
260 dma_channel_wait_for_finish_blocking(dma_chan);
261 run_time_us = micros() - start_time;
262 size_t record_count = n_transfers * 4 /
sizeof(PinBitArray);
264 log(
"waitForResult() -> result available with %u records",record_count);
Abstract Class for Capturing Logic. Create your own subclass if you want to implement your own optimi...
Definition: logic_analyzer.h:300
LogicAnalyzer & logicAnalyzer()
Provides access to the LogicAnalyzer.
Definition: logic_analyzer.h:313
virtual void setStatus(Status status)
Sets the status.
Definition: logic_analyzer.h:323
size_t available()
returns the avialable buffer entries
Definition: logic_analyzer.h:719
uint16_t numberOfPins()
Provides the number of subsequent GPIO pins which will be used for capturing.
Definition: logic_analyzer.h:591
uint16_t startPin()
Provides the GPIO number of the start pin which is used for capturing.
Definition: logic_analyzer.h:586
RingBuffer & buffer()
Provides access to the buffer.
Definition: logic_analyzer.h:611
int readCount()
provides the read count
Definition: logic_analyzer.h:649
First version of Capture implementation for Raspberry Pico using the PIO. Based on https://github....
Definition: capture_raspberry_pico.h:23
void arm()
intitialize the PIO
Definition: capture_raspberry_pico.h:156
float calculateDivider(uint32_t frequecy_value_hz)
determines the divider value
Definition: capture_raspberry_pico.h:148
virtual void captureAll()
Used to test the speed.
Definition: capture_raspberry_pico.h:53
void cancel()
cancels the capturing which is ccurrently in progress
Definition: capture_raspberry_pico.h:43
void start()
starts the processing
Definition: capture_raspberry_pico.h:125
void dump()
Dumps the result to PuleView (SUMP software)
Definition: capture_raspberry_pico.h:241
float frequencyMeasured()
Provides the measured capturing frequency.
Definition: capture_raspberry_pico.h:65
PicoCapturePIO()
Default Constructor.
Definition: capture_raspberry_pico.h:26
virtual void capture()
starts the capturing of the data
Definition: capture_raspberry_pico.h:30
void waitForResult()
Wait for result and update run_time_us and buffer available.
Definition: capture_raspberry_pico.h:258
unsigned long runtimeUs()
provides the runtime in microseconds from the capturing start to when the data is available
Definition: capture_raspberry_pico.h:60
void setAvailable(size_t avail)
Usualy you must not use this function. However for the RP PIO it is quite usefull to indicated that t...
Definition: logic_analyzer.h:224