[DemoCode下载] WDT_PowerDown看门狗定时器掉电唤醒系统

[复制链接]
1723|8
 楼主| gejigeji521 发表于 2016-10-29 19:30 | 显示全部楼层 |阅读模式
  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. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use WDT time-out interrupt event to wake-up system.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M058S.h"

  12. #define PLL_CLOCK           50000000


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global Interface Variables Declarations                                                                 */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. volatile uint8_t g_u8IsWDTWakeupINT = 0;


  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /*  Function for System Entry to Power-down Mode                                                           */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. void PowerDownFunction(void)
  21. {
  22.     printf("System enter to power-down mode.\n\n");

  23.     /* To check if all the debug messages are finished */
  24.     UART_WAIT_TX_EMPTY(UART0);

  25.     SCB->SCR = 4;

  26.     /* To program PWRCON register, it needs to disable register protection first. */
  27.     CLK->PWRCON = (CLK->PWRCON & ~(CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WAIT_CPU_Msk)) |
  28.                   CLK_PWRCON_PD_WAIT_CPU_Msk | CLK_PWRCON_PD_WU_INT_EN_Msk;
  29.     CLK->PWRCON |= CLK_PWRCON_PWR_DOWN_EN_Msk;

  30.     __WFI();
  31. }

  32. /**
  33. * @brief       IRQ Handler for WDT and WWDT Interrupt
  34. *
  35. * @param       None
  36. *
  37. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  38. *
  39. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The WDT_IRQHandler is default IRQ of WDT and WWDT, declared in startup_M058S.s.
  40. */
  41. void WDT_IRQHandler(void)
  42. {
  43.     if((WDT_GET_TIMEOUT_INT_FLAG() == 1) && (WDT_GET_TIMEOUT_WAKEUP_FLAG() == 1))
  44.     {
  45.         /* Clear WDT time-out interrupt and wake-up flag */
  46.         WDT_CLEAR_TIMEOUT_INT_FLAG();
  47.         WDT_CLEAR_TIMEOUT_WAKEUP_FLAG();

  48.         g_u8IsWDTWakeupINT = 1;

  49.         printf("WDT time-out wake-up interrupt occurred.\n");
  50.     }
  51. }

  52. void SYS_Init(void)
  53. {
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init System Clock                                                                                       */
  56.     /*---------------------------------------------------------------------------------------------------------*/
  57.     /* Enable HIRC 22.1184MHz clock */
  58.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  59.     /* Waiting for HIRC clock ready */
  60.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

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

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

  67.     /* Set core clock as PLL_CLOCK from PLL */
  68.     CLK_SetCoreClock(PLL_CLOCK);

  69.     /* Enable peripheral clock */
  70.     CLK_EnableModuleClock(UART0_MODULE);
  71.     CLK_EnableModuleClock(WDT_MODULE);

  72.     /* Peripheral clock source */
  73.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  74.     CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0);

  75.     /*---------------------------------------------------------------------------------------------------------*/
  76.     /* Init I/O Multi-function                                                                                 */
  77.     /*---------------------------------------------------------------------------------------------------------*/
  78.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  79.     SYS->P3_MFP = SYS_MFP_P30_RXD | SYS_MFP_P31_TXD;
  80. }

  81. void UART0_Init(void)
  82. {
  83.     /*---------------------------------------------------------------------------------------------------------*/
  84.     /* Init UART                                                                                               */
  85.     /*---------------------------------------------------------------------------------------------------------*/
  86.     /* Reset IP */
  87.     SYS_ResetModule(UART0_RST);

  88.     /* Configure UART0 and set UART0 Baudrate */
  89.     UART_Open(UART0, 115200);
  90. }

  91. /*---------------------------------------------------------------------------------------------------------*/
  92. /*  MAIN function                                                                                          */
  93. /*---------------------------------------------------------------------------------------------------------*/
  94. int main(void)
  95. {
  96.     /* Unlock protected registers */
  97.     SYS_UnlockReg();

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

  100.     /* Init UART0 for printf */
  101.     UART0_Init();

  102.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  103.     printf("+----------------------------------------------+\n");
  104.     printf("|    WDT Power-down and Wake-up Sample Code    |\n");
  105.     printf("+----------------------------------------------+\n\n");

  106.     printf("# WDT Settings:\n");
  107.     printf("  Clock source is 10 kHz; Enable interrupt; Enable Wake-up; Time-out interval is 2^16 * WDT clock.\n");
  108.     printf("# When WDT statr counting, system will generate a WDT time-out interrupt after 6.5536 ~ 6.656 s.\n");
  109.     printf("  Measure P0.0 low period to check time-out interval and system can be waken-up by WDT time-out event.\n\n");

  110.     /* Use P0.0 to check time-out period time */
  111.     GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  112.     P00 = 1;
  113.     P00 = 0;

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

  117.     /* Enable WDT wake-up function and select time-out interval to 2^16 * WDT clock then start WDT counting */
  118.     g_u8IsWDTWakeupINT = 0;
  119.     WDT_Open(WDT_TIMEOUT_2POW16, 0, FALSE, TRUE);

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

  122.     /* Enable WDT NVIC */
  123.     NVIC_EnableIRQ(WDT_IRQn);

  124.     /* System entry into Power-down Mode */
  125.     PowerDownFunction();

  126.     /* Check if WDT time-out interrupt and wake-up interrupt flag occurred */
  127.     while(1)
  128.     {
  129.         if(((CLK->PWRCON & CLK_PWRCON_PD_WU_STS_Msk) == CLK_PWRCON_PD_WU_STS_Msk) && (g_u8IsWDTWakeupINT == 1))
  130.             break;
  131.     }

  132.     P00 = 1;

  133.     /* Clear Power-down wake-up interrupt flag */
  134.     CLK->PWRCON |= CLK_PWRCON_PD_WU_STS_Msk;

  135.     /* Disable WDT counting */
  136.     WDT_Close();

  137.     while(1);
  138. }

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


 楼主| gejigeji521 发表于 2016-10-29 19:35 | 显示全部楼层
这个例程也说明看门狗定时器是跟系统两码事的,就是为了保护系统用的一个外设。。。
捉虫天师 发表于 2016-10-30 13:01 | 显示全部楼层
有点复杂,我再去找找教程怎么讲的。
huangcunxiake 发表于 2016-10-30 15:11 | 显示全部楼层
看门狗定时器不仅仅可以看门狗,还可以作为定时用,毕竟定时触发后,就好比普通的定时器,只不过,你这个时候之前进行喂狗就行。或者复位后做一些事情,复位后会有寄存器可以查询到复位原因
huangcunxiake 发表于 2016-10-30 15:17 | 显示全部楼层
void WDT_IRQHandler(void)
{
    if((WDT_GET_TIMEOUT_INT_FLAG() == 1) && (WDT_GET_TIMEOUT_WAKEUP_FLAG() == 1))
    {
        /* Clear WDT time-out interrupt and wake-up flag */
        WDT_CLEAR_TIMEOUT_INT_FLAG();
        WDT_CLEAR_TIMEOUT_WAKEUP_FLAG();

        g_u8IsWDTWakeupINT = 1;

        printf("WDT time-out wake-up interrupt occurred.\n");
    }
}
这里面用的就是中断后处理的内容。
 楼主| gejigeji521 发表于 2016-11-12 22:51 | 显示全部楼层
嗯,中断后处理的内容。清理中断标志,唤醒,打印。
天灵灵地灵灵 发表于 2016-11-13 22:59 | 显示全部楼层
懂了,掉电模式就是那种手机关机的模式。
捉虫天师 发表于 2016-11-14 11:26 | 显示全部楼层
这些个模式用于低功耗系统很有用
 楼主| gejigeji521 发表于 2016-12-8 11:07 | 显示全部楼层
各种中断什么的都是为了低功耗设计。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

198

主题

2509

帖子

8

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