1 SPI

1.1 concept

SPI is the abbreviation of Serial Peripheral Interface. SPI is a high-speed, full-duplex, synchronous communication bus, and it only occupies four lines on the pin of the chip, saving the pin of the chip, saving space and providing convenience for PCB layout. The principle and communication process of SPI are not the content of this article. If you are interested, you can find the information on your own.

1.2 features

(1) SPI bus is a synchronous serial interface: SPI is a kind of serial communication, which means THAT SPI relies on shared clock signals to synchronize data transmission between devices. The device that controls the clock signal is called the Master. All other connected peripherals are considered Slaves. Each device is connected to the same set of data signals to form the bus. In theory, the SPI data transfer rate is limited to the speed of the Master switch clock signal. Clock speeds are usually in the 16MHz to 25MHz range.

(2) SPI supports full-duplex data transfer: Full-duplex data transfer means that Master and Slave can exchange data at the same time. In order to support full-duplex transmission, the bus must provide the following separate signals so that the SPI has a minimum four-wire interface:

  • Master outbound Slave Inbound (MOSI);
  • Mater in Slave out (MISO);
  • Shared clock signal (CLK);
  • Common grounding reference (GND);

(3) SPI supports connecting multiple Slave devices with the same bus. In SPI communication, Slave devices adopt hardware addressing mode. Each Slave requires external chip selection signals to allow the Master to locate specific devices as the target for data transmission. This is not necessary if only one Slave is used.

SPI pin diagram:

2 Android Things

2.1 concept

Android Things is a set of tools released by Google to help developers build embedded devices on Android. Android Things encapsulates the underlying communication process into API methods that the application layer can call, greatly simplifying the process of developing embedded devices on the Android platform.

2.2 features

(1) Single self-starting APP, usually the device will only run a single program, that is, the developer’s APP will automatically start, the system APP will not be displayed.

(2) Display is optional. The device can interact with the user through buttons, touch, LED lights, voice, or other forms without a screen.

(3) Provides the interface of driving peripherals through I/O, so that developers can communicate with sensors and actuators, support GPIO, PWM, I2C, SPI and UART.

(4) User-driven API, which inherits Android Framework Services and allows apps to inject hardware events into the Framework so that other apps can access them using standard Android APIS.

(5) The Internet of Things Cloud. With Google’s Internet of Things Cloud platform, developers can easily and securely connect, manage, and access data from millions of devices around the world. The Google iot Cloud Platform, along with a number of other Google services, provides a complete solution that includes data collection, processing, analysis and visualization of iot data.

Android Things Architecture Diagram   

3 Android SPI development

The Android SPI here refers to the API development program that calls Android Things. It is worth noting that Java also has a set of mechanism called SPI for short. The full name is Service Provider Interface. SPI in Java is a mechanism for finding services for an Interface, which is similar to IOC, handing over control of assembly to ServiceLoader. SPI is a completely different concept from what is described in this article.

3.1 Applying for Permission

When developing with Android Things, you need to add the following permissions to androidManifest.xml.

<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
Copy the code

3.2 Adding a Dependency

Add dependencies to build.gradle:

CompileOnly 'com. Google. Android. Things: androidthings: 1.0'Copy the code

Under the Application tag of androidManifest.xml add:

<uses-library android:name="com.google.android.things" />
Copy the code

Note: If install APK cannot be executed on the device during the development process, you can change it to, and then install APK.

3.3 Managing SPI Devices

Android Things encapsulates the management of various peripherals in the PeripheralManager class (PeripheralManagerService in previous Android Things versions), which is acquired in a singleton mode.

PeripheralManager manager = PeripheralManager.getInstance(); List<String> deviceList = manager.getSpiBusList(); If (devicelist.isempty ()) {// if the SPI list isEmpty, there is No SPI serial port log. e(TAG, "No SPI bus available on this device."); } else { Log.e(TAG, "List of available devices: " + deviceList); }Copy the code

You can run the adb command to view the SERIAL port number of SPI in the /dev/directory. Generally, SPI is defined as “SPI1.0” or “SPI2.0”.

Note: if the prompt the requires unavailable Shared library com. Google. Android. Things; failing! You need to check that the Android system you’re developing for has the Android Things library added. If missing this library directly in PeripheralManager. GetInstance (); The collapse.

3.4 get SpiDevice

SpiDevice is an encapsulated SPI communication device object. The communication process of SPI is mainly completed by operating this object. The method for obtaining an object is as follows:

String spiDeviceName = "/dev/spidev1.0"; String spiDeviceName = "/dev/spidev1.0"; SpiDevice spiDevice = null; SpiDevice = manager. OpenSpiDevice (spiDeviceName); } catch (IOException e) { e.printStackTrace(); }Copy the code

3.5 Parameter Configuration

Since THE SPI is a type of serial communication, you need to set corresponding parameters to ensure a complete communication process. The following parameters need to be configured:

1. Configure the SPI clock

The SPI clock is shown below:

1) Idle level: the level (low or high) of the clock signal when there is no data transmission.

2) Leading edge: The leading edge of each clock pulse, also known as the rising edge.

3) Trailing edge: the transition opposite the leading edge in each clock pulse, also known as the falling edge.

SPI supports the following four modes:

  • MODE0 – The clock signal is idle and data is transmitted along the leading clock edge.
  • MODE1 – The clock signal is idle at low level and data is transmitted along the clock edge.
  • MODE2 – The clock signal is idle and data is transmitted along the leading clock edge.
  • ODE3 – The clock signal is idle at high level, and data is transmitted along the clock edge at the back.

2. Set the clock frequency

SPI communication specifies the shared clock signal in Hz. Clock signal capabilities vary from device to device. This frequency should be the same as the device configuration.

3. Configure the alignment mode

Alignment is used to set the order in which each bit is specified to be transmitted on the bus, which is also known as the byte order of the data. By default, data will be sent first with the most significant bit (MSB). That is, they are sent in order from the highest byte to the lowest byte.

4. Configure bits for each word

This method is used to set the number of bits configured to be transmitted at a time between the chip selection lines switched from a given device. The default value is 8 bits per byte.

Public void configureSpiDevice(SpiDevice SpiDevice) throws IOException {// Low clock, Spidevice.setmode (spidevice.mode0); // Set the clock frequency spidevice.setFrequency (112500); SpiDevice. SetBitsPerWord (8); / / configuration byte sequence spiDevice setBitJustification (false);Copy the code

3.5 Data Communication

SPI supports both half-duplex and full-duplex data transmission. The two methods of calling data communication are different.

1. Half-duplex communication

Half-duplex communication: The half-duplex communication mode can achieve bidirectional communication, but cannot be carried out in both directions at the same time. That is, each end of a communication channel can be either a sender or a receiver. But information can only travel in one direction at a time. For example, walkie-talkie in daily life, etc. According to the characteristics of half-duplex communication, the device can write and read in the half-duplex communication mode. Devices cannot write and read at the same time.

Send data:

public void sendCommand(SpiDevice spiDevice, byte[] buffer) throws IOException {
    spiDevice.write(buffer, buffer.length);
Copy the code

Read data:

Private class implements Runnable {public void run() {byte[] response = new byte[1024]; private class implements Runnable {public void run() {byte[] implements new byte[1024]; while (true) { spiDevice.read(response, response.length); }}}Copy the code

2. Full-duplex communication

Full Duplex is a method of using separate transmit and receive lines between the processor and the peripheral device, so that data can be transmitted simultaneously in two directions. The ability to send and receive data at the same time, as if we were talking on the phone and hearing each other’s voice at the same time.

Full-duplex communication devices can transmit and receive data at the same time. The encapsulation transfer method in SpiDevice is used for full-duplex communication. The calling method is as follows:

public void sendCommand(SpiDevice spiDevice, Byte [] buffer) throws IOException {// Sending and receiving data in full-duplex mode byte[] response = new byte[buffer.length]; spiDevice.transfer(buffer, response, buffer.length);Copy the code

4 conclusion

Android Things advantages over other platforms:

(1) Unified development framework and interface

(2) Suitable for Android developers to innovate smart devices

(3) Advanced basic framework

(4) Secure Internet of Things cloud

(5) Rich cloud service resources

Android Things also has limitations:

(1) Cost

(2) Real-time performance of hardware control

Public account: Programmer Meow (focus on Android study notes, interview questions and IT information sharing)