]> sigrok.org Git - libsigrok.git/blame - src/hardware/raspberrypi-pico/protocol.h
raspberrypi-pico: Rev2 updates
[libsigrok.git] / src / hardware / raspberrypi-pico / protocol.h
CommitLineData
dc90146e
A
1/*
2 * This file is part of the libsigrok project.
3 *
bac2a8b8 4 * Copyright (C) 2022 Shawn Walker <ac0bi00@gmail.com>
dc90146e
A
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
bac2a8b8
A
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
dc90146e 32
bac2a8b8
A
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
40SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str);
41SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch);
ac132f83
A
42int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
43 char *resp, size_t cnt);
44SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial,
45 char *str);
bac2a8b8
A
46
47typedef enum rxstate {
ac132f83
A
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;
bac2a8b8 53//TODO todo - stopped review here - renam wrptr, and review all variables
dc90146e 54struct dev_context {
bac2a8b8 55/*Configuration Parameters */
ac132f83
A
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;
bac2a8b8 87/* Tracking/status once started */
ac132f83
A
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;
bac2a8b8 104/* Serial Related */
ac132f83
A
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;
bac2a8b8
A
113
114/* Buffering Related */
ac132f83
A
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;
bac2a8b8 123/* RLE related*/
ac132f83
A
124 /*Previous sample values to duplicate for rle */
125 float a_last[MAX_ANALOG_CHANNELS];
0c792900 126 uint8_t d_last[4];
bac2a8b8
A
127
128/* SW Trigger Related */
ac132f83
A
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;
bac2a8b8 136
dc90146e
A
137};
138
bac2a8b8
A
139SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data);
140SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi);
141
ac132f83
A
142void process_D4(struct sr_dev_inst *sdi, struct dev_context *d);
143void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc);
bac2a8b8 144
ac132f83
A
145int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
146 uint32_t num_samples, uint32_t offset);
147int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
148 uint32_t num_samples);
bac2a8b8 149
ac132f83
A
150int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
151 uint32_t num_slices);
152void rle_memset(struct dev_context *devc, uint32_t num_slices);
153SR_PRIV int check_marker(struct dev_context *d, int *len);
bac2a8b8
A
154
155
dc90146e
A
156
157#endif