Note that there are some explanatory texts on larger screens.

plurals
  1. POUSB Host initialization (controlTransfer) for PIC24 Microcontroller
    primarykey
    data
    text
    <p>I am working on an application that utilizes the <strong>USB Host capabilities of Android to communicate with a micro controller based device (PIC24FJ64GB002)</strong>. The device has Microchip's USB stack on it and has been confirmed to work through testing with windows. I am now trying to establish communication with the device through the Android application. So far I have been able to detect the device (through an intent filter in the manifest file), open device and claim the control interface (interface 0). I know that I need to perform some initialization/setup of the device but I am not sure how to do this. For instance, I know that for the Arduino USB-to-Serial it is...</p> <pre><code>conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0); conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0); </code></pre> <p>and for the FTDI based Arduinos it is...</p> <pre><code>conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); </code></pre> <p>But I do not know how to set up the Microchip USB Stack on the PIC24. The code that I have so far is shown below... Thanks</p> <p><strong>MainActivity.java</strong></p> <pre><code>public class MainActivity extends Activity { private static final int VID = 0x04D8; private static final int PID = 0x000A; public static int temperature; public static int humidity; public static int lpg; public static int alcohol; ListView listView; //USB UsbDevice USBDevice_s; UsbDeviceConnection USBDeviceConnection_s; UsbEndpoint USBEndpoint_s; UsbInterface USBInterface_s; UsbManager USBManager_s; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Model.LoadModel(); listView = (ListView) findViewById(R.id.listView); String[] ids = new String[Model.Items.size()]; for (int i= 0; i &lt; ids.length; i++) { ids[i] = Integer.toString(i+1); } ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter); //Begin USB process after layout has been configured... USBManager_s = (UsbManager) getSystemService(Context.USB_SERVICE); HashMap&lt;String, UsbDevice&gt; deviceList = USBManager_s.getDeviceList(); Iterator&lt;UsbDevice&gt; deviceIterator = deviceList.values().iterator(); while(deviceIterator.hasNext()) { USBDevice_s = deviceIterator.next(); } Intent Intent_s = getIntent(); String action = Intent_s.getAction(); USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE); //If device is attached if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { USBInterface_s = USBDevice_s.getInterface(0); USBEndpoint_s = USBInterface_s.getEndpoint(0); USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s); USBDeviceConnection_s.claimInterface(USBInterface_s, true); Toast.makeText(this,"Value of device :" +USBDevice_s.getDeviceName(), Toast.LENGTH_LONG).show(); if (USBDeviceConnection_s != null &amp;&amp; USBDeviceConnection_s.claimInterface(USBInterface_s, true)) { Toast.makeText(this,"Device Open, Interface Claimed", Toast.LENGTH_LONG).show(); } if (USBDeviceConnection_s != null) { //Perform setup/initialization..... } } //If device is detached else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (USBDevice_s != null) { USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s); USBDeviceConnection_s.releaseInterface(USBInterface_s); USBDeviceConnection_s.close(); Toast.makeText(this,"Device Removed, Interface Released", Toast.LENGTH_LONG).show(); } return; } //Update Layout temperature = 1; humidity = 63; lpg = 5000; alcohol = 500; Model.LoadModel(); ItemAdapter adapter2 = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter2); adapter.notifyDataSetChanged(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: //Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT) // .show(); //finish(); break; default: break; } return true; } } </code></pre> <p><strong>Manifest File</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.byrdonatwigge.sense" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-feature android:name="android.hardware.usb.host" /&gt; &lt;uses-sdk android:minSdkVersion="12" android:targetSdkVersion="18" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.byrdonatwigge.sense.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt; &lt;action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /&gt; &lt;meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" /&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p><strong>device_filter.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;usb-device vendor-id="1240" product-id="10" /&gt; &lt;/resources&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload