]> sigrok.org Git - libsigrok.git/blob - src/hardware/raspberrypi-pico/protocol.h
raspberrypi-pico: Formatting; support force_detect, analog sizes
[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 /*/number of bytes between markers*/
31 #define MRK_STRIDE 128
32
33 /*These must be 32 or or less since many channel enable/disable masks and other elements may be only 32 bits wide.
34 Setting values larger than what a PICO can support to enable other devices, or possibly modes where
35 channels are created from internal values rather than external pins */
36 #define MAX_ANALOG_CHANNELS 8
37 #define MAX_DIGITAL_CHANNELS 32
38 /*digits input to sr_analog_init */
39 #define ANALOG_DIGITS 4
40
41 SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str);
42 SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch);
43 int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
44                        char *resp, size_t cnt);
45 SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial,
46                               char *str);
47
48 typedef enum rxstate {
49                       RX_IDLE = 0,              /*not receiving */
50                       RX_ACTIVE = 1,            /*receiving data */
51                       RX_STOPPED = 2,           /*received stop marker, waiting for byte cnt */
52                       RX_ABORT = 3,             /*received aborted marker or other error */
53 } rxstate_t;
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
88 /* Tracking/status once started */
89   /* number of bytes in the current serial input stream */
90   uint32_t bytes_avail;
91   /* Samples sent to the session */
92   uint32_t sent_samples;
93   /* count total received bytes to detect lost info*/
94   uint64_t byte_cnt;
95   /* For SW based triggering we put the device into continuous transmit and stop when 
96   we detect a sample and capture all the samples we need. trigger_fired is thus set when
97   the sw trigger logic detects a trigger.
98   For non triggered modes we send a start and a number of samples and the device 
99   transmits that much. trigger_fired is set immediately at the start. */
100   gboolean trigger_fired;
101   rxstate_t rxstate;
102   
103 /* Serial Related */
104   /* Serial data buffer */ 
105   unsigned char *buffer;
106   /* Size of incoming serial buffer*/
107   uint32_t serial_buffer_size;
108   /* Current byte in serial read stream that is being processed */
109   uint32_t ser_rdptr;
110   /* write pointer into the serial input buffer */
111   uint32_t wrptr;
112
113 /* Buffering Related */
114   /* parsed serial read data is split into each channels dedicated buffer for analog */
115   float *a_data_bufs[MAX_ANALOG_CHANNELS];
116   /* digital samples are stored packed together since cli/pulseview want it that way */
117   uint8_t *d_data_buf;
118   /* write pointer for the the per channel data buffers */
119   uint32_t cbuf_wrptr;
120   /* size of packet data buffers for each channel */
121   uint32_t sample_buf_size;
122   /* RLE related*/
123   /* Previous sample values to duplicate for rle */
124   float a_last[MAX_ANALOG_CHANNELS];
125   uint8_t d_last[4];
126
127 /* SW Trigger Related */
128   struct soft_trigger_logic *stl;
129   /* Maximum number of entries to store pre-trigger*/
130   uint32_t pretrig_entries;
131   /* Analog pre-trigger storage for software based triggering
132   because sw based only has internal storage for logic */
133   float *a_pretrig_bufs[MAX_ANALOG_CHANNELS];
134   uint32_t pretrig_wr_ptr;
135
136 };
137
138 SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data);
139 SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi);
140
141 void process_D4(struct sr_dev_inst *sdi, struct dev_context *d);
142 void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc);
143
144 int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
145                 uint32_t num_samples, uint32_t offset);
146 int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
147                      uint32_t num_samples);
148
149 int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
150                   uint32_t num_slices);
151 void rle_memset(struct dev_context *devc, uint32_t num_slices);
152 SR_PRIV int check_marker(struct dev_context *d, int *len);
153
154
155
156 #endif