This post is translated from Chinese into English through AI.View Original
AI-generated summary
The document discusses the STM32F407 microcontroller, focusing on its peripheral interfaces, particularly GPIO and ADC.
### GPIO
- The STM32F4 has a set of GPIO controlled by seven 32-bit registers, including configuration, data, and control registers.
- Each GPIO port has 16 IO pins, with specific registers controlling modes, output types, speed, and pull-up/pull-down configurations.
### ADC
- The STM32F4 features three independent ADCs, with ADC1 and ADC2 capable of dual mode for higher sampling rates.
- The ADC is a 12-bit successive approximation type with 19 channels, allowing measurement of external and internal signals.
- It supports various sampling modes and can generate interrupts based on conversion events.
### SPI
- SPI (Serial Peripheral Interface) allows full-duplex communication between master and slave devices using shift registers.
- The document outlines the pin configuration and operational modes determined by CPOL and CPHA settings.
- It describes the SPI control, status, and data registers, detailing their functionalities and bit configurations.
1.1.1.1 GPIO Port Mode Register (GPIOx_MODER) (x=A.. I)#
Used to control the working mode of GPIO
Each group of GPIO has 16 IO ports, with two register bits controlling 1 IO. The reset value of PortA is 0 xA 800 0000, corresponding to the binary 1010 1000 0000 0000 0000 0000 0000 0000, indicating that PA 15/14/13 are in alternate function mode, while the other ports are in input mode.
1.1.1.2 GPIO Port Output Type Register (GPIOx_OTYPER)#
Used to control the output type of GPIO.
Does not take effect in input mode. The lower 16 bits are valid. In default output mode, the IO port is push-pull output.
1.1.1.3 GPIO Port Output Speed Register (GPIOx_OSPEEDR)#
Also only used in output mode.
1.1.1.4 GPIO Port Pull-Up/Pull-Down Register (GPIOx_PUPDR)#
STM 32 F 4 has 3 independently usable ADCs, among which ADC 1 and ADC 2 can be combined into dual mode to increase the sampling rate. The ADC of STM 32 is a 12-bit successive approximation ADC. It includes 19 channels, capable of measuring 16 external and 2 internal signal sources as well as the Vbat channel signal. The A/D conversion of these channels can be performed in single, continuous, scan, and discontinuous sampling modes. The converted results are stored in a 16-bit data register of LSB or MSB.
When any ADCx multi-channel performs a series of conversions in any order, grouped conversions are born, with two types of grouped conversions: regular group and injected group. The regular group allows up to 16 input channels for conversion, while the injected group allows up to 4 input channels for conversion.
"Injection" breaks the original state, equivalent to an interrupt. If the injected group starts during the regular group conversion, the regular group continues conversion only after the injected group conversion is completed.
The ADC conversion time calculation formula is $$
T_{CONV}=Sample_Time+TSAR\times ADC_CLK
The sampling time is controlled by the ADC_SMPR register.
ADC_CLK is generated by APB 2, and the division factor is set by PPRE 2 in the RCC_CFGR register, with division options of 2/4/6/8/16.
### Registers
## Timer
The basic characteristics table of the timer is as follows:

## SPI
SPI stands for Serial Peripheral Interface. The block diagram of SPI is as follows:

The pin information of SPI is:
1. MISO (Master In / Slave Out): Master device data input, slave device data output.
2. MOSI (Master Out / Slave In): Master device data output, slave device data input.
3. SCLK (Serial Clock): Clock signal, output from the master device.
4. CS (Chip Select): Slave device chip select signal, output from the master device.
==Working Principle==: In the SPI communication, both the slave and master have a Shift Register, and the master initiates a transmission by writing a Byte of data to its own shift register. The shift register transmits the byte to the slave via MOSI, while the slave transmits the contents of its byte shift register back to the master via MISO, thus achieving data exchange between the two shift registers. Therefore, if only write operations are performed, the master can ignore the received data. If the master wants to read data from the slave, it sends an empty byte to trigger the slave's transmission.
SPI supports full-duplex, half-duplex, and simplex transmission modes.
### SPI Working Modes
The working mode of SPI is determined by CPOL and CPHA, both of which have 0 and 1 states, thus SPI has four working modes.
| Working Mode | CPOL | CPHA | SCL Idle State | Sample Edge | Sample Time |
| ---- | ---- | ---- | -------- | ---- | ---- |
| 0 | 0 | 0 | Low Level | Rising Edge | Odd Edge |
| 1 | 0 | 1 | Low Level | Falling Edge | Even Edge |
| 2 | 1 | 0 | High Level | Falling Edge | Odd Edge |
| 3 | 1 | 1 | High Level | Rising Edge | Even Edge |
From the table, it can be seen that CPOL determines whether the SCL idle state is high or low, and CPHA determines whether sampling occurs on the odd or even edge of the clock.
### SPI Registers
#### SPI_CR 1 (SPI Control Register 1)

- Bit 11 DFF: Data frame format, 0: 8-bit data frame, 1: 16-bit
- Bit 10 RXONLY: Receive only. 0: full-duplex, 1: receive only
- Bit 7: Frame format. 0: send MSB first, 1: send LSB first. **This bit should not be modified during communication**
- Bit 6: SPI enable. 0: disable peripheral, 1: enable peripheral.
- Bit 5-3: BR[2:0]: Control baud rate.
- 
- Bit 2: Master mode selection. 0: slave mode, 1: master mode
- Bit 1: CPOL
- Bit 0: CPHA
#### SPI_SR (SPI Status Register)

- Bits 15-9 reserved, forced to 0
- Bit 8: Frame format error. 0: no frame format error, 1: frame format error
- Bit 7: Busy flag. 0: not busy, 1: SPI is in communication state or Tx buffer not empty
- Bit 1: Send buffer empty. 0: not empty, 1: empty
- Bit 0: Receive buffer not empty. 0: empty, 1: not empty
#### SPI_DR (SPI Data Register)

When the data frame is 8 bits, only the lower eight bits are used; when it is 16 bits, the entire register is used.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.