[DemoCode下载] 定时器唤醒掉电模式

[复制链接]
1339|1
 楼主| xinxianshi 发表于 2025-1-25 13:39 | 显示全部楼层 |阅读模式
  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:22a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use timer-0 toggle-output 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_u8IsTMR0WakeupFlag = 0;
  17. volatile uint32_t g_au32TMRINTCount[4] = {0};


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

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

  26.     SCB->SCR = 4;

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

  31.     __WFI();
  32. }

  33. /**
  34. * @brief       Timer0 IRQ
  35. *
  36. * @param       None
  37. *
  38. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  39. *
  40. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_M058S.s.
  41. */
  42. void TMR0_IRQHandler(void)
  43. {
  44.     if(TIMER_GetIntFlag(TIMER0) == 1)
  45.     {
  46.         /* Clear Timer0 time-out interrupt flag */
  47.         TIMER_ClearIntFlag(TIMER0);

  48.         g_au32TMRINTCount[0]++;
  49.     }

  50.     if(TIMER_GetWakeupFlag(TIMER0) == 1)
  51.     {
  52.         /* Clear Timer0 wake-up flag */
  53.         TIMER_ClearWakeupFlag(TIMER0);

  54.         g_u8IsTMR0WakeupFlag = 1;
  55.     }
  56. }

  57. void SYS_Init(void)
  58. {
  59.     /*---------------------------------------------------------------------------------------------------------*/
  60.     /* Init System Clock                                                                                       */
  61.     /*---------------------------------------------------------------------------------------------------------*/
  62.     /* Enable HIRC 22.1184MHz clock */
  63.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  64.     /* Waiting for HIRC clock ready */
  65.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

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

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

  72.     /* Set core clock as PLL_CLOCK from PLL */
  73.     CLK_SetCoreClock(PLL_CLOCK);

  74.     /* Enable peripheral clock */
  75.     CLK_EnableModuleClock(UART0_MODULE);
  76.     CLK_EnableModuleClock(TMR0_MODULE);

  77.     /* Peripheral clock source */
  78.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  79.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_LIRC, 0);

  80.     /*---------------------------------------------------------------------------------------------------------*/
  81.     /* Init I/O Multi-function                                                                                 */
  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Set P3 multi-function pins for UART0 RXD, TXD, T0 */
  84.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P34_Msk);
  85.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD | SYS_MFP_P34_T0);
  86. }

  87. void UART0_Init(void)
  88. {
  89.     /*---------------------------------------------------------------------------------------------------------*/
  90.     /* Init UART                                                                                               */
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Reset IP */
  93.     SYS_ResetModule(UART0_RST);

  94.     /* Configure UART0 and set UART0 Baudrate */
  95.     UART_Open(UART0, 115200);
  96. }

  97. /*---------------------------------------------------------------------------------------------------------*/
  98. /*  MAIN function                                                                                          */
  99. /*---------------------------------------------------------------------------------------------------------*/
  100. int main(void)
  101. {
  102.     volatile uint32_t u32InitCount;

  103.     /* Unlock protected registers */
  104.     SYS_UnlockReg();

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

  107.     /* Init UART0 for printf */
  108.     UART0_Init();

  109.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  110.     printf("+-------------------------------------------------+\n");
  111.     printf("|    Timer0 Power-down and Wake-up Sample Code    |\n");
  112.     printf("+-------------------------------------------------+\n\n");

  113.     printf("# Timer Settings:\n");
  114.     printf("  Timer0: Clock source is LIRC(10 kHz); Toggle-output mode and frequency is 2 Hz; Enable interrupt and wake-up.\n");
  115.     printf("# System will enter to Power-down mode while Timer0 interrupt count is reaching 3;\n");
  116.     printf("  And be waken-up while Timer0 interrupt count is reaching 4.\n\n");

  117.     /* To program PWRCON register, it needs to disable register protection first. */
  118.     SYS_UnlockReg();

  119.     /* Open Timer0 frequency to 2 Hz in toggle-output mode */
  120.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1);

  121.     /* Enable Timer0 interrupt and wake-up function */
  122.     TIMER_EnableInt(TIMER0);
  123.     TIMER_EnableWakeup(TIMER0);

  124.     /* Enable Timer0 NVIC */
  125.     NVIC_EnableIRQ(TMR0_IRQn);

  126.     /* Start Timer0 counting */
  127.     TIMER_Start(TIMER0);

  128.     u32InitCount = g_u8IsTMR0WakeupFlag = g_au32TMRINTCount[0] = 0;
  129.     while(g_au32TMRINTCount[0] < 10)
  130.     {
  131.         if(g_au32TMRINTCount[0] != u32InitCount)
  132.         {
  133.             printf("Timer0 interrupt count - %d\n", g_au32TMRINTCount[0]);
  134.             if(g_au32TMRINTCount[0] == 3)
  135.             {
  136.                 PowerDownFunction();

  137.                 /* Check if Timer0 time-out interrupt and wake-up flag occurred */
  138.                 while(1)
  139.                 {
  140.                     if(((CLK->PWRCON & CLK_PWRCON_PD_WU_STS_Msk) == CLK_PWRCON_PD_WU_STS_Msk) && (g_u8IsTMR0WakeupFlag == 1))
  141.                         break;
  142.                 }
  143.                 printf("System has been waken-up done. (Timer0 interrupt count is %d)\n\n", g_au32TMRINTCount[0]);
  144.             }
  145.             u32InitCount = g_au32TMRINTCount[0];
  146.         }
  147.     }

  148.     /* Stop Timer0 counting */
  149.     TIMER_Close(TIMER0);

  150.     printf("*** PASS ***\n");

  151.     while(1);
  152. }

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


 楼主| xinxianshi 发表于 2025-1-25 13:46 | 显示全部楼层
定时器唤醒可以实现低功耗应用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1019

帖子

1

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