libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
chronovu_la8.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <glib.h>
00024 #include "sigrok.h"
00025 #include "sigrok-internal.h"
00026 
00027 struct context {
00028         unsigned int num_enabled_probes;
00029         unsigned int unitsize;
00030         char *probelist[SR_MAX_NUM_PROBES + 1];
00031         uint64_t trigger_point;
00032         uint64_t samplerate;
00033 };
00034 
00035 /**
00036  * Check if the given samplerate is supported by the LA8 hardware.
00037  *
00038  * @param samplerate The samplerate (in Hz) to check.
00039  *
00040  * @return 1 if the samplerate is supported/valid, 0 otherwise.
00041  */
00042 static int is_valid_samplerate(uint64_t samplerate)
00043 {
00044         unsigned int i;
00045 
00046         for (i = 0; i < 255; i++) {
00047                 if (samplerate == (SR_MHZ(100) / (i + 1)))
00048                         return 1;
00049         }
00050 
00051         sr_warn("la8 out: %s: invalid samplerate (%" PRIu64 "Hz)",
00052                 __func__, samplerate);
00053 
00054         return 0;
00055 }
00056 
00057 /**
00058  * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
00059  *
00060  * LA8 hardware: sample period = (divcount + 1) * 10ns.
00061  * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
00062  * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
00063  *
00064  * @param samplerate The samplerate in Hz.
00065  *
00066  * @return The divcount value as needed by the hardware, or 0xff upon errors.
00067  */
00068 static uint8_t samplerate_to_divcount(uint64_t samplerate)
00069 {
00070         if (samplerate == 0) {
00071                 sr_warn("la8 out: %s: samplerate was 0", __func__);
00072                 return 0xff;
00073         }
00074 
00075         if (!is_valid_samplerate(samplerate)) {
00076                 sr_warn("la8 out: %s: can't get divcount, samplerate invalid",
00077                         __func__);
00078                 return 0xff;
00079         }
00080 
00081         return (SR_MHZ(100) / samplerate) - 1;
00082 }
00083 
00084 static int init(struct sr_output *o)
00085 {
00086         struct context *ctx;
00087         struct sr_probe *probe;
00088         GSList *l;
00089         uint64_t samplerate;
00090 
00091         if (!o) {
00092                 sr_warn("la8 out: %s: o was NULL", __func__);
00093                 return SR_ERR_ARG;
00094         }
00095 
00096         if (!o->dev) {
00097                 sr_warn("la8 out: %s: o->dev was NULL", __func__);
00098                 return SR_ERR_ARG;
00099         }
00100 
00101         if (!o->dev->driver) {
00102                 sr_warn("la8 out: %s: o->dev->driver was NULL", __func__);
00103                 return SR_ERR_ARG;
00104         }
00105 
00106         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
00107                 sr_warn("la8 out: %s: ctx malloc failed", __func__);
00108                 return SR_ERR_MALLOC;
00109         }
00110 
00111         o->internal = ctx;
00112 
00113         /* Get the probe names and the unitsize. */
00114         /* TODO: Error handling. */
00115         for (l = o->dev->probes; l; l = l->next) {
00116                 probe = l->data;
00117                 if (!probe->enabled)
00118                         continue;
00119                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
00120         }
00121         ctx->probelist[ctx->num_enabled_probes] = 0;
00122         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
00123 
00124         if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
00125                 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
00126                                 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
00127                 /* TODO: Error checks. */
00128         } else {
00129                 samplerate = 0; /* TODO: Error or set some value? */
00130         }
00131         ctx->samplerate = samplerate;
00132 
00133         return 0; /* TODO: SR_OK? */
00134 }
00135 
00136 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
00137                  uint64_t *length_out)
00138 {
00139         struct context *ctx;
00140         uint8_t *outbuf;
00141 
00142         if (!o) {
00143                 sr_warn("la8 out: %s: o was NULL", __func__);
00144                 return SR_ERR_ARG;
00145         }
00146 
00147         if (!(ctx = o->internal)) {
00148                 sr_warn("la8 out: %s: o->internal was NULL", __func__);
00149                 return SR_ERR_ARG;
00150         }
00151 
00152         if (!data_out) {
00153                 sr_warn("la8 out: %s: data_out was NULL", __func__);
00154                 return SR_ERR_ARG;
00155         }
00156 
00157         switch (event_type) {
00158         case SR_DF_TRIGGER:
00159                 sr_dbg("la8 out: %s: SR_DF_TRIGGER event", __func__);
00160                 /* Save the trigger point for later (SR_DF_END). */
00161                 ctx->trigger_point = 0; /* TODO: Store _actual_ value. */
00162                 break;
00163         case SR_DF_END:
00164                 sr_dbg("la8 out: %s: SR_DF_END event", __func__);
00165                 if (!(outbuf = g_try_malloc(4 + 1))) {
00166                         sr_warn("la8 out: %s: outbuf malloc failed", __func__);
00167                         return SR_ERR_MALLOC;
00168                 }
00169 
00170                 /* One byte for the 'divcount' value. */
00171                 outbuf[0] = samplerate_to_divcount(ctx->samplerate);
00172                 // if (outbuf[0] == 0xff) {
00173                 //      sr_warn("la8 out: %s: invalid divcount", __func__);
00174                 //      return SR_ERR;
00175                 // }
00176 
00177                 /* Four bytes (little endian) for the trigger point. */
00178                 outbuf[1] = (ctx->trigger_point >>  0) & 0xff;
00179                 outbuf[2] = (ctx->trigger_point >>  8) & 0xff;
00180                 outbuf[3] = (ctx->trigger_point >> 16) & 0xff;
00181                 outbuf[4] = (ctx->trigger_point >> 24) & 0xff;
00182 
00183                 *data_out = outbuf;
00184                 *length_out = 4 + 1;
00185                 g_free(o->internal);
00186                 o->internal = NULL;
00187                 break;
00188         default:
00189                 sr_warn("la8 out: %s: unsupported event type: %d", __func__,
00190                         event_type);
00191                 *data_out = NULL;
00192                 *length_out = 0;
00193                 break;
00194         }
00195 
00196         return SR_OK;
00197 }
00198 
00199 static int data(struct sr_output *o, const uint8_t *data_in,
00200                 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
00201 {
00202         struct context *ctx;
00203         uint8_t *outbuf;
00204 
00205         if (!o) {
00206                 sr_warn("la8 out: %s: o was NULL", __func__);
00207                 return SR_ERR_ARG;
00208         }
00209 
00210         if (!(ctx = o->internal)) {
00211                 sr_warn("la8 out: %s: o->internal was NULL", __func__);
00212                 return SR_ERR_ARG;
00213         }
00214 
00215         if (!data_in) {
00216                 sr_warn("la8 out: %s: data_in was NULL", __func__);
00217                 return SR_ERR_ARG;
00218         }
00219 
00220         if (!(outbuf = g_try_malloc0(length_in))) {
00221                 sr_warn("la8 out: %s: outbuf malloc failed", __func__);
00222                 return SR_ERR_MALLOC;
00223         }
00224 
00225         memcpy(outbuf, data_in, length_in);
00226 
00227         *data_out = outbuf;
00228         *length_out = length_in;
00229 
00230         return SR_OK;
00231 }
00232 
00233 SR_PRIV struct sr_output_format output_chronovu_la8 = {
00234         .id = "chronovu-la8",
00235         .description = "ChronoVu LA8",
00236         .df_type = SR_DF_LOGIC,
00237         .init = init,
00238         .data = data,
00239         .event = event,
00240 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines