[DemoCode下载] 看门狗超时唤醒复位

[复制链接]
1038|2
 楼主| 小明的同学 发表于 2024-4-21 15:34 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 6 $
  5. * $Date: 18/07/10 11:21a $
  6. * @brief
  7. *           Demonstrate how to cause WDT time-out reset system event while WDT time-out reset delay period expired.
  8. *
  9. * @note
  10. * SPDX-License-Identifier: Apache-2.0
  11. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  12. *****************************************************************************/
  13. #include <stdio.h>
  14. #include "NuMicro.h"

  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global Interface Variables Declarations                                                                 */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. extern int IsDebugFifoEmpty(void);
  19. volatile uint32_t g_u32WDTINTCounts;
  20. volatile uint8_t g_u8IsWDTWakeupINT;

  21. /**
  22. * [url=home.php?mod=space&uid=247401]@brief[/url]       IRQ Handler for WDT and WWDT Interrupt
  23. *
  24. * @param       None
  25. *
  26. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  27. *
  28. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The WDT_IRQHandler is default IRQ of WDT and WWDT, declared in startup_M031Series.s.
  29. */
  30. void WDT_IRQHandler(void)
  31. {
  32.     if(g_u32WDTINTCounts < 10)
  33.         WDT_RESET_COUNTER();

  34.     if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
  35.     {
  36.         /* Clear WDT time-out interrupt flag */
  37.         WDT_CLEAR_TIMEOUT_INT_FLAG();

  38.         g_u32WDTINTCounts++;
  39.     }

  40.     if(WDT_GET_TIMEOUT_WAKEUP_FLAG() == 1)
  41.     {
  42.         /* Clear WDT time-out wake-up flag */
  43.         WDT_CLEAR_TIMEOUT_WAKEUP_FLAG();

  44.         g_u8IsWDTWakeupINT = 1;
  45.     }
  46. }

  47. void SYS_Init(void)
  48. {
  49.     /* Unlock protected registers */
  50.     SYS_UnlockReg();

  51.     /* Enable HIRC clock */
  52.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  53.     /* Waiting for HIRC clock ready */
  54.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  55.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  56.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  57.     /* Enable UART0 clock */
  58.     CLK_EnableModuleClock(UART0_MODULE);

  59.     /* Switch UART0 clock source to HIRC */
  60.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  61.     /* Switch WDT clock source to LIRC */
  62.     CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDTSEL_LIRC, 0);

  63.     /* Enable WDT clock */
  64.     CLK_EnableModuleClock(WDT_MODULE);

  65.     /* Update System Core Clock */
  66.     SystemCoreClockUpdate();

  67.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  68.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk))
  69.                     |(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  70.     /* Lock protected registers */
  71.     SYS_LockReg();

  72. }

  73. /*---------------------------------------------------------------------------------------------------------*/
  74. /*  MAIN function                                                                                          */
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. int main(void)
  77. {
  78.     /* Unlock protected registers */
  79.     SYS_UnlockReg();

  80.     /* Init System, peripheral clock and multi-function I/O */
  81.     SYS_Init();

  82.     /* Lock protected registers */
  83.     SYS_LockReg();

  84.     /* Init UART0 to 115200-8n1 for print message */
  85.     UART_Open(UART0, 115200);

  86.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  87.     printf("+----------------------------------------+\n");
  88.     printf("|    WDT Time-out Wake-up Sample Code    |\n");
  89.     printf("+----------------------------------------+\n\n");
  90.     /* To check if system has been reset by WDT time-out reset or not */
  91.     if(WDT_GET_RESET_FLAG() == 1)
  92.     {
  93.         WDT_CLEAR_RESET_FLAG();
  94.         printf("*** System has been reset by WDT time-out event ***\n\n");
  95.         while(1);
  96.     }

  97.     printf("# WDT Settings:\n");
  98.     printf("    - Clock source is LIRC                  \n");
  99.     printf("    - Time-out interval is 2^14 * WDT clock \n");
  100.     printf("      (around 0.4266 ~ 0.4270 s)            \n");
  101.     printf("    - Interrupt enable                      \n");
  102.     printf("    - Wake-up function enable               \n");
  103.     printf("    - Reset function enable                 \n");
  104.     printf("    - Reset delay period is 18 * WDT clock  \n");
  105.     printf("# System will generate a WDT time-out interrupt event after 0.4266 ~ 0.4270 s, \n");
  106.     printf("    and will be wake up from power-down mode also.\n");
  107.     printf("    (Use PA.0 high/low period time to check WDT time-out interval)\n");
  108.     printf("# When WDT interrupt counts large than 10, \n");
  109.     printf("    we will not reset WDT counter value and system will be reset immediately by WDT time-out reset signal.\n");

  110.     /* Use PA.0 to check WWDT compare match interrupt period time */
  111.     GPIO_SetMode(PA, BIT0, GPIO_MODE_OUTPUT);

  112.     PA0 = 1;

  113.     /* Because of all bits can be written in WDT Control Register are write-protected;
  114.        To program it needs to disable register protection first. */
  115.     SYS_UnlockReg();

  116.     /* Configure WDT settings and start WDT counting */
  117.     g_u32WDTINTCounts = g_u8IsWDTWakeupINT = 0;
  118.     WDT_Open(WDT_TIMEOUT_2POW14, WDT_RESET_DELAY_18CLK, TRUE, TRUE);

  119.     /* Enable WDT NVIC */
  120.     NVIC_EnableIRQ(WDT_IRQn);

  121.     /* Enable WDT interrupt function */
  122.     WDT_EnableInt();

  123.     while(1)
  124.     {
  125.         /* System enter to Power-down */
  126.         /* To program PWRCTL register, it needs to disable register protection first. */
  127.         SYS_UnlockReg();
  128.         printf("\nSystem enter to power-down mode ...\n");
  129.         /* To check if all the debug messages are finished */
  130.         while(IsDebugFifoEmpty() == 0);

  131.         CLK_PowerDown();

  132.         /* Check if WDT time-out interrupt and wake-up occurred or not */
  133.         while(g_u8IsWDTWakeupINT == 0);

  134.         g_u8IsWDTWakeupINT = 0;
  135.         PA0 ^= 1;

  136.         printf("System has been waken up done. WDT interrupt counts: %d.\n\n", g_u32WDTINTCounts);
  137.     }
  138. }

  139. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/


 楼主| 小明的同学 发表于 2024-4-21 15:34 | 显示全部楼层
你们开发单片机系统会用看门狗吗?
21mengnan 发表于 2024-4-21 18:19 | 显示全部楼层
看着不错。看门狗的用法挺简单。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部