STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
board_time.c
Go to the documentation of this file.
1#include "board_time.h"
2
3#include "main.h"
4
6
7static volatile uint32_t pps_edge_us;
8static volatile uint32_t pps_count;
9
10/*
11 * TIM2's kernel clock (RM0468 Rev 3, RCC_CFGR.TIMPRE, p.371): with TIMPRE = 0
12 * it equals HCLK when the APB1 divider is 1 or 2, else 2 x PCLK1; with
13 * TIMPRE = 1, HCLK when the divider is 1, 2 or 4, else 4 x PCLK1.
14 */
15static uint32_t tim2_kernel_hz(void) {
16 uint32_t ppre = READ_BIT(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1);
17 bool timpre = READ_BIT(RCC->CFGR, RCC_CFGR_TIMPRE) != 0U;
18 bool hclk = ppre == RCC_D2CFGR_D2PPRE1_DIV1 ||
20 (timpre && ppre == RCC_D2CFGR_D2PPRE1_DIV4);
21 if (hclk) {
22 return HAL_RCC_GetHCLKFreq();
23 }
24 return (timpre ? 4U : 2U) * HAL_RCC_GetPCLK1Freq();
25}
26
27bool board_time_start(void) {
28 bool one_mhz = tim2_kernel_hz() / (htim2.Init.Prescaler + 1U) == 1000000U;
29 /* Starts the counter and the CH4 capture interrupt together. */
30 bool started = HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_4) == HAL_OK;
31 return one_mhz && started;
32}
33
34uint32_t board_time_us(void) {
35 return htim2.Instance->CNT;
36}
37
39 board_pps_t pps;
40 uint32_t count;
41 do { /* consistent pair even if a capture lands in between */
43 pps.edge_us = pps_edge_us;
44 } while (count != pps_count);
45 pps.count = count;
46 return pps;
47}
48
49/* HAL weak override: TIM2 CH4 rising edge = GPS TIMEPULSE (PB11). */
board_pps_t board_time_last_pps(void)
Definition board_time.c:38
static uint32_t tim2_kernel_hz(void)
Definition board_time.c:15
static volatile uint32_t pps_count
Definition board_time.c:8
uint32_t board_time_us(void)
Definition board_time.c:34
bool board_time_start(void)
Definition board_time.c:27
static volatile uint32_t pps_edge_us
Definition board_time.c:7
#define READ_BIT(REG, BIT)
Definition stm32h7xx.h:211
#define RCC_D2CFGR_D2PPRE1_DIV4
APB1 clock divided by 4.
#define RCC_CFGR_TIMPRE
0x00008000
#define RCC_D2CFGR_D2PPRE1_DIV1
APB1 clock not divided.
#define RCC_D2CFGR_D2PPRE1_DIV2
APB1 clock divided by 2.
#define RCC_D2CFGR_D2PPRE1
D1PPRE1[2:0] bits (APB1 prescaler).
#define TIM2
#define RCC
uint32_t HAL_RCC_GetHCLKFreq(void)
uint32_t HAL_RCC_GetPCLK1Freq(void)
#define TIM_CHANNEL_4
Capture/compare channel 4 identifier.
HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
uint32_t HAL_TIM_ReadCapturedValue(const TIM_HandleTypeDef *htim, uint32_t Channel)
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
Definition board_time.c:50
@ HAL_TIM_ACTIVE_CHANNEL_4
The active channel is 4.
size_type count(const key_type &key) const
Definition json.hpp:19264
TIM_HandleTypeDef htim2
Definition main.c:62
@ HAL_OK
TIM Time Base Handle Structure definition.
TIM_TypeDef * Instance
Register base address.
HAL_TIM_ActiveChannel Channel
Active channel.
uint32_t count
Definition board_time.h:30
uint32_t edge_us
Definition board_time.h:29