]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_M4_AMP_STM32H745I_Discovery_IAR/CM7/stm32h7xx_hal_timebase_tim.c
Add M7/M4 AMP demo.
[freertos] / FreeRTOS / Demo / CORTEX_M7_M4_AMP_STM32H745I_Discovery_IAR / CM7 / stm32h7xx_hal_timebase_tim.c
1 /**\r
2   ******************************************************************************\r
3   * @file    FreeRTOS/FreeRTOS_AMP_Dual_RTOS/CM7/Src/stm32h7xx_hal_timebase_tim.c\r
4   * @author  MCD Application Team\r
5   * @brief   HAL time base based on the hardware TIM.\r
6   *    \r
7   *          This file overrides the native HAL time base functions (defined as weak)\r
8   *          the TIM time base:\r
9   *           + Intializes the TIM peripheral generate a Period elapsed Event each 1ms\r
10   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms\r
11   * \r
12   ******************************************************************************\r
13   * @attention\r
14   *\r
15   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.\r
16   * All rights reserved.</center></h2>\r
17   *\r
18   * This software component is licensed by ST under BSD 3-Clause license,\r
19   * the "License"; You may not use this file except in compliance with the\r
20   * License. You may obtain a copy of the License at:\r
21   *                        opensource.org/licenses/BSD-3-Clause\r
22   *\r
23   ******************************************************************************\r
24   */\r
25 \r
26 /* Includes ------------------------------------------------------------------*/\r
27 #include "stm32h7xx_hal.h"\r
28 \r
29 /* Private typedef -----------------------------------------------------------*/\r
30 /* Private define ------------------------------------------------------------*/\r
31 /* Private macro -------------------------------------------------------------*/\r
32 /* Private variables ---------------------------------------------------------*/\r
33 TIM_HandleTypeDef        TimHandle;\r
34 /* Private function prototypes -----------------------------------------------*/\r
35 void TIM6_DAC_IRQHandler(void);\r
36 /* Private functions ---------------------------------------------------------*/\r
37 \r
38 /**\r
39   * @brief  This function configures the TIM6 as a time base source. \r
40   *         The time source is configured to have 1ms time base with a dedicated \r
41   *         Tick interrupt priority. \r
42   * @note   This function is called  automatically at the beginning of program after\r
43   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). \r
44   * @param  TickPriority: Tick interrupt priority.\r
45   * @retval HAL status\r
46   */\r
47 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)\r
48 {\r
49   RCC_ClkInitTypeDef    clkconfig;\r
50   uint32_t              uwTimclock, uwAPB1Prescaler = 0U;\r
51   uint32_t              uwPrescalerValue = 0U;\r
52   uint32_t              pFLatency;\r
53   \r
54     /*Configure the TIM6 IRQ priority */\r
55   HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U);\r
56   \r
57   /* Enable the TIM6 global Interrupt */\r
58   HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);\r
59   \r
60   /* Enable TIM6 clock */\r
61   __HAL_RCC_TIM6_CLK_ENABLE();\r
62   \r
63   /* Get clock configuration */\r
64   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);\r
65   \r
66   /* Get APB1 prescaler */\r
67   uwAPB1Prescaler = clkconfig.APB1CLKDivider;\r
68   \r
69   /* Compute TIM6 clock */\r
70   if (uwAPB1Prescaler == RCC_HCLK_DIV1) \r
71   {\r
72     uwTimclock = HAL_RCC_GetPCLK1Freq();\r
73   }\r
74   else\r
75   {\r
76     uwTimclock = 2*HAL_RCC_GetPCLK1Freq();\r
77   }\r
78   \r
79   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */\r
80   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);\r
81   \r
82   /* Initialize TIM6 */\r
83   TimHandle.Instance = TIM6;\r
84   \r
85   /* Initialize TIMx peripheral as follow:\r
86   + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.\r
87   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.\r
88   + ClockDivision = 0\r
89   + Counter direction = Up\r
90   */\r
91   TimHandle.Init.Period = (1000000U / 1000U) - 1U;\r
92   TimHandle.Init.Prescaler = uwPrescalerValue;\r
93   TimHandle.Init.ClockDivision = 0;\r
94   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;\r
95   if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK)\r
96   {\r
97     /* Start the TIM time Base generation in interrupt mode */\r
98     return HAL_TIM_Base_Start_IT(&TimHandle);\r
99   }\r
100   \r
101   /* Return function status */\r
102   return HAL_ERROR;\r
103 }\r
104 \r
105 /**\r
106   * @brief  Suspend Tick increment.\r
107   * @note   Disable the tick increment by disabling TIM6 update interrupt.\r
108   * @param  None\r
109   * @retval None\r
110   */\r
111 void HAL_SuspendTick(void)\r
112 {\r
113   /* Disable TIM6 update Interrupt */\r
114   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);\r
115 }\r
116 \r
117 /**\r
118   * @brief  Resume Tick increment.\r
119   * @note   Enable the tick increment by Enabling TIM6 update interrupt.\r
120   * @param  None\r
121   * @retval None\r
122   */\r
123 void HAL_ResumeTick(void)\r
124 {\r
125   /* Enable TIM6 Update interrupt */\r
126   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);\r
127 }\r
128 \r
129 /**\r
130   * @brief  Period elapsed callback in non blocking mode\r
131   * @note   This function is called  when TIM6 interrupt took place, inside\r
132   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment\r
133   * a global variable "uwTick" used as application time base.\r
134   * @param  htim : TIM handle\r
135   * @retval None\r
136   */\r
137 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)\r
138 {\r
139   HAL_IncTick();\r
140 }\r
141 \r
142 /**\r
143   * @brief  This function handles TIM interrupt request.\r
144   * @param  None\r
145   * @retval None\r
146   */\r
147 void TIM6_DAC_IRQHandler(void)\r
148 {\r
149   HAL_TIM_IRQHandler(&TimHandle);\r
150 }\r
151 \r
152 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r