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