[DemoCode下载] M058S省电有大招,掉电

[复制链接]
852|6
 楼主| zhuotuzi 发表于 2017-1-28 10:29 | 显示全部楼层 |阅读模式
  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_RXD | SYS_MFP_P31_TXD | SYS_MFP_P34_T0;
  85. }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  150.     while(1);
  151. }

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


 楼主| zhuotuzi 发表于 2017-1-28 10:30 | 显示全部楼层
void PowerDownFunction(void)
{
    printf("\nSystem enter to power-down mode ...\n");

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

    SCB->SCR = 4;

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

    __WFI();
}
关键内容。
 楼主| zhuotuzi 发表于 2017-1-28 10:31 | 显示全部楼层
如果这些节能函数能够作为库函数就OK了。
643757107 发表于 2017-1-28 12:31 | 显示全部楼层
  /* Enable peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(TMR0_MODULE);

    /* Peripheral clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
    CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_LIRC, 0);
稳稳の幸福 发表于 2017-1-28 12:59 | 显示全部楼层
/* Open Timer0 frequency to 2 Hz in toggle-output mode */
    TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1);
没想到还可以这样做。
捉虫天师 发表于 2017-1-29 17:12 | 显示全部楼层
/* Enable peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(TMR0_MODULE);
这些应用的外设的时钟都是可以控制的,这也是为了节能而设计。
稳稳の幸福 发表于 2017-1-30 08:37 | 显示全部楼层
没有例程也真是不好操作。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3375

帖子

7

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