Permalink
Browse files

Split read/write usb stream to bulk/interrupt read/write

Set TI3410 mcr on open
Flush read interrupt on purge and on end open
Set TI3410 ep read/write has constant
First frame has been exchange with glucose meter youou !
  • Loading branch information...
1 parent 5cf4307 commit f2254b963428992079418a66e098f38e8764725f @gandalfn committed Apr 18, 2012
Showing with 188 additions and 45 deletions.
  1. +98 −5 lib/usb/ti-3410-stream.vala
  2. +89 −30 lib/usb/usb-stream.vala
  3. +0 −3 src/modules/abbott/abbott.conf
  4. +1 −7 src/modules/abbott/device.vala
View
@@ -28,6 +28,13 @@ namespace GlucoseBoard
ACTIVE = 0x02
}
+ private enum Ep
+ {
+ READ = 0x81,
+ WRITE = 0x01,
+ INTERRUPT_READ = 0x83
+ }
+
private enum Commands
{
GET_VERSION = 0x01,
@@ -70,6 +77,38 @@ namespace GlucoseBoard
INPUT = 0x80
}
+ private enum RWData
+ {
+ ADDR_SFR = 0x10,
+ ADDR_IDATA = 0x20,
+ ADDR_XDATA = 0x30,
+ ADDR_CODE = 0x40,
+ ADDR_GPIO = 0x50,
+ ADDR_I2C = 0x60,
+ ADDR_FLASH = 0x70,
+ ADDR_DSP = 0x80,
+
+ UNSPECIFIED = 0x00,
+ BYTE = 0x01,
+ WORD = 0x02,
+ DOUBLE_WORD = 0x04
+ }
+
+ private enum Addr
+ {
+ UART1_BASE = 0xFFA0,
+ UART2_BASE = 0xFFB0,
+ UART_OFFSET_LCR = 0x0002,
+ UART_OFFSET_MCR = 0x0004
+ }
+
+ private enum MCR
+ {
+ LOOP = 0x04,
+ DTR = 0x10,
+ RTS = 0x20
+ }
+
private struct UARTConfig
{
uint16 baud_rate;
@@ -136,6 +175,9 @@ namespace GlucoseBoard
RTS = 0x20
}
+ // constant
+ const int cInterfaceNum = 0;
+
// properties
private UARTConfig m_Config = UARTConfig ();
@@ -144,13 +186,11 @@ namespace GlucoseBoard
* Create new TI3410 stream serial for inDevice
*
* @param inDevice usb device to create stream for
- * @param inInterfaceNumber the usb device interface number
- * @param inEndPointRead the usb device interface end point read
*/
- public TI3410Stream (UsbDevice inDevice, uint inInterfaceNumber, uint inEndPointRead, uint inEndPointWrite)
+ public TI3410Stream (UsbDevice inDevice)
{
// Launch base constructor
- base (inDevice, inInterfaceNumber, inEndPointRead, inEndPointWrite);
+ base (inDevice, cInterfaceNum, Ep.READ, Ep.WRITE);
}
/**
@@ -248,6 +288,28 @@ namespace GlucoseBoard
}
private void
+ set_mcr () throws StreamError
+ {
+ // set config message
+ var msg = new Message (9);
+ msg[0] = RWData.ADDR_XDATA;
+ msg[1] = RWData.BYTE;
+ msg[2] = 1;
+ msg[3] = 0;
+ msg[4] = 0;
+ msg[5] = (uint8)(Addr.UART1_BASE >> 8);
+ msg[6] = (uint8)Addr.UART1_BASE;
+ msg[7] = MCR.LOOP | MCR.RTS | MCR.DTR;
+ msg[8] = MCR.RTS | MCR.DTR;
+
+ // send write_data message
+ uint8[] data = msg.raw;
+ Log.debug ("send set mcr: %s", msg.to_string ());
+ send_control_message (LibUSB.RequestType.VENDOR | LibUSB.RequestRecipient.DEVICE | LibUSB.EndpointDirection.OUT,
+ Commands.WRITE_DATA, 0, Port.RAM, ref data);
+ }
+
+ private void
open_port () throws StreamError
{
// set open port message
@@ -285,15 +347,23 @@ namespace GlucoseBoard
{
try
{
- // send set_config message
+ // send purge input message
uint8[]? data = null;
Log.debug ("send purge input port");
send_control_message (LibUSB.RequestType.VENDOR | LibUSB.RequestRecipient.DEVICE | LibUSB.EndpointDirection.OUT,
Commands.PURGE_PORT, Purge.INPUT, Port.UART1, ref data);
+ // Flush read interrupt
+ Message msg = new Message (2);
+ interrupt_read (Ep.INTERRUPT_READ, ref msg, 1000);
+
+ // send purge output message
Log.debug ("send purge output port");
send_control_message (LibUSB.RequestType.VENDOR | LibUSB.RequestRecipient.DEVICE | LibUSB.EndpointDirection.OUT,
Commands.PURGE_PORT, Purge.OUTPUT, Port.UART1, ref data);
+
+ // Flush read interrupt
+ interrupt_read (Ep.INTERRUPT_READ, ref msg, 1000);
}
catch (StreamError err)
{
@@ -379,11 +449,34 @@ namespace GlucoseBoard
// Configure serial communication
set_config ();
+ // Initialize Modem control
+ set_mcr ();
+
// Open port
open_port ();
// Start port
start_port ();
+
+ // Purge port
+ purge_port ();
+
+ // Clear and halt end point
+ clear_halt_read_ep ();
+ clear_halt_write_ep ();
+
+ // Configure serial communication
+ set_config ();
+
+ // Open port
+ open_port ();
+
+ // Start port
+ start_port ();
+
+ // Flush interrupt read
+ Message msg = new Message (2);
+ bulk_read (Ep.INTERRUPT_READ, ref msg, 1000);
}
catch (StreamError err)
{
View
@@ -242,7 +242,7 @@ namespace GlucoseBoard
*
* @throw StreamError if something goes wrong
*/
- public void
+ protected void
send_control_message (uint8 inType, uint8 inRequest, uint16 inValue, uint16 inIndex, ref uint8[]? outData) throws StreamError
{
if (!is_open)
@@ -262,6 +262,90 @@ namespace GlucoseBoard
}
/**
+ * Do a bulk read on usb device
+ *
+ * @param inEndPoint The end point address to read on
+ * @param inoutMessage The new message.
+ * @param inTimeout The waiting time for the new message in milliseconds.
+ *
+ * @throws StreamError raise when something went wrong
+ */
+ protected void
+ bulk_read (uint8 inEndPoint, ref Message inoutMessage, uint inTimeout) throws StreamError
+ {
+ int len;
+ int status = m_Handle.bulk_transfer (inEndPoint, inoutMessage.raw, out len, inTimeout);
+ if (status != LibUSB.Error.SUCCESS)
+ {
+ throw new StreamError.READ ("Error on bulk read on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointRead,
+ UsbDevice.usb_error_to_string (status));
+ }
+ }
+
+ /**
+ * Do a interrupt read on usb device
+ *
+ * @param inEndPoint The end point address to read on
+ * @param inoutMessage The new message.
+ * @param inTimeout The waiting time for the new message in milliseconds.
+ *
+ * @throws StreamError raise when something went wrong
+ */
+ protected void
+ interrupt_read (uint8 inEndPoint, ref Message inoutMessage, uint inTimeout) throws StreamError
+ {
+ int len;
+ int status = m_Handle.interrupt_transfer (inEndPoint, inoutMessage.raw, out len, inTimeout);
+ if (status != LibUSB.Error.SUCCESS)
+ {
+ throw new StreamError.READ ("Error on interrupt read on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointRead,
+ UsbDevice.usb_error_to_string (status));
+ }
+ }
+
+ /**
+ * Do a bulk write on usb device
+ *
+ * @param inEndPoint The end point address to write on
+ * @param inMessage The message to send
+ * @param inTimeout The waiting time for the new message in milliseconds.
+ *
+ * @throws StreamError raise when something went wrong
+ */
+ protected void
+ bulk_write (uint8 inEndPoint, Message inMessage, uint inTimeout) throws StreamError
+ {
+ int len;
+ int status = m_Handle.bulk_transfer (inEndPoint, inMessage.raw, out len, inTimeout);
+ if (status != LibUSB.Error.SUCCESS)
+ {
+ throw new StreamError.WRITE ("Error on bulk write on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointWrite,
+ UsbDevice.usb_error_to_string (status));
+ }
+ }
+
+ /**
+ * Do an interrupt write on usb device
+ *
+ * @param inEndPoint The end point address to write on
+ * @param inMessage The message to send
+ * @param inTimeout The waiting time for the new message in milliseconds.
+ *
+ * @throws StreamError raise when something went wrong
+ */
+ protected void
+ interrupt_write (uint8 inEndPoint, Message inMessage, uint inTimeout) throws StreamError
+ {
+ int len;
+ int status = m_Handle.interrupt_transfer (inEndPoint, inMessage.raw, out len, inTimeout);
+ if (status > LibUSB.Error.SUCCESS)
+ {
+ throw new StreamError.WRITE ("Error on interrupt write on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointWrite,
+ UsbDevice.usb_error_to_string (status));
+ }
+ }
+
+ /**
* {@inheritDoc}
*/
protected override void
@@ -273,23 +357,11 @@ namespace GlucoseBoard
if (m_ReadTransferType == LibUSB.TransferType.BULK)
{
- int len;
- int status = m_Handle.bulk_transfer ((uint8)m_EndPointRead, inoutMessage.raw, out len, inTimeout);
- if (status != LibUSB.Error.SUCCESS)
- {
- throw new StreamError.READ ("Error on read on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointRead,
- UsbDevice.usb_error_to_string (status));
- }
+ bulk_read ((uint8)m_EndPointRead, ref inoutMessage, inTimeout);
}
else if (m_ReadTransferType == LibUSB.TransferType.INTERRUPT)
{
- int len;
- int status = m_Handle.interrupt_transfer ((uint8)m_EndPointRead, inoutMessage.raw, out len, inTimeout);
- if (status != LibUSB.Error.SUCCESS)
- {
- throw new StreamError.READ ("Error on read on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointRead,
- UsbDevice.usb_error_to_string (status));
- }
+ interrupt_read ((uint8)m_EndPointRead, ref inoutMessage, inTimeout);
}
}
@@ -305,24 +377,11 @@ namespace GlucoseBoard
GlucoseBoard.Log.debug ("Send %s to usb device %s", inMessage.to_string (), m_Device.path);
if (m_WriteTransferType == LibUSB.TransferType.BULK)
{
- int len;
- int status = m_Handle.bulk_transfer ((uint8)m_EndPointWrite, inMessage.raw, out len, inTimeout);
- if (status != LibUSB.Error.SUCCESS)
- {
- throw new StreamError.WRITE ("Error on send on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointWrite,
- UsbDevice.usb_error_to_string (status));
- }
- GlucoseBoard.Log.debug ("Sent %i to usb device %s", len, m_Device.path);
+ bulk_write ((uint8)m_EndPointWrite, inMessage, inTimeout);
}
else if (m_WriteTransferType == LibUSB.TransferType.INTERRUPT)
{
- int len;
- int status = m_Handle.interrupt_transfer ((uint8)m_EndPointWrite, inMessage.raw, out len, inTimeout);
- if (status > LibUSB.Error.SUCCESS)
- {
- throw new StreamError.WRITE ("Error on read on %s %u %u: %s", m_Device.path, m_Interface, m_EndPointWrite,
- UsbDevice.usb_error_to_string (status));
- }
+ interrupt_write ((uint8)m_EndPointWrite, inMessage, inTimeout);
}
}
}
@@ -4,7 +4,4 @@ Meters=OptiumXceed
[OptiumXceed]
VendorId=0x1a61
ProductId=0x3420
-Interface=0x00
-EndPointWrite=0x01
-EndPointRead=0x81
Firmware=/etc/glucose-board/umpf3410.i51
@@ -48,9 +48,6 @@ namespace GlucoseBoard.Module.Abbott
private int m_Index;
private Config m_Config;
- private uint m_UsbInterface;
- private uint m_UsbEndPointRead;
- private uint m_UsbEndPointWrite;
private string m_Firmware;
private State m_State = State.READY;
@@ -76,13 +73,10 @@ namespace GlucoseBoard.Module.Abbott
m_Config = new Config ("abbott.conf");
// Get usb communication parameters
- m_UsbInterface = (uint)m_Config.get_integer_hex (name, "Interface");
- m_UsbEndPointRead = (uint)m_Config.get_integer_hex (name, "EndPointRead");
- m_UsbEndPointWrite = (uint)m_Config.get_integer_hex (name, "EndPointWrite");
m_Firmware = m_Config.get_string (name, "Firmware");
// Create USB stream
- m_Stream = new TI3410Stream (this, m_UsbInterface, m_UsbEndPointRead, m_UsbEndPointWrite);
+ m_Stream = new TI3410Stream (this);
// Read end point is not available load firmware
if (m_Stream.configuration_value == TI3410Stream.ConfigurationValue.BOOT)

0 comments on commit f2254b9

Please sign in to comment.