/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* Show how to use auto baud rate detection function.
*
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
void AutoBaudRate_Test(void);
void AutoBaudRate_TxTest(void);
void AutoBaudRate_RxTest(void);
void SYS_Init(void)
{
/* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
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 clock divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable HXT clock */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Enable PLL */
CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
/* Waiting for PLL stable */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* Select HCLK clock source as PLL and HCLK source divider as 2 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(UART1_MODULE);
/* Select UART module clock source as HXT and UART module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_HXT, CLK_CLKDIV0_UART1(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
/* Set PB multi-function pins for UART1 RXD(PB.6) and TXD(PB.7) */
SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB6MFP_Msk)) | SYS_GPB_MFPL_PB6MFP_UART1_RXD;
SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB7MFP_Msk)) | SYS_GPB_MFPL_PB7MFP_UART1_TXD;
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
void UART1_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART1 */
SYS_ResetModule(UART1_RST);
/* Configure UART1 and set UART1 Baudrate */
UART_Open(UART1, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* 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 */
UART0_Init();
/* Init UART1 */
UART1_Init();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %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()
{
uint32_t u32Item;
printf("\n");
printf("+-----------------------------------------------------------+\n");
printf("| Pin Configure |\n");
printf("+-----------------------------------------------------------+\n");
printf("| ______ _____ |\n");
printf("| | | | | |\n");
printf("| |Master|--UART1_TXD(PB.7) <==> UART1_RXD(PB.6)--|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()
{
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();
if(u32Item == 27) break;
printf("%c\n", u32Item);
/* Set different baud rate */
switch(u32Item)
{
case '1':
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 38400);
break;
case '2':
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 57600);
break;
default:
UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
break;
}
/* Send input pattern 0x1 for auto baud rate detection bit length is 1-bit */
UART_WRITE(UART1, 0x1);
}
while(1);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Get UART Baud Rate Function */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t GetUartBaudrate(UART_T* uart)
{
uint8_t u8UartClkSrcSel = 0, u8UartClkDivNum = 0;
uint32_t au32ClkTbl[4] = {__HXT, 0, __LXT, __HIRC};
uint32_t u32BaudDiv;
/* 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;
case UART3_BASE:
u8UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART3SEL_Msk) >> CLK_CLKSEL3_UART3SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART3DIV_Msk) >> CLK_CLKDIV4_UART3DIV_Pos;
break;
case UART4_BASE:
u8UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART4SEL_Msk) >> CLK_CLKSEL3_UART4SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART4DIV_Msk) >> CLK_CLKDIV4_UART4DIV_Pos;
break;
case UART5_BASE:
u8UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART5SEL_Msk) >> CLK_CLKSEL3_UART5SEL_Pos;
u8UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART5DIV_Msk) >> CLK_CLKDIV4_UART5DIV_Pos;
break;
}
/* Get PLL clock frequency if UART clock source selection is PLL */
if(u8UartClkSrcSel == 1)
au32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq();
/* Get UART baud rate divider */
u32BaudDiv = (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 ((au32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1) / (u32BaudDiv + 2)) >> 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 ((au32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1) / (u32BaudDiv + 2));
/* 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 ((au32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1) / (u32BaudDiv + 2)) / (((uart->BAUD & UART_BAUD_EDIVM1_Msk) >> UART_BAUD_EDIVM1_Pos) + 1);
/* Unsupported baud rate setting */
else
return 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Auto Baud Rate Function Rx Test */
/*---------------------------------------------------------------------------------------------------------*/
void AutoBaudRate_RxTest()
{
/* 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 2019 Nuvoton Technology Corp. ***/
|