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

[复制链接]
2993|2
 楼主| gejigeji521 发表于 2016-10-29 19:18 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 15/02/06 10:23a $
  6. * @brief
  7. *           Show how to generate time-out reset system event while WDT time-out reset delay period expired.
  8. *
  9. * @note
  10. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M058S.h"

  14. #define PLL_CLOCK           50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global Interface Variables Declarations                                                                 */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. volatile uint8_t g_u8IsWDTTimeoutINT = 0;


  19. /**
  20. * [url=home.php?mod=space&uid=247401]@brief[/url]       IRQ Handler for WDT and WWDT Interrupt
  21. *
  22. * @param       None
  23. *
  24. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  25. *
  26. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The WDT_IRQHandler is default IRQ of WDT and WWDT, declared in startup_M058S.s.
  27. */
  28. void WDT_IRQHandler(void)
  29. {
  30.     if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
  31.     {
  32.         /* Clear WDT time-out interrupt flag */
  33.         WDT_CLEAR_TIMEOUT_INT_FLAG();

  34.         g_u8IsWDTTimeoutINT = 1;

  35.         printf("WDT time-out interrupt occurred.\n");
  36.     }
  37. }

  38. void SYS_Init(void)
  39. {
  40.     /*---------------------------------------------------------------------------------------------------------*/
  41.     /* Init System Clock                                                                                       */
  42.     /*---------------------------------------------------------------------------------------------------------*/
  43.     /* Enable HIRC 22.1184MHz clock */
  44.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  45.     /* Waiting for HIRC clock ready */
  46.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  47.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  48.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  49.     /* Enable external XTAL 12MHz and LIRC 10KHz clock */
  50.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk);

  51.     /* Waiting for external XTAL and LIRC clock ready */
  52.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC10K_STB_Msk);

  53.     /* Set core clock as PLL_CLOCK from PLL */
  54.     CLK_SetCoreClock(PLL_CLOCK);

  55.     /* Enable peripheral clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);
  57.     CLK_EnableModuleClock(WDT_MODULE);

  58.     /* Peripheral clock source */
  59.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  60.     CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0);

  61.     /*---------------------------------------------------------------------------------------------------------*/
  62.     /* Init I/O Multi-function                                                                                 */
  63.     /*---------------------------------------------------------------------------------------------------------*/
  64.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  65.     SYS->P3_MFP = SYS_MFP_P30_RXD | SYS_MFP_P31_TXD;
  66. }

  67. void UART0_Init(void)
  68. {
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Init UART                                                                                               */
  71.     /*---------------------------------------------------------------------------------------------------------*/
  72.     /* Reset IP */
  73.     SYS_ResetModule(UART0_RST);

  74.     /* Configure UART0 and set UART0 Baudrate */
  75.     UART_Open(UART0, 115200);
  76. }

  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /*  MAIN function                                                                                          */
  79. /*---------------------------------------------------------------------------------------------------------*/
  80. int main(void)
  81. {
  82.     /* Unlock protected registers */
  83.     SYS_UnlockReg();

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

  86.     /* Init UART0 for printf */
  87.     UART0_Init();

  88.     if(WDT_GET_RESET_FLAG() == 1)
  89.     {
  90.         /* Use P0.0 to check time-out period time */
  91.         GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  92.         P00 = 1;
  93.         P00 = 0;

  94.         WDT_CLEAR_RESET_FLAG();
  95.         printf("\n*** WDT time-out reset occurred ***\n");
  96.         while(1);
  97.     }

  98.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  99.     printf("+--------------------------------------+\n");
  100.     printf("|    WDT Time-out Reset Sample Code    |\n");
  101.     printf("+--------------------------------------+\n\n");

  102.     printf("# WDT Settings:\n");
  103.     printf("  Clock source is 10 kHz; Enable interrupt; Time-out interval is 2^14 * WDT clock.\n");
  104.     printf("# When WDT start counting, system will generate a WDT time-out interrupt after 1.6384 ~ 1.7408 s.\n");
  105.     printf("  Measure P0.0 low period to check time-out interval and do not reload WDT counter.\n\n");

  106.     /* Use P0.0 to check time-out period time */
  107.     GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  108.     P00 = 1;
  109.     P00 = 0;

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

  113.     /* Enable WDT time-out reset function and select time-out interval to 2^14 * WDT clock then start WDT counting */
  114.     g_u8IsWDTTimeoutINT = 0;
  115.     WDT_Open(WDT_TIMEOUT_2POW14, 0, TRUE, FALSE);

  116.     /* Enable WDT interrupt function */
  117.     WDT_EnableInt();

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

  120.     /* Check if WDT time-out interrupt occurred */
  121.     while(g_u8IsWDTTimeoutINT == 0);

  122.     P00 = 1;

  123.     while(1);
  124. }

  125. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| gejigeji521 发表于 2016-10-29 19:19 | 显示全部楼层
看似复杂的系统初始化配置,实际上分析一遍,就很简单了。。
捉虫天师 发表于 2016-10-30 12:51 | 显示全部楼层
  WDT_Open(WDT_TIMEOUT_2POW14, 0, TRUE, FALSE);
跟那个中断的很像,关键这第三个参数是TRUE。。。一字之差,千差万别。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

2465

帖子

8

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