[DemoCode下载] M261通过欠压中断唤醒系统

[复制链接]
818|6
 楼主| wahahaheihei 发表于 2019-12-11 19:24 | 显示全部楼层 |阅读模式
  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. * @brief
  5. *           Show how to wake up system form Power-down mode by brown-out detector interrupt.
  6. *
  7. *
  8. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NuMicro.h"


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /*  Function for System Entry to Power Down Mode                                                           */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. void PowerDownFunction(void)
  17. {
  18.     /* Check if all the debug messages are finished */
  19.     UART_WAIT_TX_EMPTY(DEBUG_PORT);

  20.     /* Enable Power-down mode wake-up interrupt */
  21.     CLK->PWRCTL |= CLK_PWRCTL_PDWKIEN_Msk;

  22.     /* Enter to Power-down mode */
  23.     CLK_PowerDown();

  24. }

  25. /*---------------------------------------------------------------------------------------------------------*/
  26. /*  Brown Out Detector IRQ Handler                                                                         */
  27. /*---------------------------------------------------------------------------------------------------------*/
  28. void BOD_IRQHandler(void)
  29. {
  30.     /* Clear BOD Interrupt Flag */
  31.     SYS_CLEAR_BOD_INT_FLAG();

  32.     printf("Brown Out is Detected.\n");
  33. }

  34. /*---------------------------------------------------------------------------------------------------------*/
  35. /*  Power-down Mode Wake-up IRQ Handler                                                                    */
  36. /*---------------------------------------------------------------------------------------------------------*/
  37. void PWRWU_IRQHandler(void)
  38. {
  39.     /* Check system power down mode wake-up interrupt status flag */
  40.     if(CLK->PWRCTL & CLK_PWRCTL_PDWKIF_Msk)
  41.     {
  42.         /* Clear system power down wake-up interrupt flag */
  43.         CLK->PWRCTL |= CLK_PWRCTL_PDWKIF_Msk;

  44.         printf("System wake-up from Power-down mode.\n");
  45.     }
  46. }

  47. void SYS_Init(void)
  48. {

  49.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  50.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
  51.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;

  52.     /*---------------------------------------------------------------------------------------------------------*/
  53.     /* Init System Clock                                                                                       */
  54.     /*---------------------------------------------------------------------------------------------------------*/

  55.     /* Enable HIRC clock */
  56.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  57.     /* Wait for HIRC clock ready */
  58.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  59.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  60.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  61.     /* Enable HXT clock */
  62.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  63.     /* Wait for HXT clock ready */
  64.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  65.     /* Enable PLL */
  66.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  67.     /* Waiting for PLL stable */
  68.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  69.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  70.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  71.     /* Update System Core Clock */
  72.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  73.     SystemCoreClockUpdate();

  74.     /* Enable UART module clock */
  75.     CLK_EnableModuleClock(UART0_MODULE);

  76.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  77.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

  78.     /*---------------------------------------------------------------------------------------------------------*/
  79.     /* Init I/O Multi-function                                                                                 */
  80.     /*---------------------------------------------------------------------------------------------------------*/

  81.     /* Set multi-function pins for UART0 RXD and TXD */
  82.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  83. }

  84. void UART0_Init(void)
  85. {
  86.     /*---------------------------------------------------------------------------------------------------------*/
  87.     /* Init UART                                                                                               */
  88.     /*---------------------------------------------------------------------------------------------------------*/
  89.     /* Reset UART0 */
  90.     SYS_ResetModule(UART0_RST);

  91.     /* Configure UART0 and set UART0 baud rate */
  92.     UART_Open(UART0, 115200);
  93. }

  94. /*---------------------------------------------------------------------------------------------------------*/
  95. /*  Main Function                                                                                          */
  96. /*---------------------------------------------------------------------------------------------------------*/
  97. int32_t main(void)
  98. {

  99.     /* Unlock protected registers */
  100.     SYS_UnlockReg();

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

  103.     /* Lock protected registers */
  104.     SYS_LockReg();

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

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

  111.     /* Unlock protected registers before setting Brown-out detector function and Power-down mode */
  112.     SYS_UnlockReg();

  113.     /* Enable Brown-out detector function */
  114.     SYS_ENABLE_BOD();

  115.     /* Set Brown-out detector voltage level as 3.0V */
  116.     SYS_SET_BOD_LEVEL(SYS_BODCTL_BODVL_3_0V);

  117.     /* Enable Brown-out detector interrupt function */
  118.     SYS_DISABLE_BOD_RST();

  119.     /* Enable Brown-out detector and Power-down wake-up interrupt */
  120.     NVIC_EnableIRQ(BOD_IRQn);
  121.     NVIC_EnableIRQ(PWRWU_IRQn);

  122.     printf("System enter to Power-down mode.\n");
  123.     printf("System wake-up if VDD voltage is lower than 3.0V.\n\n");

  124.     /* Enter to Power-down mode */
  125.     PowerDownFunction();

  126.     /* Wait for Power-down mode wake-up interrupt happen */
  127.     while(1);
  128. }


  129. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/


antusheng 发表于 2019-12-11 21:36 | 显示全部楼层
这个还可以用于掉电保护数据。
antusheng 发表于 2019-12-11 21:37 | 显示全部楼层
欠压中断中把重要数据写入Flash。以前看到过这个操作的例子
xixi2017 发表于 2019-12-11 23:54 | 显示全部楼层
竟然还可以唤醒,给力
xixi2017 发表于 2019-12-11 23:54 | 显示全部楼层
貌似大部分的中断都可以唤醒休眠的系统吧
xixi2017 发表于 2019-12-11 23:55 | 显示全部楼层
休眠中大部分的中断还可以正常触发吗
huangcunxiake 发表于 2019-12-12 11:00 | 显示全部楼层
这个技术新唐给力,TI的芯片都没这个功能。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3223

帖子

12

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