0
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file stm32l4xx_hal_timebase_TIM.c
|
|
4 * @brief HAL time base based on the hardware TIM.
|
|
5 ******************************************************************************
|
|
6 *
|
|
7 * COPYRIGHT(c) 2017 STMicroelectronics
|
|
8 *
|
|
9 * Redistribution and use in source and binary forms, with or without modification,
|
|
10 * are permitted provided that the following conditions are met:
|
|
11 * 1. Redistributions of source code must retain the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer.
|
|
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
14 * this list of conditions and the following disclaimer in the documentation
|
|
15 * and/or other materials provided with the distribution.
|
|
16 * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
17 * may be used to endorse or promote products derived from this software
|
|
18 * without specific prior written permission.
|
|
19 *
|
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 *
|
|
31 ******************************************************************************
|
|
32 */
|
|
33
|
|
34 /* Includes ------------------------------------------------------------------*/
|
|
35 #include "stm32l4xx_hal.h"
|
|
36 #include "stm32l4xx_hal_tim.h"
|
|
37 /** @addtogroup STM32F7xx_HAL_Examples
|
|
38 * @{
|
|
39 */
|
|
40
|
|
41 /** @addtogroup HAL_TimeBase
|
|
42 * @{
|
|
43 */
|
|
44
|
|
45 /* Private typedef -----------------------------------------------------------*/
|
|
46 /* Private define ------------------------------------------------------------*/
|
|
47 /* Private macro -------------------------------------------------------------*/
|
|
48 /* Private variables ---------------------------------------------------------*/
|
|
49 TIM_HandleTypeDef htim6;
|
|
50 uint32_t uwIncrementState = 0;
|
|
51 /* Private function prototypes -----------------------------------------------*/
|
|
52 /* Private functions ---------------------------------------------------------*/
|
|
53
|
|
54 /**
|
|
55 * @brief This function configures the TIM6 as a time base source.
|
|
56 * The time source is configured to have 1ms time base with a dedicated
|
|
57 * Tick interrupt priority.
|
|
58 * @note This function is called automatically at the beginning of program after
|
|
59 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
|
|
60 * @param TickPriority: Tick interrupt priorty.
|
|
61 * @retval HAL status
|
|
62 */
|
|
63 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
|
64 {
|
|
65 RCC_ClkInitTypeDef clkconfig;
|
|
66 uint32_t uwTimclock = 0;
|
|
67 uint32_t uwPrescalerValue = 0;
|
|
68 uint32_t pFLatency;
|
|
69
|
|
70 /*Configure the TIM6 IRQ priority */
|
|
71 HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0);
|
|
72
|
|
73 /* Enable the TIM6 global Interrupt */
|
|
74 HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
|
|
75
|
|
76 /* Enable TIM6 clock */
|
|
77 __HAL_RCC_TIM6_CLK_ENABLE();
|
|
78
|
|
79 /* Get clock configuration */
|
|
80 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
|
|
81
|
|
82 /* Compute TIM6 clock */
|
4
|
83 uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
|
0
|
84
|
|
85 /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
|
|
86 uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
|
|
87
|
|
88 /* Initialize TIM6 */
|
|
89 htim6.Instance = TIM6;
|
|
90
|
|
91 /* Initialize TIMx peripheral as follow:
|
|
92 + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
|
|
93 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
|
|
94 + ClockDivision = 0
|
|
95 + Counter direction = Up
|
|
96 */
|
|
97 htim6.Init.Period = (1000000 / 1000) - 1;
|
|
98 htim6.Init.Prescaler = uwPrescalerValue;
|
|
99 htim6.Init.ClockDivision = 0;
|
|
100 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
101 if(HAL_TIM_Base_Init(&htim6) == HAL_OK)
|
|
102 {
|
|
103 /* Start the TIM time Base generation in interrupt mode */
|
|
104 return HAL_TIM_Base_Start_IT(&htim6);
|
|
105 }
|
|
106
|
|
107 /* Return function status */
|
|
108 return HAL_ERROR;
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * @brief Suspend Tick increment.
|
|
113 * @note Disable the tick increment by disabling TIM6 update interrupt.
|
|
114 * @param None
|
|
115 * @retval None
|
|
116 */
|
|
117 void HAL_SuspendTick(void)
|
|
118 {
|
|
119 /* Disable TIM6 update Interrupt */
|
|
120 __HAL_TIM_DISABLE_IT(&htim6, TIM_IT_UPDATE);
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * @brief Resume Tick increment.
|
|
125 * @note Enable the tick increment by Enabling TIM6 update interrupt.
|
|
126 * @param None
|
|
127 * @retval None
|
|
128 */
|
|
129 void HAL_ResumeTick(void)
|
|
130 {
|
|
131 /* Enable TIM6 Update interrupt */
|
|
132 __HAL_TIM_ENABLE_IT(&htim6, TIM_IT_UPDATE);
|
|
133 }
|
|
134
|
|
135 /**
|
|
136 * @}
|
|
137 */
|
|
138
|
|
139 /**
|
|
140 * @}
|
|
141 */
|
|
142
|
|
143 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|