当前位置:   article > 正文

基于STM32HAL库(独立看门狗)-简述_hal库看门狗32kz

hal库看门狗32kz

目录

概述

一、开发环境

二、STM32CubeMx配置

三、编码 

四、运行结果

五、总结


概述

        一个成熟靠谱的项目,离不开“看门狗”的必选项,凡是人写的程序多少都会有出现bug的情况(或芯片外设受外界干扰导致故障程序卡死、跑飞的情况)。为了避免产品变成砖头,引入看门狗是很有必要,可以有效解决程序的跑飞(确保程序在大部分情况能正常运行)。

 来自百度百科解释(看门狗)
        
看门狗定时器(WDT,Watch Dog Timer)是单片机的一个组成部分,它实际上是一个计数器,一般给看门狗一个数字,程序开始运行后看门狗开始计数。如果程序运行正常,过一段时间CPU应发出指令让看门狗置零,重新开始计数。如果看门狗增加到设定值就认为程序没有正常工作,强制整个系统复位。

STM32的内置看门狗-(详细请移步参阅-STM32F4xx中文参考手册.pdf文档)

        STM32内置两个看门狗,提供了更高的安全性、时间的精确性和使用的灵活性。两个看门狗设备(独立看门狗、窗口看门狗)可以用来检测和解决由软件错误引起的故障。当计数器达到给定的超时值时,触发一个中断(仅适用窗口看门狗)或者产生系统复位。

1)、独立看门狗(IWDG)由专用的低速时钟(LSI)驱动(32kHz),即使主时钟发生故障它仍有效。独立看门狗适合应用于需要看门狗作为一个在主程序之外 能够完全独立工作,并且对时间精度要求低的场合。
2)、窗口看门狗由从APB1时钟(84MHz)分频后得到时钟驱动(42MHz)。通过可配置的时间窗口来检测应用程序非正常的过迟或过早操作。  窗口看门狗最适合那些要求看门狗在精确计时窗口起作用的程序。

一、开发环境

1、硬件平台
     STM32F401CEU6
     内部Flash : 512Kbytes,SARM : 96 Kbytes

二、STM32CubeMx配置

 2.1、系统时钟配置

2.2、下载调试配置

2.3、TIM配置(1ms中断)

2.4、usart1配置

2.5、独立看门狗(IWDG)

A) IWDG时钟预分频系数  32分频   
B) 计数器重装载值  4095   RLR

时钟频率LSI=32KHz, 一个看门狗时钟周期就是最短超时时间。(M4内核)
最长超时时间= (IWDG_RLR寄存器最大值)x 看门狗时钟周期
超出(溢出)时间计算:单位为 ms

                  Tout=((4×2^PRER) ×RLR)/LSI时钟频率
通过公式得出:Tout=(4x2^3)×4095/32=4095ms

对应不同预分频系数的PRER值:

详解:
       Tout 为看门狗溢出时间(单位为 ms); prer 为看门狗时钟预分频值( IWDG_PR 值),范围为 0~7; rlr 为看门狗的重装载值( IWDG_RLR 的值);
我们设定prer值为6(6 代表的是 256 分频, HAL 库中可以使用宏定义标识符IWDG_PRESCALER_256),rlr值为4095,那么就可以得到Tout=256×4095/32=32760ms。这样,看门狗的溢出时间就是32760ms。只要你在32.76秒钟之内,有一次写入 0XAAAA 到 IWDG_KR,就不会导致看门狗复位(当然写入多次也是可以的)。这里需要提醒大家的是,看门狗的时钟不是准确的 32Khz,所以在喂狗的时候,最好不要太晚了,否则,有可能发生看门狗复位。

2.6、生成代码

三、编码 

1、usart.c文件

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. UART_HandleTypeDef huart1;
  25. /* USART1 init function */
  26. void MX_USART1_UART_Init(void)
  27. {
  28. /* USER CODE BEGIN USART1_Init 0 */
  29. /* USER CODE END USART1_Init 0 */
  30. /* USER CODE BEGIN USART1_Init 1 */
  31. /* USER CODE END USART1_Init 1 */
  32. huart1.Instance = USART1;
  33. huart1.Init.BaudRate = 115200;
  34. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  35. huart1.Init.StopBits = UART_STOPBITS_1;
  36. huart1.Init.Parity = UART_PARITY_NONE;
  37. huart1.Init.Mode = UART_MODE_TX_RX;
  38. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  39. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  40. if (HAL_UART_Init(&huart1) != HAL_OK)
  41. {
  42. Error_Handler();
  43. }
  44. /* USER CODE BEGIN USART1_Init 2 */
  45. /* USER CODE END USART1_Init 2 */
  46. }
  47. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  48. {
  49. GPIO_InitTypeDef GPIO_InitStruct = {0};
  50. if(uartHandle->Instance==USART1)
  51. {
  52. /* USER CODE BEGIN USART1_MspInit 0 */
  53. /* USER CODE END USART1_MspInit 0 */
  54. /* USART1 clock enable */
  55. __HAL_RCC_USART1_CLK_ENABLE();
  56. __HAL_RCC_GPIOA_CLK_ENABLE();
  57. /**USART1 GPIO Configuration
  58. PA9 ------> USART1_TX
  59. PA10 ------> USART1_RX
  60. */
  61. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  62. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  63. GPIO_InitStruct.Pull = GPIO_NOPULL;
  64. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  65. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  66. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  67. /* USART1 interrupt Init */
  68. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  69. HAL_NVIC_EnableIRQ(USART1_IRQn);
  70. /* USER CODE BEGIN USART1_MspInit 1 */
  71. /* USER CODE END USART1_MspInit 1 */
  72. }
  73. }
  74. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  75. {
  76. if(uartHandle->Instance==USART1)
  77. {
  78. /* USER CODE BEGIN USART1_MspDeInit 0 */
  79. /* USER CODE END USART1_MspDeInit 0 */
  80. /* Peripheral clock disable */
  81. __HAL_RCC_USART1_CLK_DISABLE();
  82. /**USART1 GPIO Configuration
  83. PA9 ------> USART1_TX
  84. PA10 ------> USART1_RX
  85. */
  86. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  87. /* USART1 interrupt Deinit */
  88. HAL_NVIC_DisableIRQ(USART1_IRQn);
  89. /* USER CODE BEGIN USART1_MspDeInit 1 */
  90. /* USER CODE END USART1_MspDeInit 1 */
  91. }
  92. }
  93. /* USER CODE BEGIN 1 */
  94. #include "stdio.h"
  95. #ifdef __GNUC__
  96. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  97. set to 'Yes') calls __io_putchar() */
  98. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  99. #else
  100. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  101. #endif /* __GNUC__ */
  102. /**
  103. * @brief Retargets the C library printf function to the USART.
  104. * @param None
  105. * @retval None
  106. */
  107. PUTCHAR_PROTOTYPE
  108. {
  109. /* Place your implementation of fputc here */
  110. /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  111. HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  112. return ch;
  113. }
  114. int fgetc(FILE * f)
  115. {
  116. uint8_t ch = 0;
  117. HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xffff);
  118. return ch;
  119. }
  120. /* USER CODE END 1 */

2、 tim.c文件 (本示例使用“定时器”模拟任务来执行程序逻辑代码)

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file tim.c
  5. * @brief This file provides code for the configuration
  6. * of the TIM instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "tim.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. TIM_HandleTypeDef htim1;
  25. /* TIM1 init function */
  26. void MX_TIM1_Init(void)
  27. {
  28. /* USER CODE BEGIN TIM1_Init 0 */
  29. /* USER CODE END TIM1_Init 0 */
  30. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  31. TIM_MasterConfigTypeDef sMasterConfig = {0};
  32. /* USER CODE BEGIN TIM1_Init 1 */
  33. /* USER CODE END TIM1_Init 1 */
  34. htim1.Instance = TIM1;
  35. htim1.Init.Prescaler = 84-1;
  36. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  37. htim1.Init.Period = 1000-1;
  38. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  39. htim1.Init.RepetitionCounter = 0;
  40. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  41. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  46. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  51. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  52. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  53. {
  54. Error_Handler();
  55. }
  56. /* USER CODE BEGIN TIM1_Init 2 */
  57. /* USER CODE END TIM1_Init 2 */
  58. }
  59. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
  60. {
  61. if(tim_baseHandle->Instance==TIM1)
  62. {
  63. /* USER CODE BEGIN TIM1_MspInit 0 */
  64. /* USER CODE END TIM1_MspInit 0 */
  65. /* TIM1 clock enable */
  66. __HAL_RCC_TIM1_CLK_ENABLE();
  67. /* TIM1 interrupt Init */
  68. HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
  69. HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
  70. /* USER CODE BEGIN TIM1_MspInit 1 */
  71. /* USER CODE END TIM1_MspInit 1 */
  72. }
  73. }
  74. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
  75. {
  76. if(tim_baseHandle->Instance==TIM1)
  77. {
  78. /* USER CODE BEGIN TIM1_MspDeInit 0 */
  79. /* USER CODE END TIM1_MspDeInit 0 */
  80. /* Peripheral clock disable */
  81. __HAL_RCC_TIM1_CLK_DISABLE();
  82. /* TIM1 interrupt Deinit */
  83. HAL_NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
  84. /* USER CODE BEGIN TIM1_MspDeInit 1 */
  85. /* USER CODE END TIM1_MspDeInit 1 */
  86. }
  87. }
  88. /* USER CODE BEGIN 1 */
  89. #include "stdio.h"
  90. #include "iwdg.h"
  91. uint32_t timeCount_500ms = 0;
  92. uint32_t timeCount_1000ms = 0;
  93. uint8_t times_refresh = 0;
  94. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
  95. if(htim->Instance == TIM1){ //任务计数1ms
  96. timeCount_500ms++;
  97. timeCount_1000ms++;
  98. if(timeCount_500ms==500){
  99. timeCount_500ms = 0;
  100. printf("time + 500ms\n");
  101. }
  102. if(timeCount_1000ms==1000){
  103. timeCount_1000ms = 0;
  104. printf("time + 1s\n");
  105. HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); //LED翻转 --闪烁表示喂狗成功
  106. times_refresh++;
  107. if (times_refresh < 2) {
  108. HAL_IWDG_Refresh(&hiwdg); //喂狗
  109. printf("成功喂狗!!! \n");
  110. } else if (times_refresh == 5){
  111. times_refresh = 0;
  112. printf("超时喂狗!!! \n");
  113. }
  114. }
  115. }
  116. }
  117. /* USER CODE END 1 */

3、iwdg.c 文件 默认即可
       只需关注HAL库独立窗口狗以下的API即可:

  1. HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg); //看门狗初始化
  2. HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg); //喂狗

4、main.c文件

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 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. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "iwdg.h"
  22. #include "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "stdio.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /**
  49. * @brief The application entry point.
  50. * @retval int
  51. */
  52. int main(void)
  53. {
  54. /* USER CODE BEGIN 1 */
  55. /* USER CODE END 1 */
  56. /* MCU Configuration--------------------------------------------------------*/
  57. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58. HAL_Init();
  59. /* USER CODE BEGIN Init */
  60. /* USER CODE END Init */
  61. /* Configure the system clock */
  62. SystemClock_Config();
  63. /* USER CODE BEGIN SysInit */
  64. /* USER CODE END SysInit */
  65. /* Initialize all configured peripherals */
  66. MX_GPIO_Init();
  67. MX_USART1_UART_Init();
  68. MX_TIM1_Init();
  69. MX_IWDG_Init();
  70. /* USER CODE BEGIN 2 */
  71. HAL_TIM_Base_Start_IT(&htim1);
  72. printf("heihei iwdg \r\n");
  73. /* USER CODE END 2 */
  74. /* Infinite loop */
  75. /* USER CODE BEGIN WHILE */
  76. while (1)
  77. {
  78. /* USER CODE END WHILE */
  79. /* USER CODE BEGIN 3 */
  80. }
  81. /* USER CODE END 3 */
  82. }
  83. /**
  84. * @brief System Clock Configuration
  85. * @retval None
  86. */
  87. void SystemClock_Config(void)
  88. {
  89. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  90. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  91. /** Configure the main internal regulator output voltage
  92. */
  93. __HAL_RCC_PWR_CLK_ENABLE();
  94. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  95. /** Initializes the RCC Oscillators according to the specified parameters
  96. * in the RCC_OscInitTypeDef structure.
  97. */
  98. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  99. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  100. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  101. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  102. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  103. RCC_OscInitStruct.PLL.PLLM = 25;
  104. RCC_OscInitStruct.PLL.PLLN = 168;
  105. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  106. RCC_OscInitStruct.PLL.PLLQ = 4;
  107. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  108. {
  109. Error_Handler();
  110. }
  111. /** Initializes the CPU, AHB and APB buses clocks
  112. */
  113. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  114. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  115. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  116. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  117. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  118. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  119. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  120. {
  121. Error_Handler();
  122. }
  123. }
  124. /* USER CODE BEGIN 4 */
  125. /* USER CODE END 4 */
  126. /**
  127. * @brief This function is executed in case of error occurrence.
  128. * @retval None
  129. */
  130. void Error_Handler(void)
  131. {
  132. /* USER CODE BEGIN Error_Handler_Debug */
  133. /* User can add his own implementation to report the HAL error return state */
  134. __disable_irq();
  135. while (1)
  136. {
  137. }
  138. /* USER CODE END Error_Handler_Debug */
  139. }
  140. #ifdef USE_FULL_ASSERT
  141. /**
  142. * @brief Reports the name of the source file and the source line number
  143. * where the assert_param error has occurred.
  144. * @param file: pointer to the source file name
  145. * @param line: assert_param error line source number
  146. * @retval None
  147. */
  148. void assert_failed(uint8_t *file, uint32_t line)
  149. {
  150. /* USER CODE BEGIN 6 */
  151. /* User can add his own implementation to report the file name and line number,
  152. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  153. /* USER CODE END 6 */
  154. }
  155. #endif /* USE_FULL_ASSERT */

四、运行结果

五、总结

        好了,介绍完毕。有了它,再也不用担心,程序跑飞了,希望对你有所帮助,谢谢光临!感谢参阅。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/802565
推荐阅读
相关标签
  

闽ICP备14008679号