Changes

Jump to: navigation, search

SBCX-TN-001: Android Things and SPI bus

2,372 bytes added, 13:41, 9 April 2018
no edit summary
Development have been performed on the following platform:
* CPU: based on NXP/Freescale i.MX6 Dual core* SOM: Axel lite* carrier board: [[SBC-AXEL]]* Android Version: 4.4.3 (Jelly Bean)* ARM Cortex-A8 frequency: 600 MHz* SDRAM size: 512 MByte* SDRAM frequency: 400 MHz* HDVICP2 frequency: 306 MHz
==Implementation==
TBD
=== Managing the device connection ===
In order to open a connection to a particular SPI slave, you need to know the unique name of the bus.
Boot the device and run the following commands from the serial console
<pre>
root@sbcx:/ # ls /dev/spi*
/dev/spidev32765.0
root@sbcx:/ #
</pre>
 
Once you know the target name, the api of the library eu.dave.spi_test follow the Android things nomenclature
<pre>
 
public class HomeActivity extends Activity {
// SPI Device Name
private static final String SPI_DEVICE_NAME = "/dev/spidev32765.0";
 
private SpiDevice mDevice;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDevice = new SpiDevice(SPI_DEVICE_NAME);
// Attempt to access the SPI device
try {
mDevice.Open();
} catch (IOException e) {
Log.w("Error", "Unable to access SPI device" + e.getMessage());
}
}
 
@Override
protected void onDestroy() {
super.onDestroy();
 
if (mDevice != null) {
try {
mDevice.Close();
mDevice = null;
} catch (IOException e) {
Log.w(TAG, "Unable to close SPI device", e);
}
}
}
}
</pre>
=== Configuring clock and data modes ===
Configuration method are identical to the Andriod Things api [https://developer.android.com/things/sdk/pio/spi.html#mode ref].
 
<pre>
public void configureSpiDevice(SpiDevice device) throws IOException {
// Low clock, leading edge transfer
device.setMode(SpiDevice.MODE0);
 
device.setFrequency(500000); // 16MHz
device.setBitsPerWord(8); // 8 BPW
device.setBitJustification(false); // MSB first
</pre>
 
=== Transferring data ===
Is important to note that async I/O are not supported in userspace at this time [https://www.kernel.org/doc/Documentation/spi/spidev ref]
 
<pre>
public void sendCommand(SpiDevice device, byte[] buffer) throws IOException {
byte[] response = new byte[buffer.length];
device.transfer(buffer, response, buffer.length);
...
}
 
</pre>
170
edits

Navigation menu