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