7724 views|1 replies

50

Posts

0

Resources
The OP
 

Crazy Shell AI open source drone SPI (six-axis sensor data acquisition) [Copy link]

This post was last edited by fengke on 2021-3-30 11:42

1. Introduction to ICM20602

Six-axis sensors are widely used in today's smart wearable and positioning navigation products. The best six-axis sensors are those produced by InvenSense. ICM20602 is one of its excellent six-axis sensors.

ICM20602 integrates a 3-axis accelerometer and a 3-axis gyroscope. The gyroscope range can be selected from +/-250dps, +/-500dps, +/-1000dps and +/-2000dps, while the accelerometer range can be selected from +/-2g, +/-4g, +/-8g and +/-16g.

ICM20602 supports I 2 C up to 400KHz and SPI up to 10MHz, with high interface compatibility.

The physical picture of ICM20602 is shown below.

image.png

The pinout of ICM20602 is shown in the figure below.

image.png

SPI Overview

SPI is the abbreviation of Serial Peripheral Interface, which is a high-speed, full-duplex, synchronous communication bus. It only occupies four lines on the chip pins, saving the chip pins. The four communication lines of SPI are:

(1) MISO master input slave output interface;

(2) MOSI master-out slave-in interface;

(3) SCLK is the clock signal generated by the master device;

(4) CS is the slave chip select signal controlled by the master device.

The SPI clock of STM32F103 can reach up to 18MHz and supports DMA.

SPI master-slave communication is shown in the following figure:

image.png

When there are multiple devices mounted on the SPI bus, the wiring diagram is shown in the figure below.

image.png

When SPI synchronous serial data transmission is performed between the microcontroller and peripheral devices, the data is transmitted bit by bit under the shift pulse of the master device, with the low bit in front and the high bit in the back. It is full-duplex communication. The data transmission speed is generally faster than the I2C bus, and the speed can reach several Mbp. Compared with other buses, the SPI protocol is simple and the relatively high data rate, but SPI also has its disadvantages, such as no specified flow control and no response mechanism to confirm whether the data is received.

3. SPI bus protocol

SPI communication requires the following knowledge: clock polarity, clock phase, and SPI transmission timing.

(1) Clock polarity. SPI uses the clock polarity (CPOL) to determine whether the synchronous clock (SCLK) signal line is high or low when the bus is idle. When the clock polarity is 0 (CPOL=0), the SCLK signal line is low when idle; when the clock polarity is 1 (CPOL=1), the SCLK signal line is high when idle;

(2) Clock phase. SPI uses the clock phase (CPHA) to determine when to sample the signal. When the clock phase is 1 (CPHA=1), sampling is performed on the second transition edge of the SCK signal line. Is the transition edge here a rising edge or a falling edge? This depends on the polarity of the clock. When the clock polarity is 0, the falling edge is taken; when the clock polarity is 1, the rising edge is taken; as shown in the following figure:

image.png

When the clock phase is 0 (CPHA=0), sampling is performed on the first transition edge of the SCK signal line. The transition edge is also related to the clock polarity: when the clock polarity is 0, the rising edge is taken; when the clock polarity is 1, the falling edge is taken; as shown in the following figure:

image.png

4. SPI Register

In this experiment, the hardware SPI of STM32 is used. There are many registers involved in the hardware SPI of STM32. Here we select the more important ones to explain.

(1) SPI_CR1: SPI control register 1, as shown in the following figure:

Where SPE is the SPI enable control bit, which enables SPI when it is equal to 1 and disables SPI when it is equal to 0; BR[2:0] is the SPI baud rate control bit, if BR[2:0] is equal to 000, the baud rate is f PCLK /2, if it is equal to 001 , the baud rate is f PCLK /4, if it is equal to 010, the baud rate is f PCLK /8, if it is equal to 011, the baud rate is f PCLK /16, if it is equal to 100, the baud rate is f PCLK /32, if it is equal to 101, the baud rate is f PCLK /64, if it is equal to 110, the baud rate is f PCLK /128, and if it is equal to 111, the baud rate is f PCLK /256; MSTR is the SPI master-slave mode selection bit, when it is equal to 0, it is slave mode, and when it is equal to 1, it is master mode; CPOL is the SPI clock polarity setting bit, when it is 0, the idle clock is low level, and when it is 1, the idle clock is high level; CPHA is the SPI clock phase setting bit, when it is equal to 0, it starts collecting at the first clock jump edge, and when it is equal to 1, it starts collecting at the second clock jump edge.

(2) SPI_SR: SPI status register, as shown in the following figure:

image.png

Among them, TXE is the transmit buffer status bit. When this bit is 0, the transmit buffer is not empty, and when it is 1, the transmit buffer is empty; RXNE is the receive buffer status bit. When this bit is 0, the receive buffer is empty, and when this bit is 1, the receive buffer is not empty.

(3) SPI_DR: SPI data register, used to store received or sent data. The description of SPI_DR is shown in the following figure:

image.png

DR[15:0] stores SPI data.

5. Six-axis sensor data acquisition experiment

The six-axis sensor data acquisition experiment uses the hardware SPI of STM32 to connect to the six-axis sensor ICM20602. Serial port 1 is UART1, and the computer is connected through the USB to serial port module. The six-axis data obtained by SPI is transmitted to the serial port debugging assistant on the computer through serial port 1 for display. When doing this experiment, you need to temporarily remove the vision module and connect the USB to serial port line to the vision module interface. The six-axis sensor ICM20602 is in the white RGB light box on the top of the drone. The SPI and power supply ports are connected through the soft row, as shown in the figure below.

image.png

According to the schematic diagram, we can see that the SPI interfaces of ICM20602 are: PB13, PA5, PA6, and PA7, as shown in the figure below.

image.pngimage.png

For the configuration of serial port 1, please refer to "Serial port (basic transmission and reception), configuration code (by calling the official library)

The idea of writing code to obtain ICM20602 data is as follows:

Code ideas

1

Pin Configuration

1. Define the structure;

2. Enable the clock;

3. Filling structure;

4. Load the structure.

2

SPI Configuration

1. Define the structure;

2. Enable the clock;

3. Filling structure;

4. Loading structure;

5. Enable SPI.

3

SPI read and write logic

1. Read one byte;

2. Write a byte.

4

ICM20602 driver

1. Read from the sensor;

2. Write from the sensor;

3.ICM20602 initialization.

The SPI initialization code is as follows:

image.png

The SPI read and write codes are as follows.

image.png

The initialization code of ICM20602 is as follows.

image.png

The read and write codes of ICM20602 are as follows.

image.png

Here, please note that the serial port transmission must also be configured so that data can be sent to the computer. Serial port 1 is connected to the computer through the USB to serial port module, and the ICM20602 code is obtained as follows.

image.png

image.png

Save, compile, and download the code. You can see that the USB-to-serial port module is constantly printing the high 8 bits of the X-axis acceleration of ICM20602. The data is shown in the figure below:

image.png

2.飞控开发基础-【6】SPI(六轴传感器数据获取).pdf

1.27 MB, downloads: 7

This post is from Creative Market

Latest reply

Crazy shell AI open source drone Thanks to the host for sharing the entire xi series systematically. Thank you   Details Published on 2021-9-12 13:17
 
 

46

Posts

0

Resources
2
 

Crazy shell AI open source drone Thanks to the host for sharing the entire xi series systematically. Thank you

This post is from Creative Market
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list