- Create a new project with STM32CubeMX or ..IDE.
- Configure TIM1 clock source to Internal Clock
- Set counter mode Up.
- Set Channel1 to Input Capture direct mode.on Both Edges.
- TIM1 DMA settings: Add TIM1_CH1 DMA in cyclic mode, leave other defaults.
- Generate the code for your IDE.
- Start the timer in main.c
/* USER CODE BEGIN 2 */
HAL_TIM_IC_Start_DMA(&htim1, TIM_CHANNEL_1, edge_buffer, EDGE_BUFFER_LENGTH);
/* USER CODE END 2 */
in main.c at global level add callbacks
/* USER CODE BEGIN 0 */
#define EDGE_BUFFER_LENGTH 256
uint16_t edge_buffer[EDGE_BUFFER_LENGTH];
void process(uint16_t edges[], size_t len)
{
// compute edge arrival time differences and decide which FM0 cases you have observed
}
void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
process(&edge_buffer[0], EDGE_BUFFER_LENGTH/2);
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
process(&edge_buffer[EDGE_BUFFER_LENGTH/2], EDGE_BUFFER_LENGTH/2);
}
/* USER CODE END 0 */
Implement the missing parts. Use a debugger early and often, to better understand what the code does. Tune parameters to your needs (buffer size, prescaler,...).
hth
KnarfB