STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
stm32h7xx_hal_timebase_tim.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file stm32h7xx_hal_timebase_tim.c
5 * @brief HAL time base based on the hardware TIM.
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2026 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18/* USER CODE END Header */
19
20/* Includes ------------------------------------------------------------------*/
21#include "stm32h7xx_hal.h"
22#include "stm32h7xx_hal_tim.h"
23
24/* Private typedef -----------------------------------------------------------*/
25/* Private define ------------------------------------------------------------*/
26/* Private macro -------------------------------------------------------------*/
27/* Private variables ---------------------------------------------------------*/
29/* Private function prototypes -----------------------------------------------*/
30/* Private functions ---------------------------------------------------------*/
31
32/**
33 * @brief This function configures the TIM6 as a time base source.
34 * The time source is configured to have 1ms time base with a dedicated
35 * Tick interrupt priority.
36 * @note This function is called automatically at the beginning of program after
37 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
38 * @param TickPriority: Tick interrupt priority.
39 * @retval HAL status
40 */
41HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
42{
43 RCC_ClkInitTypeDef clkconfig;
44 uint32_t uwTimclock, uwAPB1Prescaler;
45 uint32_t uwPrescalerValue;
46 uint32_t pFLatency;
47
48 /*Configure the TIM6 IRQ priority */
49 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
50 {
51 HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0);
52
53 /* Enable the TIM6 global Interrupt */
55 uwTickPrio = TickPriority;
56 }
57 else
58 {
59 return HAL_ERROR;
60 }
61
62 /* Enable TIM6 clock */
64
65 /* Get clock configuration */
66 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
67
68 /* Get APB1 prescaler */
69 uwAPB1Prescaler = clkconfig.APB1CLKDivider;
70 /* Compute TIM6 clock */
71 if (uwAPB1Prescaler == RCC_HCLK_DIV1)
72 {
73 uwTimclock = HAL_RCC_GetPCLK1Freq();
74 }
75 else
76 {
77 uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
78 }
79
80 /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
81 uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
82
83 /* Initialize TIM6 */
84 htim6.Instance = TIM6;
85
86 /* Initialize TIMx peripheral as follow:
87 * Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
88 * Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
89 * ClockDivision = 0
90 * Counter direction = Up
91 */
92 htim6.Init.Period = (1000000U / 1000U) - 1U;
93 htim6.Init.Prescaler = uwPrescalerValue;
94 htim6.Init.ClockDivision = 0;
95 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
96
98 {
99
100 /* Start the TIM time Base generation in interrupt mode */
102 }
103
104 /* Return function status */
105 return HAL_ERROR;
106}
107
108/**
109 * @brief Suspend Tick increment.
110 * @note Disable the tick increment by disabling TIM6 update interrupt.
111 * @param None
112 * @retval None
113 */
115{
116 /* Disable TIM6 update Interrupt */
118}
119
120/**
121 * @brief Resume Tick increment.
122 * @note Enable the tick increment by Enabling TIM6 update interrupt.
123 * @param None
124 * @retval None
125 */
127{
128 /* Enable TIM6 Update interrupt */
130}
131
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
#define __NVIC_PRIO_BITS
CM7 uses 4 Bits for the Priority Levels.
void HAL_ResumeTick(void)
Resume Tick increment.
void HAL_SuspendTick(void)
Suspend Tick increment.
uint32_t uwTickPrio
#define TIM6
@ TIM6_DAC_IRQn
TIM6 global and DAC1&2 underrun error interrupts.
uint32_t HAL_RCC_GetPCLK1Freq(void)
void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
#define __HAL_RCC_TIM6_CLK_ENABLE()
#define RCC_HCLK_DIV1
#define TIM_COUNTERMODE_UP
Counter used as up-counter.
HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
#define __HAL_TIM_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the specified TIM interrupt.
#define __HAL_TIM_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the specified TIM interrupt.
#define TIM_IT_UPDATE
Update interrupt.
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_ERROR
@ HAL_OK
Header file of TIM HAL module.
TIM_HandleTypeDef htim6
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the TIM6 as a time base source.
RCC System, AHB and APB busses clock configuration structure definition.
uint32_t APB1CLKDivider
The APB1 clock (PCLK1) divider.
TIM Time Base Handle Structure definition.