From 39bb876c96c95791a6c171b02565ca1a140eaa7e Mon Sep 17 00:00:00 2001 From: Felipe Herranz Date: Sun, 21 Feb 2016 13:05:49 +0100 Subject: [PATCH] CP2102 sync operations added --- .../felhr/usbserial/CP2102SerialDevice.java | 119 +++++++++++------- 1 file changed, 72 insertions(+), 47 deletions(-) diff --git a/usbserial/src/main/java/com/felhr/usbserial/CP2102SerialDevice.java b/usbserial/src/main/java/com/felhr/usbserial/CP2102SerialDevice.java index 32e3353..025bf6d 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/CP2102SerialDevice.java +++ b/usbserial/src/main/java/com/felhr/usbserial/CP2102SerialDevice.java @@ -90,56 +90,29 @@ public class CP2102SerialDevice extends UsbSerialDevice @Override public boolean open() { - if(connection.claimInterface(mInterface, true)) + boolean ret = openCP2102(); + + if(ret) { - Log.i(CLASS_ID, "Interface succesfully claimed"); + // Initialize UsbRequest + requestIN = new UsbRequest(); + requestIN.initialize(connection, inEndpoint); + + // Restart the working thread if it has been killed before and get and claim interface + restartWorkingThread(); + restartWriteThread(); + + // Create Flow control thread but it will only be started if necessary + createFlowControlThread(); + + // Pass references to the threads + setThreadsParams(requestIN, outEndpoint); + + return true; }else { - Log.i(CLASS_ID, "Interface could not be claimed"); return false; } - - // Assign endpoints - int numberEndpoints = mInterface.getEndpointCount(); - for(int i=0;i<=numberEndpoints-1;i++) - { - UsbEndpoint endpoint = mInterface.getEndpoint(i); - if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK - && endpoint.getDirection() == UsbConstants.USB_DIR_IN) - { - inEndpoint = endpoint; - }else - { - outEndpoint = endpoint; - } - } - - - // Default Setup - if(setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0) - return false; - setBaudRate(DEFAULT_BAUDRATE); - if(setControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT,null) < 0) - return false; - setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); - if(setControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0) - return false; - - // Initialize UsbRequest - requestIN = new UsbRequest(); - requestIN.initialize(connection, inEndpoint); - - // Restart the working thread if it has been killed before and get and claim interface - restartWorkingThread(); - restartWriteThread(); - - // Create Flow control thread but it will only be started if necessary - createFlowControlThread(); - - // Pass references to the threads - setThreadsParams(requestIN, outEndpoint); - - return true; } @Override @@ -155,13 +128,25 @@ public class CP2102SerialDevice extends UsbSerialDevice @Override public boolean syncOpen() { - return false; + boolean ret = openCP2102(); + if(ret) + { + // Create Flow control thread but it will only be started if necessary + createFlowControlThread(); + setSyncParams(inEndpoint, outEndpoint); + return true; + }else + { + return false; + } } @Override public void syncClose() { - + setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_DISABLE, null); + stopFlowControlThread(); + connection.releaseInterface(mInterface); } @Override @@ -478,6 +463,46 @@ public class CP2102SerialDevice extends UsbSerialDevice } } + private boolean openCP2102() + { + if(connection.claimInterface(mInterface, true)) + { + Log.i(CLASS_ID, "Interface succesfully claimed"); + }else + { + Log.i(CLASS_ID, "Interface could not be claimed"); + return false; + } + + // Assign endpoints + int numberEndpoints = mInterface.getEndpointCount(); + for(int i=0;i<=numberEndpoints-1;i++) + { + UsbEndpoint endpoint = mInterface.getEndpoint(i); + if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK + && endpoint.getDirection() == UsbConstants.USB_DIR_IN) + { + inEndpoint = endpoint; + }else + { + outEndpoint = endpoint; + } + } + + + // Default Setup + if(setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0) + return false; + setBaudRate(DEFAULT_BAUDRATE); + if(setControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT,null) < 0) + return false; + setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); + if(setControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0) + return false; + + return true; + } + private void createFlowControlThread() { flowControlThread = new FlowControlThread();