libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
command.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 3 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, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libusb.h>
00021 
00022 #include "fx2lafw.h"
00023 #include "command.h"
00024 #include "sigrok.h"
00025 #include "sigrok-internal.h"
00026 
00027 SR_PRIV int command_get_fw_version(libusb_device_handle *devhdl,
00028                                    struct version_info *vi)
00029 {
00030         int ret;
00031 
00032         ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
00033                 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
00034                 (unsigned char *)vi, sizeof(struct version_info), 100);
00035 
00036         if (ret < 0) {
00037                 sr_err("fx2lafw: Unable to get version info: %d.", ret);
00038                 return SR_ERR;
00039         }
00040 
00041         return SR_OK;
00042 }
00043 
00044 SR_PRIV int command_get_revid_version(libusb_device_handle *devhdl,
00045                                       uint8_t *revid)
00046 {
00047         int ret;
00048 
00049         ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
00050                 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
00051                 revid, 1, 100);
00052 
00053         if (ret < 0) {
00054                 sr_err("fx2lafw: Unable to get REVID: %d.", ret);
00055                 return SR_ERR;
00056         }
00057 
00058         return SR_OK;
00059 }
00060 
00061 SR_PRIV int command_start_acquisition(libusb_device_handle *devhdl,
00062                                       uint64_t samplerate)
00063 {
00064         struct cmd_start_acquisition cmd;
00065         int delay = 0, ret;
00066 
00067         /* Compute the sample rate. */
00068         if ((SR_MHZ(48) % samplerate) == 0) {
00069                 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
00070                 delay = SR_MHZ(48) / samplerate - 1;
00071                 if (delay > MAX_SAMPLE_DELAY)
00072                         delay = 0;
00073         }
00074 
00075         if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
00076                 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
00077                 delay = SR_MHZ(30) / samplerate - 1;
00078         }
00079 
00080         sr_info("fx2lafw: GPIF delay = %d, clocksource = %sMHz", delay,
00081                 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
00082 
00083         if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
00084                 sr_err("fx2lafw: Unable to sample at %" PRIu64 "Hz.",
00085                        samplerate);
00086                 return SR_ERR;
00087         }
00088 
00089         cmd.sample_delay_h = (delay >> 8) & 0xff;
00090         cmd.sample_delay_l = delay & 0xff;
00091 
00092         /* Send the control message. */
00093         ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
00094                         LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
00095                         (unsigned char *)&cmd, sizeof(cmd), 100);
00096         if (ret < 0) {
00097                 sr_err("fx2lafw: Unable to send start command: %d.", ret);
00098                 return SR_ERR;
00099         }
00100 
00101         return SR_OK;
00102 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines