/****************************************************************************
* @file main.c
* @version V1.00
* @brief Show how to use auto baud rate detection function.
*
* SPDX-License-Identifier: Apache-2.0
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2017 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void);
void UART0_Init(void);
void UART1_Init(void);
void AutoBaudRate_Test(void);
void AutoBaudRate_TxTest(void);
void AutoBaudRate_RxTest(void);
uint32_t GetUartBaudrate(UART_T *uart);
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
/* Enable HIRC clock (Internal RC 48MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Select HCLK clock source as HIRC and HCLK source divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Set both PCLK0 and PCLK1 as HCLK */
CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1;
/* Select IP clock source */
/* Select UART0 clock source is HIRC and UART module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Select UART1 clock source is HIRC and UART module clock divider as 1*/
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_HIRC, CLK_CLKDIV0_UART1(1));
/* Enable UART0 peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable UART1 peripheral clock */
CLK_EnableModuleClock(UART1_MODULE);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) | \
(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
/* Set PA multi-function pins for UART1 TXD and RXD */
SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk)) | \
(SYS_GPA_MFPL_PA2MFP_UART1_RXD | SYS_GPA_MFPL_PA3MFP_UART1_TXD);
/* Lock protected registers */
SYS_LockReg();
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART0 */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_Init(void)
{
/* Reset UART0 */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART1 */
/*---------------------------------------------------------------------------------------------------------*/
void UART1_Init(void)
{
/* Reset UART1 */
SYS_ResetModule(UART1_RST);
/* Configure UART1 and set UART1 Baudrate */
UART_Open(UART1, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
/* Init System, IP clock and multi-function I/O */
SYS_Init();
/* Init UART0 for printf */
UART0_Init();
/* Init UART1 */
UART1_Init();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
printf("\n\nCPU @ %dHz\n", SystemCoreClock);
printf("\nUART Sample Program\n");
/* UART auto baud rate sample function */
AutoBaudRate_Test();
printf("\nUART Sample Program End\n");
while(1);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Auto Baud Rate Function Test */
/*---------------------------------------------------------------------------------------------------------*/
void AutoBaudRate_Test(void)
{
uint32_t u32Item;
printf("\n");
printf("+-----------------------------------------------------------+\n");
printf("| Pin Configure |\n");
printf("+-----------------------------------------------------------+\n");
printf("| ______ _____ |\n");
printf("| | | | | |\n");
printf("| |Master|--UART1_TXD(PA.3) <====> UART1_RXD(PA.2)--|Slave| |\n");
printf("| | | | | |\n");
printf("| |______| |_____| |\n");
printf("| |\n");
printf("+-----------------------------------------------------------+\n");
printf("\n");
printf("+-----------------------------------------------------------+\n");
printf("| Auto Baud Rate Function Test |\n");
printf("+-----------------------------------------------------------+\n");
printf("| Description : |\n");
printf("| The sample code needs two boards. One is Master and |\n");
printf("| the other is slave. Master will send input pattern |\n");
printf("| 0x1 with different baud rate. It can check if Slave |\n");
printf("| calculates correct baud rate. |\n");
printf("+-----------------------------------------------------------+\n");
printf("| Please select Master or Slave test |\n");
printf("| [0] Master [1] Slave |\n");
printf("+-----------------------------------------------------------+\n");
u32Item = getchar();
if(u32Item == '0')
AutoBaudRate_TxTest();
else
AutoBaudRate_RxTest();
}
/*---------------------------------------------------------------------------------------------------------*/
/* Auto Baud Rate Function Tx Test */
/*---------------------------------------------------------------------------------------------------------*/
void AutoBaudRate_TxTest(void)
{
uint32_t u32Item;
do
{
printf("\n");
printf("+-----------------------------------------------------------+\n");
printf("| Auto Baud Rate Function Test (Master) |\n");
printf("+-----------------------------------------------------------+\n");
printf("| [1] baud rate 38400 bps |\n");
printf("| [2] baud rate 57600 bps |\n");
printf("| [3] baud rate 115200 bps |\n");
printf("| |\n");
printf("| Select baud rate and master will send 0x1 to slave ... |\n");
printf("+-----------------------------------------------------------+\n");
printf("| Quit - [ESC] |\n");
printf("+-----------------------------------------------------------+\n\n");
u32Item = getchar();
printf("%c\n", u32Item);
/* Set different baud rate */
switch(u32Item)
{
case '1':
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 38400);
break;
case '2':
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 57600);
break;
default:
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
break;
}
/* Send input pattern 0x1 for auto baud rate detection bit length is 1-bit */
UART_WRITE(UART1, 0x1);
}
while(u32Item != 27);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Get UART Baud Rate Function */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t GetUartBaudrate(UART_T *uart)
{
uint8_t u8UartClkSrcSel=0, u8UartClkDivNum=0;
uint32_t u32ClkTbl[5] = {__HXT, 0ul, __LXT, __HIRC, 0ul};
uint32_t u32Baud_Div;
/* Get UART clock source selection and UART clock divider number */
switch((uint32_t)uart)
{
case UART0_BASE:
u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UART0DIV_Msk) >> CLK_CLKDIV0_UART0DIV_Pos;
break;
case UART1_BASE:
u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART1SEL_Msk) >> CLK_CLKSEL1_UART1SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UART1DIV_Msk) >> CLK_CLKDIV0_UART1DIV_Pos;
break;
case UART2_BASE:
u8UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART2SEL_Msk) >> CLK_CLKSEL3_UART2SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART2DIV_Msk) >> CLK_CLKDIV4_UART2DIV_Pos;
break;
}
/* Get PLL clock frequency if UART clock source selection is PLL */
if (u8UartClkSrcSel == 1ul)
{
u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq();
}
/* Get PCLK clock frequency if UART clock source selection is PCLK */
else if (u8UartClkSrcSel == 4ul)
{
if (uart == (UART_T *)UART0 || uart == (UART_T *)UART2)
u32ClkTbl[u8UartClkSrcSel] = CLK_GetPCLK0Freq();
else if (uart == (UART_T *)UART1)
u32ClkTbl[u8UartClkSrcSel] = CLK_GetPCLK1Freq();
}
/* Get UART baud rate divider */
u32Baud_Div = (uart->BAUD & UART_BAUD_BRD_Msk) >> UART_BAUD_BRD_Pos;
/* Calculate UART baud rate if baud rate is set in MODE 0 */
if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_MODE0)
return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul)) >> 4;
/* Calculate UART baud rate if baud rate is set in MODE 2 */
else if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_MODE2)
return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul));
/* Calculate UART baud rate if baud rate is set in MODE 1 */
else if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_BAUDM1_Msk)
return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul)) / (((uart->BAUD & UART_BAUD_EDIVM1_Msk) >> UART_BAUD_EDIVM1_Pos) + 1ul);
/* Unsupported baud rate setting */
else
return 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Auto Baud Rate Function Rx Test */
/*---------------------------------------------------------------------------------------------------------*/
void AutoBaudRate_RxTest(void)
{
/* Enable auto baud rate detect function */
UART1->ALTCTL |= UART_ALTCTL_ABRDEN_Msk;
printf("\nreceiving input pattern... ");
/* Wait until auto baud rate detect finished or time-out */
while((UART1->ALTCTL & UART_ALTCTL_ABRIF_Msk) == 0);
if(UART1->FIFOSTS & UART_FIFOSTS_ABRDIF_Msk)
{
/* Clear auto baud rate detect finished flag */
UART1->FIFOSTS = UART_FIFOSTS_ABRDIF_Msk;
printf("Baud rate is %dbps.\n", GetUartBaudrate(UART1));
}
else if(UART1->FIFOSTS & UART_FIFOSTS_ABRDTOIF_Msk)
{
/* Clear auto baud rate detect time-out flag */
UART1->FIFOSTS = UART_FIFOSTS_ABRDTOIF_Msk;
printf("Time-out!\n");
}
}
/*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
|