/**************************************************************************//**
* @file main.c
* @version V3.00
* $Revision: 4 $
* $Date: 18/07/16 10:25a $
* @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 "NuMicro.h"
#define RXBUFSIZE 8
/*---------------------------------------------------------------------------------------------------------*/
/* 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;
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void);
void UART_TEST_HANDLE(void);
void UART_FunctionTest(void);
void SYS_Init(void)
{
SYS_UnlockReg();
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* 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 clock 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;
CLK_EnableModuleClock(UART1_MODULE);
//// CLK_EnableModuleClock(UART2_MODULE);
/* Select IP clock source */
/* Select UART1 clock source is HIRC */
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART1SEL_HIRC, CLK_CLKDIV0_UART1(1));
/* Select UART2 clock source is HIRC */
//// CLK_SetModuleClock(UART2_MODULE, CLK_CLKSEL3_UART2SEL_HIRC, CLK_CLKDIV4_UART2(1));
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
//// /* Set PB multi-function pins for UART2 RXD=PB.4 and TXD=PB.5 */
//// SYS->GPB_MFPH = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB4MFP_Msk | SYS_GPB_MFPL_PB5MFP_Msk)) | \
//// (SYS_GPB_MFPL_PB4MFP_UART2_RXD | SYS_GPB_MFPL_PB5MFP_UART2_TXD);
/* 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_PB7MFP_Msk)) | \
(SYS_GPB_MFPL_PB6MFP_UART1_RXD | SYS_GPB_MFPL_PB7MFP_UART1_TXD);
SYS_LockReg();
}
uint8_t uart_send_data[RXBUFSIZE]={0x5a,0xa5,0x00,0x00,0x00,0x00,0x01,0xee};
uint8_t send_buff = 0 ;
uint8_t send_flag = 0;
int32_t main(void)
{
SYS_UnlockReg();
SYS_Init();
SYS_LockReg();
SYS_ResetModule(UART1_RST);
UART_Open(UART1, 115200);
UART_EnableInt(UART1, (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk));
while(1);
}
/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle UART Channel 0 interrupt event */
/*---------------------------------------------------------------------------------------------------------*/
void UART1_IRQHandler(void)
{
// UART_TEST_HANDLE();
//循环发送
if(send_flag == RXBUFSIZE-1)
{
send_flag=0;
}else
{
while(UART_IS_TX_FULL(UART1));
UART_WRITE(UART1, uart_send_data[send_flag]);
send_flag++;
}
// UART_ClearIntFlag(UART1, (UART_INTSTS_RLSINT_Msk| UART_INTSTS_BUFERRINT_Msk));
}
/*---------------------------------------------------------------------------------------------------------*/
/* UART Callback function */
/*---------------------------------------------------------------------------------------------------------*/
void UART_TEST_HANDLE()
{
uint8_t u8InChar = 0xFF;
if (UART_GET_INT_FLAG(UART1,UART_INTSTS_RDAINT_Msk))
{
/* Get all the input characters */
while(UART_IS_RX_READY(UART1))
{
/* Get the character from UART Buffer */
u8InChar = UART_READ(UART1);
/* 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++;
}
}
}
if(UART_GET_INT_FLAG(UART1, UART_INTSTS_THREINT_Msk))
{
uint16_t tmp;
tmp = g_u32comRtail;
if(g_u32comRhead != tmp)
{
u8InChar = g_u8RecData[g_u32comRhead];
while(UART_IS_TX_FULL(UART1)); /* Wait Tx is not full to transmit data */
UART_WRITE(UART1, u8InChar);
g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1);
g_u32comRbytes--;
}
}
if(UART1->FIFOSTS & (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk | UART_FIFOSTS_RXOVIF_Msk))
{
UART_ClearIntFlag(UART1, (UART_INTSTS_RLSINT_Msk| UART_INTSTS_BUFERRINT_Msk));
}
}
|