]> sigrok.org Git - libsigrok.git/blob - src/hardware/raspberrypi-pico/protocol.h
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / raspberrypi-pico / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2022 Shawn Walker <ac0bi00@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef LIBSIGROK_HARDWARE_RASPBERRYPI_PICO_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_RASPBERRYPI_PICO_PROTOCOL_H
22
23 #include <stdint.h>
24 #include <glib.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27
28 /* This is used by sr_dbg/log etc to indicate where a printout came from */
29 #define LOG_PREFIX "srpico"
30
31 /* Number of bytes between markers */
32 #define MRK_STRIDE 128
33
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
40
41 /* Digits input to sr_analog_init */
42 #define ANALOG_DIGITS 4
43
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);
49
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 */
55 } rxstate_t;
56
57 struct dev_context {
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;
64         uint64_t sample_rate;
65         /* Number of samples that have been received and processed */
66         uint32_t num_samples;
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 */
73         uint32_t a_chan_mask;
74         uint32_t d_chan_mask;
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 */
80         uint8_t a_size;
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;
91
92         /* Tracking/status once started */
93         /* Number of bytes in the current serial input stream */
94         uint32_t bytes_avail;
95         /* Samples sent to the session */
96         uint32_t sent_samples;
97         /* count total received bytes to detect lost info */
98         uint64_t byte_cnt;
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
104          * start. */
105         gboolean trigger_fired;
106         rxstate_t rxstate;
107
108         /* Serial Related */
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 */
114         uint32_t ser_rdptr;
115         /* Write pointer into the serial input buffer */
116         uint32_t wrptr;
117
118         /* Buffering Related */
119         /* Parsed serial read data is split into each channels dedicated buffer
120          * for analog */
121         float *a_data_bufs[MAX_ANALOG_CHANNELS];
122         /* Digital samples are stored packed together since cli/pulseview want it
123          * that way */
124         uint8_t *d_data_buf;
125         /* Write pointer for the the per channel data buffers */
126         uint32_t cbuf_wrptr;
127         /* Size of packet data buffers for each channel */
128         uint32_t sample_buf_size;
129
130         /* RLE related*/
131         /* Previous sample values to duplicate for rle */
132         float a_last[MAX_ANALOG_CHANNELS];
133         uint8_t d_last[4];
134
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;
143 };
144
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);
147
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);
150
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);
155
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);
160
161
162 #endif