Problem 1: I mistakenly thought that reading SPI data by directly reading the SPIx->DR register can be completed.
I have never paid attention to this problem, and I am very ashamed. It turns out that the SPI clock is only generated when writing data to the DR register, and it will not be generated when reading (I have not confirmed it from any information yet, but I guess it is like this). So to read the data sent by the slave, the master must first send a "DUMMY" data. The content of this data is not important. The purpose is just to generate a set of clocks for the slave, and the slave's data is sent out along this set of clocks.
The process of master reading and writing data to slave is as follows:
Write: master writes data to DR, generates clock, and data is shifted from MOSI pin to slave's MOSI pin;
Read: master writes DUMMy to DR, generates clock, and DUMMy is sent from MOSI to slave (this data is meaningless), and the read data is shifted from slave's MISO pin to master's MISO pin.
Question 2: When configured as two-wire full-duplex, as mentioned above, when the master writes data, the SPI of stm32 also reads data into the master's DR register (although both reading and writing are DR, they are actually two different registers). Ignoring this point is the cause of this problem.
Before I read data from the acquisition chip, I need to send a read data instruction to the chip. After sending the instruction, theoretically, the acquisition chip will automatically wait for the data to be sent. As long as I send a DUMMy to generate a set of clocks on the stm32 side, I can read data from DR. However, when sending the read instruction, STM32 also reads useless data into DR. Before the data is taken away, it will not accept new data. Therefore, when DUMMY is sent later, the read register DR is not updated, so the data read is naturally wrong.
The solution is to read the data once and clear DR after sending the instruction to receive the next data.
The following is a summary of other key points of SPI.
Pin definition:
MISO: Master input/slave output
MOSI: Master output/slave input
SCK: Serial port clock, as the output of the master device and the input of the slave device
NSS: Slave select
For NSS, for the slave device, inputting a low level indicates the selection of the slave device. This signal is provided by the NSS pin in hardware NSS mode; in software NSS mode, it is controlled by the internal SSI bit, and the NSS pin can be used as a normal IO.
For the master device, if the NSS output is enabled, NSS will output a low level and can be connected to the NSS of the slave device. When the slave device is in hardware NSS mode, it will automatically become a slave SPI device (multi-master environment is not allowed). If the NSS output is turned off: operation in a multi-master environment is allowed.
Previous article:STM32F103x clock configuration
Next article:In-depth study of STM32F107VCT6 serial communication by learning USART1
Recommended ReadingLatest update time:2024-11-16 00:51
- Popular Resources
- Popular amplifiers
- usb_host_device_code
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Transplantation of real-time operating system RT-ThreadSmart on STM32MP1
- Design of switching power supply with adjustable output voltage based on STM32
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- About the parallel and series resistors at OUT in the crystal oscillator circuit
- Single-chip microcomputer controlled xc1004 four-axis SPI motion control chip information
- I would like to ask a basic question: Is the discharge speed of parallel capacitors determined by the ESR of the capacitors?
- How to choose the appropriate inductor and its parameters? | MPS invites you to learn practical tips and win gifts by commenting!
- I need help with soldering tips. Why does flux often splash out when I add tin to my soldering iron, burning my hands?
- What are the differences between RTU and DTU?
- Three key technologies of mobile robots
- FPGA implementation of terrestrial digital television symbol and carrier synchronization.pdf
- Classic: MSP430 microcontroller enters low power mode program code
- X-NUCLEO-IKS01A3 sensor test based on STM32F401RE development board 3D acceleration LIS2DW12 sensor test...