- /****************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V2.00
- * $Revision: 2 $
- * $Date: 14/12/25 10:24a $
- * @brief
- * Transmit and receive data with auto flow control.
- * This sample code needs to work with UART_Autoflow_Slave.
- * @note
- * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "M0518.h"
- #define PLL_CLOCK 50000000
- #define RXBUFSIZE 1024
- /*---------------------------------------------------------------------------------------------------------*/
- /* Define functions prototype */
- /*---------------------------------------------------------------------------------------------------------*/
- extern char GetChar(void);
- int32_t main(void);
- void AutoFlow_FunctionTxTest(void);
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable Internal RC 22.1184MHz clock */
- CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
- /* Waiting for Internal RC clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
- /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
- /* Enable external XTAL 12MHz clock */
- CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
- /* Waiting for external XTAL clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
- /* Set core clock as PLL_CLOCK from PLL */
- CLK_SetCoreClock(PLL_CLOCK);
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_EnableModuleClock(UART1_MODULE);
- /* Select UART module clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
- CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set GPB multi-function pins for UART0 RXD and TXD */
- /* Set GPB multi-function pins for UART1 RXD, TXD, nRTS and nCTS */
- SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk |
- SYS_GPB_MFP_PB4_Msk | SYS_GPB_MFP_PB5_Msk |
- SYS_GPB_MFP_PB6_Msk | SYS_GPB_MFP_PB7_Msk);
- SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD |
- SYS_GPB_MFP_PB4_UART1_RXD | SYS_GPB_MFP_PB5_UART1_TXD |
- SYS_GPB_MFP_PB6_UART1_nRTS | SYS_GPB_MFP_PB7_UART1_nCTS);
- }
- void UART0_Init()
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART0 module */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 Baudrate */
- UART_Open(UART0, 115200);
- }
- void UART1_Init()
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART1 module */
- SYS_ResetModule(UART1_RST);
- /* Configure UART1 and set UART1 Baudrate */
- UART_Open(UART1, 115200);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* MAIN function */
- /*---------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, IP clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART0 for printf */
- UART0_Init();
- /* Init UART1 for testing */
- UART1_Init();
- /*---------------------------------------------------------------------------------------------------------*/
- /* SAMPLE CODE */
- /*---------------------------------------------------------------------------------------------------------*/
- printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
- printf("\n\nUART Sample Program\n");
- /* UART auto flow sample master function */
- AutoFlow_FunctionTxTest();
- while(1);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* AutoFlow Function Tx Test */
- /*---------------------------------------------------------------------------------------------------------*/
- void AutoFlow_FunctionTxTest()
- {
- uint32_t u32i;
- printf("\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| Pin Configure |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| ______ _____ |\n");
- printf("| | | | | |\n");
- printf("| |Master|--UART1_TXD(PB.5) <==> UART1_RXD(PB.4)--|Slave| |\n");
- printf("| | |--UART1_nCTS(PB.7) <==> UART1_nRTS(PB.6)--| | |\n");
- printf("| |______| |_____| |\n");
- printf("| |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| AutoFlow Function Test (Master) |\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 1k bytes data |\n");
- printf("| to slave.Slave will check if received data is correct |\n");
- printf("| after getting 1k bytes data. |\n");
- printf("| Press any key to start... |\n");
- printf("+-----------------------------------------------------------+\n");
- GetChar();
- /* Enable RTS and CTS autoflow control */
- UART_EnableFlowCtrl(UART1);
- /* Send 1k bytes data */
- for(u32i = 0; u32i < RXBUFSIZE; u32i++)
- {
- /* Send 1 byte data */
- UART_WRITE(UART1, u32i & 0xFF);
- /* Wait if Tx FIFO is full */
- while(UART_IS_TX_FULL(UART1));
- }
- printf("\n Transmit Done\n");
- }
|