]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/raspberrypi-pico/api.c
raspberrypi-pico: Comment clean up, support a more general use case model
[libsigrok.git] / src / hardware / raspberrypi-pico / api.c
index 3842864b5a410c0e0c6e5afb5155c34a8cf516ca..e6b03648745f1ff348b0a210d60f577a2cc6cc12 100644 (file)
@@ -38,14 +38,62 @@ static const uint32_t scanopts[] = {
        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
 };
-
-//PulseView reads a sample rate config list as a min, max and step.
-//If step is 1 then it creates a 1,2,5,10 set of selects, as well as the max.
-//If step is not 1, then it gives a place to enter any value, which gives the greatest flexibility
+//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),
+       SR_KHZ(5),
+       SR_KHZ(6),
+       SR_KHZ(8),
+       SR_KHZ(10),
+       SR_KHZ(20),
+       SR_KHZ(30),
+       SR_KHZ(40),
+       SR_KHZ(50),
+       SR_KHZ(60),
+       SR_KHZ(80),
+       SR_KHZ(100),
+       SR_KHZ(125),
+       SR_KHZ(150),
+       SR_KHZ(160),//max rate of 3 ADC channels that has integer divisor/dividend
+       SR_KHZ(200),
+       SR_KHZ(250), //max rate of 2 ADC channels
+       SR_KHZ(300),
+       SR_KHZ(400),
+       SR_KHZ(500),
+       SR_KHZ(600),
+       SR_KHZ(800),
+       //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.2),
+       SR_MHZ(1.5),
+       SR_MHZ(2),
+       SR_MHZ(2.4),
+       SR_MHZ(3),
+       SR_MHZ(4),
+       SR_MHZ(5),
+       SR_MHZ(6),
+       SR_MHZ(8),
+       SR_MHZ(10),
+       SR_MHZ(15),
+       SR_MHZ(20),
+       SR_MHZ(30),
+       SR_MHZ(40),
+       SR_MHZ(60),
+       //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[] = {
@@ -136,7 +184,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList * options)
                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);
@@ -151,12 +199,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList * options)
        }
        //Expected ID response is SRPICO,AxxyDzz,VV 
        //where xx are number of analog channels, y is bytes per analog sample
-       //and zz is number of digital channels, and VV is two digit version# which must be 00
+       //and zz is number of digital channels, and VV is two digit version# which must be 02
        if ((num_read < 16)
            || (strncmp(buf, "SRPICO,A", 8))
            || (buf[11] != 'D')
            || (buf[15] != '0')
-           || (buf[16] != '0')) {
+           || (buf[16] != '2')) {
                sr_err("ERROR:Bad response string %s %d", buf, num_read);
                return NULL;
        }
@@ -248,8 +296,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList * options)
        //Note: The intent of making this buffer large is to prevent CDC serial buffer overflows.
        //However, it is likely that if the host is running slow (i.e. it's a raspberry pi model 3) that it becomes
        //compute bound and doesn't service CDC serial responses in time to not overflow the internal CDC buffers.
-       //And thus no serial buffer is large enough.  But, it's only 256K....
-       devc->serial_buffer_size = 256000;
+       //And thus no serial buffer is large enough.  But, it's only 32K....
+       devc->serial_buffer_size = 32000;
        devc->buffer = NULL;
        sr_dbg("Setting serial buffer size: %i.",
               devc->serial_buffer_size);
@@ -257,11 +305,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList * options)
        //While slices are sent as a group of one sample across all channels, sigrok wants analog 
        //channel data sent as separate packets.  
        //Logical trace values are packed together.
-       //A serial byte in normal mode never represent more than one sample so a 2x multiplier is plenty.
-       //In D4 mode a serial byte can represents 100s of samples due to RLE, but process_D4 ensures that
-       //it breaks up the rle_memset calls to prevent overflowing the sample buffer.
-       //that it doesn't overflow the sample buffers.
-       devc->sample_buf_size = devc->serial_buffer_size * 2;
+       //An RLE byte in normal mode can represent up to 1640 samples.
+       //In D4 an RLE byte can represents up to 640 samples.
+        //Rather than making the sample_buf_size 1640x the size of serial buff, we require that the process loops
+        //push samples to the session as we get anywhere close to full.
+
+       devc->sample_buf_size = devc->serial_buffer_size;
        for (i = 0; i < devc->num_a_channels; i++) {
                devc->a_data_bufs[i] = NULL;
                devc->a_pretrig_bufs[i] = NULL;
@@ -311,7 +360,7 @@ static int config_set(uint32_t key, GVariant * data,
                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);
@@ -339,7 +388,7 @@ static int config_get(uint32_t key, GVariant ** 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)
@@ -377,12 +426,10 @@ static int config_list(uint32_t key, GVariant ** data,
        }
        sr_dbg("start config_list with key %X\n", key);
        switch (key) {
-//Pulseview in  pulseview/pv/toolbars/mainbar.cpp requires list support for frequencies as a triple
-//of min,max,step.  If step is 1, then it proves a 1,2,5,10 select, but if not 1 it allows a spin box
        case SR_CONF_SAMPLERATE:
                sr_dbg("Return sample rate list");
                *data =
-                   std_gvar_samplerates_steps(ARRAY_AND_SIZE
+                   std_gvar_samplerates(ARRAY_AND_SIZE
                                               (samplerates));
                break;
 //This must be set to get SW trigger support
@@ -413,10 +460,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        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);
@@ -448,8 +496,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                                    (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);
@@ -457,7 +503,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                                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",
@@ -470,7 +515,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                } else {
 
                }
-       }                       //for all channels
+       }//for all channels
        //ensure data channels are continuous
        int invalid = 0;
        for (i = 0; i < 32; i++) {
@@ -485,7 +530,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                        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) {
@@ -500,26 +545,20 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        sr_dbg("bps %d\n", devc->bytes_per_slice);
 
        //Apply sample rate limits
-       //Save off the lower rate values which are hacked way of getting configs to the device
-       uint8_t cfg_bits;
-       cfg_bits = (devc->sample_rate % 10 & 0x6);      //Only bits 2&1 are used as cfg_bits
-       devc->sample_rate -= cfg_bits;
-       sr_warn("Capture device cfg_bits of 0x%X from sample rate %lld",
-               cfg_bits, devc->sample_rate);
-       if ((a_enabled == 3) && (devc->sample_rate > 166660)) {
+       //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 166.660khz");
-               devc->sample_rate = 166660;
+                   ("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
@@ -528,14 +567,15 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                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 =
@@ -543,15 +583,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                        //Always increment the divisor so that we go down in frequency to avoid max sample rate issues
                        commondivint++;
                        devc->sample_rate = 24000000ULL / commondivint;
-                       //While the common divisor is an integer, that does not mean the resulting sample rate is, and
-                       //we want to keep the sample_rate divisible by 10 to support the cfg_bits
-                       while ((devc->sample_rate % 10)
-                              && (commondivint < 4800)) {
-                               commondivint++;
-                               devc->sample_rate =
-                                   24000000ULL / commondivint;
-                               //sr_err(" sample rate of %llu div %u\n\r",devc->sample_rate,commondivint); 
-                       }
                        //Make sure the divisor increement didn't make use go too low.
                        if (devc->sample_rate < 5000) {
                                devc->sample_rate = 50000;
@@ -562,9 +593,12 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                }
 
        }
-       //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
@@ -580,32 +614,38 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                }
        }
 
-       //modulo 10 to add cfg_bits back in
-       //All code above should create overrides that are multiples of 10, but add a check just in case.
-       if (devc->sample_rate % 10) {
-               sr_err("Output sample rate %llu not mod 10",
-                      devc->sample_rate);
-               devc->sample_rate = (devc->sample_rate / 10) * 10;
-       }
-
-       devc->sample_rate += cfg_bits;
-       if (cfg_bits) {
-               sr_warn
-                   ("Embedding cfg_bits of 0x%X in sample_rate %lld\n\r",
-                    cfg_bits, devc->sample_rate);
-       }
-       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;
@@ -638,10 +678,62 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                        return SR_ERR_MALLOC;
                }
        }
-
+       devc->pretrig_entries =
+           (devc->capture_ratio * devc->limit_samples) / 100;
+        //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 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, unless HW based triggers are implemented
        if ((trigger = sr_session_trigger_get(sdi->session))) {
-               devc->pretrig_entries =
-                   (devc->capture_ratio * devc->limit_samples) / 100;
+                if (g_slist_length(trigger->stages) > 1)
+                        return SR_ERR_NA;
+
+                struct sr_trigger_stage *stage;
+                struct sr_trigger_match *match;
+               GSList *l;
+               stage = g_slist_nth_data(trigger->stages, 0);
+                if (!stage)
+                        return SR_ERR_ARG;
+                for (l = stage->matches; l; l = l->next) {
+                        match = l->data;
+                        if (!match->match)
+                                continue;
+                        if (!match->channel->enabled)
+                                continue;
+                        int idx = match->channel->index;
+                        int8_t val;
+                        switch(match->match){
+                           case SR_TRIGGER_ZERO:
+                             val=0; break;
+                           case SR_TRIGGER_ONE:
+                             val=1; break;
+                           case SR_TRIGGER_RISING:
+                             val=2; break;
+                           case SR_TRIGGER_FALLING:
+                             val=3; break;
+                           case SR_TRIGGER_EDGE:
+                             val=4; break;
+                           default:
+                             val=-1;
+                       }
+                       sr_info("Trigger value idx %d match %d",idx,match->match);
+                        //Only set trigger on enabled channels
+                        if((val>=0) && ((devc->d_chan_mask>>idx)&1)){
+                         sprintf(&tmpstr[0], "t%d%02d\n", val,idx+2);
+                         if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
+                           sr_err("Trigger cfg to device failed");
+                           return SR_ERR;
+                         }   
+
+                       }
+                }
+               sprintf(&tmpstr[0], "p%d\n", devc->pretrig_entries);
+               if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
+                   sr_err("Pretrig to device failed");
+                           return SR_ERR; 
+                }              
                devc->stl =
                    soft_trigger_logic_new(sdi, trigger,
                                           devc->pretrig_entries);
@@ -728,21 +820,16 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
                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;
@@ -752,10 +839,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
                        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;
 }