Exemplo n.º 1
0
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);
                device->uartThreadId = 0;
                callbackFn(errorStatus);
            }
            break;
        }
    }
    CY_DEBUG_PRINT_INFO ("Exiting notification thread \n");
    libusb_free_transfer (transfer);
END:
   free (inputParameters);
   return NULL;
}
Exemplo n.º 2
0
/*
   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;
    }
}