Launching soon!

This commit is contained in:
[ Kristjan Komloši HomePC ]
2017-03-28 13:33:10 +02:00
parent db1ecbddbe
commit 2cdf7fb91c
276 changed files with 19 additions and 113542 deletions
+156
View File
@@ -0,0 +1,156 @@
set(VERSION_INFO_MAJOR 1)
set(VERSION_INFO_MINOR 0)
set(VERSION_INFO_PATCH 0)
if (NOT CMAKE_INSTALL_LIBDIR)
include(GNUInstallDirs)
endif (NOT CMAKE_INSTALL_LIBDIR)
# Fall back to just "lib" if the item provided by GNUInstallDirs doesn't exist
# For example, on Ubuntu 13.10 with CMake 2.8.11.2,
# /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} doesn't exist.
if (NOT EXISTS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
message(STATUS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} does not exist. Defaulting libcyusbserial install location to ${CMAKE_INSTALL_PREFIX}/lib.")
set(CMAKE_INSTALL_LIBDIR lib)
endif()
################################################################################
# Include paths
################################################################################
set(LIBCYUSBSERIAL_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/../include
)
################################################################################
# Build dependencies
################################################################################
if(MSVC)
set(LIBCYUSBSERIAL_INCLUDES ${LIBCYUSBSERIAL_INCLUDES} ${MSVC_C99_INCLUDES})
find_package(LibPThreadsWin32 REQUIRED)
if(LIBPTHREADSWIN32_FOUND)
set(HAVE_THREADS true)
set(LIBCYUSBSERIAL_INCLUDES
${LIBCYUSBSERIAL_INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS})
set(LIBCYUSBSERIAL_LIBS
${LIBCYUSBSERIAL_LIBS} ${LIBPTHREADSWIN32_LIBRARIES})
add_definitions(-DHAVE_STRUCT_TIMESPEC)
else()
set(HAVE_THREADS false)
endif()
else(MSVC)
find_package(Threads REQUIRED)
if(Threads_FOUND)
set(HAVE_THREADS true)
else()
set(HAVE_THREADS false)
endif()
endif(MSVC)
if (NOT HAVE_THREADS)
message(FATAL_ERROR "pthreads not found. This is required to build libcyusbserial.")
endif()
if(NOT LIBUSB_FOUND)
message(FATAL_ERROR "libusb-1.0 not found. This is required to use the libcyusbserial libusb backend. For binary releases, try setting LIBUSB_PATH.")
else(NOT LIBUSB_FOUND)
if(LIBUSB_VERSION)
if(NOT LIBUSB_VERSION VERSION_LESS "1.0.10")
add_definitions(-DHAVE_LIBUSB_GET_VERSION)
endif()
if(WIN32)
# We require v1.0.19 because it provides Windows 8 USB 3.0
# speed detection fixes, additional AMD/Intel USB 3.0 root
# hub support, and some fixes to issues reported on v1.0.18
# that yielded corrupted samples.
if(${LIBUSB_VERSION} VERSION_LESS "1.0.19")
message(FATAL_ERROR "libusb v1.0.19 is required in Windows.\n"
"Please update libusb or consider using the Cypress backend if this is not possible.\n"
"Detected version: ${LIBUSB_VERSION}\n")
endif()
elseif(APPLE)
# A number of important changes were included in libusb
# v1.0.16 hrough v1.0.18, including SuperSpeed support, 64-bit support,
# and various build and crash fixes.
if(${LIBUSB_VERSION} VERSION_LESS "1.0.18")
message(FATAL_ERROR "libusb v1.0.18 is required in OS X. Please update libusb."
"Detected version: ${LIBUSB_VERSION}\n")
endif()
elseif(UNIX)
# A number of reported issues supposedly became resolved after
# updating to >= 1.0.16.
if(${LIBUSB_VERSION} VERSION_LESS "1.0.16")
message(WARNING "\nlibusb >= 1.0.16 is HIGHLY recommended. "
"If you experience issues or poor performance, please try updating libusb.\n"
"Detected version: ${LIBUSB_VERSION}")
endif()
else()
message(WARNING "Unexpected system type. Please report this warning to developers.")
endif()
else()
message(WARNING "Not checking libcyusbserial/libusb compatibility because LIBUSB_VERSION is not defined.")
endif()
set(LIBCYUSBSERIAL_INCLUDES ${LIBCYUSBSERIAL_INCLUDES} ${LIBUSB_INCLUDE_DIRS})
endif(NOT LIBUSB_FOUND)
if(MSVC)
set(LIBCYUSBSERIAL_INCLUDES ${LIBCYUSBSERIAL_INCLUDES}
${LIBPTHREADSWIN32_INCLUDE_DIRS})
endif()
include_directories(${LIBCYUSBSERIAL_INCLUDES})
################################################################################
# Configure source files
################################################################################
set(LIBCYUSBSERIAL_SOURCE
cyusb.c
cyuart.c
cyi2c.c
cyspi.c
cyphdc.c
cyjtag.c
cymisc.c
)
if(MSVC)
set(LIBCYUSBSERIAL_SOURCE ${LIBCYUSBSERIAL_SOURCE}
${PROJECT_SOURCE_DIR}/windows/gettimeofday.c
${PROJECT_SOURCE_DIR}/windows/usleep.c
)
endif(MSVC)
add_library(cyusbserial SHARED ${LIBCYUSBSERIAL_SOURCE})
################################################################################
# Build configuration
################################################################################
if(MSVC)
add_definitions(-DCYUSBSERIAL_EXPORTS)
set(LIBCYUSBSERIAL_LIBS ${LIBCYUSBSERIAL_LIBS} ${LIBPTHREADSWIN32_LIBRARIES})
else()
set(LIBCYUSBSERIAL_LIBS ${LIBCYUSBSERIAL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
endif(MSVC)
set(LIBCYUSBSERIAL_LIBS ${LIBCYUSBSERIAL_LIBS} ${LIBUSB_LIBRARIES})
target_link_libraries(cyusbserial ${LIBCYUSBSERIAL_LIBS})
# Set shared library version
set_target_properties(cyusbserial PROPERTIES SOVERSION ${VERSION_INFO_MAJOR})
################################################################################
# Library installation information
################################################################################
install(TARGETS cyusbserial
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so/.dylib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # .a/.lib
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} # .dll
)
+255
View File
@@ -0,0 +1,255 @@
/*
* Common header file of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** \file CyUSBCommon.h
* \brief Common header file of Cypress USB Serial
*/
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <pthread.h>
#include <libusb.h>
#include "CyUSBSerial.h"
typedef struct CY_DEVICE {
#pragma pack(1)
unsigned char inEndpoint;
unsigned char outEndpoint;
unsigned char interruptEndpoint;
unsigned char interfaceNum;
bool i2cCancelEvent;
bool spiCancelEvent;
bool uartCancelEvent;
bool rtsValue;
bool dtrValue;
unsigned short numEndpoints;
CY_FLOW_CONTROL_MODES uartFlowControlMode;
struct libusb_transfer *spiTransfer;
struct libusb_transfer *uartTransfer;
libusb_device_handle *devHandle;
pthread_t spiThread;
bool spiThreadRunning;
pthread_t uartThread;
bool uartThreadRunning;
#pragma pack()
pthread_mutex_t readLock;
pthread_mutex_t writeLock;
pthread_mutex_t notificationLock;
#pragma pack(1)
CY_DEVICE_TYPE deviceType;
#pragma pack()
}CY_DEVICE,*PCY_DEVICE;
CY_RETURN_STATUS CyResetPipe (CY_HANDLE handl, UINT8);
#define CY_DEBUG_PRINT_INFO(...) //User need to enable this
#define CY_DEBUG_PRINT_ERROR(...) //printf
#define DUMP_DATA 1
#ifdef DUMP_DATA
#define CY_DUMP_DATA(INPUT,LEN)\
{\
int i = 0, len = LEN;\
while ((len)) {\
printf ("%x ", (INPUT)[i++]);\
len--;\
}\
printf ("\n");\
}
#else
#define CY_DUMP_DATA (INPUT, LEN) do { }while (0);
#endif
#define CY_USB_SERIAL_TIMEOUT 5000
#define CY_EVENT_NOTIFICATION_TIMEOUT 0 //This will make the transfer infinite
#define CY_VENDOR_REQUEST_DEVICE_TO_HOST 0xC0
#define CY_VENDOR_REQUEST_HOST_TO_DEVICE 0x40
#define CY_CLASS_INTERFACE_REQUEST_DEVICE_TO_HOST 0XA1
#define CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE 0x21
//I2C related macros
#define CY_SCB_INDEX_POS 15
#define CY_I2C_CONFIG_LENGTH 16
#define CY_I2C_WRITE_COMMAND_POS 3
#define CY_I2C_WRITE_COMMAND_LEN_POS 4
#define CY_I2C_GET_STATUS_LEN 3
#define CY_I2C_MODE_WRITE 1
#define CY_I2C_MODE_READ 0
#define CY_I2C_ERROR_BIT (1)
#define CY_I2C_ARBITRATION_ERROR_BIT (1 << 1)
#define CY_I2C_NAK_ERROR_BIT (1 << 2)
#define CY_I2C_BUS_ERROR_BIT (1 << 3)
#define CY_I2C_STOP_BIT_ERROR (1 << 4)
#define CY_I2C_BUS_BUSY_ERROR (1 << 5)
#define CY_I2C_ENABLE_PRECISE_TIMING 1
#define CY_I2C_EVENT_NOTIFICATION_LEN 3
//SPI related Macros
#define CY_SPI_CONFIG_LEN 16
#define CY_SPI_EVENT_NOTIFICATION_LEN 2
#define CY_SPI_READ_BIT (1)
#define CY_SPI_WRITE_BIT (1 << 1)
#define CY_SPI_SCB_INDEX_BIT (1 << 15)
#define CY_SPI_GET_STATUS_LEN 4
#define CY_SPI_UNDERFLOW_ERROR (1)
#define CY_SPI_BUS_ERROR (1 << 1)
//Vendor UART related macros
#define CY_UART_SET_FLOW_CONTROL_CMD 0x60
#define CY_UART_SEND_BREAK_CMD 0x17
#define CY_UART_CONFIG_LEN 16
#define CY_SET_LINE_CONTROL_STATE_CMD 0x22
#define CY_UART_EVENT_NOTIFICATION_LEN 10
#define CY_UART_SERIAL_STATE_CARRIER_DETECT 1
#define CY_UART_SERIAL_STATE_TRANSMISSION_CARRIER (1 << 1)
#define CY_UART_SERIAL_STATE_BREAK_DETECTION (1<< 2)
#define CY_UART_SERIAL_STATE_RING_SIGNAL_DETECTION (1 << 3)
#define CY_UART_SERIAL_STATE_FRAMING_ERROR (1 << 4)
#define CY_UART_SERIAL_STATE_PARITY_ERROR (1 << 5)
#define CY_UART_SERIAL_STATUE_OVERRUN (1 << 6)
//Bootloader related macros
#define CY_BOOT_CONFIG_SIZE 64
#define CY_DEVICE_CONFIG_SIZE 512
#define CY_FIRMWARE_BREAKUP_SIZE 4096
#define CY_GET_SILICON_ID_LEN 4
#define CY_GET_FIRMWARE_VERSION_LEN 8
#define CY_GET_SIGNATURE_LEN 4
//JTAG related Macros
#define CY_JTAG_OUT_EP 0x04
#define CY_JTAG_IN_EP 0x85
//GPIO related Macros
#define CY_GPIO_GET_LEN 2
#define CY_GPIO_SET_LEN 1
//PHDC related macros
#define CY_PHDC_SET_FEATURE 0X03
#define CY_PHDC_SET_FEATURE_WVALUE 0x0101
#define CY_PHDC_CLR_FEATURE 0X01
#define CY_PHDC_CLR_FEATURE_WVALUE 0x1
#define CY_PHDC_GET_DATA_STATUS 0x00
#define CY_PHDC_GET_STATUS_LEN 2
typedef enum CY_VENDOR_CMDS
{
CY_GET_VERSION_CMD = 0xB0, /**< Get the version of the boot-loader.
* value = 0, index = 0, length = 4;
* data_in = 32 bit version.
*/
CY_GET_SIGNATURE_CMD = 0xBD, /**< Get the signature of the firmware
* It is suppose to be 'CYUS' for normal firmware
* and 'CYBL' for Bootloader.
*/
CY_UART_GET_CONFIG_CMD = 0xC0, /**< Retreive the 16 byte UART configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_in = 16 byte configuration.
*/
CY_UART_SET_CONFIG_CMD, /**< Update the 16 byte UART configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_out = 16 byte configuration information.
*/
CY_SPI_GET_CONFIG_CMD, /**< Retreive the 16 byte SPI configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_in = 16 byte configuration.
*/
CY_SPI_SET_CONFIG_CMD, /**< Update the 16 byte SPI configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_out = 16 byte configuration information.
*/
CY_I2C_GET_CONFIG_CMD, /**< Retreive the 16 byte I2C configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_in = 16 byte configuration.
*/
CY_I2C_SET_CONFIG_CMD = 0xC5, /**< Update the 16 byte I2C configuration information.
* MS bit of value indicates the SCB index.
* length = 16, data_out = 16 byte configuration information.
*/
CY_I2C_WRITE_CMD, /**< Perform I2C write operation.
* value = bit0 - start, bit1 - stop, bit3 - start on idle,
* bits[14:8] - slave address, bit15 - scbIndex. length = 0. The
* data is provided over the bulk endpoints.
*/
CY_I2C_READ_CMD, /**< Perform I2C read operation.
* value = bit0 - start, bit1 - stop, bit2 - Nak last byte,
* bit3 - start on idle, bits[14:8] - slave address, bit15 - scbIndex,
* length = 0. The data is provided over the bulk endpoints.
*/
CY_I2C_GET_STATUS_CMD, /**< Retreive the I2C bus status.
* value = bit0 - 0: TX 1: RX, bit15 - scbIndex, length = 3,
* data_in = byte0: bit0 - flag, bit1 - bus_state, bit2 - SDA state,
* bit3 - TX underflow, bit4 - arbitration error, bit5 - NAK
* bit6 - bus error,
* byte[2:1] Data count remaining.
*/
CY_I2C_RESET_CMD, /**< The command cleans up the I2C state machine and frees the bus.
* value = bit0 - 0: TX path, 1: RX path; bit15 - scbIndex,
* length = 0.
*/
CY_SPI_READ_WRITE_CMD = 0xCA, /**< The command starts a read / write operation at SPI.
* value = bit 0 - RX enable, bit 1 - TX enable, bit 15 - scbIndex;
* index = length of transfer.
*/
CY_SPI_RESET_CMD, /**< The command resets the SPI pipes and allows it to receive new
* request.
* value = bit 15 - scbIndex
*/
CY_SPI_GET_STATUS_CMD, /**< The command returns the current transfer status. The count will match
* the TX pipe status at SPI end. For completion of read, read all data
* at the USB end signifies the end of transfer.
* value = bit 15 - scbIndex
*/
CY_JTAG_ENABLE_CMD = 0xD0, /**< Enable JTAG module */
CY_JTAG_DISABLE_CMD, /**< Disable JTAG module */
CY_JTAG_READ_CMD, /**< JTAG read vendor command */
CY_JTAG_WRITE_CMD, /**< JTAG write vendor command */
CY_GPIO_GET_CONFIG_CMD = 0xD8, /**< Get the GPIO configuration */
CY_GPIO_SET_CONFIG_CMD, /**< Set the GPIO configuration */
CY_GPIO_GET_VALUE_CMD, /**< Get GPIO value */
CY_GPIO_SET_VALUE_CMD, /**< Set the GPIO value */
CY_PROG_USER_FLASH_CMD = 0xE0, /**< Program user flash area. The total space available is 512 bytes.
* This can be accessed by the user from USB. The flash area
* address offset is from 0x0000 to 0x00200 and can be written to
* page wise (128 byte).
*/
CY_READ_USER_FLASH_CMD, /**< Read user flash area. The total space available is 512 bytes.
* This can be accessed by the user from USB. The flash area
* address offset is from 0x0000 to 0x00200 and can be written to
* page wise (128 byte).
*/
CY_DEVICE_RESET_CMD = 0xE3, /**< Performs a device reset from firmware */
} CY_VENDOR_CMDS;
extern libusb_context *glContext;
+686
View File
@@ -0,0 +1,686 @@
/*
* I2C routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
#pragma pack(1)
typedef struct
{
UINT32 frequency; /**< Frequency of operation. Only valid values are
100KHz and 400KHz. */
UINT8 sAddress; /**< Slave address to be used when in slave mode. */
BOOL isMsbFirst; /**< Whether to transmit most significant bit first. */
BOOL isMaster; /**< Whether to block is to be configured as a master:
CyTrue - The block functions as I2C master.
CyFalse - The block functions as I2C slave. */
BOOL sIgnore; /**< Ignore general call in slave mode. */
BOOL clockStretch; /**< Wheteher to stretch clock in case of no FIFO availability. */
BOOL isLoopback; /**< Whether to loop back TX data to RX. Valid only
for debug purposes. */
UCHAR reserved[6]; /**< Reserved for future use */
} CyUsI2cConfig_t;
#pragma pack()
#ifdef CY_I2C_ENABLE_PRECISE_TIMING
struct timeval startTimeWrite, endTimeWrite, startTimeRead, endTimeRead;
//Timer helper functions for proper timing
void startI2cTick (bool isWrite) {
if (isWrite)
gettimeofday (&startTimeWrite, NULL);
else
gettimeofday (&startTimeRead, NULL);
}
UINT32 getI2cLapsedTime (bool isWrite){
signed int currentTime_sec, currentTime_usec, currentTime;
if (isWrite){
gettimeofday (&endTimeWrite, NULL);
currentTime_sec = (endTimeWrite.tv_sec - startTimeWrite.tv_sec) * 1000;
currentTime_usec = ((endTimeWrite.tv_usec - startTimeWrite.tv_usec)) / 1000;
currentTime = currentTime_sec + currentTime_usec;
return (unsigned int)currentTime;
}
else{
gettimeofday (&endTimeRead, NULL);
currentTime_sec = (endTimeRead.tv_sec - startTimeRead.tv_sec) * 1000;
currentTime_usec = ((endTimeRead.tv_usec - startTimeRead.tv_usec)) / 1000;
currentTime = currentTime_sec + currentTime_usec;
return (unsigned int)currentTime;
}
}
#endif
CY_RETURN_STATUS handleI2cError (UINT8 i2cStatus){
if (i2cStatus & CY_I2C_NAK_ERROR_BIT){
CY_DEBUG_PRINT_ERROR ("CY:Error Nacked by device ...Function is %s\n", __func__);
return CY_ERROR_I2C_NAK_ERROR;
}
if (i2cStatus & CY_I2C_BUS_ERROR_BIT){
CY_DEBUG_PRINT_ERROR ("CY:Error bus error occured... Function is %s\n", __func__);
return CY_ERROR_I2C_BUS_ERROR;
}
if (i2cStatus & CY_I2C_ARBITRATION_ERROR_BIT){
CY_DEBUG_PRINT_ERROR ("CY:Error Arbitration error occured.. Function is %s\n", __func__);
return CY_ERROR_I2C_ARBITRATION_ERROR;
}
if (i2cStatus & CY_I2C_STOP_BIT_ERROR){
CY_DEBUG_PRINT_ERROR ("CY:Error Stop bit set by master..Function is %s\n", __func__);
return CY_ERROR_I2C_STOP_BIT_SET;
}
else {
//We should never hit this case!!!!
CY_DEBUG_PRINT_ERROR ("CY:Unknown error..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
//Used internally by read and write API to check if data is received at the I2C end.
CY_RETURN_STATUS CyI2cGetStatus (CY_HANDLE handle, bool mode, UCHAR *i2cStatus);
CY_RETURN_STATUS waitForNotification (CY_HANDLE handle, UINT16 *bytesPending, UINT32 ioTimeout);
/*
* This API gets the current I2C config
* for the particluar interface of the device
*/
CY_RETURN_STATUS CyGetI2cConfig (
CY_HANDLE handle,
CY_I2C_CONFIG *i2cConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CyUsI2cConfig_t localI2cConfig;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 scbIndex = 0;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (i2cConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_I2C) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c ..Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_I2C_GET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0x00;
wLength = CY_I2C_CONFIG_LENGTH;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localI2cConfig, wLength, ioTimeout);
if (rStatus == CY_I2C_CONFIG_LENGTH){
CY_DEBUG_PRINT_INFO ("CY: Read I2C config ...size is %d \n", rStatus);
i2cConfig->frequency = localI2cConfig.frequency;
i2cConfig->slaveAddress = localI2cConfig.sAddress;
i2cConfig->isMaster = localI2cConfig.isMaster;
i2cConfig->isClockStretch = localI2cConfig.clockStretch;
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Error time out ....Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in doing I2C read ...libusb error is %d function is %s!\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
* This API sets I2C config of the device for that
* interface
*/
CY_RETURN_STATUS CySetI2cConfig (
CY_HANDLE handle,
CY_I2C_CONFIG *i2cConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
CyUsI2cConfig_t localI2cConfig;
int rStatus;
CY_DEVICE *device = NULL;
libusb_device_handle *devHandle;
UINT16 scbIndex = 0;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (i2cConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (i2cConfig->frequency < 1000 || i2cConfig->frequency > 400000){
CY_DEBUG_PRINT_ERROR ("CY:Error frequency trying to set in out of ..range Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if ((i2cConfig->slaveAddress % 2) != 0){
CY_DEBUG_PRINT_ERROR ("CY:Error slave address needs to even..Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
scbIndex = device->interfaceNum;
if (device->deviceType != CY_TYPE_I2C) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c ..Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_I2C_SET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0x00;
wLength = CY_I2C_CONFIG_LENGTH;
//We need to pass entire 16 bytes config structure to firmware
//but we will not expose all the structure elements to user.
//so filling some of the values.
memset (&localI2cConfig, 0, CY_I2C_CONFIG_LENGTH);
localI2cConfig.frequency = i2cConfig->frequency;
localI2cConfig.sAddress = i2cConfig->slaveAddress;
localI2cConfig.isMaster = i2cConfig->isMaster;
localI2cConfig.clockStretch = i2cConfig->isClockStretch;
localI2cConfig.isMsbFirst = 1;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localI2cConfig, wLength, ioTimeout);
if (rStatus == CY_I2C_CONFIG_LENGTH){
CY_DEBUG_PRINT_INFO ("CY: Setting I2C config successful ...\n");
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Error time out ....Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in doing I2C read ...libusb error is %d function is %s!\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
* This API reads I2C data from the specified interface of the device
* interface
*/
CY_RETURN_STATUS CyI2cRead (
CY_HANDLE handle,
CY_I2C_DATA_CONFIG *i2cDataConfig,
CY_DATA_BUFFER *readBuffer,
UINT32 ioTimeout
)
{
int rStatus;
CY_DEVICE *device = NULL;
libusb_device_handle *devHandle;
UINT16 wValue = 0, wIndex, wLength, bytesPending = 0;
UINT8 bmRequestType, bmRequest;
UCHAR i2cStatus[CY_I2C_GET_STATUS_LEN];
UINT16 scbIndex = 0;
bool mode = CY_I2C_MODE_READ;
UINT32 elapsedTime;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((readBuffer == NULL) || (readBuffer->buffer == NULL) || (readBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
readBuffer->transferCount = 0;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_I2C) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c ..Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (pthread_mutex_trylock (&device->readLock) == 0){
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
i2cDataConfig->slaveAddress = ((i2cDataConfig->slaveAddress & 0x7F) | (scbIndex << 7));
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_I2C_READ_CMD;
wValue = ((i2cDataConfig->isStopBit) | (i2cDataConfig->isNakBit << 1));
wValue |= (((i2cDataConfig->slaveAddress) << 8));
wIndex = readBuffer->length;
wLength = 0;
rStatus = CyI2cGetStatus (handle, mode, (UCHAR *)i2cStatus);
if (rStatus == CY_SUCCESS)
{
if ((i2cStatus[0] & CY_I2C_ERROR_BIT)){
CY_DEBUG_PRINT_ERROR ("CY:Error device busy ... function is %s \n", __func__);
pthread_mutex_unlock (&device->readLock);
return CY_ERROR_I2C_DEVICE_BUSY;
}
}
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == LIBUSB_ERROR_NO_DEVICE){
CY_DEBUG_PRINT_ERROR ("CY:Error device disconnected ... \n");
pthread_mutex_unlock (&device->readLock);
return CY_ERROR_DEVICE_NOT_FOUND;
}
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY:Error in sending Read vendor command ... Libusb Error is %d .. Function is %s \n", rStatus, __func__);
pthread_mutex_unlock (&device->readLock);
return CY_ERROR_I2C_DEVICE_BUSY;
}
//Hoping that previous calls do not take much time!!
#ifdef CY_I2C_ENABLE_PRECISE_TIMING
startI2cTick(false);
#endif
rStatus = libusb_bulk_transfer (devHandle, device->inEndpoint, readBuffer->buffer, readBuffer->length,
(int*)&readBuffer->transferCount, ioTimeout);
#ifdef CY_I2C_ENABLE_PRECISE_TIMING
elapsedTime = getI2cLapsedTime(false);
//Giving an extra 10 msec to notification to findout the status
ioTimeout = (ioTimeout - elapsedTime);
if (ioTimeout == 0)
ioTimeout = 10;
#endif
if (rStatus == LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY: Successfully read i2c data.. %d bytes Read ...\n", readBuffer->transferCount);
bytesPending = readBuffer->length;
rStatus = waitForNotification (handle, &bytesPending, ioTimeout);
if (rStatus)
readBuffer->transferCount = (readBuffer->length - bytesPending);
else
readBuffer->transferCount = readBuffer->length;
pthread_mutex_unlock (&device->readLock);
return rStatus;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Timeout error ..Function is %s\n", __func__);
pthread_mutex_unlock (&device->readLock);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_INFO ("Pipe Error \n");
rStatus = CyResetPipe (handle, device->outEndpoint);
if (rStatus != CY_SUCCESS){
CY_DEBUG_PRINT_ERROR ("Error in reseting the pipe \n");
}
else {
CY_DEBUG_PRINT_INFO ("Reset pipe succeded \n");
}
rStatus = CyI2cGetStatus (handle, mode, (UCHAR *)i2cStatus);
if (rStatus == CY_SUCCESS)
{
CyI2cReset (handle, mode);
rStatus = handleI2cError (i2cStatus[0]);
pthread_mutex_unlock (&device->readLock);
return rStatus;
}
else {
pthread_mutex_unlock (&device->readLock);
return CY_ERROR_I2C_DEVICE_BUSY;
}
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
pthread_mutex_unlock (&device->readLock);
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
pthread_mutex_unlock (&device->readLock);
CY_DEBUG_PRINT_ERROR ("CY: Error in doing I2C read ...libusb error is %d function is %s!\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
else{
CY_DEBUG_PRINT_ERROR ("CY: Error API busy in servicing previous request... function is %s!\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
* This API writes I2C data into the specified interface of the device
*/
CY_RETURN_STATUS CyI2cWrite (
CY_HANDLE handle,
CY_I2C_DATA_CONFIG *i2cDataConfig,
CY_DATA_BUFFER *writeBuffer,
UINT32 ioTimeout
)
{
int rStatus;
UCHAR i2cStatus[CY_I2C_GET_STATUS_LEN];
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue = 0, wIndex, wLength, bytesPending = 0;
UINT8 bmRequestType, bmRequest;
UINT16 scbIndex = 0;
BOOL mode = CY_I2C_MODE_WRITE;
UINT32 elapsedTime;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((writeBuffer == NULL) || (writeBuffer->buffer == NULL) || (writeBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
writeBuffer->transferCount = 0;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
scbIndex = device->interfaceNum;
if (device->deviceType != CY_TYPE_I2C){
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c ..Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (pthread_mutex_trylock (&device->writeLock) == 0){
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_I2C_WRITE_CMD;
i2cDataConfig->slaveAddress = ((i2cDataConfig->slaveAddress & 0x7F) | (scbIndex << 7));
wValue = ((i2cDataConfig->isStopBit));
wValue |= (((i2cDataConfig->slaveAddress) << 8));
wIndex = (UINT16)(writeBuffer->length);
wLength = 0;
CY_DEBUG_PRINT_INFO ("wValue is %x \n", wValue);
//Send I2C write vendor command before actually sending the data over bulk ep
rStatus = CyI2cGetStatus (handle, mode, (UCHAR *)i2cStatus);
if (rStatus == CY_SUCCESS)
{
if ((i2cStatus[0] & CY_I2C_ERROR_BIT)){
CY_DEBUG_PRINT_ERROR ("CY:Error ... Device busy ... function is %s \n", __func__);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_I2C_DEVICE_BUSY;
}
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE){
CY_DEBUG_PRINT_ERROR ("CY:Error device not found \n");
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_DEVICE_NOT_FOUND;
}
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == LIBUSB_ERROR_NO_DEVICE){
CY_DEBUG_PRINT_ERROR ("CY:Error device not found \n");
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_DEVICE_NOT_FOUND;
}
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY:Error in sending write vendor command ... Libusb Error is %d \n", rStatus);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_I2C_DEVICE_BUSY;
}
//After vendor command is sent send the actual data to be sent to i2c devic
#ifdef CY_I2C_ENABLE_PRECISE_TIMING
startI2cTick(true);
#endif
rStatus = libusb_bulk_transfer (devHandle, device->outEndpoint, writeBuffer->buffer, writeBuffer->length,
(int*)&(writeBuffer->transferCount), ioTimeout);
#ifdef CY_I2C_ENABLE_PRECISE_TIMING
elapsedTime = getI2cLapsedTime(true);
ioTimeout = (ioTimeout - elapsedTime);
//Giving an extra 10 msec to notification to findout the status
if (ioTimeout == 0)
ioTimeout = 10;
#endif
//Once the data is sent to usbserial, check if it was actually written to i2c device.
if (rStatus == LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY: Successfully written i2c data.. %d bytes written ...\n", writeBuffer->transferCount);
bytesPending = writeBuffer->length;
rStatus = waitForNotification (handle, &bytesPending, ioTimeout);
if (rStatus)
writeBuffer->transferCount = (writeBuffer->length - bytesPending);
else
writeBuffer->transferCount = writeBuffer->length;
pthread_mutex_unlock (&device->writeLock);
return rStatus;
}
//Transaction is stallled when we hit some I2C error while the transfer
//was going on. After we hit this error clear stall and check why we hit this by
//CyGetStatus.
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_INFO ("CY:Pipe Error ... Function is %s\n", __func__);
rStatus = CyResetPipe (handle, device->outEndpoint);
if (rStatus != CY_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in reseting the pipe ..Function is %s\n", __func__);
}
else {
CY_DEBUG_PRINT_INFO ("Reset pipe succeded \n");
}
rStatus = CyI2cGetStatus (handle, mode, (UCHAR *)i2cStatus);
if (rStatus == CY_SUCCESS)
{
CyI2cReset (handle, mode);
rStatus = handleI2cError (i2cStatus[0]);
pthread_mutex_unlock (&device->writeLock);
return rStatus;
}
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s\n", __func__);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Error time out ....Function is %s\n", __func__);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_IO_TIMEOUT;
}
else{
CY_DEBUG_PRINT_ERROR ("CY: Error in doing I2C read ...libusb error is %d function is %s!\n", rStatus, __func__);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_REQUEST_FAILED;
}
}
else{
CY_DEBUG_PRINT_ERROR ("CY:API busy with servicing previous request... function is %s!\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_REQUEST_FAILED;
}
/*
* This API gets the current status of the I2C data transaction
*/
CY_RETURN_STATUS CyI2cGetStatus (
CY_HANDLE handle,
bool mode,
UCHAR *i2cStatus
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength, bmRequestType, bmRequest;;
UINT16 scbIndex = 0;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
if (i2cStatus == NULL)
return CY_ERROR_INVALID_PARAMETER;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_I2C) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c .. \n");
return CY_ERROR_REQUEST_FAILED;
}
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_I2C_GET_STATUS_CMD;
wValue = ((scbIndex << CY_SCB_INDEX_POS) | mode);
wIndex = 0;
wLength = CY_I2C_GET_STATUS_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, (UCHAR*)i2cStatus, wLength, ioTimeout);
if (rStatus < CY_I2C_GET_STATUS_LEN){
CY_DEBUG_PRINT_INFO ("CY:Error in sending I2C Get Status command...Libusb error is %d\n", rStatus);
return rStatus;
}
return CY_SUCCESS;
}
/*
* This API resets the I2C module
*/
CY_RETURN_STATUS CyI2cReset (
CY_HANDLE handle,
BOOL resetMode
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength, bmRequestType, bmRequest;
UINT16 scbIndex = 0;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_I2C) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not i2c .. \n");
return CY_ERROR_REQUEST_FAILED;
}
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_I2C_RESET_CMD;
wValue = ((scbIndex << CY_SCB_INDEX_POS) | resetMode );
wIndex = 0;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY:Error in sending I2C Reset command ..libusb error is %d\n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
return CY_SUCCESS;
}
static void LIBUSB_CALL i2c_notification_cb(struct libusb_transfer *transfer)
{
UINT32 *completed = transfer->user_data;
*completed = 1;
}
CY_RETURN_STATUS waitForNotification (CY_HANDLE handle, UINT16 *bytesPending, UINT32 ioTimeout){
UINT32 transferCompleted = 0, length = CY_I2C_EVENT_NOTIFICATION_LEN;
CY_DEVICE *device;
libusb_device_handle *devHandle;
struct libusb_transfer *transfer;
CY_RETURN_STATUS errorStatus, rStatus;
UCHAR i2cStatus[CY_I2C_EVENT_NOTIFICATION_LEN];
struct timeval time;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
transfer = libusb_alloc_transfer(0);
if (transfer == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error in allocating trasnfer \n");
errorStatus = CY_ERROR_ALLOCATION_FAILED;
(*bytesPending) = 0;
return errorStatus;
//callbackFn (errorStatus, 0);
}
libusb_fill_interrupt_transfer (transfer, devHandle, device->interruptEndpoint, i2cStatus, length,
i2c_notification_cb, &transferCompleted, ioTimeout);
if (libusb_submit_transfer (transfer)){
CY_DEBUG_PRINT_ERROR ("CY:Error in submitting interrupt transfer ...\n");
libusb_cancel_transfer (transfer);
libusb_free_transfer (transfer);
(*bytesPending) = 0;
//callbackFn (CY_ERROR_REQUEST_FAILED, 0);
return CY_ERROR_REQUEST_FAILED;
}
time.tv_sec = 0;
time.tv_usec = 50;//polling timeout.
while (transferCompleted == 0){
libusb_handle_events_timeout (glContext, &time);
}
transferCompleted = 0;
if (transfer->status == LIBUSB_TRANSFER_COMPLETED){
CY_DEBUG_PRINT_INFO ("CY:Info successfully recieved data on interrupt pipe length is %d \n", transfer->actual_length);
if (i2cStatus[0] & 0x80){ //Error notification is for write
if ((i2cStatus[0] & CY_I2C_ERROR_BIT)){
CY_DEBUG_PRINT_INFO ("Bytes pending is %x %x %x", i2cStatus[0], i2cStatus[1], i2cStatus[2]);
//There was some error, so reset the i2c module and usb module
//of the device, so branch out of the loop(Check below for the errors reported).
rStatus = CyI2cReset (device, CY_I2C_MODE_WRITE);
if (rStatus != CY_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY:i2c reset failed \n");
}
//Report the amount of byte that were actually written
memcpy(bytesPending, &i2cStatus[1], 2);
errorStatus = handleI2cError (i2cStatus[0]);
}
else
errorStatus = CY_SUCCESS;
}
else //Error notification is for read
{
if ((i2cStatus[0] & CY_I2C_ERROR_BIT)){
CY_DEBUG_PRINT_INFO ("Bytes pending is %x %x %x", i2cStatus[0], i2cStatus[1], i2cStatus[2]);
rStatus = CyI2cReset (device, CY_I2C_MODE_READ);
if (rStatus != CY_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY:i2c reset failed \n");
}
//Report the amount of byte that were actually written
memcpy(bytesPending, &i2cStatus[1], 2);
errorStatus = handleI2cError (i2cStatus[0]);
}
else
errorStatus = CY_SUCCESS;
}
libusb_free_transfer (transfer);
return errorStatus;
}
else{
libusb_cancel_transfer (transfer);
if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT){
CY_DEBUG_PRINT_ERROR ("CY:Error Timeout in getting i2c transfer status ....\n");
CyI2cGetStatus (handle, 1, (UCHAR *)&errorStatus);
errorStatus = CY_ERROR_IO_TIMEOUT;
}
if (transfer->status == LIBUSB_TRANSFER_OVERFLOW){
CY_DEBUG_PRINT_ERROR ("CY:Error buffer overFlow in i2c transfer status ....\n");
errorStatus = CY_ERROR_BUFFER_OVERFLOW;
}
if (transfer->status != LIBUSB_TRANSFER_COMPLETED){
CY_DEBUG_PRINT_ERROR ("CY:Error in i2c transfer status ... Libusb transfer error is %d \n", transfer->status);
errorStatus = CY_ERROR_REQUEST_FAILED;
}
libusb_free_transfer (transfer);
return CY_ERROR_REQUEST_FAILED;
}
}
+248
View File
@@ -0,0 +1,248 @@
/*
* JTAG routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
/*
* This API enables the Jtag module
*/
CY_RETURN_STATUS CyJtagEnable (
CY_HANDLE handle
)
{
UINT16 wValue, wIndex, wLength;
UINT16 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_JTAG) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not jtag ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_JTAG_ENABLE_CMD;
wValue = 0x00;
wIndex = 0;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus >= 0){
CY_DEBUG_PRINT_INFO ("CY: JTAG enable successfully \n");
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
* This API disables the Jtag module
*/
CY_RETURN_STATUS CyJtagDisable (
CY_HANDLE handle
)
{
UINT16 wValue, wIndex, wLength;
UINT16 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_JTAG) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not jtag ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_JTAG_DISABLE_CMD;
wValue = 0x00;
wIndex = 0;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus >= 0){
CY_DEBUG_PRINT_INFO ("CY: JTAG disable successfully \n");
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error while enabling JTAG ..\n");
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error while enabling JTAG ...libusb error is %d function is %s!\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
* This API is used to do jtag write
*/
CY_RETURN_STATUS CyJtagWrite (
CY_HANDLE handle,
CY_DATA_BUFFER *writeBuffer,
UINT32 ioTimeout
)
{
int rStatus = 0;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength;
UINT16 bmRequestType, bmRequest;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((writeBuffer == NULL) || (writeBuffer->buffer == NULL) || (writeBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_JTAG) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not jtag ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_JTAG_WRITE_CMD;
wValue = writeBuffer->length;
wIndex = 0;
wLength = 0;
writeBuffer->transferCount = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY: JTAG Vendor command failed %d...function is %s \n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
rStatus = libusb_bulk_transfer (devHandle, CY_JTAG_OUT_EP, writeBuffer->buffer, writeBuffer->length,
(int*)&(writeBuffer->transferCount), ioTimeout);
if (rStatus == CY_SUCCESS) {
CY_DEBUG_PRINT_ERROR ("CY: Number of bytes written is .... %d \n", writeBuffer->transferCount);
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:TimeOut error ...Function is %s %d\n", __func__, writeBuffer->transferCount);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_ERROR ("CY:Pipe error Function is %s \n", __func__);
CyResetPipe (handle, CY_JTAG_OUT_EP);
return CY_ERROR_PIPE_HALTED;
}
else if (rStatus == LIBUSB_ERROR_OVERFLOW){
CY_DEBUG_PRINT_ERROR ("CY:Error Buffer Overflow..Function is %s \n", __func__);
return CY_ERROR_BUFFER_OVERFLOW;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s \n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in Function %s...Libusb Error is %d !\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This API is used to read JTAG data from device interface
*/
CY_RETURN_STATUS CyJtagRead (
CY_HANDLE handle,
CY_DATA_BUFFER *readBuffer,
UINT32 ioTimeout
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength;
UINT16 bmRequestType, bmRequest;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((readBuffer == NULL) || (readBuffer->buffer == NULL) || (readBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_JTAG) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not jtag ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_JTAG_READ_CMD;
wValue = readBuffer->length;
wIndex = 0;
wLength = 0;
readBuffer->transferCount = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus < 0){
CY_DEBUG_PRINT_INFO ("CY: JTAG Vendor Command failed %d.. Function is %s \n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
rStatus = libusb_bulk_transfer (devHandle, CY_JTAG_IN_EP, readBuffer->buffer, readBuffer->length,
(int*)&(readBuffer->transferCount), ioTimeout);
if (rStatus == CY_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY: Number of bytes read is .... %d \n", readBuffer->transferCount);
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:TimeOut error ...Function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_ERROR ("CY:Pipe error Function is %s \n", __func__);
CyResetPipe (handle, CY_JTAG_IN_EP);
return CY_ERROR_PIPE_HALTED;
}
else if (rStatus == LIBUSB_ERROR_OVERFLOW){
CY_DEBUG_PRINT_ERROR ("CY:Error Buffer Overflow..Function is %s \n", __func__);
return CY_ERROR_BUFFER_OVERFLOW;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s \n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function is %s ...Libusb Error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
+591
View File
@@ -0,0 +1,591 @@
/*
* Miscellaneous routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
typedef struct NOTIFICATION_CB_PARAM{
CY_HANDLE handle;
CY_EVENT_NOTIFICATION_CB_FN notificationCbFn;
}NOTIFICATION_CB_PARAM;
/*
This API is used to Read the Bootloder version
*/
CY_RETURN_STATUS CyGetFirmwareVersion(
CY_HANDLE handle,
CY_FIRMWARE_VERSION *firmwareVersion
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_GET_VERSION_CMD;
wValue = 0x00;
wIndex = 0x00;
wLength = CY_GET_FIRMWARE_VERSION_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)firmwareVersion, wLength, ioTimeout);
if (rStatus > 0){
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
The API resets the device
*/
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CyResetDevice (
CY_HANDLE handle /*Valid device handle*/
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle \n");
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_DEVICE_RESET_CMD;
wValue = 0xA6B6;
wIndex = 0xADBA;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
//return buffer will tell the status of the command
if (rStatus == LIBUSB_SUCCESS)
return CY_SUCCESS;
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CySetGpioValue (
CY_HANDLE handle, /*Valid device handle*/
UINT8 gpioNumber, /*GPIO configuration value*/
UINT8 value /*Value that needs to be set*/
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest, buffer[CY_GPIO_SET_LEN];
int rStatus;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (value)
value = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_GPIO_SET_VALUE_CMD;
wValue = gpioNumber;
wIndex = value;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, buffer, wLength, ioTimeout);
if (rStatus >= 0){
CY_DEBUG_PRINT_INFO ("CY: Get Configuration of GPIO succedded...size is %d \n", rStatus);
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CyGetGpioValue (
CY_HANDLE handle, /*Valid device handle*/
UINT8 gpioNumber, /*GPIO configuration value*/
UINT8 *value /*Value that needs to be set*/
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest, buffer[CY_GPIO_GET_LEN];
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_GPIO_GET_VALUE_CMD;
wValue = gpioNumber;
wIndex = 0x00;
wLength = CY_GPIO_GET_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, buffer, wLength, ioTimeout);
if (rStatus == CY_GPIO_GET_LEN){
CY_DEBUG_PRINT_INFO ("CY: Get GPIO Configuration succedded...size is %d \n", rStatus);
//return buffer will tell the status of the command
if (buffer[0] == 0){
(*value) = buffer[1];
return CY_SUCCESS;
}
else
return CY_ERROR_REQUEST_FAILED;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
static void LIBUSB_CALL uart_notification_cb(struct libusb_transfer *transfer)
{
UINT32 *completed = transfer->user_data;
*completed = 1;
}
void* uartSetEventNotifcation (void *inputParameters)
{
int rStatus, transferCompleted = 0, length = CY_UART_EVENT_NOTIFICATION_LEN;
CY_DEVICE *device;
libusb_device_handle *devHandle;
struct libusb_transfer *transfer;
UINT16 errorStatus = 0;
UCHAR uartStatus[CY_UART_EVENT_NOTIFICATION_LEN];
struct timeval time;
CY_EVENT_NOTIFICATION_CB_FN callbackFn;
NOTIFICATION_CB_PARAM *cbParameters = (NOTIFICATION_CB_PARAM*)inputParameters;
callbackFn = cbParameters->notificationCbFn;
device = (CY_DEVICE *)cbParameters->handle;
devHandle = device->devHandle;
callbackFn = cbParameters->notificationCbFn;
device->uartTransfer = transfer = libusb_alloc_transfer(0);
if (transfer == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error in allocating trasnfer \n");
errorStatus |= CY_ERROR_EVENT_FAILED_BIT;
callbackFn(errorStatus);
goto END;
}
while (device->uartCancelEvent == false){
libusb_fill_interrupt_transfer (transfer, devHandle, device->interruptEndpoint, uartStatus, length,
uart_notification_cb, &transferCompleted, CY_EVENT_NOTIFICATION_TIMEOUT);
rStatus = libusb_submit_transfer (transfer);
if (rStatus){
CY_DEBUG_PRINT_ERROR ("CY:Error submitting uart interrupt token ... Libusb error is %d\n", rStatus);
errorStatus |= CY_ERROR_EVENT_FAILED_BIT;
callbackFn(errorStatus);
break;
}
time.tv_sec = 0;
time.tv_usec = 50;//polling timeout.
while (transferCompleted == 0){
libusb_handle_events_timeout (glContext, &time);
}
transferCompleted = 0;
if (transfer->status == LIBUSB_TRANSFER_COMPLETED){
CY_DEBUG_PRINT_INFO ("Successfully read and recieved data %d \n", transfer->actual_length);
memcpy (&errorStatus, &uartStatus[8], 2);
printf ("%x %x ", uartStatus[8], uartStatus[9]);
callbackFn (errorStatus);
errorStatus = 0;
}
else{
errorStatus |= CY_ERROR_EVENT_FAILED_BIT;
if (device->uartCancelEvent == false){
CY_DEBUG_PRINT_ERROR ("CY:Error uart interrupt thread encountered error... Libusb transmission error is %d \n", transfer->status);
pthread_join (device->uartThread, NULL);
device->uartThreadRunning = false;
callbackFn(errorStatus);
}
break;
}
}
CY_DEBUG_PRINT_INFO ("Exiting notification thread \n");
libusb_free_transfer (transfer);
END:
free (inputParameters);
return NULL;
}
static void LIBUSB_CALL spi_notification_cb(struct libusb_transfer *transfer)
{
UINT32 *completed = transfer->user_data;
*completed = 1;
}
void* spiSetEventNotifcation (void *inputParameters)
{
int transferCompleted = 0, length = CY_SPI_EVENT_NOTIFICATION_LEN;
CY_DEVICE *device;
libusb_device_handle *devHandle;
struct libusb_transfer *transfer;
UINT16 spiStatus = 0;
unsigned char spiStatusBuffer[2] = {0, 0};
UINT16 errorStatus = 0;
struct timeval time;
CY_EVENT_NOTIFICATION_CB_FN callbackFn;
NOTIFICATION_CB_PARAM *cbParameters = (NOTIFICATION_CB_PARAM*)inputParameters;
callbackFn = cbParameters->notificationCbFn;
device = (CY_DEVICE *)cbParameters->handle;
devHandle = device->devHandle;
callbackFn = cbParameters->notificationCbFn;
device->spiTransfer = transfer = libusb_alloc_transfer(0);
if (transfer == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error in allocating trasnfer \n");
errorStatus |= CY_ERROR_EVENT_FAILED_BIT;
callbackFn (errorStatus);
goto END;
}
libusb_fill_interrupt_transfer (transfer, devHandle, device->interruptEndpoint, spiStatusBuffer, length,
spi_notification_cb, &transferCompleted, CY_EVENT_NOTIFICATION_TIMEOUT);
while (device->spiCancelEvent == false){
if (libusb_submit_transfer (transfer)){
CY_DEBUG_PRINT_ERROR ("CY:Error submitting spi interrupt token ... \n");
errorStatus |= CY_ERROR_EVENT_FAILED_BIT;
callbackFn(errorStatus);
break;
}
time.tv_sec = 0;
time.tv_usec = 50;//polling timeout.
while (transferCompleted == 0){
libusb_handle_events_timeout (glContext, &time);
}
transferCompleted = 0;
spiStatus = (spiStatusBuffer[1] << 8) | spiStatusBuffer[0];
if (transfer->status == LIBUSB_TRANSFER_COMPLETED){
CY_DEBUG_PRINT_INFO ("Successfully read and recieved data %d \n", transfer->actual_length);
if (spiStatus & CY_SPI_UNDERFLOW_ERROR){
errorStatus |= (CY_SPI_TX_UNDERFLOW_BIT);
}
if (spiStatus & CY_SPI_BUS_ERROR){
errorStatus |= (CY_SPI_BUS_ERROR_BIT);
}
callbackFn (errorStatus);
errorStatus = 0;
}
else{
spiStatus |= CY_ERROR_EVENT_FAILED_BIT;
if (device->spiCancelEvent == false){
pthread_join (device->spiThread, NULL);
device->spiThreadRunning = false;
CY_DEBUG_PRINT_ERROR ("CY:Error spi interrupt thread was cancelled... Libusb transmission error is %d \n", transfer->status);
callbackFn (spiStatus);
}
break;
}
}
libusb_free_transfer (transfer);
END:
free (inputParameters);
pthread_exit (NULL);
return NULL;
}
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CySetEventNotification(
CY_HANDLE handle, /*Valid handle to communicate with device*/
CY_EVENT_NOTIFICATION_CB_FN notificationCbFn /*Call back function in case on error during Uart data transfers*/
)
{
CY_DEVICE *device;
NOTIFICATION_CB_PARAM *args = NULL;
int ret;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (notificationCbFn == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE*)handle;
pthread_mutex_lock (&device->notificationLock);
args = (NOTIFICATION_CB_PARAM *)malloc (sizeof (NOTIFICATION_CB_PARAM));
args->handle = handle;
args->notificationCbFn = notificationCbFn;
if (device->deviceType == CY_TYPE_SPI){
if (device->spiThreadRunning) {
CY_DEBUG_PRINT_ERROR ("CY:Error already notification thread exists ... Function is %s \n", __func__);
free (args);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_STATUS_MONITOR_EXIST;
}
ret = pthread_create (&device->spiThread, NULL, spiSetEventNotifcation, (void *) args);
if (ret == 0){
device->spiThreadRunning = true;
pthread_mutex_unlock (&device->notificationLock);
return CY_SUCCESS;
}
else {
pthread_join (device->spiThread, NULL);
device->spiThreadRunning = false;
free (args);
pthread_mutex_unlock (&device->notificationLock);
CY_DEBUG_PRINT_ERROR ("CY:Error creating spi notification thread ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
else if (device->deviceType == CY_TYPE_UART){
if (device->uartThreadRunning) {
CY_DEBUG_PRINT_ERROR ("CY:Error already notification thread exists ... Function is %s \n", __func__);
free (args);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_STATUS_MONITOR_EXIST;
}
ret = pthread_create (&device->uartThread, NULL, uartSetEventNotifcation, (void *) args);
if (ret == 0){
device->uartThreadRunning = true;
pthread_mutex_unlock (&device->notificationLock);
return CY_SUCCESS;
}
else {
pthread_join (device->uartThread, NULL);
device->uartThreadRunning = false;
free (args);
pthread_mutex_unlock (&device->notificationLock);
CY_DEBUG_PRINT_ERROR ("CY:Error creating uart notification thread ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error unknown device type ....Function is %s \n", __func__);
free (args);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_REQUEST_FAILED;
}
}
/*The API is used to cancel the uart Event notification*/
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CyAbortEventNotification(
CY_HANDLE handle /*Valid handle to communicate with device*/
)
{
CY_DEVICE *device;
device = (CY_DEVICE*)handle;
pthread_mutex_lock (&device->notificationLock);
if (device->deviceType == CY_TYPE_UART){
if (!device->uartThreadRunning){
CY_DEBUG_PRINT_ERROR ("CY:Error uart event notification not created ....function is %s \n", __func__);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_REQUEST_FAILED;
}
device->uartCancelEvent = true;
libusb_cancel_transfer (device->uartTransfer);
pthread_join (device->uartThread, NULL);
device->uartThreadRunning = false;
device->uartCancelEvent = false;
pthread_mutex_unlock (&device->notificationLock);
return CY_SUCCESS;
}
else if (device->deviceType == CY_TYPE_SPI){
if (!device->spiThreadRunning){
CY_DEBUG_PRINT_ERROR ("CY:Error spi event notification not created ....function is %s \n", __func__);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_REQUEST_FAILED;
}
device->spiCancelEvent = true;
libusb_cancel_transfer (device->spiTransfer);
pthread_join (device->spiThread, NULL);
device->spiThreadRunning = false;
device->spiCancelEvent = false;
pthread_mutex_unlock (&device->notificationLock);
return CY_SUCCESS;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error.. unknown device type ....function is %s \n", __func__);
pthread_mutex_unlock (&device->notificationLock);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
The API is used to programme user flash area
*/
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CyProgUserFlash (
CY_HANDLE handle, /*Valid device handle*/
CY_DATA_BUFFER *progBuffer, /*data buffer containing buffer address, length to write*/
UINT32 flashAddress, /*Address to the data is written*/
UINT32 ioTimeout /*Timeout value of the API*/
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
if ((progBuffer == NULL) || (progBuffer->buffer == NULL))
return CY_ERROR_INVALID_PARAMETER;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_PROG_USER_FLASH_CMD;
wValue = 0;
wIndex = flashAddress;
wLength = progBuffer->length;
CY_DEBUG_PRINT_INFO ("CY:The Length is %d , Value is %d and index is %d\n", wLength, wValue, wIndex);
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, progBuffer->buffer, wLength, ioTimeout);
if (rStatus > 0){
(progBuffer->transferCount) = rStatus;
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
(progBuffer->transferCount) = 0;
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
(progBuffer->transferCount) = 0;
return CY_ERROR_REQUEST_FAILED;
}
}
/*
The API is used to programme user flash area
*/
CYWINEXPORT CY_RETURN_STATUS WINCALLCONVEN CyReadUserFlash (
CY_HANDLE handle, /*Valid device handle*/
CY_DATA_BUFFER *readBuffer, /*data buffer containing buffer address, length to write*/
UINT32 flashAddress, /*Address to the data is written*/
UINT32 ioTimeout /*Timeout value of the API*/
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
if (readBuffer == NULL)
return CY_ERROR_INVALID_PARAMETER;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_READ_USER_FLASH_CMD;
wValue = 0;
wIndex = flashAddress;
wLength = readBuffer->length;
CY_DEBUG_PRINT_INFO ("CY:The Length is %d , Value is %d and index is %d\n", wLength, wValue, wIndex);
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, readBuffer->buffer, wLength, ioTimeout);
if (rStatus > 0){
(readBuffer->transferCount) = rStatus;
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
(readBuffer->transferCount) = 0;
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
(readBuffer->transferCount) = 0;
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This API is used to get the signature of the device. It would be CYUS when we are in actual device mode
and CYBL when we are bootloader mode
*/
CY_RETURN_STATUS CyGetSignature (
CY_HANDLE handle,
UCHAR *signature
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_GET_SIGNATURE_CMD;
wValue = 0x00;
wIndex = 0x00;
wLength = CY_GET_SIGNATURE_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)signature, wLength, ioTimeout);
if (rStatus > 0){
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
+130
View File
@@ -0,0 +1,130 @@
/*
* PHDC routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
/*
PHDC clear feature
*/
CY_RETURN_STATUS CyPhdcClrFeature (CY_HANDLE handle)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_PHDC_CLR_FEATURE;
wValue = CY_PHDC_CLR_FEATURE_WVALUE;
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == 0)
return CY_SUCCESS;
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
return CY_SUCCESS;
}
/*
PHDC set feature
*/
CY_RETURN_STATUS CyPhdcSetFeature (CY_HANDLE handle)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_PHDC_SET_FEATURE;
wValue = CY_PHDC_SET_FEATURE_WVALUE;
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == 0)
return CY_SUCCESS;
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
PHDC clear feature
*/
CY_RETURN_STATUS CyPhdcGetStatus (CY_HANDLE handle, UINT16 *dataStatus)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
bmRequestType = CY_CLASS_INTERFACE_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_PHDC_GET_DATA_STATUS;
wValue = 0x00;
wIndex = device->interfaceNum;
wLength = CY_PHDC_GET_STATUS_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)dataStatus, wLength, ioTimeout);
if (rStatus > 0)
return CY_SUCCESS;
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..function is %s \n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ...libusb error is %d!\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
+644
View File
@@ -0,0 +1,644 @@
/*
* SPI routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
#include <signal.h>
#include <sys/time.h>
#pragma pack (1)
typedef struct args {
CY_HANDLE handle;
UCHAR *readBuffer;
UINT32 length;
UINT32 ioTimeout;
CY_RETURN_STATUS rStatus;
UINT32 transferCount;
}args;
typedef struct
{
UINT32 frequency;
UINT8 dataWidth;
UCHAR mode;
UCHAR xferMode;
BOOL isMsbFirst;
BOOL isMaster;
BOOL isContinuous;
BOOL isSelectPrecede;
BOOL cpha;
BOOL cpol;
BOOL isLoopback;
UCHAR reserved[2];
} CyUsSpiConfig_t;
#pragma pack()
struct timeval startSpiTimeWrite, endSpiTimeWrite, startSpiTimeRead, endSpiTimeRead;
//Timer helper functions for proper timing
void startSpiTick (bool isWrite) {
if (isWrite)
gettimeofday (&startSpiTimeWrite, NULL);
else
gettimeofday (&startSpiTimeRead, NULL);
}
UINT32 getSpiLapsedTime (bool isWrite){
signed int currentTime_sec, currentTime_usec, currentTime;
if (isWrite){
gettimeofday (&endSpiTimeWrite, NULL);
currentTime_sec = (endSpiTimeWrite.tv_sec - startSpiTimeWrite.tv_sec) * 1000;
currentTime_usec = ((endSpiTimeWrite.tv_usec - startSpiTimeWrite.tv_usec)) / 1000;
currentTime = currentTime_sec + currentTime_usec;
return (unsigned int)currentTime;
}
else{
gettimeofday (&endSpiTimeRead, NULL);
currentTime_sec = (endSpiTimeRead.tv_sec - startSpiTimeRead.tv_sec) * 1000;
currentTime_usec = ((endSpiTimeRead.tv_usec - startSpiTimeRead.tv_usec)) / 1000;
currentTime = currentTime_sec + currentTime_usec;
return (unsigned int)currentTime;
}
}
/*
This API gets the current SPI config
for the particluar interface of the device
*/
CY_RETURN_STATUS CyGetSpiConfig (
CY_HANDLE handle,
CY_SPI_CONFIG *spiConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT16 bmRequestType, bmRequest;
CyUsSpiConfig_t localSpiConfig;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
UINT8 scbIndex = 0;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (spiConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not spi ..Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (device->interfaceNum > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_SPI_GET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0;
wLength = CY_SPI_CONFIG_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localSpiConfig, wLength, ioTimeout);
if (rStatus == CY_SPI_CONFIG_LEN){
//CY_DUMP_DATA ((unsigned char*)&localSpiConfig, wLength);
CY_DEBUG_PRINT_INFO ("CY: Read SPI config successfully %d\n", rStatus);
spiConfig->frequency = localSpiConfig.frequency;
spiConfig->dataWidth = localSpiConfig.dataWidth;
spiConfig->protocol = localSpiConfig.mode;
spiConfig->isMsbFirst = localSpiConfig.isMsbFirst;
spiConfig->isMaster = localSpiConfig.isMaster;
spiConfig->isContinuousMode = localSpiConfig.isContinuous;
spiConfig->isSelectPrecede = localSpiConfig.isSelectPrecede;
spiConfig->isCpha = localSpiConfig.cpha;
spiConfig->isCpol = localSpiConfig.cpol;
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ... Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s...libusb error is %d !\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This API sets SPI config of the device for that
interface
*/
CY_RETURN_STATUS CySetSpiConfig (
CY_HANDLE handle,
CY_SPI_CONFIG *spiConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
CyUsSpiConfig_t localSpiConfig;
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
UINT8 scbIndex = 0;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (spiConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not spi ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (spiConfig->frequency < 1000 || spiConfig->frequency > 3000000){
CY_DEBUG_PRINT_ERROR ("CY:Error frequency trying to set in out of range ... Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (spiConfig->protocol == CY_SPI_TI){
if (!(spiConfig->isCpol == false && spiConfig->isCpha == true && spiConfig->isContinuousMode == false)){
CY_DEBUG_PRINT_ERROR ("CY:Error ... Wrong configuration for SPI TI mode \n");
return CY_ERROR_REQUEST_FAILED;
}
}
if (spiConfig->protocol == CY_SPI_NS){
if (!(spiConfig->isCpol == false && spiConfig->isCpha == false && spiConfig->isSelectPrecede == false)){
CY_DEBUG_PRINT_ERROR ("CY:Error ... Wrong configuration for SPI ti mode \n");
return CY_ERROR_REQUEST_FAILED;
}
}
else{
if (spiConfig->isSelectPrecede != false){
CY_DEBUG_PRINT_ERROR ("CY:Error ... Wrong configuration for SPI motorola mode \n");
return CY_ERROR_REQUEST_FAILED;
}
}
if (device->interfaceNum > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SPI_SET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0;
wLength = CY_SPI_CONFIG_LEN;
//We will not expose all the spi config structure elements to user.
//Fill in rest of the values.
memset (&localSpiConfig, 0, CY_SPI_CONFIG_LEN);
localSpiConfig.frequency = spiConfig->frequency;
localSpiConfig.dataWidth = spiConfig->dataWidth;
localSpiConfig.mode = spiConfig->protocol;
localSpiConfig.isMsbFirst = spiConfig->isMsbFirst;
localSpiConfig.isMaster = spiConfig->isMaster;
localSpiConfig.isContinuous = spiConfig->isContinuousMode;
localSpiConfig.isSelectPrecede = spiConfig->isSelectPrecede;
localSpiConfig.cpha = spiConfig->isCpha;
localSpiConfig.cpol = spiConfig->isCpol;
//CY_DUMP_DATA ((unsigned char*)&localSpiConfig, wLength);
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localSpiConfig, wLength, ioTimeout);
if (rStatus == CY_SPI_CONFIG_LEN){
CY_DEBUG_PRINT_INFO ("CY: Setting SPI config success ...\n");
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Time out error ..Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in function %s ... !libusb error is %d\n", __func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*Api will reset the spi module*/
CY_RETURN_STATUS CySpiReset (CY_HANDLE handle)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength, bmRequestType, bmRequest;;
UINT16 scbIndex = 0;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not spi ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_SPI_RESET_CMD;
wValue = ((scbIndex << CY_SCB_INDEX_POS));
wIndex = 0;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY:Error in sending spi reset command...Libusb error is %d\n", rStatus);
return rStatus;
}
return CY_SUCCESS;
}
/*
This API reads SPI data from the specified interface of the device
interface
*/
static void LIBUSB_CALL spi_read_cb(struct libusb_transfer *transfer)
{
UINT32 *completed = transfer->user_data;
*completed = 1;
}
//We adopted for async method here because there are 2 thread polling same fd
// i.e both read and write are polling same fd when one event triggers and other one is
//not completed then another thread will wait for more than 60sec.
CY_RETURN_STATUS CySpiRead (
CY_HANDLE handle,
CY_DATA_BUFFER *readBuffer,
UINT32 ioTimeout
)
{
struct libusb_transfer *readTransfer;
CY_DEVICE *device;
libusb_device_handle *devHandle;
int readCompleted = 0;
struct timeval time;
int r;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
readBuffer->transferCount = 0;
readTransfer = libusb_alloc_transfer(0);
if (readTransfer == NULL){
CY_DEBUG_PRINT_ERROR("CY:Error in allocating transfers \n");
return CY_ERROR_ALLOCATION_FAILED;
}
libusb_fill_bulk_transfer(readTransfer, devHandle, device->inEndpoint, readBuffer->buffer, readBuffer->length,
spi_read_cb, &readCompleted, ioTimeout);
libusb_submit_transfer (readTransfer);
time.tv_sec = (ioTimeout / 1000);
time.tv_usec = ((ioTimeout % 1000) * 1000);//polling timeout.
while (readCompleted == 0){
r = libusb_handle_events_timeout_completed(glContext, &time, &readCompleted);
if (r < 0) {
if (r == LIBUSB_ERROR_INTERRUPTED)
continue;
libusb_cancel_transfer(readTransfer);
while (!readCompleted)
if (libusb_handle_events_completed(glContext, &readCompleted) < 0)
break;
readBuffer->transferCount = readTransfer->actual_length;
libusb_free_transfer(readTransfer);
return r;
}
}
if (readTransfer->status == LIBUSB_TRANSFER_COMPLETED){
readBuffer->transferCount = readTransfer->actual_length;
libusb_free_transfer (readTransfer);
return CY_SUCCESS;
}
else{
if (readTransfer->status == LIBUSB_TRANSFER_TIMED_OUT){
//We should not be hitting this case.. As the time out is infinite!!
CY_DEBUG_PRINT_ERROR ("CY:Timeout error in doing SPI read/write .... %d Libusb errors %d\n",
readTransfer->actual_length,readTransfer->status);
readBuffer->transferCount = readTransfer->actual_length;
CySpiReset (handle);
libusb_free_transfer (readTransfer);
return CY_ERROR_IO_TIMEOUT;
}
if (readTransfer->status == LIBUSB_TRANSFER_OVERFLOW){
//Need to handle this properly!
CY_DEBUG_PRINT_ERROR ("CY:OverFlow error in doing SPI read/write .... Libusb errors %d %d \n",
readTransfer->status, readTransfer->actual_length);
readBuffer->transferCount = readTransfer->actual_length;
CySpiReset (handle);
libusb_free_transfer (readTransfer);
return CY_ERROR_BUFFER_OVERFLOW;
}
if (readTransfer->status != LIBUSB_TRANSFER_COMPLETED){
CY_DEBUG_PRINT_ERROR ("CY:Error in doing SPI read/write .... Libusb errors are %d %d\n",
readTransfer->status, readTransfer->actual_length);
readBuffer->transferCount = readTransfer->actual_length;
CySpiReset (handle);
libusb_free_transfer (readTransfer);
//If timer is not completed then it implies we have timeout error
return CY_ERROR_REQUEST_FAILED;
}
}
return CY_ERROR_REQUEST_FAILED;
}
/*Internal SPI get status API for Write operation*/
CY_RETURN_STATUS CyGetSpiStatus (CY_HANDLE handle,
int *spiStatus
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT16 wValue, wIndex, wLength, bmRequestType, bmRequest;;
UINT16 scbIndex = 0;
UINT32 ioTimeout = 0;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not spi ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
scbIndex = device->interfaceNum;
if (scbIndex > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_SPI_GET_STATUS_CMD;
wValue = ((scbIndex << CY_SCB_INDEX_POS));
wIndex = 0;
wLength = CY_SPI_GET_STATUS_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,wValue, wIndex, (UCHAR*)spiStatus, wLength, ioTimeout);
if (rStatus < CY_SPI_GET_STATUS_LEN){
CY_DEBUG_PRINT_INFO ("CY:Error in sending spi Get Status command...Libusb error is %d\n", rStatus);
return rStatus;
}
return CY_SUCCESS;
}
/* Function to write on to SPI alone*/
CY_RETURN_STATUS CySpiWrite (
CY_HANDLE handle,
CY_DATA_BUFFER *writeBuffer,
UINT32 ioTimeout
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
int spiStatus = 1;
UINT32 newIoTimeout = ioTimeout, elapsedTime = 0, loopCount = 1;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error device type is not spi ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
startSpiTick (true);
rStatus = libusb_bulk_transfer (devHandle, device->outEndpoint, writeBuffer->buffer, writeBuffer->length,
(int*)&(writeBuffer->transferCount), newIoTimeout);
elapsedTime = getSpiLapsedTime(true);
newIoTimeout = ioTimeout - elapsedTime;
//because we have a sleep of 1 msec after every getstatus
if (newIoTimeout)
loopCount = (newIoTimeout);
if (rStatus == LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY: Successfully written SPI data.. %d bytes Read ...\n", writeBuffer->transferCount);
while (loopCount){
usleep (1000);
rStatus = CyGetSpiStatus (handle, &spiStatus);
if (rStatus == CY_SUCCESS){
if (spiStatus == 0){
return CY_SUCCESS;
}
}
else {
//Should never hit this case
CY_DEBUG_PRINT_ERROR ("CY:Error in getting spi status \n");
return CY_ERROR_REQUEST_FAILED;
}
if (ioTimeout)
loopCount--;
}
if (loopCount == 0 && spiStatus > 0){
writeBuffer->length = 0;
CySpiReset (handle);
return CY_ERROR_IO_TIMEOUT;
}
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:Error TimeOut ...function is %s\n", __func__);
CySpiReset (handle);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_ERROR ("CY:Error Pipe error..function is %s\n", __func__);
CySpiReset (handle);
CyResetPipe (handle, device->outEndpoint);
return CY_ERROR_PIPE_HALTED;
}
else if (rStatus == LIBUSB_ERROR_OVERFLOW){
CY_DEBUG_PRINT_ERROR ("CY:Error Buffer Overflow...function is %s\n", __func__);
return CY_ERROR_BUFFER_OVERFLOW;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY:Error Device Disconnected ...function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error in writing SPI data ...Libusb Error is %d and bytes read is %d!\n", rStatus, writeBuffer->transferCount);
return CY_ERROR_REQUEST_FAILED;
}
return CY_ERROR_REQUEST_FAILED;
}/*
API to wrap up the data
*/
void spiCollectData (void *inputParameters) {
UINT32 readLength = 0, length;
CY_DATA_BUFFER readBuffer;
args *inputData = (args *) inputParameters;
UCHAR *buffer;
CY_RETURN_STATUS rStatus = CY_SUCCESS;
buffer = readBuffer.buffer = inputData->readBuffer;
length = readBuffer.length = inputData->length;
CY_HANDLE handle = inputData->handle;
int newTimeout = inputData->ioTimeout, elapsedTime;
while (readLength != length && newTimeout >= 0 && rStatus == CY_SUCCESS){
//Get current time
//Buffer is pointing to next address where we are suppose to fill the data
readBuffer.buffer = &buffer[readLength];
//Updated length which total length minus the total length of data read
readBuffer.length = length - readLength;
//Libusb fix for mac os!!
//ISSUE:when api times out in MAC it comes back and say read length = 0!!
#ifdef __APPLE__
if (readBuffer.length > 64)
readBuffer.length = 64;
#endif
startSpiTick (false);
rStatus = CySpiRead (handle, &readBuffer, newTimeout);
elapsedTime = getSpiLapsedTime (false);
//Do this only when newTimeout is non zero
if (newTimeout){
newTimeout = newTimeout - elapsedTime;
//If timeout is 0 then libusb considers that as infinite
//So forcefully make the loop to comeout
if (newTimeout <= 0)
rStatus = CY_ERROR_IO_TIMEOUT;
}
if (rStatus != CY_SUCCESS){
readLength += readBuffer.transferCount;
break;
}
readLength += readBuffer.transferCount;
}
if (readLength != length && rStatus == CY_ERROR_IO_TIMEOUT){
CySpiReset (handle);
}
inputData->transferCount = readLength;
inputData->rStatus = rStatus;
}
/*
* Api used to do read as well as write on spi
*/
CY_RETURN_STATUS CySpiReadWrite (CY_HANDLE handle,
CY_DATA_BUFFER *readBuffer,
CY_DATA_BUFFER *writeBuffer,
UINT32 ioTimeout)
{
struct args threadParameter;
UINT32 ret;
pthread_t readThreadID;
CY_DEVICE *device;
libusb_device_handle *devHandle;
CY_RETURN_STATUS rStatus;
unsigned short spiTransferMode = 0, scbIndex = 0;
UINT16 wValue, wIndex = 0, wLength;
UINT16 bmRequestType, bmRequest;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle.. Function is %s \n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (readBuffer == NULL && writeBuffer == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_SPI) {
CY_DEBUG_PRINT_ERROR ("CY:Error opened device is not spi .. \n");
return CY_ERROR_REQUEST_FAILED;
}
//Set both the bits and change it accordingly based on parameters parameters
spiTransferMode |= ((CY_SPI_READ_BIT) | (CY_SPI_WRITE_BIT));
if ((readBuffer == NULL || readBuffer->length == 0 || readBuffer->buffer == NULL))
spiTransferMode &= ~(CY_SPI_READ_BIT);
if ((writeBuffer == NULL || writeBuffer->length == 0 || writeBuffer->buffer == NULL))
spiTransferMode &= ~(CY_SPI_WRITE_BIT);
//if none of the bit is set it implies parameters sent is wrong
if (spiTransferMode == 0){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter.. Function is %s \n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (device->interfaceNum > 0)
scbIndex = 1;
//In read only case we take length to be equal to readBuffer length.
//But in write or in write/read case we take length = writeBuffer length.
if (spiTransferMode == 0x1)
wIndex = readBuffer->length;
else
wIndex = writeBuffer->length;
spiTransferMode |= (scbIndex << 15);
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SPI_READ_WRITE_CMD;
wValue = (spiTransferMode);
wLength = 0;
if (pthread_mutex_trylock (&device->writeLock) == 0){
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, 5000);
if (rStatus){
CY_DEBUG_PRINT_ERROR ("CY:Error Sending spi read write vendor command failed ... Libusb error is %d\n", rStatus);
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_REQUEST_FAILED;
}
//Read Bit is not set then write Only
if (!(spiTransferMode & CY_SPI_READ_BIT)) {
writeBuffer->transferCount = 0;
if (readBuffer)
readBuffer->transferCount = 0;
rStatus = CySpiWrite (handle, writeBuffer, ioTimeout);
pthread_mutex_unlock (&device->writeLock);
return rStatus;
}
//Write Bit is not set then read only
if (!(spiTransferMode & CY_SPI_WRITE_BIT)) {
// We are starting a thread so that we can collect all the data
// FIX for short length packet issue on SPI.
readBuffer->transferCount = 0;
if (writeBuffer)
writeBuffer->transferCount = 0;
threadParameter.handle = handle;
threadParameter.readBuffer = readBuffer->buffer;
threadParameter.length = readBuffer->length;
threadParameter.ioTimeout = ioTimeout;
ret = pthread_create (&readThreadID, NULL, (void *)spiCollectData, (void *)&threadParameter);
if (ret){
CY_DEBUG_PRINT_ERROR ("CY:Error in creating read thread ... Reading failed \n");
pthread_mutex_unlock (&device->writeLock);
readBuffer->transferCount = 0;
return CY_ERROR_REQUEST_FAILED;
}
pthread_join (readThreadID, NULL);
readBuffer->transferCount = threadParameter.transferCount;
pthread_mutex_unlock (&device->writeLock);
return threadParameter.rStatus;
}
writeBuffer->transferCount = 0;
readBuffer->transferCount = 0;
threadParameter.handle = handle;
threadParameter.readBuffer = readBuffer->buffer;
threadParameter.length = readBuffer->length;
threadParameter.ioTimeout = ioTimeout;
ret = pthread_create (&readThreadID, NULL, (void *)spiCollectData, (void *)&threadParameter);
if (ret){
CY_DEBUG_PRINT_ERROR ("CY:Error in creating read thread ... Reading failed \n");
readBuffer->transferCount = 0;
pthread_mutex_unlock (&device->writeLock);
return CY_ERROR_REQUEST_FAILED;
}
rStatus = CySpiWrite (handle, writeBuffer, ioTimeout);
if (rStatus == CY_SUCCESS) {
pthread_join (readThreadID, NULL);
rStatus = threadParameter.rStatus;
readBuffer->transferCount = threadParameter.transferCount;
}
else {
pthread_join (readThreadID, NULL);
readBuffer->transferCount = threadParameter.transferCount;
}
pthread_mutex_unlock (&device->writeLock);
return rStatus;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error API busy in service previous request ... Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
return rStatus;
}
+588
View File
@@ -0,0 +1,588 @@
/*
* UART routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
#pragma pack(1)
typedef struct {
CY_UART_BAUD_RATE baudRate;
UINT8 pinType;
UINT8 dataWidth;
UINT8 stopBits;
UINT8 mode;
UINT8 parity;
UINT8 isMsbFirst;
UINT8 txRetry;
UINT8 rxInvertPolarity;
UINT8 rxIgnoreError;
UINT8 isFlowControl;
UINT8 isLoopBack;
UINT8 flags;
}CyUsUartConfig_t;
#pragma pack()
//Timer helper functions for proper timing
UINT32 getUartLapsedTime (struct timeval startTime){
signed int currentTime_sec, currentTime_usec, currentTime;
struct timeval endTime;
gettimeofday (&endTime, NULL);
currentTime_sec = (endTime.tv_sec - startTime.tv_sec) * 1000;
currentTime_usec = ((endTime.tv_usec - startTime.tv_usec)) / 1000;
currentTime = currentTime_sec + currentTime_usec;
return (unsigned int)currentTime;
}
/*
This API gets the current UART configuration of the
device.Such as GPIO's assigned, flowcontrol, BaudRate
etc.
*/
CY_RETURN_STATUS CyGetUartConfig (
CY_HANDLE handle,
PCY_UART_CONFIG uartConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CY_DEVICE *device;
CyUsUartConfig_t localUartConfig;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
UINT8 scbIndex = 0;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (uartConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameter..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (device->interfaceNum > 0)
scbIndex = 1;
bmRequestType = CY_VENDOR_REQUEST_DEVICE_TO_HOST;
bmRequest = CY_UART_GET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0;
wLength = CY_UART_CONFIG_LEN;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localUartConfig, wLength, ioTimeout);
//Since we are not exposing all the configuration elements
//parse and fill only relevant elements.
if (rStatus == CY_UART_CONFIG_LEN){
uartConfig->dataWidth = localUartConfig.dataWidth;
uartConfig->baudRate = localUartConfig.baudRate;
uartConfig->stopBits = localUartConfig.stopBits;
uartConfig->parityMode = (CY_UART_PARITY_MODE)localUartConfig.parity;;
uartConfig->isDropOnRxErrors = localUartConfig.rxIgnoreError;
//We are currently ignoring rest of the bits
CY_DEBUG_PRINT_INFO ("CY:Successfully read UART Config\n");
return CY_SUCCESS;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error in reading UART config ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This API sets the current UART configuration of the
device.Such as GPIO's assigned, flowcontrol, BaudRate
etc.
*/
CY_RETURN_STATUS CySetUartConfig (
CY_HANDLE handle,
CY_UART_CONFIG *uartConfig
)
{
UINT16 wValue, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
CyUsUartConfig_t localUartConfig;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
UINT8 scbIndex = 0;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (uartConfig == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameter..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (uartConfig->dataWidth < 7 || uartConfig->dataWidth > 8){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameter..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (uartConfig->stopBits < 1 || uartConfig->stopBits > 2){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameter..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->interfaceNum > 0)
scbIndex = 1;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_UART_SET_CONFIG_CMD;
wValue = (scbIndex << CY_SCB_INDEX_POS);
wIndex = 0;
wLength = CY_UART_CONFIG_LEN;
memset (&localUartConfig, 0, CY_UART_CONFIG_LEN);
//Fill in rest of the UART config structure elements
//that are not exposed in API with default values
localUartConfig.baudRate = uartConfig->baudRate;
localUartConfig.dataWidth = uartConfig->dataWidth;
localUartConfig.stopBits = uartConfig->stopBits;
localUartConfig.parity = (UCHAR) uartConfig->parityMode;
localUartConfig.rxIgnoreError = uartConfig->isDropOnRxErrors;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, (unsigned char*)&localUartConfig, wLength, ioTimeout);
if (rStatus == CY_UART_CONFIG_LEN){
CY_DEBUG_PRINT_INFO ("CY:Successfully Set UART Config \n");
return CY_SUCCESS;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error in Setting UART config ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api writes the Data to UART block of the
device.
*/
CY_RETURN_STATUS CyUartWrite (
CY_HANDLE handle,
CY_DATA_BUFFER* writeBuffer,
unsigned int ioTimeOut
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((writeBuffer == NULL) || (writeBuffer->buffer == NULL) || (writeBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameters..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
writeBuffer->transferCount = 0;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
rStatus = libusb_bulk_transfer (devHandle, device->outEndpoint, writeBuffer->buffer, writeBuffer->length,
(int *)&((writeBuffer->transferCount)), ioTimeOut);
if (rStatus == CY_SUCCESS) {
CY_DEBUG_PRINT_INFO ("CY: SuccessFull in Wrting Data,%d bytes were transfered \n", (writeBuffer->transferCount));
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
CY_DEBUG_PRINT_ERROR ("CY:TimeOut error ...Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
CY_DEBUG_PRINT_ERROR ("CY:Pipe error endpoint Halted ...Function is %s\n", __func__);
CyResetPipe (handle, device->outEndpoint);
return CY_ERROR_PIPE_HALTED;
}
else if (rStatus == LIBUSB_ERROR_OVERFLOW){
CY_DEBUG_PRINT_ERROR ("CY:Error Buffer Overflow occured ...Function is %s\n", __func__);
return CY_ERROR_BUFFER_OVERFLOW;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected .... Function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Unknown error ....Libusb error is %d Function is %s\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api Reads the Data from UART block of the
device.
*/
CY_RETURN_STATUS CyUartRead (
CY_HANDLE handle,
CY_DATA_BUFFER* readBuffer,
unsigned int ioTimeOut
)
{
int rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
UINT32 length, totalRead = 0, newIoTimeout = ioTimeOut, elapsedTime;
int transferCount;
UCHAR *buffer;
struct timeval startTime;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if ((readBuffer == NULL) || (readBuffer->buffer == NULL) || (readBuffer->length == 0)){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameters..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
length = readBuffer->length;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
readBuffer->transferCount = 0;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
//Collect all the data in low baud rate for uart. As we get data in short packet
do {
// buffer will be pointing to new pointer
buffer = &(readBuffer->buffer[totalRead]);
//Start the tick
gettimeofday(&startTime, NULL);
rStatus = libusb_bulk_transfer (devHandle, device->inEndpoint, buffer, length,
&transferCount, newIoTimeout);
elapsedTime = getUartLapsedTime(startTime);
//Get the new timeout.
newIoTimeout = newIoTimeout - elapsedTime;
//Initialise totalRead to initially read + bytes returned now
totalRead += transferCount;
//length will initial length - transferCount
length = (length - transferCount);
}while ((rStatus == CY_SUCCESS) && (totalRead != readBuffer->length) && (newIoTimeout > 0));
if (newIoTimeout <= 0 && totalRead != readBuffer->length)
rStatus = LIBUSB_ERROR_TIMEOUT;
if (rStatus == CY_SUCCESS){
//CY_DUMP_DATA (readBuffer->buffer, readBuffer->transferCount);
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_INFO ("CY: SuccessFull in Reading Data,%d bytes were transfered \n", (readBuffer->transferCount));
return CY_SUCCESS;
}
else if (rStatus == LIBUSB_ERROR_TIMEOUT){
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_ERROR ("CY:TimeOut error... Function is %s\n", __func__);
return CY_ERROR_IO_TIMEOUT;
}
else if (rStatus == LIBUSB_ERROR_PIPE){
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_ERROR ("CY:Pipe error endpoint Halted ...Function is %s\n", __func__);
CyResetPipe (handle, device->inEndpoint);
return CY_ERROR_PIPE_HALTED;
}
else if (rStatus == LIBUSB_ERROR_OVERFLOW){
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_ERROR ("CY:Error Buffer Overflow occured ...Function is %s\n", __func__);
return CY_ERROR_BUFFER_OVERFLOW;
}
else if (rStatus == LIBUSB_ERROR_NO_DEVICE) {
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_ERROR ("CY: Device Disconnected ....Function is %s\n", __func__);
return CY_ERROR_DEVICE_NOT_FOUND;
}
else {
readBuffer->transferCount = totalRead;
CY_DEBUG_PRINT_ERROR ("CY: Unknown error ....Libusb error is %d Function is %s\n", rStatus, __func__);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api sets the hardware flow control
*/
CY_RETURN_STATUS CyUartSetHwFlowControl (
CY_HANDLE handle,
CY_FLOW_CONTROL_MODES mode
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (mode > 3){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid parameter..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_UART_SET_FLOW_CONTROL_CMD;
wValue |= mode;
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus < 0){
CY_DEBUG_PRINT_ERROR ("CY:Error in setting uart flow control ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
device->uartFlowControlMode = mode;
return CY_SUCCESS;
}
/*
Api gets the current flow control mode
*/
CY_RETURN_STATUS CyUartGetHwFlowControl (
CY_HANDLE handle,
CY_FLOW_CONTROL_MODES *mode
)
{
CY_DEVICE *device;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
if (mode == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid input parameters..Function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
device = (CY_DEVICE *)handle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
(*mode) = device->uartFlowControlMode;
return CY_SUCCESS;
}
/* The API is used to break
*/
CYWINEXPORT CY_RETURN_STATUS CyUartSetBreak(
CY_HANDLE handle, /*Valid handle to communicate with device*/
UINT16 timeout /*Break timeout value in milliseconds */
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_UART_SEND_BREAK_CMD;
wValue = timeout;
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in setting break ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
return CY_SUCCESS;
}
/*
This Api sets the RTS UART pins High
*/
CY_RETURN_STATUS CyUartSetRts (
CY_HANDLE handle
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus;
UINT32 ioTimeout = CY_USB_SERIAL_TIMEOUT;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SET_LINE_CONTROL_STATE_CMD;
wValue |= (1 << 1) | (device->dtrValue);
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == CY_SUCCESS){
device->rtsValue = 1;
return CY_SUCCESS;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error in setting RTS of UART ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api clears the RTS UART pin and makes it low
*/
CY_RETURN_STATUS CyUartClearRts (
CY_HANDLE handle
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SET_LINE_CONTROL_STATE_CMD;
wValue = (device->dtrValue);
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == CY_SUCCESS){
device->rtsValue = 0;
return CY_SUCCESS;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error in clearing RTS of UART ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api sets the DTR UART pin High
*/
CY_RETURN_STATUS CyUartSetDtr (
CY_HANDLE handle
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SET_LINE_CONTROL_STATE_CMD;
wValue = ((device->rtsValue) << 1) | 1;
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == CY_SUCCESS){
device->dtrValue = 1;
return CY_SUCCESS;
}
else {
CY_DEBUG_PRINT_ERROR ("CY:Error in setting DTR of UART ... Libusb Error is %d \n", rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
/*
This Api clears the DTR UART pin and makes it low
*/
CY_RETURN_STATUS CyUartClearDtr (
CY_HANDLE handle
)
{
UINT16 wValue = 0, wIndex, wLength;
UINT8 bmRequestType, bmRequest;
int rStatus, ioTimeout = CY_USB_SERIAL_TIMEOUT ;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid handle..Function is %s\n", __func__);
return CY_ERROR_INVALID_HANDLE;
}
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType != CY_TYPE_UART){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device type needs to be uart..Function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
bmRequestType = CY_CLASS_INTERFACE_REQUEST_HOST_TO_DEVICE;
bmRequest = CY_SET_LINE_CONTROL_STATE_CMD;
wValue = ((device->rtsValue) << 1);
wIndex = device->interfaceNum;
wLength = 0;
rStatus = libusb_control_transfer (devHandle, bmRequestType, bmRequest,
wValue, wIndex, NULL, wLength, ioTimeout);
if (rStatus == CY_SUCCESS){
device->dtrValue = 0;
return CY_SUCCESS;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error in function %s... Libusb Error is %d \n",__func__, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
}
+638
View File
@@ -0,0 +1,638 @@
/*
* USB routines of Cypress USB Serial
* Copyright (C) 2013 Cypress Semiconductor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CyUSBCommon.h"
libusb_context *glContext = NULL;
static bool glDriverInit = false;
static libusb_device **glDeviceList;
static int glNumDevices;
/*The API initializes the Libusb library
*/
pthread_mutex_t criticalSection;
CY_RETURN_STATUS CyLibraryInit ()
{
int rStatus = LIBUSB_SUCCESS;
if (!glContext)
rStatus = libusb_init (&glContext);
if (glDriverInit != true){
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Driver Init Failed ...\n");
return CY_ERROR_DRIVER_INIT_FAILED;
}
glNumDevices = libusb_get_device_list (glContext, &glDeviceList);
if (glNumDevices < 0){
CY_DEBUG_PRINT_ERROR ("CY:Building device list Failed ...\n");
glNumDevices = -1;
return CY_ERROR_DRIVER_INIT_FAILED;
}
pthread_mutex_init (&criticalSection, NULL);
glDriverInit = true;
return CY_SUCCESS;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error ... library already initialized \n");
return CY_ERROR_DRIVER_INIT_FAILED;
}
}
/*
This API needs to be called after Calling CyGetListofDevices.
*/
CY_RETURN_STATUS CyLibraryExit ()
{
if (glDriverInit == true){
if (glNumDevices >= 0)
libusb_free_device_list (glDeviceList, 1);
if (glContext) {
libusb_exit (glContext);
glContext = NULL;
}
glDriverInit = false;
pthread_mutex_destroy (&criticalSection);
return CY_SUCCESS;
}
CY_DEBUG_PRINT_ERROR ("CY:Error ... Library not initialized \n");
return CY_ERROR_REQUEST_FAILED;
}
/*
* This function Gets the number of all the devices currently
* Connected to the host (It includes Cypress Device as well as
* no Cypress Devices connected)
*/
CY_RETURN_STATUS CyGetListofDevices (
UINT8 *numDevices
)
{
// Use this variable to call libusb_close and exit of the application
if (numDevices == NULL)
return CY_ERROR_INVALID_PARAMETER;
if (!glDriverInit){
CY_DEBUG_PRINT_ERROR ("CY:Error Library not initialised ...function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
pthread_mutex_lock (&criticalSection);
libusb_free_device_list (glDeviceList, 1);
glNumDevices = (*numDevices) = libusb_get_device_list (glContext, &glDeviceList);
pthread_mutex_unlock (&criticalSection);
if (glNumDevices < 0){
CY_DEBUG_PRINT_ERROR ("CY:Building device list Failed ...function is %s\n", __func__);
glNumDevices = -1;
(*numDevices) = -1;
return CY_ERROR_REQUEST_FAILED;
}
return CY_SUCCESS;
}
/* This function gets all the neccessary info such as VID,PID,
String Descriptors and if is a cypress serial device you will
get the info on class and device type
*/
CY_RETURN_STATUS CyGetDeviceInfo (
UINT8 deviceNumber,
CY_DEVICE_INFO *deviceInfo
)
{
struct libusb_device_descriptor deviceDesc;
int rStatus;
UINT32 numInterfaces;
UINT8 iManufacturer, iProduct, iSerial;
libusb_device *usbDevice;;
struct libusb_config_descriptor *configDesc;
libusb_device_handle *devHandle;
// Get the list of descriptor info for the device
if (glDriverInit == false){
CY_DEBUG_PRINT_ERROR ("CY:Error Library not initialised ...function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (deviceInfo == NULL)
return CY_ERROR_INVALID_PARAMETER;
pthread_mutex_lock (&criticalSection);
if (deviceNumber >= glNumDevices){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device number... \n");
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_INVALID_PARAMETER;
}
usbDevice = glDeviceList[deviceNumber];
rStatus = libusb_get_device_descriptor (usbDevice, &deviceDesc);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error ... unable to retrieve device descriptor \n");
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
deviceInfo->vidPid.vid = deviceDesc.idVendor;
deviceInfo->vidPid.pid = deviceDesc.idProduct;
// Get the all the index of the String descriptors so that it can be used
// to retrieve the string descriptor info.
iManufacturer = deviceDesc.iManufacturer;
iProduct = deviceDesc.iProduct;
iSerial = deviceDesc.iSerialNumber;
//Get the Device handle so that we can communicate with the device retreiving
// descriptor info
deviceInfo->manufacturerName[0] = '\0';
deviceInfo->productName[0] = '\0';
deviceInfo->serialNum[0] = '\0';
rStatus = libusb_open (usbDevice, &devHandle);
if (rStatus == LIBUSB_ERROR_ACCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error ...Insufficient permission... Libusb error is %d \n", rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_ACCESS_DENIED;
}
else if (rStatus != CY_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in opening the device... Libusb error is %d \n", rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
if (iManufacturer > 0){
rStatus = libusb_get_string_descriptor_ascii (devHandle, iManufacturer, deviceInfo->manufacturerName, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Getting Manufacturer name Error is <%x> \n", rStatus);
}
}
if (iProduct > 0){
rStatus = libusb_get_string_descriptor_ascii (devHandle, iProduct, deviceInfo->productName, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Getting product name Error is <%d> \n", rStatus);
}
}
if (iSerial > 0){
rStatus = libusb_get_string_descriptor_ascii (devHandle, iSerial, deviceInfo->serialNum, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Getting Serial name <%d>\n", rStatus);
}
}
rStatus = libusb_get_config_descriptor (usbDevice, 0, &configDesc);
if (rStatus == LIBUSB_SUCCESS){
UINT32 index_i = 0;
const struct libusb_interface *interface;
numInterfaces = configDesc->bNumInterfaces;
deviceInfo->numInterfaces = numInterfaces;
interface = configDesc->interface;
while ((numInterfaces) && (index_i < CY_MAX_DEVICE_INTERFACE)){
deviceInfo->deviceClass[index_i] = (CY_DEVICE_CLASS)interface->altsetting->bInterfaceClass;
if (deviceInfo->deviceClass[index_i] == CY_CLASS_VENDOR){
deviceInfo->deviceType[index_i] = (CY_DEVICE_TYPE)interface->altsetting->bInterfaceSubClass;
}
else
deviceInfo->deviceType[index_i] = CY_TYPE_DISABLED;
index_i++;
numInterfaces--;
interface++;
}
libusb_free_config_descriptor(configDesc);
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in Getting config descriptor ...Libusb error is %d \n", rStatus);
if (devHandle)
libusb_close (devHandle);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
if (devHandle)
libusb_close (devHandle);
pthread_mutex_unlock (&criticalSection);
return CY_SUCCESS;
}
/* This function gets all the neccessary info such as VID,PID,
String Descriptors and if is a cypress serial device you will
get the info on class and device type
*/
CY_RETURN_STATUS CyGetDeviceInfoVidPid (
CY_VID_PID vidPid,
UINT8 *deviceNumber,
PCY_DEVICE_INFO deviceInfoList,
UINT8 *deviceCount,
UINT8 infoListLength
)
{
struct libusb_device_descriptor deviceDesc;
int rStatus = CY_ERROR_DRIVER_INIT_FAILED;
UINT32 numInterfaces, index = 0;
int devNum;
UINT8 iManufacturer, iProduct, iSerial;
libusb_device *usbDevice;
struct libusb_config_descriptor *configDesc;
libusb_device_handle *devHandle = NULL;
PCY_DEVICE_INFO deviceInfo;
if (glDriverInit == false){
CY_DEBUG_PRINT_ERROR ("CY:Error Library not initialised ...function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if ((infoListLength) < 1){
CY_DEBUG_PRINT_ERROR ("CY:Error invalid device info list length specified should be > 0 .. function is %s\n", __func__);
return CY_ERROR_INVALID_PARAMETER;
}
if (deviceNumber == NULL || deviceInfoList == NULL || deviceCount == NULL)
return CY_ERROR_INVALID_PARAMETER;
// Get the list of descriptor info for the device
(*deviceCount) = 0;
pthread_mutex_lock (&criticalSection);
for (devNum = 0; devNum < glNumDevices; devNum++) {
//We are making sure that we do not overrun
//the list.
deviceInfo = &(deviceInfoList [index]);
usbDevice = glDeviceList[devNum];
rStatus = libusb_get_device_descriptor (usbDevice, &deviceDesc);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in getting device descriptor for device-%d... Libusb Error is %d \n", devNum, rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
if ((deviceDesc.idVendor != vidPid.vid) || (deviceDesc.idProduct != vidPid.pid)){
continue;
}
(*deviceCount)++;
if (index > infoListLength){
continue;
}
rStatus = libusb_open (usbDevice, &devHandle);
if (rStatus == LIBUSB_ERROR_ACCESS){
CY_DEBUG_PRINT_ERROR ("CY:Insufficient permission ... Libusb error is %d \n", rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_ACCESS_DENIED;
}
else if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Opening the Device ...Error is %d \n", rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
deviceNumber[index] = devNum;
index++;
deviceInfo->vidPid.vid = deviceDesc.idVendor;
deviceInfo->vidPid.pid = deviceDesc.idProduct;
// Get all the index of the String descriptors so that it can be used
// to retrieve the string descriptor info.
iManufacturer = deviceDesc.iManufacturer;
iProduct = deviceDesc.iProduct;
iSerial = deviceDesc.iSerialNumber;
//Get the Device handle so that we can communicate with the device retreiving
// descriptor info
//Initialise manufacturer, product and serial names
deviceInfo->manufacturerName[0] = '\0';
deviceInfo->productName[0] = '\0';
deviceInfo->serialNum[0] = '\0';
if (iManufacturer > 0) {
rStatus = libusb_get_string_descriptor_ascii (devHandle, iManufacturer, deviceInfo->manufacturerName, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY:Error in Getting Manufacturer name Error is <%d> \n",rStatus);
}
}
if (iProduct > 0){
rStatus = libusb_get_string_descriptor_ascii (devHandle, iProduct, deviceInfo->productName, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY:Error in Getting product name Error is <%d> \n", rStatus);
}
}
if (iSerial > 0){
rStatus = libusb_get_string_descriptor_ascii (devHandle, iSerial, deviceInfo->serialNum, 256);
if (rStatus <= LIBUSB_SUCCESS){
CY_DEBUG_PRINT_INFO ("CY:Error in Getting Serial name <%d>\n", rStatus);
}
}
CY_DEBUG_PRINT_INFO ("Manufacturer name <%s> \nProduct Name <%s> \nserial number <%s> \n",
deviceInfo->manufacturerName,deviceInfo->productName,deviceInfo->serialNum);
rStatus = libusb_get_config_descriptor (usbDevice, 0, &configDesc);
if (rStatus == LIBUSB_SUCCESS){
int index_i = 0;
const struct libusb_interface *interfaceDesc;
numInterfaces = configDesc->bNumInterfaces;
deviceInfo->numInterfaces = numInterfaces;
interfaceDesc = configDesc->interface;
while ((numInterfaces) && (index_i < CY_MAX_DEVICE_INTERFACE)){
deviceInfo->deviceClass[index_i] = (CY_DEVICE_CLASS)interfaceDesc->altsetting->bInterfaceClass;
if (deviceInfo->deviceClass[index_i] == CY_CLASS_VENDOR)
deviceInfo->deviceType[index_i] = (CY_DEVICE_TYPE)interfaceDesc->altsetting->bInterfaceSubClass;
else
deviceInfo->deviceType[index_i] = CY_TYPE_DISABLED;
index_i++;
numInterfaces--;
interfaceDesc++;
}
}
else {
CY_DEBUG_PRINT_ERROR ("CY: Error in Getting config descriptor ... Libusb Error is %d\n", rStatus);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DEVICE_INFO_FETCH_FAILED;
}
libusb_free_config_descriptor (configDesc);
libusb_close (devHandle);
}
if ((*deviceCount) == 0)
rStatus = CY_ERROR_DEVICE_NOT_FOUND;
pthread_mutex_unlock (&criticalSection);
return rStatus;
}
/*
This API will claim the interface in the device
To make sure only claimed application speaks to device.
*/
CY_RETURN_STATUS CySelectInterface (
CY_HANDLE handle,
UINT8 interfaceNum
)
{
UINT32 rStatus, numEP;
CY_DEVICE *device;
libusb_device_handle *devHandle;
libusb_device *usbDev;
struct libusb_config_descriptor *configDesc;
const struct libusb_interface *interfaceDesc;
const struct libusb_endpoint_descriptor *epDesc;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
usbDev = libusb_get_device (devHandle);
if (usbDev == NULL){
CY_DEBUG_PRINT_ERROR ("CY:Error Invalide handle ..function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
//Get the config descriptor and parse it to get the
//interface and endpoint descriptor
rStatus = libusb_get_config_descriptor (usbDev, 0, &configDesc);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Getting Config Desc ...function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
interfaceDesc = configDesc->interface;
//Interface Number should be a valid one and should not exceed
// total number of interfaces
if (interfaceNum >= configDesc->bNumInterfaces){
CY_DEBUG_PRINT_ERROR ("CY:Interface Number not valid... \n");
libusb_free_config_descriptor (configDesc);
return CY_ERROR_REQUEST_FAILED;
}
if (libusb_kernel_driver_active (devHandle, interfaceNum)){
CY_DEBUG_PRINT_ERROR ("CY:Kernel driver active on the interface number %d \n", interfaceNum);;
//User can uncomment this section if needed.
#ifdef CY_DETACH_KERNEL_DRIVER
if (!libusb_detach_kernel_driver (devHandle, interfaceNum)){
CY_DEBUG_PRINT_ERROR ("CY:Kernel driver detach failed %d\n", interfaceNum);
return CY_ERROR_REQUEST_FAILED;
}
#else
return CY_ERROR_REQUEST_FAILED;
#endif
}
rStatus = libusb_claim_interface (devHandle, interfaceNum);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in claiming interface -interface num %d... Libusb error is %d \n", interfaceNum, rStatus);
return CY_ERROR_REQUEST_FAILED;
}
device->interfaceNum = interfaceNum;
while (interfaceNum--)
interfaceDesc++;
epDesc = interfaceDesc->altsetting->endpoint;
numEP = interfaceDesc->altsetting->bNumEndpoints;
device->numEndpoints = numEP;
// Check the total number of endpoints interface has
// and get all the endpoint add
CY_DEBUG_PRINT_INFO ("CY:Info The total number of endpoints are %d \n", numEP);
while (numEP){
if (epDesc->bmAttributes == 0x2){ //Bulk EP checking
if (epDesc->bEndpointAddress & 0x80)
device->inEndpoint = epDesc->bEndpointAddress;
else
device->outEndpoint = epDesc->bEndpointAddress;
}
else if (epDesc->bmAttributes == 0x3) //Interrupt EP checking (We have only one interrupt EP)
device->interruptEndpoint = epDesc->bEndpointAddress;
epDesc++;
numEP--;
}
CY_DEBUG_PRINT_INFO ("CY:Info The Endpoints are in %d and out %d and interrup %d\n",
device->inEndpoint, device->outEndpoint, device->interruptEndpoint);
libusb_free_config_descriptor (configDesc);
return CY_SUCCESS;
}
/*
* This API selects the type of the device
*/
void CySelectDeviceType (CY_DEVICE *device, libusb_device *libUsbdev, unsigned char interfaceNum)
{
int rStatus, numInterfaces;
struct libusb_config_descriptor *configDesc;
int index = 0;
const struct libusb_interface *interfaceDesc;
device->deviceType = CY_TYPE_DISABLED;
rStatus = libusb_get_config_descriptor (libUsbdev, 0, &configDesc);
if (0 == rStatus){
interfaceDesc = configDesc->interface;
numInterfaces = configDesc->bNumInterfaces;
if (interfaceNum >= numInterfaces)
return;
while (index != interfaceNum) {
index++;
interfaceDesc++;
}
if (interfaceDesc->altsetting->bInterfaceClass == CY_CLASS_VENDOR)
device->deviceType = (CY_DEVICE_TYPE)interfaceDesc->altsetting->bInterfaceSubClass;
libusb_free_config_descriptor (configDesc);
}
CY_DEBUG_PRINT_INFO ("CY:Info The device type is %d \n", device->deviceType);
}
/*
The Api Gets the handle for the specified device number
(refer to usage guide and example for usage)
and this handle should be called for further communication
with the device
*/
CY_RETURN_STATUS CyOpen (
unsigned char deviceNumber,
unsigned char interfaceNum,
CY_HANDLE *handle
)
{
libusb_device_handle *devHandle;
libusb_device *dev;
CY_DEVICE *device;
int rStatus;
if (glDriverInit == false){
CY_DEBUG_PRINT_ERROR ("CY:Error Library not initialised ...function is %s\n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
pthread_mutex_lock (&criticalSection);
if (glDriverInit == true){
if (deviceNumber >= glNumDevices){
CY_DEBUG_PRINT_ERROR ("CY:Error ... Invalid device number ... \n");
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_INVALID_PARAMETER;
}
dev = glDeviceList [deviceNumber];
rStatus = libusb_open (dev, &devHandle);
if (rStatus == LIBUSB_ERROR_ACCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in opening the device ..Access denied \n");
handle = NULL;
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_ACCESS_DENIED;
}
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in Opening the Device ...Error is %d \n", rStatus);
handle = NULL;
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
device = (CY_DEVICE *)malloc(sizeof (CY_DEVICE));
if (device == NULL){
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_ALLOCATION_FAILED;
}
device->devHandle = devHandle;
(*handle) = device;
rStatus = CySelectInterface (device, interfaceNum);
if (rStatus != CY_SUCCESS){
libusb_close (devHandle);
free (device);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
CySelectDeviceType (device, dev, interfaceNum);
if (device->deviceType == CY_TYPE_UART) {
CyUartSetRts (*handle);
CyUartSetDtr (*handle);
if (!CyUartSetHwFlowControl (*handle, CY_UART_FLOW_CONTROL_DISABLE))
device->uartFlowControlMode = CY_UART_FLOW_CONTROL_DISABLE;
}
//initialising structure elements
device->spiThreadRunning = false;
device->uartThreadRunning = false;
device->spiCancelEvent = false;
device->uartCancelEvent = false;
device->spiTransfer = NULL;
device->uartTransfer = NULL;
if (pthread_mutex_init (&device->readLock, NULL)){
CY_DEBUG_PRINT_ERROR ("CY:Error initializing the read mutex .. Function is %s \n", __func__);
libusb_close (devHandle);
free (device);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
if (pthread_mutex_init (&device->writeLock, NULL)){
CY_DEBUG_PRINT_ERROR ("CY:Error initializing the write mutex .. Function is %s \n", __func__);
libusb_close (devHandle);
free (device);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
if (pthread_mutex_init (&device->notificationLock, NULL)){
CY_DEBUG_PRINT_ERROR ("CY:Error initializing the write mutex .. Function is %s \n", __func__);
libusb_close (devHandle);
free (device);
pthread_mutex_unlock (&criticalSection);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
pthread_mutex_unlock (&criticalSection);
return CY_SUCCESS;
}
else{
CY_DEBUG_PRINT_ERROR ("CY:Error iniitalise library by calling CyLibraryInit()....function is %s\n", __func__);
return CY_ERROR_DRIVER_OPEN_FAILED;
}
}
/*
The Api Closes the handle and needs to be called only if CyGetNumDevices
or CyOpen is called
*/
CY_RETURN_STATUS CyClose (
CY_HANDLE handle
)
{
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
if (device->deviceType == CY_TYPE_UART) {
CyUartClearRts (handle);
CyUartClearDtr (handle);
CyUartSetHwFlowControl (handle, CY_UART_FLOW_CONTROL_DISABLE);
}
if (glDriverInit == true){
if (device->deviceType == CY_TYPE_SPI || device->deviceType == CY_TYPE_UART){
if (device->spiThreadRunning || device->uartThreadRunning){
CyAbortEventNotification(handle);
}
}
if (pthread_mutex_destroy (&device->readLock)){
CY_DEBUG_PRINT_ERROR ("CY:Error de initializing the read mutex .. Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (pthread_mutex_destroy (&device->writeLock)){
CY_DEBUG_PRINT_ERROR ("CY:Error de initializing the write mutex .. Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
if (pthread_mutex_destroy (&device->notificationLock)){
CY_DEBUG_PRINT_ERROR ("CY:Error de initializing the write mutex .. Function is %s \n", __func__);
return CY_ERROR_REQUEST_FAILED;
}
libusb_close ((libusb_device_handle*)devHandle);
free (device);
}
return CY_SUCCESS;
}
/*
This Api will reset the pipe and clears the endpoint
*/
CY_RETURN_STATUS CyResetPipe (
CY_HANDLE handle,
UINT8 endPointAddress
)
{
UINT32 rStatus;
CY_DEVICE *device;
libusb_device_handle *devHandle;
if (handle == NULL)
return CY_ERROR_INVALID_HANDLE;
device = (CY_DEVICE *)handle;
devHandle = device->devHandle;
rStatus = libusb_clear_halt ((libusb_device_handle *)devHandle, endPointAddress);
if (rStatus != LIBUSB_SUCCESS){
CY_DEBUG_PRINT_ERROR ("CY:Error in resetting the pipe ... \n");
return CY_ERROR_REQUEST_FAILED;
}
return CY_SUCCESS;
}
/*
This Api will get the library version,patch
and build number
*/
CY_RETURN_STATUS CyGetLibraryVersion (
CY_HANDLE handle,
PCY_LIBRARY_VERSION version
)
{
version->majorVersion = CY_US_VERSION_MAJOR;
version->minorVersion = CY_US_VERSION_MINOR;
version->patch = CY_US_VERSION_PATCH;
version->buildNumber = CY_US_VERSION_BUILD;
return CY_SUCCESS;
}