SR_CONF_CONN, //Required OS name for the port, i.e. /dev/ttyACM0
SR_CONF_SERIALCOMM, //Optional config of the port, i.e. 115200/8n1
};
-//The host can either provide a std_gvar_samplerates_steps or a std_gvar_samplerates.
+//Sample rate can either provide a std_gvar_samplerates_steps or a std_gvar_samplerates.
//The latter is just a long list of every supported rate.
//For the steps, pulseview/pv/toolbars/mainbar.cpp will do a min,max,step. If step is
//1 then it provides a 1,2,5,10 select otherwise it allows a spin box.
//Going with the full list because while the spin box is more flexible, it is harder to read
-/*
-static const uint64_t samplerates[] = {
- SR_HZ(10),
- SR_MHZ(120),
- SR_HZ(2),
-};
-*/
static const uint64_t samplerates[] = {
SR_KHZ(5),
SR_KHZ(6),
SR_KHZ(500),
SR_KHZ(600),
SR_KHZ(800),
- //Give finer granularity near the thresholds of RLE effectiveness
+ //Give finer granularity near the thresholds of RLE effectiveness ~1-4Msps
+ //Also use 1.2 and 2.4 as likely max values for ADC overclocking
SR_MHZ(1),
- SR_MHZ(1.25),
+ SR_MHZ(1.2),
SR_MHZ(1.5),
SR_MHZ(2),
- SR_MHZ(2.5),
+ SR_MHZ(2.4),
SR_MHZ(3),
SR_MHZ(4),
SR_MHZ(5),
SR_MHZ(30),
SR_MHZ(40),
SR_MHZ(60),
- SR_MHZ(120)
+ //The baseline 120Mhz PICO clock won't support an 80 or 100
+ //with non fractional divisor, but an overclocked version or one
+ //that modified sysclk could
+ SR_MHZ(80),
+ SR_MHZ(100),
+ SR_MHZ(120),
+ //These may not be practically useful, but someone might want to
+ //try to make it work with overclocking
+ SR_MHZ(150),
+ SR_MHZ(200),
+ SR_MHZ(240)
};
static const uint32_t drvopts[] = {
serial_close(serial);
g_usleep(100000);
if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
- sr_err("2st serial open fail");
+ sr_err("2nd serial open fail");
return NULL;
}
g_usleep(100000);
break;
case SR_CONF_LIMIT_SAMPLES:
devc->limit_samples = g_variant_get_uint64(data);
- sr_dbg("config_set slimit %lld\n", devc->limit_samples);
+ sr_dbg("config_set slimit %" PRIu64 "\n", devc->limit_samples);
break;
case SR_CONF_CAPTURE_RATIO:
devc->capture_ratio = g_variant_get_uint64(data);
switch (key) {
case SR_CONF_SAMPLERATE:
*data = g_variant_new_uint64(devc->sample_rate);
- sr_spew("sample rate get of %lld", devc->sample_rate);
+ sr_spew("sample rate get of %" PRIu64 "", devc->sample_rate);
break;
case SR_CONF_CAPTURE_RATIO:
if (!sdi)
case SR_CONF_SAMPLERATE:
sr_dbg("Return sample rate list");
*data =
-// std_gvar_samplerates_steps(ARRAY_AND_SIZE
-// (samplerates));
std_gvar_samplerates(ARRAY_AND_SIZE
(samplerates));
break;
struct sr_channel *ch;
struct sr_trigger *trigger;
char tmpstr[20];
+ char buf[32];
GSList *l;
int a_enabled = 0, d_enabled = 0, len;
serial = sdi->conn;
- int i;
+ int i,num_read;
devc = sdi->priv;
sr_dbg("Enter acq start");
sr_dbg("dsbstart %d", devc->dig_sample_bytes);
(ch->enabled << ch->index);
a_enabled++;
}
-// sr_dbg("A%d en %d mask 0x%X",ch->index,ch->enabled,devc->a_chan_mask);
-
}
if (ch->name[0] == 'D') {
devc->d_chan_mask &= ~(1 << ch->index);
devc->d_chan_mask |=
(ch->enabled << ch->index);
d_enabled++;
- // sr_dbg("D%d en %d mask 0x%X",ch->index,ch->enabled,devc->d_chan_mask);
}
}
sr_info("Channel enable masks D 0x%X A 0x%X",
} else {
}
- } //for all channels
+ }//for all channels
//ensure data channels are continuous
int invalid = 0;
for (i = 0; i < 32; i++) {
invalid = 1;
}
}
- //recalculate bytes_per_slice.
+ //recalculate bytes_per_slice based on which analog channels are enabled
devc->bytes_per_slice = (a_enabled * devc->a_size);
for (i = 0; i < devc->num_d_channels; i += 7) {
sr_dbg("bps %d\n", devc->bytes_per_slice);
//Apply sample rate limits
+ //While earlier versions forced a lower sample rate, the PICO seems to allow
+ //ADC overclocking, and by not enforcing these limits it may support other devices.
+ //Thus call sr_err to get something into the device logs, but allowing it to progress.
if ((a_enabled == 3) && (devc->sample_rate > 160000)) {
sr_err
- ("ERROR:3 channel ADC sample rate dropped to 160khz");
- devc->sample_rate = 160000;
+ ("WARN:3 channel ADC sample rate above 160khz");
}
if ((a_enabled == 2) && (devc->sample_rate > 250000)) {
sr_err
- ("ERROR:2 channel ADC sample rate dropped to 250khz");
- devc->sample_rate = 250000;
+ ("WARN:2 channel ADC sample rate above 250khz");
}
if ((a_enabled == 1) && (devc->sample_rate > 500000)) {
sr_err
- ("ERROR:1 channel ADC sample rate dropped to 500khz");
- devc->sample_rate = 500000;
+ ("WARN:1 channel ADC sample rate above 500khz");
}
//Depending on channel configs, rates below 5ksps are possible
//but such a low rate can easily stream and this eliminates a lot
sr_err("Sample rate override to min of 5ksps");
devc->sample_rate = 5000;
}
+ //While PICO specs a max clock ~120-125Mhz, it does overclock in many cases
+ //so leaving is a warning.
if (devc->sample_rate > 120000000) {
- sr_err("Sample rate override to max of 120Msps");
- devc->sample_rate = 12000000;
+ sr_err("WARN: Sample rate above 120Msps");
}
//It may take a very large number of samples to notice, but if digital and analog are enabled
//and either PIO or ADC are fractional the samples will skew over time.
//24Mhz is the max common divisor to the 120Mhz and 48Mhz ADC clock
- //so force an integer divisor to it.
+ //so force an integer divisor to 24Mhz.
if ((a_enabled > 0) && (d_enabled > 0)) {
if (24000000ULL % (devc->sample_rate)) {
uint32_t commondivint =
}
}
- //If we are only digital only or only analog print a warning that the
+ //If we are only digital or only analog print a warning that the
//fractional divisors aren't a true PLL fractional feedback loop and thus
//could have sample to sample variation.
+ //These warnings of course assume that the device is programmed with the expected ratios
+ //but non PICO implementations, or PICO implementations that use different divisors could avoid.
+ //This generally won't be a problem because most of the sampe_rate pulldown values are integer divisors.
if (a_enabled > 0) {
if (48000000ULL % (devc->sample_rate * a_enabled)) {
sr_warn
}
}
- sprintf(&tmpstr[0], "R%llu\n", devc->sample_rate);
+
+ sprintf(tmpstr, "L%" PRIu64 "\n", devc->limit_samples);
if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
- sr_err("Sample rate to device failed");
+ sr_err("Sample limit to device failed");
return SR_ERR;
}
- sprintf(tmpstr, "L%lld\n", devc->limit_samples);
- if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
- sr_err("Sample limit to device failed");
+ //To support future devices that may allow the analog scale/offset to change, call get_dev_cfg again to get new values
+ if(raspberrypi_pico_get_dev_cfg(sdi) != SR_OK){
+ sr_err("get_dev_cfg failure on start");
+ return SR_ERR;
+ }
+
+ //With all other params set, we use the final sample rate setting as an opportunity for the device
+ //to communicate any errors in configuration.
+ //A single "*" indicates success.
+ //A "*" with subsequent data is success, but allows for the device to print something
+ //to the error console without aborting.
+ //A non "*" in the first character blocks the start
+ sprintf(tmpstr, "R%llu\n", devc->sample_rate);
+ num_read = send_serial_w_resp(serial, tmpstr, buf, 30);
+ buf[num_read]=0;
+ if((num_read>1)&&(buf[0]=='*')){
+ sr_err("Sample rate to device success with resp %s",buf);
+ }
+ else if(!((num_read==1)&&(buf[0]=='*'))){
+ sr_err("Sample rate to device failed");
+ if(num_read>0){
+ buf[num_read]=0;
+ sr_err("sample_rate error string %s",buf);
+ }
return SR_ERR;
}
-
-
devc->sent_samples = 0;
devc->byte_cnt = 0;
devc->bytes_avail = 0;
//While the driver supports the passing of trigger info to the device
//it has been found that the sw overhead of supporting triggering and
//pretrigger buffer entries etc.. ends up slowing the cores down enough
- //that the effect continous sample rate isn't much higher than that of sending
+ //that the effective continous sample rate isn't much higher than that of sending
//untriggered samples across USB. Thus this code will remain but likely may
- //not be used by the device.
+ //not be used by the device, unless HW based triggers are implemented
if ((trigger = sr_session_trigger_get(sdi->session))) {
if (g_slist_length(trigger->stages) > 1)
return SR_ERR_NA;
if (len)
sr_err("Dropping %d device bytes\n\r", len);
} while (len > 0);
-
-
-
if (devc->buffer) {
g_free(devc->buffer);
devc->buffer = NULL;
}
-
for (int i = 0; i < devc->num_a_channels; i++) {
if (devc->a_data_bufs[i]) {
g_free(devc->a_data_bufs[i]);
devc->a_data_bufs[i] = NULL;
}
}
-
if (devc->d_data_buf) {
g_free(devc->d_data_buf);
devc->d_data_buf = NULL;
g_free(devc->a_pretrig_bufs[i]);
devc->a_pretrig_bufs[i] = NULL;
}
-
serial = sdi->conn;
serial_source_remove(sdi->session, serial);
-
return SR_OK;
}
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
#define _GNU_SOURCE
#include <config.h>
return SR_OK;
}
-//Issue a command that expects a string return, return length of string
+//Issue a command that expects a string return that is less than 30 characters.
+//returns the length of string
int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
char *resp, size_t cnt)
{
if (num_read > 0)
break;
}
- //sr_spew("rwprsp1 i %d nr %d",i,num_read);
//Since the serial port is usb CDC we can't calculate timeouts based on baud rate but
//even if the response is split between two USB transfers 10ms should be plenty.
num_read +=
serial_read_blocking(serial, &(resp[num_read]), cnt - num_read,
10);
- //sr_spew("rwrsp2 nr %d",num_read);
-
if ((num_read < 1) || (num_read > 30)) {
sr_err("ERROR:Serial_w_resp failed (%d).", num_read);
return -1;
char buf[2];
int num_read;
//In case we have left over transfer from the device, drain them
+ //These should not exist in normal operation
while ((num_read = serial_read_blocking(serial, buf, 2, 10))) {
- //sr_dbg("swack drops 2 previous bytes %d %d",buf[0],buf[1]);
+ sr_dbg("swack drops 2 bytes %d %d",buf[0],buf[1]);
}
send_serial_str(serial, str);
//1000ms timeout
}
//If the wrptr is non-zero due to a residual from the previous serial transfer don't double count it towards byte_cnt
devc->byte_cnt += slice_bytes - (devc->wrptr);
- sr_spew("process slice avail %d rdptr %d sb %d byte_cnt %lld",
+ sr_spew("process slice avail %d rdptr %d sb %d byte_cnt %" PRIu64 "",
devc->bytes_avail, devc->ser_rdptr, slice_bytes,
devc->byte_cnt);
//Must have a full slice or one rle byte
packet.payload = &analog;
sr_session_send(sdi, &packet);
g_slist_free(analog.meaning->channels);
- } //if enabled
- } //for channels
+ }//if enabled
+ }//for channels
return 0;
}
analog.num_samples = num_post;
analog.data =
(devc->a_pretrig_bufs[i]) + start_post;
- //sr_spew("ring buf %d starts at %p",i,(void *) devc->a_pretrig_bufs[i]);
- //sr_spew("analog data %d starts at %p",i,(void *) analog.data);
- //sr_spew("Sending A%d ring buffer oldest ",i);
for (uint32_t j = 0;
j < analog.num_samples; j++) {
fptr =
analog.data +
(j * sizeof(float));
- //sr_spew("RNGDCT%d j %d %f %p",i,j,*fptr,(void *)fptr);
}
sr_session_send(sdi, &packet);
}
}
g_slist_free(analog.meaning->channels);
sr_dbg("Sending A%d ring buffer done ", i);
- } //if enabled
- } //for channels
+ }//if enabled
+ }//for channels
return 0;
}
sr_spew
("Process_group sending %d post trig samples dsb %d",
num_samples, devc->dig_sample_bytes);
- //for(int z=0;(z<num_samples);z++){
- // sr_spew("0x%X ",devc->d_data_buf[z]);
- //}
if (devc->num_d_channels) {
packet.type = SR_DF_LOGIC;
packet.payload = &logic;
sr_session_send(sdi, &packet);
}
send_analog(sdi, devc, num_samples, 0);
- } //num_sample>0
+ }//num_samples >0
devc->sent_samples += num_samples;
return 0;
- } //trigger_fired
+ }//trigger_fired
else {
size_t num_ring_samples;
size_t sptr;
size_t numtail;
size_t numwrap;
size_t srcptr;
- //sr_spew("Process_group check %d pre trig samples",num_slices);
//The trigger_offset is -1 if no trigger is found, but if a trigger is found
//then trigger_offset is the offset into the data buffer sent to it.
//The pre_trigger_samples is the total number of samples before the trigger, but limited to
num_slices *
devc->dig_sample_bytes,
&pre_trigger_samples);
- //A trigger offset >=0 indicate a trigger was seen. The stl will isue the trigger to the session
+ //A trigger offset >=0 indicates a trigger was seen. The stl will isue the trigger to the session
//and will forward all pre trigger logic samples, but we must send any post trigger logic
//and all pre and post trigger analog signals
- // sr_dbg("trggr_off %d",trigger_offset);
- // sr_dbg("pre_samp %d",pre_trigger_samples);
if (trigger_offset > -1) {
devc->trigger_fired = TRUE;
devc->sent_samples += pre_trigger_samples;
srcptr = cbuf_wrptr_cpy - num_ring_samples;
sr_spew("RNG num %zu sptr %zu eptr %zu ",
num_ring_samples, sptr, eptr);
- //sr_spew("RNG srcptr %zu nt %zu nw %zu",srcptr,numtail,numwrap);
for (i = 0; i < devc->num_a_channels; i++) {
if ((devc->a_chan_mask >> i) & 1) {
//copy tail
devc->a_data_bufs
[i]
[srcptr + j];
- //sr_spew("RNGCpyT C%d src %zu dest %zu",i,srcptr+j,sptr+j);
} //for j
} //if chan_mask
} //for channels
devc->a_data_bufs
[i]
[srcptr + j];
- //sr_spew("RNGCpyW C%d src %zu dest %zu",i,srcptr+j,j);
} //for j
} //if chan_mask
} //for channels
(numwrap) ? numwrap : (eptr +
1) %
devc->pretrig_entries;
- //sr_dbg("RNG pwrptr new %u",devc->pretrig_wr_ptr);
} //if any analog channel enabled and pretrig_entries
} //else (trigger not detected)
} //trigger not set on function entry
didx=devc->cbuf_wrptr*devc->dig_sample_bytes;
for (k = 0; k < devc->dig_sample_bytes; k++) {
devc->d_data_buf[didx + k] = devc->d_last[k];
- // sr_spew("k %d j %d v 0x%X p %d didx %d",k,j,devc->d_data_buf[(devc->cbuf_wrptr)+k],(devc->cbuf_wrptr)+k,didx);
}
// cbuf_wrptr always counts slices/samples (and not the bytes in the buffer)
// regardless of mode
sr_dbg("Reached non active state in receive %d",
devc->rxstate);
//don't return - we may be waiting for a final bytecnt
- //return TRUE;
}
if (devc->rxstate == RX_IDLE) {
//This is the normal end condition where we do one more receive
devc->buffer[devc->wrptr + len] = 0;
//Add the "#" so that spaces are clearly seen
sr_dbg("rx string %s#", devc->buffer);
- //This is not guaranteed to be a dataloss condition, but definitely indicates we are
- //processing data right at the incoming rate.
- //With the addition of the byte_cnt sent at the end we will detect any dataloss conditions
- //and thus this is disabled
- //if(len>=(int)bytes_rem-8){
- // sr_err("ERROR: Serial buffer near or at max depth, data from device may have been lost");
- //}
devc->bytes_avail = (devc->wrptr + len);
sr_spew
("rx len %d bytes_avail %ul sent_samples %ul wrptr %u",
len, devc->bytes_avail, devc->sent_samples,
devc->wrptr);
- //sr_err("rx len %d ",len);
} else if (len == 0) {
return TRUE;
} else {
sr_err("ERROR:Negative serial read code %d", len);
sdi->driver->dev_acquisition_stop(sdi);
return FALSE;
- } //len>0
- //This can be used as a bit bucket to drop all samples to see how host processing time effects
- //the devices ability to send data. Obviously no data will be forwarded to the session so it will hang
- // return TRUE;
+ }// if len>0
//Process the serial read data
devc->ser_rdptr = 0;
//But they may not use all of it, and thus the residual unused bytes are shifted to the start of the buffer
//for the next call.
residual_bytes = devc->bytes_avail - devc->ser_rdptr;
- //sr_spew("Residuals resid %d avail %d rdptr %d wrptr %d\n",residual_bytes,devc->bytes_avail,devc->ser_rdptr,devc->wrptr);
if (residual_bytes) {
for (i = 0; i < residual_bytes; i++) {
devc->buffer[i] =
}
//It's possible we need one more serial transfer to get the byte_cnt, so print that here
sr_dbg("Haven't seen byte_cnt + yet");
- } //RX_STOPPED
+ } //if RX_STOPPED
//If at the sample limit, send a "+" in case we are in continuous mode and need
//to stop the device. Not that even in non continous mode there might be cases where get an extra
//sample or two...
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
-//This is used by sr_dbg/log etc
-#define LOG_PREFIX "srgn"
+//This is used by sr_dbg/log etc to indicate where a printout came from
+#define LOG_PREFIX "srpico"
//number of bytes between markers
#define MRK_STRIDE 128
-//This must be 32 or or less since many channel enable/disable masks and other elements may be only 32 bits wide.
-//But is reduced further based on pico board limitations
-#define MAX_ANALOG_CHANNELS 3
-#define MAX_DIGITAL_CHANNELS 21
+//These must be 32 or or less since many channel enable/disable masks and other elements may be only 32 bits wide.
+//Setting values larger than what a PICO can support to enable other devices, or possibly modes where
+//channels are created from internal values rather than external pins
+#define MAX_ANALOG_CHANNELS 8
+#define MAX_DIGITAL_CHANNELS 32
//digits input to sr_analog_init
#define ANALOG_DIGITS 4
RX_STOPPED = 2, //received stop marker, waiting for byte cnt
RX_ABORT = 3, //received aborted marker or other error
} rxstate_t;
-//TODO todo - stopped review here - renam wrptr, and review all variables
struct dev_context {
-/*Configuration Parameters */
+//Configuration Parameters
//It is up to the user to understand sample rates and serial download speed etc and
// do the right thing. i.e. don't expect continuous streaming bandwidth greater
//than serial link speed etc...
uint16_t bytes_per_slice;
//The number of bytes needed to store all channels for one sample in the device data buff
uint32_t dig_sample_bytes;
-/* Tracking/status once started */
+// Tracking/status once started
//number of bytes in the current serial input stream
uint32_t bytes_avail;
- //Samples sent to the session */
+ //Samples sent to the session
uint32_t sent_samples;
- //count total received bytes to detect lost info*/
+ //count total received bytes to detect lost info
uint64_t byte_cnt;
//For SW based triggering we put the device into continuous transmit and stop when
// we detect a sample and capture all the samples we need. trigger_fired is thus set when
//error been detected
// gboolean device_stopped;
rxstate_t rxstate;
-/* Serial Related */
+// Serial Related
// Serial data buffer
unsigned char *buffer;
//Size of incoming serial buffer
//write pointer into the serial input buffer
uint32_t wrptr;
-/* Buffering Related */
- /* parsed serial read data is split into each channels dedicated buffer for analog */
+// Buffering Related
+ // parsed serial read data is split into each channels dedicated buffer for analog
float *a_data_bufs[MAX_ANALOG_CHANNELS];
- /*digital samples are stored packed together since cli/pulseview want it that way */
+ // digital samples are stored packed together since cli/pulseview want it that way
uint8_t *d_data_buf;
- /*write point for the the per channel data buffers */
+ // write pointer for the the per channel data buffers
uint32_t cbuf_wrptr;
- /*size of packet data buffers for each channel */
+ // size of packet data buffers for each channel
uint32_t sample_buf_size;
-/* RLE related*/
- /*Previous sample values to duplicate for rle */
+// RLE related
+ // Previous sample values to duplicate for rle
float a_last[MAX_ANALOG_CHANNELS];
uint8_t d_last[4];
-/* SW Trigger Related */
+// SW Trigger Related
struct soft_trigger_logic *stl;
//Maximum number of entries to store pre-trigger
uint32_t pretrig_entries;
- /* Analog pre-trigger storage for software based triggering
- because sw based only has internal storage for logic */
+ // Analog pre-trigger storage for software based triggering
+ // because sw based only has internal storage for logic
float *a_pretrig_bufs[MAX_ANALOG_CHANNELS];
uint32_t pretrig_wr_ptr;