Stm32 SPI initialization and use

Source: Internet
Author: User

Serial peripheral interface (SPI ). Initialization steps:
1. Connect to the SPI peripheral clock and set it through RCC-> apb2enr.
2. Connect the peripheral clock of the reused gpio, which is also set through RCC-> apb2enr
For more information about how to connect the gpio clock, see section 8.1.4 of the stm32 reference manual.
As mentioned in the manual: For the multiplexing output function, the port must be configured as the multiplexing output mode (push-pull or open-Miss ).
3. Set the reusable gpio to push the output and set the clock.
It cannot be set as an open or missing output. When it is set to open or leak output, the output on the oscilloscope is a sawtooth wave, rather than a square wave.
4. Configure Spix-> CR1 to set the SPI working mode. Finally enable SPI
5. send and receive data.
The same function can be used to send and receive data. Because SPI synchronizes input and output, data is already accepted when sending data.
Configure spi1CodeAs follows:

 Void Spigot Init ( Void ) {RCC-> apb2enr | = 1 <12; // Enable the spi1 clock RCC-> apb2enr | = 1 <2; // Configure the output of the take Function Gpioa-> CRL & = 0x000fffff; gpioa-> CRL | = 0xbbb00000; // Pa5.6.7 multiplexing, push and pull the output 50 m clock (cannot be configured as an open/drain, otherwise the output is a sawtooth wave) Gpioa-> ODR | = 0x7 <5; spi1-> CR1 | = 0 <11; // 8bit Data Format Spi1-> CR1 | = 0 <10; // Full duplex mode Spi1-> CR1 | = 1 <9; // Software NSS Management Spi1-> CR1 | = 1 <8; spi1-> CR1 | = 0 <7; // Msbfirst Spi1-> CR1 | = 7 <3; // Set the clock fsck = FCPU/256 Spi1-> CR1 | = 1 <2; // SPI host Spi1-> CR1 | = 1 <1; // In idle mode, sck is 1 cpol = 1 Spi1-> CR1 | = 1 <0; // Data sampling starts from the second time edge Spi1-> CR1 | = 1 <6;// Enable SPI }

Now you can read and write data:

 
U8 spigot readwritebyte (u8 data ){// While (spi1-> Sr & 1 <7) = 0); // wait for spi1 to be idleWhile(Spi1-> Sr & 1 <1) = 0 );// The buffer waiting for sending is empty.Spi1-> DR = data;While(Spi1-> Sr & 1 <0) = 0 );ReturnSpi1-> Dr ;}

Technorati labels: cm3, stm32, SPI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.