[DemoCode下载] Nano103的掉电检测

[复制链接]
691|1
 楼主| heisexingqisi 发表于 2018-8-23 21:31 | 显示全部楼层 |阅读模式
AN, se, gp, TI, TE
  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: 4 $
  5. * $Date: 15/12/29 2:56p $
  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) 2015 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 "Nano103.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->GPA_MFPL;
  39.     _Pin_Setting[1] = SYS->GPA_MFPH;
  40.     _Pin_Setting[2] = SYS->GPB_MFPL;
  41.     _Pin_Setting[3] = SYS->GPB_MFPH;
  42.     _Pin_Setting[4] = SYS->GPC_MFPL;
  43.     _Pin_Setting[5] = SYS->GPC_MFPH;
  44.     _Pin_Setting[6] = SYS->GPD_MFPL;
  45.     _Pin_Setting[7] = SYS->GPD_MFPH;
  46.     _Pin_Setting[8] = SYS->GPE_MFPL;
  47.     _Pin_Setting[9] = SYS->GPF_MFPL;

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

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

  64.     /* Set function pin to GPIO mode */
  65.     SYS->GPA_MFPL = 0;
  66.     SYS->GPA_MFPH = 0;
  67.     SYS->GPB_MFPL = (SYS_GPB_MFPL_PB0MFP_UART0_RXD | SYS_GPB_MFPL_PB1MFP_UART0_TXD); // exclude debug port;
  68.     SYS->GPB_MFPH = 0;
  69.     SYS->GPC_MFPL = 0;
  70.     SYS->GPC_MFPH = 0;
  71.     SYS->GPD_MFPL = 0;
  72.     SYS->GPD_MFPH = 0;
  73.     SYS->GPE_MFPL = 0;
  74.     //SYS->GPE_MFPH = 0;
  75.     //SYS->GPF_MFPL = 0x0000FF00;   // exclude GPF3:HXT_OUT, GPF2:HXT_IN
  76.     SYS->GPF_MFPL = 0x00FFFF00; // exclude GPF5:ICE_DAT, GPF4:ICE_CLK, GPF3:HXT_OUT, GPF2:HXT_IN

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

  85.     //CLK->PWRCTL |= CLK_PWRCTL_PDWKIEN_Msk;  /* Enable wake up interrupt source */
  86.     //NVIC_EnableIRQ(PDWU_IRQn);             /* Enable IRQ request for PDWU interrupt */

  87.     SYS_UnlockReg(); /* Unlock protected registers */
  88.     SCB->SCR = SCB_SCR_SLEEPDEEP_Msk; /* Set Deep sleep */
  89.     CLK->PWRCTL |= (CLK_PWRCTL_PDEN_Msk | CLK_PWRCTL_PDWKDLY_Msk ); /* Enable Power down and wake up */

  90.     //__WFI();   /* system really enter power down here ! */
  91. }

  92. /**
  93.   * @brief  Power Down check function.
  94.   * @param  None.
  95.   * @return None.
  96.   */
  97. void PowerDown_Check()
  98. {
  99.     uint32_t i,j;
  100.     volatile uint32_t Tmp_NVIC_ISER;
  101.     uint32_t gpio_error_count = 0;
  102.     char *symbo_gpio[6] = {"A", "B", "C", "D", "E", "F"};
  103.     GPIO_T *tGPIO;
  104.     uint32_t SYS_MFP = 0xffffffff;

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

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

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

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

  109.     for(j = 0; j < 11; j++) { /* Check MFP is set GPIO mode or not */
  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->GPF_MFPL & SYS_GPF_MFPL_PF3MFP_XT1_IN) != SYS_GPF_MFPL_PF3MFP_XT1_IN) { /* Check HXT_OUT pin */
  119.         printf("\nGPF Pin 2 not set HXT_OUT !!!");
  120.         printf("\nPlease Check GPF Pin 2 not connect crystal !!!");
  121.     }

  122.     if( (SYS->GPF_MFPL & SYS_GPF_MFPL_PF2MFP_XT1_OUT) != SYS_GPF_MFPL_PF2MFP_XT1_OUT) { /* Check HXT_IN pin */
  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))) { /* Check Pin status */
  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) /* Check HXT_EN */
  146.         printf("\nHXT Enable");
  147.     else
  148.         printf("\nHXT Disable");

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

  153.     if(CLK->PWRCTL & CLK_PWRCTL_HIRC0_EN) /* Check HIRCO_EN */
  154.         printf("\nHIRC Enable");
  155.     else
  156.         printf("\nHIRC Disable");

  157.     if(CLK->PWRCTL & CLK_PWRCTL_LIRC_EN) /* Check LICR_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) /* Check Power Down Enable */
  164.         printf("\nChip Power-down mode Enable ");
  165.     else
  166.         printf("\nChip Power-down mode Disable !!!");

  167.     if(SCB->SCR & 0x04) /* Check SCB configuration */
  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) /* Check Wake up delay */
  176.         printf("\nWake-up Delay Counter Enable");
  177.     else
  178.         printf("\nWake-up Delay Counter Disable");

  179.     if(CLK->PWRCTL & CLK_PWRCTL_WAKEINT_EN) /* Check Wake up interrupt */
  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)) ) /* Check NVIC IRQ */
  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)) /* Check Wake up interrupt pending */
  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->WKINTSTS & 0x1) /* Check Wake up status */
  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. /**
  201. *  @brief  Init system clock and I/O multi function .
  202. *  @param  None
  203. *  @return None
  204. */
  205. void SYS_Init(void)
  206. {
  207.     /*---------------------------------------------------------------------------------------------------------*/
  208.     /* Init System Clock                                                                                       */
  209.     /*---------------------------------------------------------------------------------------------------------*/
  210.     /* Unlock protected registers */
  211.     SYS_UnlockReg();

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

  214.     /* Select IP clock source */
  215.     CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART0SEL_Msk;
  216.     CLK->CLKSEL1 |= CLK_CLKSEL1_UART0SEL_HIRC;// Clock source from HIRC

  217.     /* Update System Core Clock */
  218.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  219.     SystemCoreClockUpdate();

  220.     /*---------------------------------------------------------------------------------------------------------*/
  221.     /* Init I/O Multi-function                                                                                 */
  222.     /*---------------------------------------------------------------------------------------------------------*/
  223.     /* Set PB multi-function pins for UART0 RXD and TXD  */
  224.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk|SYS_GPB_MFPL_PB1MFP_Msk);
  225.     SYS->GPB_MFPL |=  (SYS_GPB_MFPL_PB0MFP_UART0_RXD|SYS_GPB_MFPL_PB1MFP_UART0_TXD);

  226.     /* Lock protected registers */
  227.     SYS_LockReg();

  228. }

  229. /**
  230. *  @brief  Config UART0.
  231. *  @param  None
  232. *  @return None
  233. */
  234. void UART0_Init()
  235. {
  236.     UART_Open(UART0, 115200); /* set UART baudrate is 115200 */
  237. }

  238. /**
  239.   * @brief  Main routine.
  240.   * @param  None.
  241.   * @return None.
  242.   */
  243. int32_t main(void)
  244. {
  245.     /* Init System, IP clock and multi-function I/O */
  246.     SYS_Init();
  247.     /* Init UART0 for printf */
  248.     UART0_Init();

  249.     /* Prepare to enter power down mode, to demo the usage of PowerDown_Check(),
  250.        this function does not actually put this system to  power down mode  */
  251.     Enter_PowerDown();
  252.     /* Check system state before enter power down mode */
  253.     PowerDown_Check();

  254.     while(1);
  255. }



  256. /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/





 楼主| heisexingqisi 发表于 2018-8-23 21:36 | 显示全部楼层
#define DEBUG_MSG   printf
这个才是亮点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

2770

帖子

2

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