硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4098|回复: 8
收起左侧

[NUCLEO-H743ZI] 【STM32H743实验例程】实验4:STM32H743的HAL库延迟使用TIM7,方便后面移植各种主流RTOS

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107034
QQ
发表于 2018-4-4 01:40:21 | 显示全部楼层 |阅读模式
实验介绍
开发平台:官方STM32H743 NUCLEO板子
开发环境:MDK5.25正式版
软件版本:
(1)CMSIS软件包 V5.3.0
(2)H7的HAL库版本 V1.2.0
例程下载:
实验4:STM32H743的HAL库延迟使用TIM7.7z (1.07 MB, 下载次数: 294)



  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32h7xx_hal_timebase_tim.c
  4.   * @author  MCD Application Team
  5.   * @version V1.2.0
  6.   * @date    29-December-2017
  7.   * @brief   HAL time base based on the hardware TIM.
  8.   *   
  9.   *          This file overrides the native HAL time base functions (defined as weak)
  10.   *          the TIM time base:
  11.   *           + Intializes the TIM peripheral generate a Period elapsed Event each 1ms
  12.   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
  13.   *
  14.   ******************************************************************************
  15.   * @attention
  16.   *
  17.   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  18.   *
  19.   * Redistribution and use in source and binary forms, with or without modification,
  20.   * are permitted provided that the following conditions are met:
  21.   *   1. Redistributions of source code must retain the above copyright notice,
  22.   *      this list of conditions and the following disclaimer.
  23.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  24.   *      this list of conditions and the following disclaimer in the documentation
  25.   *      and/or other materials provided with the distribution.
  26.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  27.   *      may be used to endorse or promote products derived from this software
  28.   *      without specific prior written permission.
  29.   *
  30.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  34.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.   *
  41.   ******************************************************************************
  42.   */

  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32h7xx_hal.h"
  45. /** @addtogroup STM32H7xx_HAL_Driver
  46.   * @{
  47.   */

  48. /** @addtogroup HAL_TimeBase_TIM
  49.   * @{
  50.   */

  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. TIM_HandleTypeDef        TimHandle;
  56. /* Private function prototypes -----------------------------------------------*/
  57. void TIM7_IRQHandler(void);
  58. /* Private functions ---------------------------------------------------------*/

  59. /**
  60.   * @brief  This function configures the TIM7 as a time base source.
  61.   *         The time source is configured to have 1ms time base with a dedicated
  62.   *         Tick interrupt priority.
  63.   * @note   This function is called  automatically at the beginning of program after
  64.   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  65.   * @param  TickPriority: Tick interrupt priority.
  66.   * @retval HAL status
  67.   */
  68. HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
  69. {
  70.   RCC_ClkInitTypeDef    clkconfig;
  71.   uint32_t              uwTimclock, uwAPB1Prescaler = 0U;
  72.   uint32_t              uwPrescalerValue = 0U;
  73.   uint32_t              pFLatency;
  74.   
  75.     /*Configure the TIM7 IRQ priority */
  76.   HAL_NVIC_SetPriority(TIM7_IRQn, TickPriority ,0U);
  77.   
  78.   /* Enable the TIM7 global Interrupt */
  79.   HAL_NVIC_EnableIRQ(TIM7_IRQn);
  80.   
  81.   /* Enable TIM7 clock */
  82.   __HAL_RCC_TIM7_CLK_ENABLE();
  83.   
  84.   /* Get clock configuration */
  85.   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  86.   
  87.   /* Get APB1 prescaler */
  88.   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  89.   
  90.   /* Compute TIM7 clock */
  91.   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  92.   {
  93.     uwTimclock = HAL_RCC_GetPCLK1Freq();
  94.   }
  95.   else
  96.   {
  97.     uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
  98.   }
  99.   
  100.   /* Compute the prescaler value to have TIM7 counter clock equal to 1MHz */
  101.   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  102.   
  103.   /* Initialize TIM7 */
  104.   TimHandle.Instance = TIM7;
  105.   
  106.   /* Initialize TIMx peripheral as follow:
  107.   + Period = [(TIM7CLK/1000) - 1]. to have a (1/1000) s time base.
  108.   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  109.   + ClockDivision = 0
  110.   + Counter direction = Up
  111.   */
  112.   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  113.   TimHandle.Init.Prescaler = uwPrescalerValue;
  114.   TimHandle.Init.ClockDivision = 0;
  115.   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  116.   if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  117.   {
  118.     /* Start the TIM time Base generation in interrupt mode */
  119.     return HAL_TIM_Base_Start_IT(&TimHandle);
  120.   }
  121.   
  122.   /* Return function status */
  123.   return HAL_ERROR;
  124. }

  125. /**
  126.   * @brief  Suspend Tick increment.
  127.   * @note   Disable the tick increment by disabling TIM7 update interrupt.
  128.   * @param  None
  129.   * @retval None
  130.   */
  131. void HAL_SuspendTick(void)
  132. {
  133.   /* Disable TIM7 update Interrupt */
  134.   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  135. }

  136. /**
  137.   * @brief  Resume Tick increment.
  138.   * @note   Enable the tick increment by Enabling TIM7 update interrupt.
  139.   * @param  None
  140.   * @retval None
  141.   */
  142. void HAL_ResumeTick(void)
  143. {
  144.   /* Enable TIM7 Update interrupt */
  145.   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  146. }

  147. /**
  148.   * @brief  Period elapsed callback in non blocking mode
  149.   * @note   This function is called  when TIM7 interrupt took place, inside
  150.   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  151.   * a global variable "uwTick" used as application time base.
  152.   * @param  htim : TIM handle
  153.   * @retval None
  154.   */
  155. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  156. {
  157.   HAL_IncTick();
  158. }

  159. /**
  160.   * @brief  This function handles TIM interrupt request.
  161.   * @param  None
  162.   * @retval None
  163.   */
  164. void TIM7_IRQHandler(void)
  165. {
  166.   HAL_TIM_IRQHandler(&TimHandle);
  167. }

  168. /**
  169.   * @}
  170.   */

  171. /**
  172.   * @}
  173.   */

  174. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码


回复

使用道具 举报

610

主题

3062

回帖

4912

积分

至尊会员

积分
4912
发表于 2018-4-4 23:03:31 | 显示全部楼层
本帖最后由 hpdell 于 2018-4-5 00:09 编辑

你好,请教下,为何  HAL库延迟使用TIM7,而不是用 系统提供的system 作为延时啊 ??

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107034
QQ
 楼主| 发表于 2018-4-5 00:15:38 | 显示全部楼层
hpdell 发表于 2018-4-4 23:03
你好,请教下,为何  HAL库延迟使用TIM7,而不是用 系统提供的system 作为延时啊 ??

方便留给RTOS用。因为RTOS要使用滴答定时器。
回复

使用道具 举报

610

主题

3062

回帖

4912

积分

至尊会员

积分
4912
发表于 2018-4-5 07:29:17 | 显示全部楼层
eric2013 发表于 2018-4-5 00:15
方便留给RTOS用。因为RTOS要使用滴答定时器。

貌似有些明白了点了,就是 hal库使用的延时 与 rtos 使用的系统时钟分开
回复

使用道具 举报

6

主题

122

回帖

140

积分

初级会员

积分
140
发表于 2018-4-10 11:30:15 | 显示全部楼层
不是很懂,是不是可以理解为在RTOS下可以使用TIM7进行us级别的定时(或者延时)?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107034
QQ
 楼主| 发表于 2018-4-10 12:08:22 | 显示全部楼层
dy84082666 发表于 2018-4-10 11:30
不是很懂,是不是可以理解为在RTOS下可以使用TIM7进行us级别的定时(或者延时)?

是因为ST搞的那个HAL库使用滴答定时器作为超时判断了,很多的API函数都有用。而你的RTOS也要使用这个,怎么办,能各自配套一个当然是最好的,互不影响。
回复

使用道具 举报

6

主题

122

回帖

140

积分

初级会员

积分
140
发表于 2018-4-10 13:58:55 | 显示全部楼层
eric2013 发表于 2018-4-10 12:08
是因为ST搞的那个HAL库使用滴答定时器作为超时判断了,很多的API函数都有用。而你的RTOS也要使用这个,怎 ...

噢,是这样。谢谢解惑。完全明白了。
回复

使用道具 举报

56

主题

905

回帖

1073

积分

至尊会员

积分
1073
发表于 2018-4-10 19:47:04 | 显示全部楼层
一般来讲我只用ucos。 t7拿来作为usart了。为啥都看中t7,因为它太弱了好欺负。。。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107034
QQ
 楼主| 发表于 2018-4-11 01:33:32 | 显示全部楼层
roguebear 发表于 2018-4-10 19:47
一般来讲我只用ucos。 t7拿来作为usart了。为啥都看中t7,因为它太弱了好欺负。。。

,之前是T6,后来我一看,我还要用DAC,把T6改成T7了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-15 05:22 , Processed in 0.219405 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表