[Embedded engineer interview high-frequency questions] Do you know SPI

Frequently Asked Questions about SPI in Interviews

What are SPIs?

The SPI interface is a full-duplex three-wire synchronous serial peripheral interface first proposed by Motorola, which adopts a master-slave mode (Master Slave) architecture; it supports multi-slave mode applications, and generally only supports a single Master. The clock is controlled by the Master. Under the clock shift pulse, the data is transmitted bit by bit, with the high bit in front and the low bit in the back (MSB first); the SPI interface has two unidirectional data lines, which are full-duplex communication. The data rate in can reach the level of several Mbps.

SPI generally uses 4 wires for communication?

Answer:
1. MISO ———— master device data input, slave device data output
2, MOSI ———— master device data output, slave device data input
3, SCLK ———— clock signal, controlled by the master device Generate
4. NSS(CS) ————– chip select signal from the slave device, controlled by the master device

SPI has four operating modes, mode 0, mode 1, mode 2, and mode 3. What is their difference?

Answer: In order to exchange data with peripherals, the polarity and phase of the output serial synchronous clock can be configured according to the working requirements of the peripherals. The clock polarity (CPOL) has no significant impact on the transmission protocol.

If CPOL=0, the idle state of the serial synchronous clock is low level; if CPOL=1, the idle state of the serial synchronous clock is high level.

The clock phase (CPHA) can be configured to select one of two different transport protocols for data transmission. If CPHA=0, the data is sampled on the first transition edge (rising or falling) of the serial synchronous clock; if CPHA=1, the data is sampled on the second transition edge (rising or falling) of the serial synchronous clock sampling.

The phase and polarity of the SPI master module and the peripheral tone clock it communicates with should be the same.

What are the differences between hardware SPI and software analog SPI?

Answer: The efficiency of hardware SPI is higher than that of software simulation SPI. When writing a program, just write the data to be sent in the register, and the hardware will automatically send it to you. Software simulation of SPI requires a device to pull the clock up and down. , data serial output and so on. Hardware SPI must be supported by the processor before it can be used. Software SPI does not require specific requirements, and general IO ports are available.

Simple hand tear program

Analyze the circular shift register from the following figure, start sending from the high bit, receive the low bit of the register, and then complete the 8-bit data exchange between the master and slave through each shift, and complete the reading and writing of data.
insert image description here

//GPIO模拟SPI(同时读写,写一个数据,通过返回一个数据)
uchar SPI_RW(uchar byte)//byte是写的数据,返回值是需要读的数据  
{
    
      
uchar bit_ctr;  
for(bit_ctr=;bit_ctr<8;bit_ctr++)  
{
    
      
MOSI=(byte&0x80);//写,移动最高有效位到MOSI线上   
byte=(byte<<1);//左移一位   
SCK=1; //时钟线置高   
byte|=MISO;//读,取当前MISO线上的值,放在byte最低位上   
SCK=0;//时钟线置低   
}  
return(byte);  
}  

Guess you like

Origin blog.csdn.net/freee12/article/details/109556954