]> sigrok.org Git - libsigrok.git/blob - src/hardware/raspberrypi-pico/protocol.h
raspberrypi-pico: Rev2 updates
[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
29 #define LOG_PREFIX "srgn"
30  //number of bytes between markers
31 #define MRK_STRIDE 128
32
33 //This must be 32 or or less since many channel enable/disable masks and other elements may be only 32 bits wide.
34 //But is reduced further based on pico board limitations
35 #define MAX_ANALOG_CHANNELS 3
36 #define MAX_DIGITAL_CHANNELS 21
37 //digits input to sr_analog_init
38 #define ANALOG_DIGITS 4
39
40 SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str);
41 SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch);
42 int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
43                        char *resp, size_t cnt);
44 SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial,
45                               char *str);
46
47 typedef enum rxstate {
48         RX_IDLE = 0,            //not receiving
49         RX_ACTIVE = 1,          //receiving data
50         RX_STOPPED = 2,         //received stop marker, waiting for byte cnt
51         RX_ABORT = 3,           //received aborted marker or other error
52 } rxstate_t;
53 //TODO todo - stopped review here - renam wrptr, and review all variables
54 struct dev_context {
55 /*Configuration Parameters */
56         //It is up to the user to understand sample rates and serial download speed etc and 
57         // do the right thing. i.e. don't expect continuous streaming bandwidth greater 
58         //than serial link speed etc...
59         //The number of samples the user expects to see.
60         uint64_t limit_samples;
61         uint64_t sample_rate;
62         //Number of samples that have been received and processed
63         uint32_t num_samples;
64         //Initial Number of analog and digital channels.  
65         //This is set by initial device config.  Channels can be disabled/enabled, 
66         //but can not be added/removed once driver is loaded. 
67         uint16_t num_a_channels;
68         uint16_t num_d_channels;
69         //Masks of enabled channels based on user input
70         uint32_t a_chan_mask;
71         uint32_t d_chan_mask;
72         // Channel groups -each analog channel is it's own group
73         struct sr_channel_group **analog_groups;
74         struct sr_channel_group *digital_group;
75         //Data size in bytes for each analog channel in bytes 
76         //must be 1 as only single byte samples are supported in this version
77         uint8_t a_size;
78         //Offset and scale for each analog channel to covert bytes to float
79         float a_offset[MAX_ANALOG_CHANNELS];
80         float a_scale[MAX_ANALOG_CHANNELS];
81         // % ratio of pre-trigger to post trigger samples
82         uint64_t capture_ratio;
83         // total number of bytes of data sent for one sample across all channels
84         uint16_t bytes_per_slice;
85         //The number of bytes needed to store all channels for one sample in the device data buff
86         uint32_t dig_sample_bytes;
87 /* Tracking/status once started */
88         //number of bytes in the current serial input stream
89         uint32_t bytes_avail;
90         //Samples sent to the session */
91         uint32_t sent_samples;
92         //count total received bytes to detect lost info*/
93         uint64_t byte_cnt;
94         //For SW based triggering we put the device into continuous transmit and stop when 
95         // we detect a sample and capture all the samples we need. trigger_fired is thus set when
96         // the sw trigger logic detects a trigger.
97         //For non triggered modes we send a start and a number of samples and the device 
98         //transmits that much. trigger_fired is set immediately at the start.
99         gboolean trigger_fired;
100         //Has the device, via an "!" indicated it has stopped sending data, or has a marker 
101         //error been detected
102         //        gboolean device_stopped;
103         rxstate_t rxstate;
104 /* Serial Related */
105         // Serial data buffer 
106         unsigned char *buffer;
107         //Size of incoming serial buffer
108         uint32_t serial_buffer_size;
109         //Current byte in serial read stream that is being processed
110         uint32_t ser_rdptr;
111         //write pointer into the serial input buffer
112         uint32_t wrptr;
113
114 /* Buffering Related */
115         /* parsed serial read data is split into each channels dedicated buffer for analog */
116         float *a_data_bufs[MAX_ANALOG_CHANNELS];
117         /*digital samples are stored packed together since cli/pulseview want it that way */
118         uint8_t *d_data_buf;
119         /*write point for the the per channel data buffers */
120         uint32_t cbuf_wrptr;
121         /*size of packet data buffers for each channel */
122         uint32_t sample_buf_size;
123 /* RLE related*/
124         /*Previous sample values to duplicate for rle */
125         float a_last[MAX_ANALOG_CHANNELS];
126         uint8_t d_last[4];
127
128 /* SW Trigger Related */
129         struct soft_trigger_logic *stl;
130         //Maximum number of entries to store pre-trigger
131         uint32_t pretrig_entries;
132         /* Analog pre-trigger storage for software based triggering
133            because sw based only has internal storage for logic */
134         float *a_pretrig_bufs[MAX_ANALOG_CHANNELS];
135         uint32_t pretrig_wr_ptr;
136
137 };
138
139 SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data);
140 SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi);
141
142 void process_D4(struct sr_dev_inst *sdi, struct dev_context *d);
143 void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc);
144
145 int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
146                 uint32_t num_samples, uint32_t offset);
147 int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
148                      uint32_t num_samples);
149
150 int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
151                   uint32_t num_slices);
152 void rle_memset(struct dev_context *devc, uint32_t num_slices);
153 SR_PRIV int check_marker(struct dev_context *d, int *len);
154
155
156
157 #endif