示例#1
0
/* 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;
}/*
示例#2
0
文件: cyjtag.c 项目: 01org/intelRSD
/*
   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;
    
    bmRequestType = CY_VENDOR_REQUEST_HOST_TO_DEVICE;
    bmRequest = CY_JTAG_READ_CMD;
    wValue = readBuffer->length;
    wIndex = 0;
    wLength = 0;

    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;
    }
    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;
    }
}