[DemoCode下载] 掉电检测

[复制链接]
714|1
 楼主| huahuagg 发表于 2019-5-30 23:58 | 显示全部楼层 |阅读模式
se, TI, TE, pi, gp
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 11 $
  5. * $Date: 15/06/23 1:37p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Sample code which implements a function to test system state before
  7. *           entering power-down mode. If a system consumes more power than
  8. *           expected in power-down mode, this function can be used to check if
  9. *           there is any system setting that may cause power leakage.
  10. *
  11. * @note
  12. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  13. *****************************************************************************/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdint.h>
  18. #include "Nano1X2Series.h"
  19. #include "sys.h"

  20. #ifdef __DEBUG_MSG
  21. #define DEBUG_MSG   printf
  22. #else
  23. #define DEBUG_MSG(...)
  24. #endif


  25. /* External functions */


  26. /* Global variables */
  27. __IO int32_t   _Wakeup_Flag = 0;    /* 1 indicates system wake up from power down mode */
  28. __IO uint32_t  _Pin_Setting[11];    /* store Px_H_MFP and Px_L_MFP */
  29. __IO uint32_t  _PullUp_Setting[6];  /* store GPIOx_PUEN */



  30. /**
  31.   * @brief  Store original setting of multi-function pin selection.
  32.   * @param  None.
  33.   * [url=home.php?mod=space&uid=266161]@return[/url] None.
  34.   */
  35. void SavePinSetting()
  36. {
  37.     /* Save Pin selection setting */
  38.     _Pin_Setting[0] = SYS->PA_L_MFP;
  39.     _Pin_Setting[1] = SYS->PA_H_MFP;
  40.     _Pin_Setting[2] = SYS->PB_L_MFP;
  41.     _Pin_Setting[3] = SYS->PB_H_MFP;
  42.     _Pin_Setting[4] = SYS->PC_L_MFP;
  43.     _Pin_Setting[5] = SYS->PC_H_MFP;
  44.     _Pin_Setting[6] = SYS->PD_L_MFP;
  45.     _Pin_Setting[7] = SYS->PD_H_MFP;
  46.     _Pin_Setting[8] = SYS->PE_L_MFP;
  47.     _Pin_Setting[9] = SYS->PE_H_MFP;
  48.     _Pin_Setting[10] = SYS->PF_L_MFP;

  49.     /* Save Pull-up setting */
  50.     _PullUp_Setting[0] =  PA->PUEN;
  51.     _PullUp_Setting[1] =  PB->PUEN;
  52.     _PullUp_Setting[2] =  PC->PUEN;
  53.     _PullUp_Setting[3] =  PD->PUEN;
  54.     _PullUp_Setting[4] =  PE->PUEN;
  55.     _PullUp_Setting[5] =  PF->PUEN;
  56. }



  57. /**
  58.   * @brief  Save multi-function pin setting and then go to power down.
  59.   * @param  None.
  60.   * @return None.
  61.   */
  62. void Enter_PowerDown()
  63. {
  64.     /* Back up original setting */
  65.     SavePinSetting();

  66.     /* Set function pin to GPIO mode */
  67.     SYS->PA_L_MFP = 0;
  68.     SYS->PA_H_MFP = 0;
  69.     SYS->PB_L_MFP = 0;
  70.     //SYS->PB_H_MFP = 0;
  71.     SYS->PB_H_MFP = (SYS_PB_H_MFP_PB13_MFP_UART0_RX | SYS_PB_H_MFP_PB14_MFP_UART0_TX);
  72.     SYS->PC_L_MFP = 0;
  73.     SYS->PC_H_MFP = 0;
  74.     SYS->PD_L_MFP = 0;
  75.     SYS->PD_H_MFP = 0;
  76.     SYS->PE_L_MFP = 0;
  77.     SYS->PE_H_MFP = 0;
  78.     //SYS->PF_L_MFP = 0x0000FF00;   // exclude GPF3:HXT_OUT, GPF2:HXT_IN
  79.     SYS->PF_L_MFP = 0x00FFFF00; // exclude GPF5:ICE_DAT, GPF4:ICE_CLK, GPF3:HXT_OUT, GPF2:HXT_IN

  80.     /* Enable GPIO pull up */
  81.     PA->PUEN = 0xFFFF;
  82.     PB->PUEN = 0xFFFF;
  83.     PC->PUEN = 0xFFFF;
  84.     PD->PUEN = 0xFFFF;
  85.     PE->PUEN = 0xFFFF;
  86.     //PF->PUEN = 0x0033;      /* exclude GPF3 and GPF2 which are HXT OUT/IN */
  87.     PF->PUEN = 0x0003;      /* exclude GPF5:ICE_DAT, GPF4:ICE_CLK, GPF3:HXT_OUT, GPF2:HXT_IN */

  88.     //CLK->PWRCTL |= CLK_PWRCTL_PD_WK_IE_Msk;  /* Enable wake up interrupt source */
  89.     //NVIC_EnableIRQ(PDWU_IRQn);             /* Enable IRQ request for PDWU interrupt */

  90.     SYS_UnlockReg();
  91.     SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
  92.     CLK->PWRCTL |= (CLK_PWRCTL_PD_EN_Msk | CLK_PWRCTL_WK_DLY_Msk );

  93.     //__WFI();   /* system really enter power down here ! */
  94. }


  95. void PowerDown_Check()
  96. {
  97.     uint32_t i,j;
  98.     volatile uint32_t Tmp_NVIC_ISER;
  99.     uint32_t gpio_error_count = 0;
  100.     char *symbo_gpio[6] = {"A", "B", "C", "D", "E", "F"};
  101.     GPIO_T *tGPIO;
  102.     uint32_t SYS_MFP = 0xffffffff;

  103.     printf("\n/***** PowerDown Check *****/");

  104.     printf("\n/***** GPIO Check: *****/");

  105.     //save NVIC setting
  106.     Tmp_NVIC_ISER = NVIC->ISER[0];

  107.     //disables a device-specific interrupt in the NVIC interrupt controller
  108.     NVIC->ICER[0] = 0xFFFFFFFF;

  109.     for(j = 0; j < 11; j++) {
  110.         SYS_MFP = *((volatile unsigned int *)(SYS_BASE + 0x30 + (j *4)));

  111.         for(i = 0; i < 8; i++) {
  112.             if(SYS_MFP & (0x7 << (i*4))) {
  113.                 gpio_error_count++;
  114.                 printf("\nGP%s Pin %d not set GPIO !!", symbo_gpio[(j/2)], (i+(8*(j%2))) );
  115.             }
  116.         }
  117.     }

  118.     if( (SYS->PF_L_MFP & SYS_PF_L_MFP_PF2_MFP_XT1_IN) != SYS_PF_L_MFP_PF2_MFP_XT1_IN) {
  119.         printf("\nGPF Pin 2 not set HXT_OUT !!!");
  120.         printf("\nPlease Check GPF Pin 2 not connect crystal !!!");
  121.     }

  122.     if( (SYS->PF_L_MFP & SYS_PF_L_MFP_PF3_MFP_XT1_OUT) != SYS_PF_L_MFP_PF3_MFP_XT1_OUT) {
  123.         printf("\nGPF Pin 3 not set HXT_IN !!!");
  124.         printf("\nPlease Check GPF Pin 3 not connect crystal !!!");
  125.     }

  126.     for(j = 0; j < 6; j++) {
  127.         tGPIO =(GPIO_T*)((uint32_t)PA + (j * (0x40)));

  128.         for(i = 0; i < 16; i++) {
  129.             if((j == 4) & (i > 9)) // GPE
  130.                 break;

  131.             if((j == 5) & (i > 5)) // GPF
  132.                 break;

  133.             if(!(tGPIO->PIN & (1 << i))) {
  134.                 gpio_error_count++;
  135.                 printf("\nGP%s Pin %d can not pull high!!", symbo_gpio[j], i);
  136.             }
  137.         }
  138.     }

  139.     if(gpio_error_count)
  140.         printf("\nGPIO Check fail !!");
  141.     else
  142.         printf("\nGPIO Check pass");

  143.     printf("\n");

  144.     printf("\n/***** Clock Check: *****/");

  145.     if(CLK->PWRCTL & CLK_PWRCTL_HXT_EN)
  146.         printf("\nHXT Enable");
  147.     else
  148.         printf("\nHXT Disable");

  149.     if(CLK->PWRCTL & CLK_PWRCTL_LXT_EN)
  150.         printf("\nLXT Enable");
  151.     else
  152.         printf("\nLXT Disable");

  153.     if(CLK->PWRCTL & CLK_PWRCTL_HIRC_EN)
  154.         printf("\nHIRC Enable");
  155.     else
  156.         printf("\nHIRC Disable");

  157.     if(CLK->PWRCTL & CLK_PWRCTL_LIRC_EN)
  158.         printf("\nLIRC Enable");
  159.     else
  160.         printf("\nLIRC Disable");

  161.     printf("\n");
  162.     printf("\n/***** Power Down setting check: *****/");
  163.     if(CLK->PWRCTL & CLK_PWRCTL_PWRDOWN_EN)
  164.         printf("\nChip Power-down mode Enable ");
  165.     else
  166.         printf("\nChip Power-down mode Disable !!!");

  167.     if(SCB->SCR & 0x04)
  168.         printf("\nSCB->SCR set (0x%x) : ok", SCB->SCR);
  169.     else
  170.         printf("\nSCB->SCR set (0x%x) : error", SCB->SCR);

  171.     //Restore NVIC->ISER setting
  172.     NVIC->ISER[0] = Tmp_NVIC_ISER;

  173.     printf("\n");
  174.     printf("\n/***** Wake-up setting check: *****/");
  175.     if(CLK->PWRCTL & CLK_PWRCTL_DELY_EN)
  176.         printf("\nWake-up Delay Counter Enable");
  177.     else
  178.         printf("\nWake-up Delay Counter Disable");

  179.     if(CLK->PWRCTL & CLK_PWRCTL_WAKEINT_EN)
  180.         printf("\nPower-down Mode Wake-up Interrupt Enable");
  181.     else
  182.         printf("\nPower-down Mode Wake-up Interrupt Disable");

  183.     if( NVIC->ISER[0] & (1 << ((uint32_t)(PDWU_IRQn) & 0x1F)) )
  184.         printf("\nEnable PDWU_IRQn request for PDWU interrupt");
  185.     else
  186.         printf("\nDisable PDWU_IRQn request for PDWU interrupt");

  187.     printf("\n");
  188.     printf("\n/***** ISR check: *****/");
  189.     if(NVIC_GetPendingIRQ(PDWU_IRQn))
  190.         printf("\nPowerDown WakeUp Interrupt status is pending. (check fail)");
  191.     else
  192.         printf("\nPowerDown WakeUp Interrupt status is not pending. (check ok)");

  193.     if(CLK->WK_INTSTS & 0x1)
  194.         printf("\nWake-up Interrupt Status is set. (check fail)");
  195.     else
  196.         printf("\nWake-up Interrupt Status is not set. (check ok)");

  197.     printf("\n");
  198.     printf("\n/***** PowerDown Check End *****/");
  199. }

  200. void SYS_Init(void)
  201. {
  202.     /*---------------------------------------------------------------------------------------------------------*/
  203.     /* Init System Clock                                                                                       */
  204.     /*---------------------------------------------------------------------------------------------------------*/
  205.     /* Unlock protected registers */
  206.     SYS_UnlockReg();

  207.     /* Enable IP clock */
  208.     CLK->APBCLK |= CLK_APBCLK_UART0_EN; // UART0 Clock Enable

  209.     /* Select IP clock source */
  210.     CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk;
  211.     CLK->CLKSEL1 |= (0x3 << CLK_CLKSEL1_UART_S_Pos);// Clock source from HIRC

  212.     /* Update System Core Clock */
  213.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  214.     SystemCoreClockUpdate();

  215.     /*---------------------------------------------------------------------------------------------------------*/
  216.     /* Init I/O Multi-function                                                                                 */
  217.     /*---------------------------------------------------------------------------------------------------------*/
  218.     /* Set PB multi-function pins for UART0 RXD and TXD  */
  219.     SYS->PB_L_MFP &= ~(SYS_PB_L_MFP_PB0_MFP_Msk|SYS_PB_L_MFP_PB1_MFP_Msk);
  220.     SYS->PB_L_MFP |=  (SYS_PB_L_MFP_PB0_MFP_UART0_TX|SYS_PB_L_MFP_PB1_MFP_UART0_RX);

  221.     /* Lock protected registers */
  222.     SYS_LockReg();

  223. }

  224. void UART0_Init()
  225. {
  226.     /*---------------------------------------------------------------------------------------------------------*/
  227.     /* Init UART                                                                                               */
  228.     /*---------------------------------------------------------------------------------------------------------*/
  229.     UART_Open(UART0, 115200);
  230. }

  231. /**
  232.   * @brief  Main routine.
  233.   * @param  None.
  234.   * @return None.
  235.   */
  236. int32_t main(void)
  237. {
  238.     SYS_Init();
  239.     UART0_Init();

  240.     /* Prepare to enter power down mode, to demo the usage of PowerDown_Check(),
  241.        this function does not actually put this system to  power down mode  */
  242.     Enter_PowerDown();
  243.     /* Check system state before enter power down mode */
  244.     PowerDown_Check();

  245. }



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





 楼主| huahuagg 发表于 2019-5-30 23:58 | 显示全部楼层
Nano102系列的例子,非常经典。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1430

帖子

2

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