STM32 Input Capture Mode.
Capture mode is a special timer mode that works like this: when the logic level on a certain microcontroller pin changes, the value of the counter register is written to another register, called the capture register.
What is it for?
This mode lets you measure the pulse width or period of a signal.
The STM32 capture mode has some features:
The CCMR1 (for channels 1 and 2) and CCMR2 (for channels 3 and 4) registers, as well as the CCER and DIER registers, are used to configure capture mode.
Let's take a closer look at the bit fields of the CCMR2 register responsible for configuring timer channel 4, which is the one we'll configure in the example. I'd also like to note that the same register contains bit fields used when configuring the timer in compare mode.

CC4S — sets the direction of channel four (input or output). When the channel is configured as an input, it maps the capture signal to it
IC4PSC – sets the division factor for the capture signal
IC4F — configures the input filter: in addition to the number of samples during which the microcontroller won't respond to input signals, you can also set the sampling frequency. Essentially, we set the delay time from the arrival of an edge to the "confirming" sample.

Now let's look at the CCER register.

CC4E — enables/disables capture mode.
CC4P — sets the edge on which capture occurs: 0 — rising, 1 — falling.
And the DIER register.

CC4DE — enables DMA requests.
CC4IE — enables the capture interrupt.
After a capture occurs, a capture event is generated that sets the corresponding flag. This can lead to an interrupt and a DMA request, if they are enabled in the DIER register. In addition, a capture event can be generated by software by setting a bit field in the EGR event generation register:

The CC1G, CC2G, CC3G and CC4G bit fields let you generate an event on the corresponding capture/compare channel.
By the way, CCR1, CCR2, CCR3 and CCR4 are the capture registers in which the timer value is saved on the capture signal.
To monitor capture signal generation, the SR register has two flags for each channel.

CC4IF — set when a capture signal is generated; these flags are cleared by software or by reading the corresponding capture/compare register.
CC4OF — set if the CC4IF flag has not been cleared and another capture signal arrives. This flag is cleared by software by writing zero.
Now let's put this knowledge into practice: from a signal generator we'll feed a 50Hz sine wave to the TIM5_CH4 input and try to measure its period. To speed up the process, let's use DMA. Which MCU pin corresponds to channel 4 of TIM5 can be found in the MCU datasheet in the Pinouts and pin description section.

For DMA we need the address of the CCR4 register; here's how to find it. Open RM0008 and find the TIM5 start address in the Register boundary addresses table.
The offset of the CCR4 register can be found in the same document in the register map section.

The screenshot shows that the signal period is 20ms, which is correct. In the next article I'll describe how to measure the frequency of a signal.
What is it for?
This mode lets you measure the pulse width or period of a signal.
The STM32 capture mode has some features:
- you can choose which edge is active
- you can divide the input signal frequency with a prescaler (1, 2, 4, 8)
- each capture channel has a built-in input filter
- another timer can serve as the capture signal source
- each channel has two flags: the first is set when a capture occurs, the second if a capture occurs while the first flag is still set
The CCMR1 (for channels 1 and 2) and CCMR2 (for channels 3 and 4) registers, as well as the CCER and DIER registers, are used to configure capture mode.
Let's take a closer look at the bit fields of the CCMR2 register responsible for configuring timer channel 4, which is the one we'll configure in the example. I'd also like to note that the same register contains bit fields used when configuring the timer in compare mode.

CC4S — sets the direction of channel four (input or output). When the channel is configured as an input, it maps the capture signal to it
- 00 — the channel works as an output
- 01 — the channel works as an input, capture signal — TI4
- 10 — the channel works as an input, capture signal — TI3
- 11 — the channel works as an input, capture signal — TRC
IC4PSC – sets the division factor for the capture signal
- 00 — no prescaler, the capture signal IC1PS is generated on every event
- 01 — the capture signal is generated on every second event
- 10 — the capture signal is generated on every fourth event
- 11 — the capture signal is generated on every eighth event
IC4F — configures the input filter: in addition to the number of samples during which the microcontroller won't respond to input signals, you can also set the sampling frequency. Essentially, we set the delay time from the arrival of an edge to the "confirming" sample.

Now let's look at the CCER register.

CC4E — enables/disables capture mode.
CC4P — sets the edge on which capture occurs: 0 — rising, 1 — falling.
And the DIER register.

CC4DE — enables DMA requests.
CC4IE — enables the capture interrupt.
After a capture occurs, a capture event is generated that sets the corresponding flag. This can lead to an interrupt and a DMA request, if they are enabled in the DIER register. In addition, a capture event can be generated by software by setting a bit field in the EGR event generation register:

The CC1G, CC2G, CC3G and CC4G bit fields let you generate an event on the corresponding capture/compare channel.
By the way, CCR1, CCR2, CCR3 and CCR4 are the capture registers in which the timer value is saved on the capture signal.
To monitor capture signal generation, the SR register has two flags for each channel.

CC4IF — set when a capture signal is generated; these flags are cleared by software or by reading the corresponding capture/compare register.
CC4OF — set if the CC4IF flag has not been cleared and another capture signal arrives. This flag is cleared by software by writing zero.
Now let's put this knowledge into practice: from a signal generator we'll feed a 50Hz sine wave to the TIM5_CH4 input and try to measure its period. To speed up the process, let's use DMA. Which MCU pin corresponds to channel 4 of TIM5 can be found in the MCU datasheet in the Pinouts and pin description section.

For DMA we need the address of the CCR4 register; here's how to find it. Open RM0008 and find the TIM5 start address in the Register boundary addresses table.
The offset of the CCR4 register can be found in the same document in the register map section.

#include "stm32f10x.h"
#define TIM5_CCR4_Address ((u32)0x40000C00+0x40)
#define DMA_BUFF_SIZE 2
uint16_t buff[DMA_BUFF_SIZE];//Buffer
uint16_t volatile T;
void DMA2_Channel1_IRQHandler (void)
{
T = (buff[1] > buff[0]) ? (buff[1] - buff[0]) : (65535+ buff[1] - buff[0]);
DMA2->IFCR |= DMA_IFCR_CGIF1;
}
void Init_DMA(void)
{
RCC->AHBENR |= RCC_AHBENR_DMA2EN; //Enable clocking of the DMA module
DMA2_Channel1->CPAR = TIM5_CCR4_Address; //Set the peripheral address - the TIM5 capture register CCR4
DMA2_Channel1->CMAR = (uint32_t)buff; //Set the memory address - the base address of the array in RAM
DMA2_Channel1->CCR &= ~DMA_CCR1_DIR; //Set the data transfer direction, from peripheral to memory
DMA2_Channel1->CNDTR = DMA_BUFF_SIZE; //Number of values to transfer
DMA2_Channel1->CCR &= ~DMA_CCR1_PINC; //Don't increment the peripheral address after each transfer
DMA2_Channel1->CCR |= DMA_CCR1_MINC; //Increment the memory address after each transfer.
DMA2_Channel1->CCR |= DMA_CCR1_PSIZE_0; //Peripheral data size - 16 bits
DMA2_Channel1->CCR |= DMA_CCR1_MSIZE_0; //Memory data size - 16 bits
DMA2_Channel1->CCR |= DMA_CCR1_PL; //Priority - very high
DMA2_Channel1->CCR |= DMA_CCR1_CIRC; //Enable DMA circular mode
DMA2_Channel1->CCR |= DMA_CCR1_TCIE;//Enable the transfer complete interrupt
DMA2_Channel1->CCR |= DMA_CCR1_EN; //Enable DMA channel 1
}
int main(void)
{
Init_DMA();
//enable clocking of port A, alternate functions and the timer
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
TIM5->PSC = 56000-1;//new frequency 1Khz
TIM5->CCMR2 |= TIM_CCMR2_CC4S_0;//select TI4 for TIM5_CH4
TIM5->CCMR2 &= ~(TIM_CCMR2_IC4F | TIM_CCMR2_IC4PSC);//no filter and no prescaler
TIM5->CCER &= ~TIM_CCER_CC4P;//capture on the rising edge
TIM5->CCER |= TIM_CCER_CC4E;//enable capture mode for channel 4
TIM5->DIER |= TIM_DIER_CC4DE;//enable DMA requests
//TIM5->DIER |= TIM_DIER_CC4IE; //enable the capture interrupt
TIM5->CR1 |= TIM_CR1_CEN; //start the counter
//NVIC->ISER[1] |= NVIC_ISER_SETENA_18; //TIM5 Interrupt
NVIC->ISER[1] |= NVIC_ISER_SETENA_24; //DMA Interrupt
while(1)
{
}
}
The screenshot shows that the signal period is 20ms, which is correct. In the next article I'll describe how to measure the frequency of a signal.

