- /****************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V3.00
- * $Revision: 7 $
- * $Date: 15/09/02 10:05a $
- * @brief
- * Transmit and receive data in UART IrDA mode.
- * This sample code needs to work with UART_IrDA_Slave.
- * @note
- * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "M451Series.h"
- #define PLL_CLOCK 72000000
- /*---------------------------------------------------------------------------------------------------------*/
- /* Define functions prototype */
- /*---------------------------------------------------------------------------------------------------------*/
- extern char GetChar(void);
- int32_t main(void);
- void IrDA_FunctionTxTest(void);
- /*---------------------------------------------------------------------------------------------------------*/
- /* IrDA Function Transmit Test */
- /*---------------------------------------------------------------------------------------------------------*/
- void IrDA_FunctionTxTest()
- {
- uint8_t u8OutChar;
- printf("\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| Pin Configure |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| ______ _______ |\n");
- printf("| | | | | |\n");
- printf("| |Master|--UART1_TXD(PB.3) <==> UART1_RXD(PB.2)--|Slave | |\n");
- printf("| | | | | |\n");
- printf("| |______| |_______| |\n");
- printf("| |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| IrDA 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 data based on |\n");
- printf("| terminal input and Slave will printf received data on |\n");
- printf("| terminal screen. |\n");
- printf("+-----------------------------------------------------------+\n");
- /*
- UART0 is set to debug port and connect with PC firstly.
- The IrDA sample code needs two module board to execute.
- Set the master board is IrDA TX Mode and the other is IrDA Rx mode.
- Inputting char on terminal will be sent to the UART0 of master.
- After the master receiving, the inputting char will send to UART0 again.
- At the same time, it also sends to UART1 TX pin by IrDA mode.
- Slave will print received char after UART1 send out.
- Note that IrDA mode is ONLY used when baud rate equation is selected mode 0.
- */
- printf("\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| IrDA Function Mode Test (Master) |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| 1). Input char by UART0 terminal. |\n");
- printf("| 2). UART1 will send a char according to step 1. |\n");
- printf("| 3). Return step 1. (Press '0' to exit) |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("\nIrDA Sample Code Start. \n");
- /* Set IrDA Tx Mode, Baud Rate configuration must be used MODE0 */
- UART_SelectIrDAMode(UART1, 57600, UART_IRDA_TXEN);
- /* Wait Terminal input to send data to UART1 TX pin */
- do
- {
- u8OutChar = GetChar();
- printf(" Input: %c , Send %c out\n", u8OutChar, u8OutChar);
- UART_WRITE(UART1, u8OutChar);
- }
- while(u8OutChar != '0');
- printf("\nIrDA Sample Code End.\n");
- }
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable HIRC clock (Internal RC 22.1184MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Wait for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Select HCLK clock source as HIRC and and HCLK source divider as 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Enable HXT clock (external XTAL 12MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Wait for HXT clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_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 as HXT and UART module clock divider as 1 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
- CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
- SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
- SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
- /* Set PB multi-function pins for UART1 RXD(PB.2) and TXD(PB.3) */
- SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
- SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB2MFP_UART1_RXD | SYS_GPB_MFPL_PB3MFP_UART1_TXD);
- }
- 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, peripheral 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("\nUART Sample Program\n");
- /* UART sample IrDA Master function */
- IrDA_FunctionTxTest();
- while(1);
- }
|