2 * This file is part of the libsigrok project.
4 * Copyright (C) 2022 Shawn Walker <ac0bi00@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef LIBSIGROK_HARDWARE_RASPBERRYPI_PICO_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_RASPBERRYPI_PICO_PROTOCOL_H
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 /* This is used by sr_dbg/log etc to indicate where a printout came from */
29 #define LOG_PREFIX "srpico"
31 /* Number of bytes between markers */
32 #define MRK_STRIDE 128
34 /* These must be 32 or or less since many channel enable/disable masks and other
35 * elements may be only 32 bits wide. Setting values larger than what a PICO can
36 * support to enable other devices, or possibly modes where channels are created
37 * from internal values rather than external pins */
38 #define MAX_ANALOG_CHANNELS 8
39 #define MAX_DIGITAL_CHANNELS 32
41 /* Digits input to sr_analog_init */
42 #define ANALOG_DIGITS 4
44 SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str);
45 SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch);
46 int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
47 char *resp, size_t cnt);
48 SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial, char *str);
50 typedef enum rxstate {
51 RX_IDLE = 0, /* Not receiving */
52 RX_ACTIVE = 1, /* Receiving data */
53 RX_STOPPED = 2, /* Received stop marker, waiting for byte cnt */
54 RX_ABORT = 3, /* Received aborted marker or other error */
58 /* Configuration Parameters
59 * It is up to the user to understand sample rates and serial download speed
60 * etc and do the right thing. i.e. don't expect continuous streaming
61 * bandwidth greater than serial link speed etc... */
62 /* The number of samples the user expects to see. */
63 uint64_t limit_samples;
65 /* Number of samples that have been received and processed */
67 /* Initial Number of analog and digital channels.
68 * This is set by initial device config. Channels can be disabled/enabled,
69 * but can not be added/removed once driver is loaded. */
70 uint16_t num_a_channels;
71 uint16_t num_d_channels;
72 /* Masks of enabled channels based on user input */
75 /* Channel groups - each analog channel is its own group */
76 struct sr_channel_group **analog_groups;
77 struct sr_channel_group *digital_group;
78 /* Data size in bytes for each analog channel in bytes must be 1 as only
79 * single byte samples are supported in this version */
81 /* Offset and scale for each analog channel to covert bytes to float */
82 float a_offset[MAX_ANALOG_CHANNELS];
83 float a_scale[MAX_ANALOG_CHANNELS];
84 /* % ratio of pre-trigger to post trigger samples */
85 uint64_t capture_ratio;
86 /* Total number of bytes of data sent for one sample across all channels */
87 uint16_t bytes_per_slice;
88 /* The number of bytes needed to store all channels for one sample in the
89 * device data buffer */
90 uint32_t dig_sample_bytes;
92 /* Tracking/status once started */
93 /* Number of bytes in the current serial input stream */
95 /* Samples sent to the session */
96 uint32_t sent_samples;
97 /* count total received bytes to detect lost info */
99 /* For SW-based triggering we put the device into continuous transmit and
100 * stop when we detect a sample and capture all the samples we need.
101 * trigger_fired is thus set when the sw trigger logic detects a trigger.
102 * For non triggered modes we send a start and a number of samples and the
103 * device transmits that much. trigger_fired is set immediately at the
105 gboolean trigger_fired;
109 /* Serial data buffer */
110 unsigned char *buffer;
111 /* Size of incoming serial buffer*/
112 uint32_t serial_buffer_size;
113 /* Current byte in serial read stream that is being processed */
115 /* Write pointer into the serial input buffer */
118 /* Buffering Related */
119 /* Parsed serial read data is split into each channels dedicated buffer
121 float *a_data_bufs[MAX_ANALOG_CHANNELS];
122 /* Digital samples are stored packed together since cli/pulseview want it
125 /* Write pointer for the the per channel data buffers */
127 /* Size of packet data buffers for each channel */
128 uint32_t sample_buf_size;
131 /* Previous sample values to duplicate for rle */
132 float a_last[MAX_ANALOG_CHANNELS];
135 /* SW trigger related */
136 struct soft_trigger_logic *stl;
137 /* Maximum number of entries to store pre-trigger */
138 uint32_t pretrig_entries;
139 /* Analog pre-trigger storage for software based triggering
140 * because sw based only has internal storage for logic */
141 float *a_pretrig_bufs[MAX_ANALOG_CHANNELS];
142 uint32_t pretrig_wr_ptr;
145 SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data);
146 SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi);
148 void process_D4(struct sr_dev_inst *sdi, struct dev_context *d);
149 void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc);
151 int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
152 uint32_t num_samples, uint32_t offset);
153 int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
154 uint32_t num_samples);
156 int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
157 uint32_t num_slices);
158 void rle_memset(struct dev_context *devc, uint32_t num_slices);
159 SR_PRIV int check_marker(struct dev_context *d, int *len);