https://www.nuvoton.com/tool-and-software/bsp-and-example-code/board-support-package/numicro-m0-board-support-package/
https://www.nuvoton.com/resource-download.jsp?tp_GUID=SW1820200914190828
下面这个是串口的发送与接收
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 4 $
* $Date: 18/07/12 9:43a $
* @brief
* Transmit and receive data from PC terminal via the RS232 interface.
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#define RXBUFSIZE 1024
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint8_t g_u8RecData[RXBUFSIZE] = {0};
volatile uint32_t g_u32comRbytes = 0;
volatile uint32_t g_u32comRhead = 0;
volatile uint32_t g_u32comRtail = 0;
volatile int32_t g_bWait = TRUE;
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
void USCI_UART_TEST_HANDLE(void);
void USCI_UART_FunctionTest(void);
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* 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));
/* Enable UART0 clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Switch UART0 clock source to HIRC */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Enable UUART0 clock */
CLK_EnableModuleClock(USCI0_MODULE);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and cyclesPerUs 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 UUART0 multi-function pins */
SYS->GPA_MFPH = (SYS->GPA_MFPH & ~(SYS_GPA_MFPH_PA9MFP_Msk | SYS_GPA_MFPH_PA10MFP_Msk)) |
(SYS_GPA_MFPH_PA9MFP_USCI0_DAT1 | SYS_GPA_MFPH_PA10MFP_USCI0_DAT0);
/* Lock protected registers */
SYS_LockReg();
}
void USCI0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init USCI */
/*---------------------------------------------------------------------------------------------------------*/
/* Configure USCI0 as UART mode */
UUART_Open(UUART0, 115200);
}
int main()
{
SYS_Init();
/* Init UART0 to 115200-8n1 for print message */
UART_Open(UART0, 115200);
/* Init USCI0 for test */
USCI0_Init();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
printf("\nUSCI UART Sample Program\n");
/* USCI UART sample function */
USCI_UART_FunctionTest();
while (1);
}
/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle USCI UART interrupt event */
/*---------------------------------------------------------------------------------------------------------*/
void USCI01_IRQHandler(void)
{
USCI_UART_TEST_HANDLE();
}
/*---------------------------------------------------------------------------------------------------------*/
/* USCI UART Callback function */
/*---------------------------------------------------------------------------------------------------------*/
void USCI_UART_TEST_HANDLE()
{
uint8_t u8InChar = 0xFF;
uint32_t u32IntSts = UUART_GET_PROT_STATUS(UUART0);
if (u32IntSts & UUART_PROTSTS_RXENDIF_Msk)
{
/* Clear RX end interrupt flag */
UUART_CLR_PROT_INT_FLAG(UUART0, UUART_PROTSTS_RXENDIF_Msk);
printf("\nInput:");
/* Get all the input characters */
while (!UUART_IS_RX_EMPTY(UUART0))
{
/* Get the character from USCI UART Buffer */
u8InChar = UUART_READ(UUART0);
printf("%c ", u8InChar);
if (u8InChar == '0')
{
g_bWait = FALSE;
}
/* 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++;
}
}
printf("\nTransmission Test:");
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* USCI UART Function Test */
/*---------------------------------------------------------------------------------------------------------*/
void USCI_UART_FunctionTest()
{
uint8_t u8InChar = 0xFF;
uint32_t u32Temp;
printf("+-----------------------------------------------------------+\n");
printf("| USCI UART Function Test |\n");
printf("+-----------------------------------------------------------+\n");
printf("| Description : |\n");
printf("| The sample code will print input char on terminal |\n");
printf("| Please enter any to start (Press '0' to exit) |\n");
printf("+-----------------------------------------------------------+\n");
/*
Using a RS232 cable to connect USCI-UART0 and PC.
USCI-UART0 is set to debug port. USCI-UART0 is enable RX end interrupt.
When inputting char to terminal screen, RX end interrupt will happen and
USCI-UART0 will print the received char on screen.
*/
/* Enable USCI UART interrupt */
UUART_ENABLE_TRANS_INT(UUART0, UUART_INTEN_RXENDIEN_Msk);
NVIC_EnableIRQ(USCI_IRQn);
while (g_bWait)
{
u32Temp = g_u32comRtail;
if (g_u32comRhead != u32Temp)
{
u8InChar = g_u8RecData[g_u32comRhead];
while (UUART_IS_TX_FULL(UUART0)); /* Wait Tx is not full to transmit data */
UUART_WRITE(UUART0, u8InChar);
g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1);
g_u32comRbytes--;
}
}
/* Disable USCI UART receive and transmit end interrupt */
UUART_DISABLE_TRANS_INT(UUART0, UUART_INTEN_RXENDIEN_Msk | UUART_INTEN_TXENDIEN_Msk);
NVIC_DisableIRQ(USCI_IRQn);
g_bWait = TRUE;
printf("\nUSCI UART Sample Demo End.\n");
}
/*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/
|