2#ifdef ARDUINO_ARCH_RP2040
7#include "hardware/pio.h"
8#include "hardware/dma.h"
9#include "hardware/structs/bus_ctrl.h"
14namespace 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);
180 for (uint i = 0; i < pin_count; i++) {
181 pio_gpio_init(pio, pin_base + i);
183 pio_sm_set_consecutive_pindirs(pio, sm, pin_base, pin_count,
false);
187 pio_sm_config c = pio_get_default_sm_config();
188 sm_config_set_in_pins(&c, pin_base);
189 sm_config_set_wrap(&c, offset, offset);
190 sm_config_set_clkdiv(&c, divider_value);
194 sm_config_set_in_shift(&c,
true,
true, 32);
195 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX);
196 pio_sm_init(pio, sm, offset, &c);
199 log(
"- Arming trigger");
200 pio_sm_set_enabled(pio, sm,
false);
203 pio_sm_clear_fifos(pio, sm);
204 pio_sm_restart(pio, sm);
206 dma_channel_config dma_config = dma_channel_get_default_config(dma_chan);
207 channel_config_set_read_increment(&dma_config,
false);
208 channel_config_set_write_increment(&dma_config,
true);
209 channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_32);
210 channel_config_set_dreq(&dma_config, pio_get_dreq(pio, sm,
false));
212 n_transfers = n_samples /4 *
sizeof(PinBitArray);
213 dma_channel_configure(dma_chan, &dma_config,
224 start_time = micros();
225 pio_sm_set_enabled(pio, sm,
true);
257 log(
"dump() - ended with %u records", count);
261 log(
"dump() - aborted");
267 log(
"waitForResult()");
268 dma_channel_wait_for_finish_blocking(dma_chan);
269 run_time_us = micros() - start_time;
270 size_t record_count = n_transfers * 4 /
sizeof(PinBitArray);
272 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:316
LogicAnalyzer & logicAnalyzer()
Provides access to the LogicAnalyzer.
Definition logic_analyzer.h:329
virtual void setStatus(Status status)
Sets the status.
Definition logic_analyzer.h:339
RingBuffer & buffer()
Provides access to the buffer.
Definition logic_analyzer.h:626
size_t available()
returns the avialable buffer entries
Definition logic_analyzer.h:734
uint16_t numberOfPins()
Provides the number of subsequent GPIO pins which will be used for capturing.
Definition logic_analyzer.h:606
uint16_t startPin()
Provides the GPIO number of the start pin which is used for capturing.
Definition logic_analyzer.h:601
int readCount()
provides the read count
Definition logic_analyzer.h:664
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:249
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:266
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:240
Generic implementation of SUMP based logic analyzer.
void write(PinBitArray bits)
writes the status of all activated pins to the capturing device
Definition logic_analyzer.h:64
void log(const char *fmt,...)
Prints the content to the logger output stream.
Definition logic_analyzer.h:104