text: /**************************************************************************//**
   * @file     main.c
   * @version  V3.00
   * $Revision: 2 $
   * $Date: 16/10/25 4:27p $
   * @brief
   *           Transmit and receive data from PC terminal through RS232 interface.
   *
   * @note
   * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
   *
   ******************************************************************************/
  #include "stdio.h"
  #include "NUC1261.h"
  
  #define PLLCTL_SETTING  CLK_PLLCTL_72MHz_HXT
  #define PLL_CLOCK       72000000
  
  #define RXBUFSIZE 1024
  
  /*---------------------------------------------------------------------------------------------------------*/
  /* Global variables                                                                                        */
  /*---------------------------------------------------------------------------------------------------------*/
  uint8_t g_u8RecData[RXBUFSIZE]  = {0};
  
  volatile uint32_t g_u32comRbytes = 0;
  volatile uint32_t g_u32comRhead  = 0;
  volatile uint32_t g_u32comRtail  = 0;
  volatile int32_t g_bWait         = TRUE;
  
  /*---------------------------------------------------------------------------------------------------------*/
  /* Define functions prototype                                                                              */
  /*---------------------------------------------------------------------------------------------------------*/
  int32_t main(void);
  void UART_TEST_HANDLE(void);
  void UART_FunctionTest(void);
  
  
  void SYS_Init(void)
  {
  
      /*---------------------------------------------------------------------------------------------------------*/
      /* Init System Clock                                                                                       */
      /*---------------------------------------------------------------------------------------------------------*/
  
      /* Enable HIRC clock (Internal RC 22.1184MHz) */
      CLK->PWRCTL |= CLK_PWRCTL_HIRCEN_Msk;
  
      /* Wait for HIRC clock ready */
      while(!(CLK->STATUS & CLK_STATUS_HIRCSTB_Msk));
  
      /* Select HCLK clock source as HIRC and HCLK clock divider as 1 */
      CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLKSEL_Msk)) | CLK_CLKSEL0_HCLKSEL_HIRC;
      CLK->CLKDIV0 = (CLK->CLKDIV0 & (~CLK_CLKDIV0_HCLKDIV_Msk)) | CLK_CLKDIV0_HCLK(1);
  
      /* Set PLL to Power-down mode */
      CLK->PLLCTL |= CLK_PLLCTL_PD_Msk;
  
      /* Enable HXT clock (external XTAL 12MHz) */
      CLK->PWRCTL |= CLK_PWRCTL_HXTEN_Msk;
  
      /* Wait for HXT clock ready */
      while(!(CLK->STATUS & CLK_STATUS_HXTSTB_Msk));
  
      /* Set core clock as PLL_CLOCK from PLL */
      CLK->PLLCTL = PLLCTL_SETTING;
      while(!(CLK->STATUS & CLK_STATUS_PLLSTB_Msk));
      CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLKSEL_Msk)) | CLK_CLKSEL0_HCLKSEL_PLL;
  
      /* Update System Core Clock */
      PllClock        = PLL_CLOCK;            // PLL
      SystemCoreClock = PLL_CLOCK / 1;        // HCLK
      CyclesPerUs     = PLL_CLOCK / 1000000;  // For CLK_SysTickDelay()
  
      /* Enable UART module clock */
      CLK->APBCLK0 |= CLK_APBCLK0_UART0CKEN_Msk;
  
      /* Select UART module clock source as HXT and UART module clock divider as 1 */
      CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_UARTSEL_Msk)) | CLK_CLKSEL1_UARTSEL_HXT;
      CLK->CLKDIV0 = (CLK->CLKDIV0 & (~CLK_CLKDIV0_UARTDIV_Msk)) | CLK_CLKDIV0_UART(1);
  
      /*---------------------------------------------------------------------------------------------------------*/
      /* Init I/O Multi-function                                                                                 */
      /*---------------------------------------------------------------------------------------------------------*/
  
      /* Set PA multi-function pins for UART0 RXD and TXD */
      SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA3MFP_Msk | SYS_GPA_MFPL_PA2MFP_Msk);
      SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);
  }
  
  void UART0_Init()
  {
      /*---------------------------------------------------------------------------------------------------------*/
      /* Init UART                                                                                               */
      /*---------------------------------------------------------------------------------------------------------*/
      /* Reset UART0 */
      SYS->IPRST1 |=  SYS_IPRST1_UART0RST_Msk;
      SYS->IPRST1 &= ~SYS_IPRST1_UART0RST_Msk;
  
      /* Configure UART0 and set UART0 baud rate */
      UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
      UART0->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  }
  
  /*---------------------------------------------------------------------------------------------------------*/
  /*  Main Function                                                                                          */
  /*---------------------------------------------------------------------------------------------------------*/
  int32_t main(void)
  {
  
      /* Unlock protected registers */
      SYS_UnlockReg();
  
      /* Init System, peripheral clock and multi-function I/O */
      SYS_Init();
  
      /* Lock protected registers */
      SYS_LockReg();
  
      /* Init UART0 for printf and test */
      UART0_Init();
  
      printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
  
      printf("\nUART Sample Program\n");
  
      /* UART sample function */
      UART_FunctionTest();
  
      while(1);
  
  }
  
  /*---------------------------------------------------------------------------------------------------------*/
  /* ISR to handle UART Channel 0 interrupt event                                                            */
  /*---------------------------------------------------------------------------------------------------------*/
  void UART02_IRQHandler(void)
  {
      UART_TEST_HANDLE();
  }
  
  /*---------------------------------------------------------------------------------------------------------*/
  /* UART Callback function                                                                                  */
  /*---------------------------------------------------------------------------------------------------------*/
  void UART_TEST_HANDLE()
  {
      uint8_t u8InChar = 0xFF;
      uint32_t u32IntSts = UART0->INTSTS;
  
      if(u32IntSts & UART_INTSTS_RDAINT_Msk)
      {
          printf("\nInput:");
  
          /* Get all the input characters */
          while(UART0->INTSTS & UART_INTSTS_RDAIF_Msk)
          {
              /* Get the character from UART Buffer */
              u8InChar = UART0->DAT;
  
              printf("%c ", u8InChar);
  
              if(u8InChar == '0')
              {
                  g_bWait = FALSE;
              }
  
              /* Check if buffer full */
              if(g_u32comRbytes < RXBUFSIZE)
              {
                  /* Enqueue the character */
                  g_u8RecData[g_u32comRtail] = u8InChar;
                  g_u32comRtail = (g_u32comRtail == (RXBUFSIZE - 1)) ? 0 : (g_u32comRtail + 1);
                  g_u32comRbytes++;
              }
          }
          printf("\nTransmission Test:");
      }
  
      if(u32IntSts & UART_INTSTS_THREINT_Msk)
      {
          uint16_t tmp;
          tmp = g_u32comRtail;
          if(g_u32comRhead != tmp)
          {
              u8InChar = g_u8RecData[g_u32comRhead];
              while(UART_IS_TX_FULL(UART0));  /* Wait Tx is not full to transmit data */
              UART_WRITE(UART0, u8InChar);
              g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1);
              g_u32comRbytes--;
          }
      }
  }
  
  /*---------------------------------------------------------------------------------------------------------*/
  /*  UART Function Test                                                                                     */
  /*---------------------------------------------------------------------------------------------------------*/
  void UART_FunctionTest()
  {
      printf("+-----------------------------------------------------------+\n");
      printf("|  UART Function Test                                       |\n");
      printf("+-----------------------------------------------------------+\n");
      printf("|  Description :                                            |\n");
      printf("|    The sample code will print input char on terminal      |\n");
      printf("|    Please enter any to start     (Press '0' to exit)      |\n");
      printf("+-----------------------------------------------------------+\n");
  
      /*
          Using a RS232 cable to connect UART0 and PC.
          UART0 is set to debug port. UART0 is enable RDA and RLS interrupt.
          When inputting char to terminal screen, RDA interrupt will happen and
          UART0 will print the received char on screen.
      */
  
      /* Enable UART RDA and THRE Interrupt */
      UART0->INTEN |= UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk;
      NVIC_EnableIRQ(UART02_IRQn);
      while(g_bWait);
  
      /* Disable UART RDA and THRE Interrupt */
      UART0->INTEN &= ~(UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk);
      NVIC_DisableIRQ(UART02_IRQn);
      g_bWait = TRUE;
      printf("\nUART Sample Demo End.\n");
  
  }
  
  
  
  
  