[DemoCode下载] M261的串口唤醒

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


  11. #define PLL_CLOCK   64000000
  12. #define RS485_ADDRESS 0xC0

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Define functions prototype                                                                              */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. void UART_DataWakeUp(void);
  17. void UART_CTSWakeUp(void);
  18. void UART_RxThresholdWakeUp(void);
  19. void UART_RS485WakeUp(void);
  20. void UART_PowerDown_TestItem(void);
  21. void UART_PowerDownWakeUpTest(void);

  22. /*---------------------------------------------------------------------------------------------------------*/
  23. /*  Function for System Entry to Power Down Mode                                                           */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. void PowerDownFunction(void)
  26. {
  27.     /* Check if all the debug messages are finished */
  28.     UART_WAIT_TX_EMPTY(DEBUG_PORT);

  29.     /* Enter to Power-down mode */
  30.     CLK_PowerDown();
  31. }

  32. void SYS_Init(void)
  33. {
  34.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  35.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
  36.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;

  37.     /* Set PF multi-function pins for X32_OUT(PF.4) and X32_IN(PF.5) */
  38.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF4MFP_Msk)) | SYS_GPF_MFPL_PF4MFP_X32_OUT;
  39.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF5MFP_Msk)) | SYS_GPF_MFPL_PF5MFP_X32_IN;

  40.     /*---------------------------------------------------------------------------------------------------------*/
  41.     /* Init System Clock                                                                                       */
  42.     /*---------------------------------------------------------------------------------------------------------*/

  43.     /* Enable HIRC, HXT and LXT clock */
  44.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
  45.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
  46.     CLK_EnableXtalRC(CLK_PWRCTL_LXTEN_Msk);

  47.     /* Wait for HIRC, HXT and LXT clock ready */
  48.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
  49.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
  50.     CLK_WaitClockReady(CLK_STATUS_LXTSTB_Msk);

  51.     /* Enable PLL */
  52.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  53.     /* Waiting for PLL stable */
  54.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

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

  57.     /* Enable UART module clock */
  58.     CLK_EnableModuleClock(UART0_MODULE);
  59.     CLK_EnableModuleClock(UART1_MODULE);

  60.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  61.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
  62.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_HIRC, CLK_CLKDIV0_UART1(1));

  63.     /*---------------------------------------------------------------------------------------------------------*/
  64.     /* Init I/O Multi-function                                                                                 */
  65.     /*---------------------------------------------------------------------------------------------------------*/

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

  68.     /* Set PB multi-function pins for UART1 RXD(PB.6), TXD(PB.7) and nCTS(PB.9) */
  69.     SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB6MFP_Msk)) | SYS_GPB_MFPL_PB6MFP_UART1_RXD;
  70.     SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB7MFP_Msk)) | SYS_GPB_MFPL_PB7MFP_UART1_TXD;
  71.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~SYS_GPB_MFPH_PB9MFP_Msk)) | SYS_GPB_MFPH_PB9MFP_UART1_nCTS;

  72. }

  73. void UART0_Init(void)
  74. {
  75.     /*---------------------------------------------------------------------------------------------------------*/
  76.     /* Init UART                                                                                               */
  77.     /*---------------------------------------------------------------------------------------------------------*/

  78.     /* Reset UART0 */
  79.     SYS_ResetModule(UART0_RST);

  80.     /* Configure UART0 and set UART0 baud rate */
  81.     UART_Open(UART0, 115200);
  82. }

  83. void UART1_Init(void)

  84. {
  85.     /*---------------------------------------------------------------------------------------------------------*/
  86.     /* Init UART                                                                                               */
  87.     /*---------------------------------------------------------------------------------------------------------*/

  88.     /* Reset UART1 */
  89.     SYS_ResetModule(UART1_RST);

  90.     /* Configure UART1 and set UART1 Baudrate */
  91.     UART_Open(UART1, 9600);

  92. }

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

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

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

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

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

  106.     /* Init UART1 for test */
  107.     UART1_Init();

  108.     /*---------------------------------------------------------------------------------------------------------*/
  109.     /* SAMPLE CODE                                                                                             */
  110.     /*---------------------------------------------------------------------------------------------------------*/

  111.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  112.     printf("UART Sample Program\n\n");

  113.     /* UART Power-down and Wake-up sample function */
  114.     UART_PowerDownWakeUpTest();

  115.     printf("\nUART Sample Program End\n");

  116.     while(1);

  117. }

  118. /*---------------------------------------------------------------------------------------------------------*/
  119. /* ISR to handle UART Channel 1 interrupt event                                                            */
  120. /*---------------------------------------------------------------------------------------------------------*/
  121. void UART1_IRQHandler(void)
  122. {
  123.     uint32_t u32Data;

  124.     if(UART_GET_INT_FLAG(UART1, UART_INTSTS_WKINT_Msk))     /* UART wake-up interrupt flag */
  125.     {
  126.         UART_ClearIntFlag(UART1, UART_INTSTS_WKINT_Msk);
  127.         printf("UART wake-up.\n");
  128.         UART_WAIT_TX_EMPTY(DEBUG_PORT);
  129.     }
  130.     else if(UART_GET_INT_FLAG(UART1, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk))  /* UART receive data available flag */
  131.     {
  132.         while(UART_GET_RX_EMPTY(UART1) == 0)
  133.         {
  134.             u32Data = UART_READ(UART1);
  135.             if(u32Data & UART_DAT_PARITY_Msk)
  136.                 printf("Address: 0x%X\n", (u32Data & 0xFF));
  137.             else
  138.                 printf("Data: 0x%X\n", u32Data);
  139.         }
  140.     }

  141. }

  142. /*---------------------------------------------------------------------------------------------------------*/
  143. /*  UART nCTS Wake-up Function                                                                             */
  144. /*---------------------------------------------------------------------------------------------------------*/
  145. void UART_CTSWakeUp(void)
  146. {
  147.     /* Enable UART nCTS wake-up function */
  148.     UART1->WKCTL |= UART_WKCTL_WKCTSEN_Msk;

  149.     printf("System enter to Power-down mode.\n");
  150.     printf("Toggle UART1 nCTS to wake-up system.\n\n");
  151. }

  152. /*---------------------------------------------------------------------------------------------------------*/
  153. /*  UART Data Wake-up Function                                                                             */
  154. /*---------------------------------------------------------------------------------------------------------*/
  155. void UART_DataWakeUp(void)
  156. {
  157.     /* Enable UART data wake-up function */
  158.     UART1->WKCTL |= UART_WKCTL_WKDATEN_Msk;

  159.     /* Set UART data wake-up start bit compensation value.
  160.        It indicates how many clock cycle selected by UART_CLK does the UART controller can get the first bit (start bit)
  161.        when the device is wake-up from power-down mode.
  162.        If UART_CLK is selected as HIRC(12MHz) and the HIRC stable time is about 8.602us,
  163.        the data wake-up start bit compensation value can be set as 104. */
  164.     UART1->DWKCOMP = 104;

  165.     printf("System enter to Power-down mode.\n");
  166.     printf("Send data with baud rate 9600bps to UART1 to wake-up system.\n\n");
  167. }

  168. /*---------------------------------------------------------------------------------------------------------*/
  169. /*  UART Rx threshold and time-out Function                                                                */
  170. /*---------------------------------------------------------------------------------------------------------*/
  171. void UART_RxThresholdWakeUp(void)
  172. {
  173.     /* Select UART clock source as LXT */
  174.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_LXT, CLK_CLKDIV0_UART1(1));

  175.     /* Set UART baud rate and baud rate compensation */
  176.     UART_Open(UART1, 9600);
  177.     UART1->BRCOMP = 0xA5;

  178.     /* Enable UART Rx Threshold and Rx time-out wake-up function */
  179.     UART1->WKCTL |= UART_WKCTL_WKRFRTEN_Msk | UART_WKCTL_WKTOUTEN_Msk;

  180.     /* Set Rx FIFO interrupt trigger level */
  181.     UART1->FIFO = (UART1->FIFO & (~UART_FIFO_RFITL_Msk)) | UART_FIFO_RFITL_4BYTES;

  182.     /* Enable UART Rx time-out function */
  183.     UART_SetTimeoutCnt(UART1, 40);

  184.     printf("System enter to Power-down mode.\n");
  185.     printf("Send data with baud rate 9600bps to UART1 to wake-up system.\n\n");
  186. }

  187. /*---------------------------------------------------------------------------------------------------------*/
  188. /*  UART RS485 address match (AAD mode) function                                                           */
  189. /*---------------------------------------------------------------------------------------------------------*/
  190. void UART_RS485WakeUp(void)
  191. {
  192.     /* Select UART clock source as LXT */
  193.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_LXT, CLK_CLKDIV0_UART1(1));

  194.     /* Set UART baud rate and baud rate compensation */
  195.     UART_Open(UART1, 9600);
  196.     UART1->BRCOMP = 0xA5;

  197.     /* RS485 address match (AAD mode) setting */
  198.     UART_SelectRS485Mode(UART1, UART_ALTCTL_RS485AAD_Msk, RS485_ADDRESS);

  199.     /* Enable parity source selection function */
  200.     UART1->LINE |= (UART_LINE_PSS_Msk | UART_LINE_PBE_Msk);

  201.     /* Enable UART RS485 address match, Rx Threshold and Rx time-out wake-up function */
  202.     UART1->WKCTL |= UART_WKCTL_WKRFRTEN_Msk | UART_WKCTL_WKRS485EN_Msk | UART_WKCTL_WKTOUTEN_Msk;

  203.     /* Enable UART Rx time-out function */
  204.     UART_SetTimeoutCnt(UART1, 40);

  205.     printf("System enter to Power-down mode.\n");
  206.     printf("Send RS485 address byte 0x%X with baud rate 9600bps to UART1 to wake-up system.\n\n", RS485_ADDRESS);
  207. }

  208. /*---------------------------------------------------------------------------------------------------------*/
  209. /*  UART Power-down and Wake-up Menu                                                                       */
  210. /*---------------------------------------------------------------------------------------------------------*/
  211. void UART_PowerDown_TestItem(void)
  212. {
  213.     printf("+-----------------------------------------------------------+\n");
  214.     printf("|  UART Power-down and wake-up test                         |\n");
  215.     printf("+-----------------------------------------------------------+\n");
  216.     printf("| [1] nCTS wake-up test                                     |\n");
  217.     printf("| [2] Data wake-up test                                     |\n");
  218.     printf("| [3] Rx threshold and time-out wake-up test                |\n");
  219.     printf("| [4] RS485 wake-up test                                    |\n");
  220.     printf("+-----------------------------------------------------------+\n");
  221.     printf("| Quit                                           - [Others] |\n");
  222.     printf("+-----------------------------------------------------------+\n");
  223.     printf("Please Select key (1~4): ");
  224. }

  225. /*---------------------------------------------------------------------------------------------------------*/
  226. /*  UART Power-down and Wake-up Test Function                                                              */
  227. /*---------------------------------------------------------------------------------------------------------*/
  228. void UART_PowerDownWakeUpTest(void)
  229. {
  230.     uint32_t u32Item;

  231.     printf("Due to PLL clock stable too slow.\n");
  232.     printf("Before demo UART wake-up, this demo code will switch HCLK from PLL to HIRC.\n");

  233.     /* Unlock protected registers */
  234.     SYS_UnlockReg();

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

  237.     /* Lock protected registers */
  238.     SYS_LockReg();

  239.     printf("\nCPU @ %dHz\n", SystemCoreClock);

  240.     /* Enable UART wake-up and receive data available interrupt */
  241.     UART_EnableInt(UART1, UART_INTEN_WKIEN_Msk | UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk);

  242.     UART_PowerDown_TestItem();
  243.     u32Item = getchar();
  244.     printf("%c\n\n", u32Item);
  245.     switch(u32Item)
  246.     {
  247.         case '1':
  248.             UART_CTSWakeUp();
  249.             break;
  250.         case '2':
  251.             UART_DataWakeUp();
  252.             break;
  253.         case '3':
  254.             UART_RxThresholdWakeUp();
  255.             break;
  256.         case '4':
  257.             UART_RS485WakeUp();
  258.             break;
  259.         default:
  260.             return;
  261.     }

  262.     /* Unlock protected registers before entering Power-down mode */
  263.     SYS_UnlockReg();

  264.     /* Enter to Power-down mode */
  265.     PowerDownFunction();

  266.     /* Lock protected registers after entering Power-down mode */
  267.     SYS_LockReg();

  268.     printf("Enter any key to end test.\n\n");
  269.     getchar();

  270.     /* Disable UART wake-up function */
  271.     UART1->WKCTL = 0;

  272.     /* Disable UART Interrupt */
  273.     UART_DisableInt(UART1, UART_INTEN_WKIEN_Msk | UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk);

  274.     /* Unlock protected registers */
  275.     SYS_UnlockReg();

  276.     /* Set core clock as PLL_CLOCK from PLL */
  277.     CLK_SetCoreClock(PLL_CLOCK);

  278.     /* Lock protected registers */
  279.     SYS_LockReg();

  280. }

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


 楼主| wahahaheihei 发表于 2019-12-11 17:53 | 显示全部楼层
  1. /****************************************************************************
  2. * @file     main.c
  3. * @version  V3.00
  4. * @brief    Show how to wake up system form Power-down mode by USCI interrupt in UART mode.
  5. *
  6. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"


  11. #define PLL_CLOCK   64000000

  12. /*---------------------------------------------------------------------------------------------------------*/
  13. /* Define functions prototype                                                                              */
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. int32_t main(void);
  16. void USCI_UART_DataWakeUp(void);
  17. void USCI_UART_CTSWakeUp(void);
  18. void USCI_UART_PowerDown_TestItem(void);
  19. void USCI_UART_PowerDownWakeUpTest(void);


  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /*  Function for System Entry to Power Down Mode                                                           */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. void PowerDownFunction(void)
  24. {
  25.     /* Check if all the debug messages are finished */
  26.     UART_WAIT_TX_EMPTY(DEBUG_PORT);

  27.     /* Enter to Power-down mode */
  28.     CLK_PowerDown();
  29. }

  30. void SYS_Init(void)
  31. {

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

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init System Clock                                                                                       */
  37.     /*---------------------------------------------------------------------------------------------------------*/

  38.     /* Enable HIRC clock */
  39.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  40.     /* Wait for HIRC clock ready */
  41.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  44.     /* Enable HXT clock */
  45.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  46.     /* Wait for HXT clock ready */
  47.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  48.     /* Enable PLL */
  49.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  50.     /* Waiting for PLL stable */
  51.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

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

  54.     /* Enable UART and USCI module clock */
  55.     CLK_EnableModuleClock(UART0_MODULE);
  56.     CLK_EnableModuleClock(USCI0_MODULE);

  57.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  58.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  59.     /*---------------------------------------------------------------------------------------------------------*/
  60.     /* Init I/O Multi-function                                                                                 */
  61.     /*---------------------------------------------------------------------------------------------------------*/

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

  64.     /* Set PE multi-function pins for USCI0_DAT0(PE.3), USCI0_DAT1(PE.4) and USCI0_CTL0(PE.6) */
  65.     SYS->GPE_MFPL = (SYS->GPE_MFPL & (~SYS_GPE_MFPL_PE3MFP_Msk)) | SYS_GPE_MFPL_PE3MFP_USCI0_DAT0;
  66.     SYS->GPE_MFPL = (SYS->GPE_MFPL & (~SYS_GPE_MFPL_PE4MFP_Msk)) | SYS_GPE_MFPL_PE4MFP_USCI0_DAT1;
  67.     SYS->GPE_MFPL = (SYS->GPE_MFPL & (~SYS_GPE_MFPL_PE6MFP_Msk)) | SYS_GPE_MFPL_PE6MFP_USCI0_CTL0;

  68. }

  69. void UART0_Init(void)
  70. {
  71.     /*---------------------------------------------------------------------------------------------------------*/
  72.     /* Init UART                                                                                               */
  73.     /*---------------------------------------------------------------------------------------------------------*/
  74.     /* Reset UART0 */
  75.     SYS_ResetModule(UART0_RST);

  76.     /* Configure UART0 and set UART0 baud rate */
  77.     UART_Open(UART0, 115200);
  78. }

  79. void USCI0_Init(void)
  80. {
  81.     /*---------------------------------------------------------------------------------------------------------*/
  82.     /* Init USCI                                                                                               */
  83.     /*---------------------------------------------------------------------------------------------------------*/
  84.     /* Reset USCI0 */
  85.     SYS_ResetModule(USCI0_RST);

  86.     /* Configure USCI0 as UART mode */
  87.     UUART_Open(UUART0, 9600);
  88. }

  89. /*---------------------------------------------------------------------------------------------------------*/
  90. /*  Main Function                                                                                          */
  91. /*---------------------------------------------------------------------------------------------------------*/
  92. int32_t main(void)
  93. {

  94.     /* Unlock protected registers */
  95.     SYS_UnlockReg();

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

  98.     /* Lock protected registers */
  99.     SYS_LockReg();

  100.     /* Init UART0 for printf */
  101.     UART0_Init();

  102.     /* Init USCI0 for test */
  103.     USCI0_Init();

  104.     /*---------------------------------------------------------------------------------------------------------*/
  105.     /* SAMPLE CODE                                                                                             */
  106.     /*---------------------------------------------------------------------------------------------------------*/

  107.     printf("\n\nCPU @ %dHz\n", SystemCoreClock);

  108.     printf("USCI UART Sample Program\n\n");

  109.     /* USCI UART Power-down and Wake-up sample function */
  110.     USCI_UART_PowerDownWakeUpTest();

  111.     printf("\nUSCI UART Sample Program End\n");

  112.     while(1);

  113. }

  114. /*---------------------------------------------------------------------------------------------------------*/
  115. /* ISR to handle USCI0 interrupt event                                                                     */
  116. /*---------------------------------------------------------------------------------------------------------*/
  117. void USCI0_IRQHandler(void)
  118. {
  119.     uint32_t u32IntSts = UUART_GET_PROT_STATUS(UUART0);
  120.     uint32_t u32WkSts = UUART_GET_WAKEUP_FLAG(UUART0);

  121.     if(u32WkSts & UUART_WKSTS_WKF_Msk)  /* USCI UART wake-up flag */
  122.     {
  123.         UUART_CLR_WAKEUP_FLAG(UUART0);
  124.         printf("USCI UART wake-up.\n");
  125.     }
  126.     else if(u32IntSts & UUART_PROTSTS_RXENDIF_Msk)  /* USCI UART receive end interrupt flag */
  127.     {
  128.         UUART_CLR_PROT_INT_FLAG(UUART0, UUART_PROTSTS_RXENDIF_Msk);

  129.         while(UUART_GET_RX_EMPTY(UUART0) == 0)
  130.         {
  131.             printf("Data: 0x%X\n", UUART_READ(UUART0));
  132.         }
  133.     }
  134. }

  135. /*---------------------------------------------------------------------------------------------------------*/
  136. /*  UART nCTS Wake-up Function                                                                             */
  137. /*---------------------------------------------------------------------------------------------------------*/
  138. void USCI_UART_CTSWakeUp(void)
  139. {
  140.     /* Enable UART nCTS wake-up function */
  141.     UUART_EnableWakeup(UUART0, UUART_PROTCTL_CTSWKEN_Msk);

  142.     printf("System enter to Power-down mode.\n");
  143.     printf("Toggle USCI-UART0 nCTS to wake-up system.\n\n");
  144. }

  145. /*---------------------------------------------------------------------------------------------------------*/
  146. /*  UART Data Wake-up Function                                                                             */
  147. /*---------------------------------------------------------------------------------------------------------*/
  148. void USCI_UART_DataWakeUp(void)
  149. {

  150.     /* Enable UART data wake-up function */
  151.     UUART_EnableWakeup(UUART0, UUART_PROTCTL_DATWKEN_Msk);

  152.     /* Set UART data wake-up counter.
  153.        It indicates how many clock cycle does UART can get the first bit (start bit)
  154.        when the device is wake-up from Power-down mode.
  155.        Clock is (USCI clock source)/((CLKDIV+1)(PDSCNT+1)) */
  156.     UUART0->PROTCTL = (UUART0->PROTCTL & (~UUART_PROTCTL_WAKECNT_Msk)) | (2 << UUART_PROTCTL_WAKECNT_Pos);

  157.     printf("System enter to Power-down mode.\n");
  158.     printf("Send data with baud rate 9600bps to USCI-UART0 to wake-up system.\n\n");
  159. }

  160. /*---------------------------------------------------------------------------------------------------------*/
  161. /*  UART Power-down and Wake-up Menu                                                                       */
  162. /*---------------------------------------------------------------------------------------------------------*/
  163. void USCI_UART_PowerDown_TestItem(void)
  164. {
  165.     printf("+-----------------------------------------------------------+\n");
  166.     printf("|  USCI-UART Power-down and wake-up test                    |\n");
  167.     printf("+-----------------------------------------------------------+\n");
  168.     printf("| [1] nCTS wake-up test                                     |\n");
  169.     printf("| [2] Data wake-up test                                     |\n");
  170.     printf("+-----------------------------------------------------------+\n");
  171.     printf("| Quit                                           - [Others] |\n");
  172.     printf("+-----------------------------------------------------------+\n");
  173.     printf("Please Select key (1~2): ");
  174. }

  175. /*---------------------------------------------------------------------------------------------------------*/
  176. /*  UART Power-down and Wake-up Test Function                                                              */
  177. /*---------------------------------------------------------------------------------------------------------*/
  178. void USCI_UART_PowerDownWakeUpTest(void)
  179. {
  180.     uint32_t u32Item;

  181.     printf("Due to PLL clock stable too slow.\n");
  182.     printf("Before demo USCI UART wake-up, this demo code will switch HCLK from PLL to HIRC.\n");

  183.     /* Unlock protected registers */
  184.     SYS_UnlockReg();

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

  187.     /* Lock protected registers */
  188.     SYS_LockReg();

  189.     printf("\nCPU @ %dHz\n", SystemCoreClock);

  190.     /* Set UUART0 Line config */
  191.     UUART_SetLine_Config(UUART0, 9600, UUART_WORD_LEN_8, UUART_PARITY_NONE, UUART_STOP_BIT_1);

  192.     /* Enable UART receive end interrupt */
  193.     UUART_ENABLE_TRANS_INT(UUART0, UUART_INTEN_RXENDIEN_Msk);
  194.     NVIC_EnableIRQ(USCI0_IRQn);

  195.     USCI_UART_PowerDown_TestItem();
  196.     u32Item = getchar();
  197.     printf("%c\n\n", u32Item);
  198.     switch(u32Item)
  199.     {
  200.         case '1':
  201.             USCI_UART_CTSWakeUp();
  202.             break;
  203.         case '2':
  204.             USCI_UART_DataWakeUp();
  205.             break;
  206.         default:
  207.             return;
  208.     }

  209.     /* Unlock protected registers before entering Power-down mode */
  210.     SYS_UnlockReg();

  211.     /* Enter to Power-down mode */
  212.     PowerDownFunction();

  213.     /* Wait until USCI UART data transmission is finished */
  214.     while(UUART0->PROTSTS & UUART_PROTSTS_RXBUSY_Msk);

  215.     /* Lock protected registers after entering Power-down mode */
  216.     SYS_LockReg();

  217.     printf("Enter any key to end test.\n\n");
  218.     getchar();

  219.     /* Disable UART wake-up function */
  220.     UUART_DisableWakeup(UUART0);

  221.     /* Disable UART receive end interrupt */
  222.     UUART_DISABLE_TRANS_INT(UUART0, UUART_INTEN_RXENDIEN_Msk);
  223.     NVIC_DisableIRQ(USCI0_IRQn);

  224.     /* Unlock protected registers */
  225.     SYS_UnlockReg();

  226.     /* Set core clock as PLL_CLOCK from PLL */
  227.     CLK_SetCoreClock(PLL_CLOCK);

  228.     /* Lock protected registers */
  229.     SYS_LockReg();

  230.     /* Set UUART0 Line config */
  231.     UUART_SetLine_Config(UUART0, 9600, UUART_WORD_LEN_8, UUART_PARITY_NONE, UUART_STOP_BIT_1);

  232. }

  233. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
antusheng 发表于 2019-12-11 21:36 | 显示全部楼层
CTS唤醒
xuanhuanzi 发表于 2019-12-11 23:14 | 显示全部楼层
库函数化的不彻底啊,看到不少条寄存器的操作
xixi2017 发表于 2019-12-11 23:56 | 显示全部楼层
UART nCTS Wake-up Function
这种不是Rx,Tx两根线的
huangcunxiake 发表于 2019-12-12 11:00 | 显示全部楼层
需要额外的接口啊。
小明的同学 发表于 2019-12-12 20:33 | 显示全部楼层
一般嵌入式都使用2根线,这个不太使用
wanduzi 发表于 2019-12-13 19:18 | 显示全部楼层
操作非常给力。
antusheng 发表于 2019-12-14 21:27 | 显示全部楼层
利用了完整串口的特性
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3223

帖子

12

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