STM32 SPI Notes

Publisher:莫愁前路Latest update time:2015-05-13 Source: 51heiKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
        I thought SPI was very simple, so I used it directly without looking at it carefully. This time, when I was debugging a chip, a strange problem occurred. I thought it was a problem with the program logic, and I wasted several days without finding the cause. Today, I obediently checked some manuals and finally found a clue in the "STM32 Incomplete Manual". Now I simply summarize SPI. First, let me talk about the problems I encountered recently.

        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.
Keywords:STM32 Reference address:STM32 SPI Notes

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

Introduction to the descriptors of STM32 CustomHID
As the saying goes, it is better to read the source code than the protocol manual! In the USB project, the USB descriptors are defined in the usb_desc.c file. Here are the USB descriptor definition codes, and the meaning of each value has been annotated in detail. Of course, if you don't understand the details, it is
[Microcontroller]
STM32 Flash read and write; storage content and method corresponding to Flash address
Concept: The smallest unit of information in a computer is a bit, which is a binary bit. 8 bits make up a Byte, which is 1 byte. 1 storage unit stores 1 byte, and each storage unit corresponds to a 32-bit address, so it is important to say it three times: For a 32-bit ARM CPU A 32-bit address points to 1 byte!!! A 32-
[Microcontroller]
STM32 button detection
The key detection of STM32 is relatively simple. First, initialize the connected i/o step by step, and then write a key scanning function, which is similar to that of the 51 microcontroller. The following is a typical example: Use buttons to control LEDs: key.h file #ifndef__KEY_H #define__KEY_H  #include"sys.h“ #
[Microcontroller]
STM32 dynamically changes PWM wave frequency and duty cycle
STM32 PWM wave dynamic frequency modulation and duty cycle adjustment Take TIM3_CH1 as an example (1) Working principle of timer The time base unit of the timer consists of three parts: ① automatic loading register (TIMx_ARR), ② prescaler register (TIMx_PSC), ③ counter register (TIMx_CNT). Set the automatic loading
[Microcontroller]
STM32 dynamically changes PWM wave frequency and duty cycle
The process of establishing a simple stm32 program
1. System initialization configuration Directly call the SystemInit() function in the system_stm32f10x..c file. This function already contains the function SysSetClock() for setting the system clock. The default clock in the function is 72MHZ, so there is no need to change it. 2. Turn on the clock of the corresponding
[Microcontroller]
STM32 re-understand GPIO configuration and configure PWM wave output
I recently made an SD card BOOTLOARDER program. During the test, I thought of using the LED light of the board to make a firefly light. I downloaded the APP file to the board through BOOTLOARDER to check the flashing of the LED light. Maybe because I was too confident that such a small program could handle it, I didn'
[Microcontroller]
STM32 re-understand GPIO configuration and configure PWM wave output
stm32-Serial port accepts variable length data methods (3 types)
Method 1: The serial port receives data, and the timer is used to determine whether the data reception is completed after the timeout. Method 2: DMA accept + IDLE interrupt Implementation idea: Use the serial port 1 of STM32F103, configure it to the idle interrupt IDLE mode and enable DMA reception, and set the receiv
[Microcontroller]
STM32 general timer
There are many types of timers in STM32, which are divided into 2 advanced controller timers, 4 ordinary timers, 2 basic timers, 2 watchdog timers, and 1 system tick timer SysTick according to their functions. The key to the timer is the calculation of the timing time. For example, when using a timer to control the
[Microcontroller]
STM32 general timer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号