diff options
Diffstat (limited to 'gr-fcd/lib/fcd')
-rw-r--r-- | gr-fcd/lib/fcd/Makefile.am | 40 | ||||
-rw-r--r-- | gr-fcd/lib/fcd/fcd.c | 904 | ||||
-rw-r--r-- | gr-fcd/lib/fcd/fcd.h | 87 | ||||
-rw-r--r-- | gr-fcd/lib/fcd/fcdhidcmd.h | 305 |
4 files changed, 1336 insertions, 0 deletions
diff --git a/gr-fcd/lib/fcd/Makefile.am b/gr-fcd/lib/fcd/Makefile.am new file mode 100644 index 000000000..0eff4785b --- /dev/null +++ b/gr-fcd/lib/fcd/Makefile.am @@ -0,0 +1,40 @@ +# +# Copyright 2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +include $(top_srcdir)/Makefile.common + +AM_CPPFLAGS = \ + $(STD_DEFINES_AND_INCLUDES) \ + $(WITH_INCLUDES) \ + $(USB_CPPFLAGS) \ + -I$(top_srcdir)/gr-fcd/lib/hid + +noinst_LTLIBRARIES = libfcd.la + +libfcd_la_SOURCES = fcd.c + +noinst_HEADERS = \ + fcd.h \ + fcdhidcmd.h + +libfcd_la_LIBADD = \ + $(top_builddir)/gr-fcd/lib/hid/libhid.la + diff --git a/gr-fcd/lib/fcd/fcd.c b/gr-fcd/lib/fcd/fcd.c new file mode 100644 index 000000000..7e5e82998 --- /dev/null +++ b/gr-fcd/lib/fcd/fcd.c @@ -0,0 +1,904 @@ +/*************************************************************************** + * This file is part of Qthid. + * + * Copyright (C) 2010 Howard Long, G6LVB + * CopyRight (C) 2011 Alexandru Csete, OZ9AEC + * Mario Lorenz, DL5MLO + * + * Qthid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Qthid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qthid. If not, see <http://www.gnu.org/licenses/>. + * + ***************************************************************************/ + +#define FCD +#include <string.h> +#ifdef WIN32 + #include <malloc.h> +#else + #include <stdlib.h> +#endif +#include "hidapi.h" +#include "fcd.h" +#include "fcdhidcmd.h" +#include <stdio.h> + + +#define FALSE 0 +#define TRUE 1 +typedef int BOOL; + + +const unsigned short _usVID=0x04D8; /*!< USB vendor ID. */ +const unsigned short _usPID=0xFB56; /*!< USB product ID. */ + + + +/** \brief Open FCD device. + * \return Pointer to the FCD HID device or NULL if none found + * + * This function looks for FCD devices connected to the computer and + * opens the first one found. + */ +static hid_device *fcdOpen(void) +{ + struct hid_device_info *phdi=NULL; + hid_device *phd=NULL; + char *pszPath=NULL; + + phdi=hid_enumerate(_usVID,_usPID); + if (phdi==NULL) + { + return NULL; // No FCD device found + } + + pszPath=strdup(phdi->path); + if (pszPath==NULL) + { + return NULL; + } + + hid_free_enumeration(phdi); + phdi=NULL; + + if ((phd=hid_open_path(pszPath)) == NULL) + { + free(pszPath); + pszPath=NULL; + + return NULL; + } + + free(pszPath); + pszPath=NULL; + + return phd; +} + + +/** \brief Close FCD HID device. */ +static void fcdClose(hid_device *phd) +{ + hid_close(phd); +} + + +/** \brief Get FCD mode. + * \return The current FCD mode. + * \sa FCD_MODE_ENUM + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetMode(void) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + FCD_MODE_ENUM fcd_mode = FCD_MODE_NONE; + + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + /* Send a BL Query Command */ + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_QUERY; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + fcdClose(phd); + phd = NULL; + + /* first check status bytes then check which mode */ + if (aucBufIn[0]==FCD_CMD_BL_QUERY && aucBufIn[1]==1) { + + /* In bootloader mode we have the string "FCDBL" starting at acBufIn[2] **/ + if (strncmp((char *)(aucBufIn+2), "FCDBL", 5) == 0) { + fcd_mode = FCD_MODE_BL; + } + /* In application mode we have "FCDAPP_18.06" where the number is the FW version */ + else if (strncmp((char *)(aucBufIn+2), "FCDAPP", 6) == 0) { + fcd_mode = FCD_MODE_APP; + } + /* either no FCD or firmware less than 18f */ + else { + fcd_mode = FCD_MODE_NONE; + } + } + + return fcd_mode; +} + + +/** \brief Get FCD firmware version as string. + * \param str The returned vesion number as a 0 terminated string (must be pre-allocated) + * \return The current FCD mode. + * \sa FCD_MODE_ENUM + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetFwVerStr(char *str) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + FCD_MODE_ENUM fcd_mode = FCD_MODE_NONE; + + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + /* Send a BL Query Command */ + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_QUERY; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + fcdClose(phd); + phd = NULL; + + /* first check status bytes then check which mode */ + if (aucBufIn[0]==FCD_CMD_BL_QUERY && aucBufIn[1]==1) { + + /* In bootloader mode we have the string "FCDBL" starting at acBufIn[2] **/ + if (strncmp((char *)(aucBufIn+2), "FCDBL", 5) == 0) { + fcd_mode = FCD_MODE_BL; + } + /* In application mode we have "FCDAPP_18.06" where the number is the FW version */ + else if (strncmp((char *)(aucBufIn+2), "FCDAPP", 6) == 0) { + strncpy(str, (char *)(aucBufIn+9), 5); + str[5] = 0; + fcd_mode = FCD_MODE_APP; + } + /* either no FCD or firmware less than 18f */ + else { + fcd_mode = FCD_MODE_NONE; + } + } + + return fcd_mode; +} + + +/** \brief Get hardware and firmware dependent FCD capabilities. + * \param fcd_caps Pointer to an FCD_CAPS_STRUCT + * \return The current FCD mode. + * + * This function queries the FCD and extracts the hardware and firmware dependent + * capabilities. Currently these capabilities are: + * - Bias T (available since S/N TBD) + * - Cellular block (the certified version of the FCD) + * When the FCD is in application mode, the string returned by the query command is + * (starting at index 2): + * FCDAPP 18.08 Brd 1.0 No blk + * 1.0 means no bias tee, 1.1 means there is a bias tee + * 'No blk' means it is not cellular blocked. + * + * Ref: http://uk.groups.yahoo.com/group/FCDevelopment/message/303 + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetCaps(FCD_CAPS_STRUCT *fcd_caps) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + FCD_MODE_ENUM fcd_mode = FCD_MODE_NONE; + + /* clear output buffer */ + fcd_caps->hasBiasT = 0; + fcd_caps->hasCellBlock = 0; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + /* Send a BL Query Command */ + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_QUERY; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + fcdClose(phd); + phd = NULL; + + /* first check status bytes then check which mode */ + if (aucBufIn[0]==FCD_CMD_BL_QUERY && aucBufIn[1]==1) { + + /* In bootloader mode we have the string "FCDBL" starting at acBufIn[2] **/ + if (strncmp((char *)(aucBufIn+2), "FCDBL", 5) == 0) { + fcd_mode = FCD_MODE_BL; + } + /* In application mode we have "FCDAPP 18.08 Brd 1.0 No blk" (see API doc) */ + else if (strncmp((char *)(aucBufIn+2), "FCDAPP", 6) == 0) { + + /* Bias T */ + fcd_caps->hasBiasT = (aucBufIn[21] == '1') ? 1 : 0; + + /* cellular block */ + if (strncmp((char *)(aucBufIn+23), "No blk", 6) == 0) { + fcd_caps->hasCellBlock = 0; + } else { + fcd_caps->hasCellBlock = 1; + } + + fcd_mode = FCD_MODE_APP; + } + /* either no FCD or firmware less than 18f */ + else { + fcd_mode = FCD_MODE_NONE; + } + } + + return fcd_mode; +} + + +/** \brief Get hardware and firmware dependent FCD capabilities as string. + * \param caps_str Pointer to a pre-allocated string buffer where the info will be copied. + * \return The current FCD mode. + * + * This function queries the FCD and copies the returned string into the caps_str parameter. + * THe return buffer must be at least 28 characters. + * When the FCD is in application mode, the string returned by the query command is + * (starting at index 2): + * FCDAPP 18.08 Brd 1.0 No blk + * 1.0 means no bias tee, 1.1 means there is a bias tee + * 'No blk' means it is not cellular blocked. + * + * Ref: http://uk.groups.yahoo.com/group/FCDevelopment/message/303 + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetCapsStr(char *caps_str) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + FCD_MODE_ENUM fcd_mode = FCD_MODE_NONE; + + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + /* Send a BL Query Command */ + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_QUERY; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + fcdClose(phd); + phd = NULL; + + /* first check status bytes then check which mode */ + if (aucBufIn[0]==FCD_CMD_BL_QUERY && aucBufIn[1]==1) { + + /* In bootloader mode we have the string "FCDBL" starting at acBufIn[2] **/ + if (strncmp((char *)(aucBufIn+2), "FCDBL", 5) == 0) { + fcd_mode = FCD_MODE_BL; + } + /* In application mode we have "FCDAPP 18.08 Brd 1.0 No blk" (see API doc) */ + else if (strncmp((char *)(aucBufIn+2), "FCDAPP", 6) == 0) { + + strncpy(caps_str, (char *)(aucBufIn+2), 27); + caps_str[27] = 0; + + fcd_mode = FCD_MODE_APP; + } + /* either no FCD or firmware less than 18f */ + else { + fcd_mode = FCD_MODE_NONE; + } + } + + return fcd_mode; +} + + + +/** \brief Reset FCD to bootloader mode. + * \return FCD_MODE_NONE + * + * This function is used to switch the FCD into bootloader mode in which + * various firmware operations can be performed. + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppReset(void) +{ + hid_device *phd=NULL; + //unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + // Send an App reset command + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_APP_RESET; + hid_write(phd, aucBufOut, 65); + + /** FIXME: hid_read() will occasionally hang due to a pthread_cond_wait() never returning. + It seems that the read_callback() in hid-libusb.c will never receive any + data during the reconfiguration. Since the same logic works in the native + windows application, it could be a libusb thing. Anyhow, since the value + returned by this function is not used, we may as well just skip the hid_read() + and return FME_NONE. + Correct switch from APP to BL mode can be observed in /var/log/messages (linux) + (when in bootloader mode the device version includes 'BL') + */ + /* + memset(aucBufIn,0xCC,65); // Clear out the response buffer + hid_read(phd,aucBufIn,65); + + if (aucBufIn[0]==FCDCMDAPPRESET && aucBufIn[1]==1) + { + FCDClose(phd); + phd=NULL; + return FME_APP; + } + FCDClose(phd); + phd=NULL; + return FME_BL; + */ + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_NONE; + +} + + +/** \brief Set FCD frequency with kHz resolution. + * \param nFreq The new frequency in kHz. + * \return The FCD mode. + * + * This function sets the frequency of the FCD with 1 kHz resolution. The parameter + * nFreq must already contain any necessary frequency correction. + * + * \sa fcdAppSetFreq + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetFreqkHz(int nFreq) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + // Send an App reset command + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_APP_SET_FREQ_KHZ; + aucBufOut[2] = (unsigned char)nFreq; + aucBufOut[3] = (unsigned char)(nFreq>>8); + aucBufOut[4] = (unsigned char)(nFreq>>16); + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]==FCD_CMD_APP_SET_FREQ_KHZ && aucBufIn[1]==1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; +} + + +/** \brief Set FCD frequency with Hz resolution. + * \param nFreq The new frequency in Hz. + * \return The FCD mode. + * + * This function sets the frequency of the FCD with 1 Hz resolution. The parameter + * nFreq must already contain any necessary frequency correction. + * + * \sa fcdAppSetFreq + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetFreq(int nFreq) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + // Send an App reset command + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_APP_SET_FREQ_HZ; + aucBufOut[2] = (unsigned char)nFreq; + aucBufOut[3] = (unsigned char)(nFreq>>8); + aucBufOut[4] = (unsigned char)(nFreq>>16); + aucBufOut[5] = (unsigned char)(nFreq>>24); + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]==FCD_CMD_APP_SET_FREQ_HZ && aucBufIn[1]==1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; +} + + + +/** \brief Reset FCD to application mode. + * \return FCD_MODE_NONE + * + * This function is used to switch the FCD from bootloader mode + * into application mode. + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlReset(void) +{ + hid_device *phd=NULL; +// unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + // Send an BL reset command + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_RESET; + hid_write(phd, aucBufOut, 65); + + /** FIXME: hid_read() will hang due to a pthread_cond_wait() never returning. + It seems that the read_callback() in hid-libusb.c will never receive any + data during the reconfiguration. Since the same logic works in the native + windows application, it could be a libusb thing. Anyhow, since the value + returned by this function is not used, we may as well jsut skip the hid_read() + and return FME_NONE. + Correct switch from BL to APP mode can be observed in /var/log/messages (linux) + (when in bootloader mode the device version includes 'BL') + */ + /* + memset(aucBufIn,0xCC,65); // Clear out the response buffer + hid_read(phd,aucBufIn,65); + + if (aucBufIn[0]==FCDCMDBLRESET && aucBufIn[1]==1) + { + FCDClose(phd); + phd=NULL; + return FME_BL; + } + FCDClose(phd); + phd=NULL; + return FME_APP; + */ + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_NONE; + +} + + +/** \brief Erase firmware from FCD. + * \return The FCD mode + * + * This function deletes the firmware from the FCD. This is required + * before writing new firmware into the FCD. + * + * \sa fcdBlWriteFirmware + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlErase(void) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + // Send an App reset command + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_ERASE; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]==FCD_CMD_BL_ERASE && aucBufIn[1]==1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; + } + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; +} + + +/** \brief Write new firmware into the FCD. + * \param pc Pointer to the new firmware data + * \param n64size The number of bytes in the data + * \return The FCD mode + * + * This function is used to upload new firmware into the FCD flash. + * + * \sa fcdBlErase + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlWriteFirmware(char *pc, int64_t n64Size) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + uint32_t u32AddrStart; + uint32_t u32AddrEnd; + uint32_t u32Addr; + BOOL bFinished=FALSE; + + phd = fcdOpen(); + + if (phd==NULL) + { + return FCD_MODE_NONE; + } + + // Get the valid flash address range + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_GET_BYTE_ADDR_RANGE; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_GET_BYTE_ADDR_RANGE || aucBufIn[1]!=1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + u32AddrStart= + aucBufIn[2]+ + (((uint32_t)aucBufIn[3])<<8)+ + (((uint32_t)aucBufIn[4])<<16)+ + (((uint32_t)aucBufIn[5])<<24); + u32AddrEnd= + aucBufIn[6]+ + (((uint32_t)aucBufIn[7])<<8)+ + (((uint32_t)aucBufIn[8])<<16)+ + (((uint32_t)aucBufIn[9])<<24); + + // Set start address for flash + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_SET_BYTE_ADDR; + aucBufOut[2] = ((unsigned char)u32AddrStart); + aucBufOut[3] = ((unsigned char)(u32AddrStart>>8)); + aucBufOut[4] = ((unsigned char)(u32AddrStart>>16)); + aucBufOut[5] = ((unsigned char)(u32AddrStart>>24)); + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_SET_BYTE_ADDR || aucBufIn[1]!=1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + // Write blocks + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_WRITE_FLASH_BLOCK; + for (u32Addr=u32AddrStart; u32Addr+47<u32AddrEnd && u32Addr+47<n64Size && !bFinished; u32Addr+=48) + { + memcpy(&aucBufOut[3], &pc[u32Addr], 48); + + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_WRITE_FLASH_BLOCK || aucBufIn[1]!=1) + { + bFinished = TRUE; + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + } + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; +} + + +/** \brief Verify firmware in FCd flash. + * \param pc Pointer to firmware data to verify against. + * \param n64Size Size of the data in pc. + * \return The FCD_MODE_BL if verification was succesful. + * + * This function verifies the firmware currently in the FCd flash against the firmware + * image pointed to by pc. The function return FCD_MODE_BL if the verification is OK and + * FCD_MODE_APP otherwise. + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlVerifyFirmware(char *pc, int64_t n64Size) +{ + hid_device *phd=NULL; + unsigned char aucBufIn[65]; + unsigned char aucBufOut[65]; + uint32_t u32AddrStart; + uint32_t u32AddrEnd; + uint32_t u32Addr; + BOOL bFinished=FALSE; + + phd = fcdOpen(); + + if (phd==NULL) + { + return FCD_MODE_NONE; + } + + // Get the valid flash address range + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_GET_BYTE_ADDR_RANGE; + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_GET_BYTE_ADDR_RANGE || aucBufIn[1]!=1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + u32AddrStart= + aucBufIn[2]+ + (((uint32_t)aucBufIn[3])<<8)+ + (((uint32_t)aucBufIn[4])<<16)+ + (((uint32_t)aucBufIn[5])<<24); + + u32AddrEnd= + aucBufIn[6]+ + (((uint32_t)aucBufIn[7])<<8)+ + (((uint32_t)aucBufIn[8])<<16)+ + (((uint32_t)aucBufIn[9])<<24); + + // Set start address for flash + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_SET_BYTE_ADDR; + aucBufOut[2] = ((unsigned char)u32AddrStart); + aucBufOut[3] = ((unsigned char)(u32AddrStart>>8)); + aucBufOut[4] = ((unsigned char)(u32AddrStart>>16)); + aucBufOut[5] = ((unsigned char)(u32AddrStart>>24)); + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_SET_BYTE_ADDR || aucBufIn[1]!=1) + { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + // Read blocks + aucBufOut[0] = 0; // Report ID, ignored + aucBufOut[1] = FCD_CMD_BL_READ_FLASH_BLOCK; + for (u32Addr=u32AddrStart; u32Addr+47<u32AddrEnd && u32Addr+47<n64Size && !bFinished; u32Addr+=48) + { + hid_write(phd, aucBufOut, 65); + memset(aucBufIn, 0xCC, 65); // Clear out the response buffer + hid_read(phd, aucBufIn, 65); + + if (aucBufIn[0]!=FCD_CMD_BL_READ_FLASH_BLOCK || aucBufIn[1]!=1) + { + bFinished = TRUE; + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + if (memcmp(&aucBufIn[2],&pc[u32Addr],48)!=0) + { + bFinished = TRUE; + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + } + + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; +} + + + +/** \brief Write FCD parameter (e.g. gain or filter) + * \param u8Cmd The command byte / parameter ID, see FCD_CMD_APP_SET_* + * \param pu8Data The parameter value to be written + * \param u8len Length of pu8Data in bytes + * \return One of FCD_MODE_NONE, FCD_MODE_APP or FCD_MODE_BL (see description) + * + * This function can be used to set the value of a parameter in the FCD for which there is no + * high level API call. It gives access to the low level API of the FCD and the caller is expected + * to be aware of the various FCD commands, since they are required to be supplied as parameter + * to this function. + * + * The return value can be used to determine the success or failure of the command execution: + * - FCD_MODE_APP : Reply from FCD was as expected (nominal case). + * - FCD_MODE_BL : Reply from FCD was not as expected. + * - FCD_MODE_NONE : No FCD was found + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetParam(uint8_t u8Cmd, uint8_t *pu8Data, uint8_t u8len) +{ + hid_device *phd=NULL; + unsigned char aucBufOut[65]; + unsigned char aucBufIn[65]; + + + phd = fcdOpen(); + + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + aucBufOut[0]=0; // Report ID, ignored + aucBufOut[1]=u8Cmd; + memcpy(aucBufOut+2, pu8Data,u8len); + hid_write(phd,aucBufOut,65); + + /* we must read after each write in order to empty FCD/HID buffer */ + memset(aucBufIn,0xCC,65); // Clear out the response buffer + hid_read(phd,aucBufIn,65); + + /* Check the response, if OK return FCD_MODE_APP */ + if (aucBufIn[0]==u8Cmd && aucBufIn[1]==1) { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + /* Response did not contain the expected bytes */ + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; + +} + + +/** \brief Read FCD parameter (e.g. gain or filter) + * \param u8Cmd The command byte / parameter ID, see FCD_CMD_APP_GET_* + * \param pu8Data TPointer to buffer where the parameter value(s) will be written + * \param u8len Length of pu8Data in bytes + * \return One of FCD_MODE_NONE, FCD_MODE_APP or FCD_MODE_BL (see description) + * + * This function can be used to read the value of a parameter in the FCD for which there is no + * high level API call. It gives access to the low level API of the FCD and the caller is expected + * to be aware of the various FCD commands, since they are required to be supplied as parameter + * to this function. + * + * The return value can be used to determine the success or failure of the command execution: + * - FCD_MODE_APP : Reply from FCD was as expected (nominal case). + * - FCD_MODE_BL : Reply from FCD was not as expected. + * - FCD_MODE_NONE : No FCD was found + */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppGetParam(uint8_t u8Cmd, uint8_t *pu8Data, uint8_t u8len) +{ + hid_device *phd=NULL; + unsigned char aucBufOut[65]; + unsigned char aucBufIn[65]; + + phd = fcdOpen(); + if (phd == NULL) + { + return FCD_MODE_NONE; + } + + aucBufOut[0]=0; // Report ID, ignored + aucBufOut[1]=u8Cmd; + hid_write(phd,aucBufOut,65); + + memset(aucBufIn,0xCC,65); // Clear out the response buffer + hid_read(phd,aucBufIn,65); + /* Copy return data to output buffer (even if cmd exec failed) */ + memcpy(pu8Data,aucBufIn+2,u8len); + + /* Check status bytes in returned data */ + if (aucBufIn[0]==u8Cmd && aucBufIn[1]==1) { + fcdClose(phd); + phd = NULL; + + return FCD_MODE_APP; + } + + /* Response did not contain the expected bytes */ + fcdClose(phd); + phd = NULL; + + return FCD_MODE_BL; +} + diff --git a/gr-fcd/lib/fcd/fcd.h b/gr-fcd/lib/fcd/fcd.h new file mode 100644 index 000000000..50983b473 --- /dev/null +++ b/gr-fcd/lib/fcd/fcd.h @@ -0,0 +1,87 @@ +/*************************************************************************** + * This file is part of Qthid. + * + * Copyright (C) 2010 Howard Long, G6LVB + * CopyRight (C) 2011 Alexandru Csete, OZ9AEC + * Mario Lorenz, DL5MLO + * + * Qthid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Qthid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qthid. If not, see <http://www.gnu.org/licenses/>. + * + ***************************************************************************/ + +#ifndef FCD_H +#define FCD_H 1 + + +#ifdef FCD +#define EXTERN +#define ASSIGN (x) =x +#else +#define EXTERN extern +#define ASSIGN(x) +#endif + +#ifdef _WIN32 +#define FCD_API_EXPORT __declspec(dllexport) +#define FCD_API_CALL _stdcall +#else +#define FCD_API_EXPORT +#define FCD_API_CALL +#endif + +#include <inttypes.h> + + +/** \brief FCD mode enumeration. */ +typedef enum { + FCD_MODE_NONE, /*!< No FCD detected. */ + FCD_MODE_BL, /*!< FCD present in bootloader mode. */ + FCD_MODE_APP /*!< FCD present in aplpication mode. */ +} FCD_MODE_ENUM; // The current mode of the FCD: none inserted, in bootloader mode or in normal application mode + +/** \brief FCD capabilities that depend on both hardware and firmware. */ +typedef struct { + unsigned char hasBiasT; /*!< Whether FCD has hardware bias tee (1=yes, 0=no) */ + unsigned char hasCellBlock; /*!< Whether FCD has cellular blocking. */ +} FCD_CAPS_STRUCT; + +#ifdef __cplusplus +extern "C" { +#endif + +/* Application functions */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetMode(void); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetFwVerStr(char *str); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetCaps(FCD_CAPS_STRUCT *fcd_caps); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdGetCapsStr(char *caps_str); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppReset(void); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetFreqkHz(int nFreq); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetFreq(int nFreq); + +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppSetParam(uint8_t u8Cmd, uint8_t *pu8Data, uint8_t u8len); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdAppGetParam(uint8_t u8Cmd, uint8_t *pu8Data, uint8_t u8len); + + +/* Bootloader functions */ +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlReset(void); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlErase(void); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlWriteFirmware(char *pc, int64_t n64Size); +EXTERN FCD_API_EXPORT FCD_API_CALL FCD_MODE_ENUM fcdBlVerifyFirmware(char *pc, int64_t n64Size); + + +#ifdef __cplusplus +} +#endif + +#endif // FCD_H diff --git a/gr-fcd/lib/fcd/fcdhidcmd.h b/gr-fcd/lib/fcd/fcdhidcmd.h new file mode 100644 index 000000000..fad55f15f --- /dev/null +++ b/gr-fcd/lib/fcd/fcdhidcmd.h @@ -0,0 +1,305 @@ +/*************************************************************************** + * This file is part of Qthid. + * + * Copyright (C) 2010 Howard Long, G6LVB + * CopyRight (C) 2011 Alexandru Csete, OZ9AEC + * Mario Lorenz, DL5MLO + * + * Qthid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Qthid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Qthid. If not, see <http://www.gnu.org/licenses/>. + * + ***************************************************************************/ +#ifndef FCDHIDCMD_H +#define FCD_HID_CMD_H 1 + + +/* Commands applicable in bootloader mode */ +#define FCD_CMD_BL_QUERY 1 /*!< Returns string with "FCDAPP version". */ +#define FCD_CMD_BL_RESET 8 /*!< Reset to application mode. */ +#define FCD_CMD_BL_ERASE 24 /*!< Erase firmware from FCD flash. */ +#define FCD_CMD_BL_SET_BYTE_ADDR 25 /*!< TBD */ +#define FCD_CMD_BL_GET_BYTE_ADDR_RANGE 26 /*!< Get address range. */ +#define FCD_CMD_BL_WRITE_FLASH_BLOCK 27 /*!< Write flash block. */ +#define FCD_CMD_BL_READ_FLASH_BLOCK 28 /*!< Read flash block. */ + +/* Commands applicable in application mode */ +#define FCD_CMD_APP_SET_FREQ_KHZ 100 /*!< Send with 3 byte unsigned little endian frequency in kHz. */ +#define FCD_CMD_APP_SET_FREQ_HZ 101 /*!< Send with 4 byte unsigned little endian frequency in Hz, returns with actual frequency set in Hz */ +#define FCD_CMD_APP_GET_FREQ_HZ 102 /*!< Returns 4 byte unsigned little endian frequency in Hz. */ + +#define FCD_CMD_APP_GET_IF_RSSI 104 /*!< Supposed to return 1 byte unsigned IF RSSI (-35dBm=0, -10dBm=70) but it is not functional. */ +#define FCD_CMD_APP_GET_PLL_LOCK 105 /*!< Returns 1 bit, true if locked. */ + +#define FCD_CMD_APP_SET_DC_CORR 106 /*!< Send with 2 byte unsigned I DC correction followed by 2 byte unsigned Q DC correction. 32768 is the default centre value. */ +#define FCD_CMD_APP_GET_DC_CORR 107 /*!< Returns 2 byte unsigned I DC correction followed by 2 byte unsigned Q DC correction. 32768 is the default centre value. */ +#define FCD_CMD_APP_SET_IQ_CORR 108 /*!< Send with 2 byte signed phase correction followed by 2 byte unsigned gain correction. 0 is the default centre value for phase correction, 32768 is the default centre value for gain. */ +#define FCD_CMD_APP_GET_IQ_CORR 109 /*!< Returns 2 byte signed phase correction followed by 2 byte unsigned gain correction. 0 is the default centre value for phase correction, 32768 is the default centre value for gain. */ + +#define FCD_CMD_APP_SET_LNA_GAIN 110 /*!< Send a 1 byte value, see enums for reference. */ +#define FCD_CMD_APP_SET_LNA_ENHANCE 111 +#define FCD_CMD_APP_SET_BAND 112 +#define FCD_CMD_APP_SET_RF_FILTER 113 +#define FCD_CMD_APP_SET_MIXER_GAIN 114 +#define FCD_CMD_APP_SET_BIAS_CURRENT 115 +#define FCD_CMD_APP_SET_MIXER_FILTER 116 +#define FCD_CMD_APP_SET_IF_GAIN1 117 +#define FCD_CMD_APP_SET_IF_GAIN_MODE 118 +#define FCD_CMD_APP_SET_IF_RC_FILTER 119 +#define FCD_CMD_APP_SET_IF_GAIN2 120 +#define FCD_CMD_APP_SET_IF_GAIN3 121 +#define FCD_CMD_APP_SET_IF_FILTER 122 +#define FCD_CMD_APP_SET_IF_GAIN4 123 +#define FCD_CMD_APP_SET_IF_GAIN5 124 +#define FCD_CMD_APP_SET_IF_GAIN6 125 +#define FCD_CMD_APP_SET_BIAS_TEE 126 /*!< Bias T for ext LNA. Send with one byte: 1=ON, 0=OFF. */ + +#define FCD_CMD_APP_GET_LNA_GAIN 150 // Retrieve a 1 byte value, see enums for reference +#define FCD_CMD_APP_GET_LNA_ENHANCE 151 +#define FCD_CMD_APP_GET_BAND 152 +#define FCD_CMD_APP_GET_RF_FILTER 153 +#define FCD_CMD_APP_GET_MIXER_GAIN 154 +#define FCD_CMD_APP_GET_BIAS_CURRENT 155 +#define FCD_CMD_APP_GET_MIXER_FILTER 156 +#define FCD_CMD_APP_GET_IF_GAIN1 157 +#define FCD_CMD_APP_GET_IF_GAIN_MODE 158 +#define FCD_CMD_APP_GET_IF_RC_FILTER 159 +#define FCD_CMD_APP_GET_IF_GAIN2 160 +#define FCD_CMD_APP_GET_IF_GAIN3 161 +#define FCD_CMD_APP_GET_IF_FILTER 162 +#define FCD_CMD_APP_GET_IF_GAIN4 163 +#define FCD_CMD_APP_GET_IF_GAIN5 164 +#define FCD_CMD_APP_GET_IF_GAIN6 165 +#define FCD_CMD_APP_GET_BIAS_TEE 166 /*!< Bias T. 1=ON, 0=OFF. */ + +#define FCD_CMD_APP_SEND_I2C_BYTE 200 +#define FCD_CMD_APP_RECV_I2C_BYTE 201 + +#define FCD_CMD_APP_RESET 255 // Reset to bootloader + + +typedef enum +{ + TLGE_N5_0DB=0, + TLGE_N2_5DB=1, + TLGE_P0_0DB=4, + TLGE_P2_5DB=5, + TLGE_P5_0DB=6, + TLGE_P7_5DB=7, + TLGE_P10_0DB=8, + TLGE_P12_5DB=9, + TLGE_P15_0DB=10, + TLGE_P17_5DB=11, + TLGE_P20_0DB=12, + TLGE_P25_0DB=13, + TLGE_P30_0DB=14 +} TUNER_LNA_GAIN_ENUM; + +typedef enum +{ + TLEE_OFF=0, + TLEE_0=1, + TLEE_1=3, + TLEE_2=5, + TLEE_3=7 +} TUNER_LNA_ENHANCE_ENUM; + +typedef enum +{ + TBE_VHF2, + TBE_VHF3, + TBE_UHF, + TBE_LBAND +} TUNER_BAND_ENUM; + +typedef enum +{ + // Band 0, VHF II + TRFE_LPF268MHZ=0, + TRFE_LPF299MHZ=8, + // Band 1, VHF III + TRFE_LPF509MHZ=0, + TRFE_LPF656MHZ=8, + // Band 2, UHF + TRFE_BPF360MHZ=0, + TRFE_BPF380MHZ=1, + TRFE_BPF405MHZ=2, + TRFE_BPF425MHZ=3, + TRFE_BPF450MHZ=4, + TRFE_BPF475MHZ=5, + TRFE_BPF505MHZ=6, + TRFE_BPF540MHZ=7, + TRFE_BPF575MHZ=8, + TRFE_BPF615MHZ=9, + TRFE_BPF670MHZ=10, + TRFE_BPF720MHZ=11, + TRFE_BPF760MHZ=12, + TRFE_BPF840MHZ=13, + TRFE_BPF890MHZ=14, + TRFE_BPF970MHZ=15, + // Band 2, L band + TRFE_BPF1300MHZ=0, + TRFE_BPF1320MHZ=1, + TRFE_BPF1360MHZ=2, + TRFE_BPF1410MHZ=3, + TRFE_BPF1445MHZ=4, + TRFE_BPF1460MHZ=5, + TRFE_BPF1490MHZ=6, + TRFE_BPF1530MHZ=7, + TRFE_BPF1560MHZ=8, + TRFE_BPF1590MHZ=9, + TRFE_BPF1640MHZ=10, + TRFE_BPF1660MHZ=11, + TRFE_BPF1680MHZ=12, + TRFE_BPF1700MHZ=13, + TRFE_BPF1720MHZ=14, + TRFE_BPF1750MHZ=15 +} TUNER_RF_FILTER_ENUM; + +typedef enum +{ + TMGE_P4_0DB=0, + TMGE_P12_0DB=1 +} TUNER_MIXER_GAIN_ENUM; + +typedef enum +{ + TBCE_LBAND=0, + TBCE_1=1, + TBCE_2=2, + TBCE_VUBAND=3 +} TUNER_BIAS_CURRENT_ENUM; + +typedef enum +{ + TMFE_27_0MHZ=0, + TMFE_4_6MHZ=8, + TMFE_4_2MHZ=9, + TMFE_3_8MHZ=10, + TMFE_3_4MHZ=11, + TMFE_3_0MHZ=12, + TMFE_2_7MHZ=13, + TMFE_2_3MHZ=14, + TMFE_1_9MHZ=15 +} TUNER_MIXER_FILTER_ENUM; + +typedef enum +{ + TIG1E_N3_0DB=0, + TIG1E_P6_0DB=1 +} TUNER_IF_GAIN1_ENUM; + +typedef enum +{ + TIGME_LINEARITY=0, + TIGME_SENSITIVITY=1 +} TUNER_IF_GAIN_MODE_ENUM; + +typedef enum +{ + TIRFE_21_4MHZ=0, + TIRFE_21_0MHZ=1, + TIRFE_17_6MHZ=2, + TIRFE_14_7MHZ=3, + TIRFE_12_4MHZ=4, + TIRFE_10_6MHZ=5, + TIRFE_9_0MHZ=6, + TIRFE_7_7MHZ=7, + TIRFE_6_4MHZ=8, + TIRFE_5_3MHZ=9, + TIRFE_4_4MHZ=10, + TIRFE_3_4MHZ=11, + TIRFE_2_6MHZ=12, + TIRFE_1_8MHZ=13, + TIRFE_1_2MHZ=14, + TIRFE_1_0MHZ=15 +} TUNER_IF_RC_FILTER_ENUM; + +typedef enum +{ + TIG2E_P0_0DB=0, + TIG2E_P3_0DB=1, + TIG2E_P6_0DB=2, + TIG2E_P9_0DB=3 +} TUNER_IF_GAIN2_ENUM; + +typedef enum +{ + TIG3E_P0_0DB=0, + TIG3E_P3_0DB=1, + TIG3E_P6_0DB=2, + TIG3E_P9_0DB=3 +} TUNER_IF_GAIN3_ENUM; + +typedef enum +{ + TIG4E_P0_0DB=0, + TIG4E_P1_0DB=1, + TIG4E_P2_0DB=2 +} TUNER_IF_GAIN4_ENUM; + +typedef enum +{ + TIFE_5_50MHZ=0, + TIFE_5_30MHZ=1, + TIFE_5_00MHZ=2, + TIFE_4_80MHZ=3, + TIFE_4_60MHZ=4, + TIFE_4_40MHZ=5, + TIFE_4_30MHZ=6, + TIFE_4_10MHZ=7, + TIFE_3_90MHZ=8, + TIFE_3_80MHZ=9, + TIFE_3_70MHZ=10, + TIFE_3_60MHZ=11, + TIFE_3_40MHZ=12, + TIFE_3_30MHZ=13, + TIFE_3_20MHZ=14, + TIFE_3_10MHZ=15, + TIFE_3_00MHZ=16, + TIFE_2_95MHZ=17, + TIFE_2_90MHZ=18, + TIFE_2_80MHZ=19, + TIFE_2_75MHZ=20, + TIFE_2_70MHZ=21, + TIFE_2_60MHZ=22, + TIFE_2_55MHZ=23, + TIFE_2_50MHZ=24, + TIFE_2_45MHZ=25, + TIFE_2_40MHZ=26, + TIFE_2_30MHZ=27, + TIFE_2_28MHZ=28, + TIFE_2_24MHZ=29, + TIFE_2_20MHZ=30, + TIFE_2_15MHZ=31 +} TUNER_IF_FILTER_ENUM; + +typedef enum +{ + TIG5E_P3_0DB=0, + TIG5E_P6_0DB=1, + TIG5E_P9_0DB=2, + TIG5E_P12_0DB=3, + TIG5E_P15_0DB=4 +} TUNER_IF_GAIN5_ENUM; + +typedef enum +{ + TIG6E_P3_0DB=0, + TIG6E_P6_0DB=1, + TIG6E_P9_0DB=2, + TIG6E_P12_0DB=3, + TIG6E_P15_0DB=4 +} TUNER_IF_GAIN6_ENUM; + + +#endif // FCDHIDCMD_H |