The SPI interface has four different data transmission timings, depending on the combination of the two bits of Cpol and CPHL. The four timings are shown in Figure 1,
The relationship between time series and Cpol and CPHL can also be seen.
Figure 1
The Cpol is used to determine the level at which the SCK clock signal is idle, cpol=0, the idle level is low, cpol=1,
The idle level is high. Cpha is used to determine the sampling time, cpha=0, the first clock in each cycle along the sample,
Cpha=1, the second clock in each cycle is sampled along.
By the above we can configure the SPI mode more than the timing of the actual device.
Find the definition of SPI mode in the header file as follows:
- #define SPI_CPHA 0x01 /* Clock phase */
- #define SPI_CPOL 0x02 /* Clock polarity */
- #define Spi_mode_0 |0* */
- #define Spi_mode_1 (0| Spi_cpha)
- #define Spi_mode_2 (Spi_cpol|)
- #define Spi_mode_3 (Spi_cpol| Spi_cpha)
- It is easy to analyze the configuration of the SPI four mode, the subsequent rate, the number of bytes is well configured.
- int spi0mode = spi_mode_2; Configuration mode
- IOCTL (Spi0handle, Spi_ioc_wr_mode, &spi0mode);
- IOCTL (Spi0handle, Spi_ioc_rd_mode, &spi0mode);
- struct Spi_ioc_transfer sspi0tr;
- Sspi0tr.speed_hz = 6000000; Configuration Rate 6M
-
- IOCTL (Spi0handle, Spi_ioc_wr_max_speed_hz, &sspi0tr.speed_hz);
- IOCTL (Spi0handle, Spi_ioc_rd_max_speed_hz, &sspi0tr.speed_hz);
- Sspi0tr.bits_per_word = 16; Number of configuration bytes
- Sspi0tr.len = 2;
-
- ioctl (Spi0handle, Spi_ioc_wr_bits_per_word, &sspi0tr.bits _per_word);
- ioctl (Spi0handle, Spi_ioc_rd_bits_per_word, &sspi0tr.bits_per_word);
After the
is configured, it is read-write, and read and write, there are two parameters to note: 1,tx_buf,rx_buf These two parameters determine the SPI read-write, if read-only or write-only case, =null the other unused parameter. 2,spi_ioc_message (n) where n determines the number of SPI operations, if you want to read and write several times the SPI changes N is the
Am335x The application layer's SPI operation