Files
youled62d_bluetooth/component/xc6xx_drivers/Drivers/test_case/UART/uart.c
T
moyuhai e8074162f3 V1.0
2026-06-11 13:00:46 +08:00

134 lines
4.4 KiB
C

/*!
* \file uart.c
*
* \brief Target uart implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "uart.h"
#include "xc6xxx_hal_uart.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief uart_demo
* @details
* @param void
* @retval void
*/
void uart_demo(void)
{
DEBUG("__UART_DEMO__\n");
uint8_t rxbuff[26];
UART_TypeDef *uart_handle = XC_UART2; // XC_UART0;//XC_UART1;//XC_UART2;
if (uart_handle == XC_UART1) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Mux = GPIO_Mux0;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Int = NOT_INT;
GPIO_InitStruct.Pin = GPIO_3;
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
GPIO_InitStruct.FunSel = UART1_TX;
GPIO_Init(&GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_2;
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
GPIO_InitStruct.FunSel = UART1_RX;
GPIO_Init(&GPIO_InitStruct);
UART_InitTypeDef UART_InitStruct = {0};
UART_InitStruct.Parity = UART_PARITY_DISABLE;
UART_InitStruct.StopBits = UART_STOP_1_BITS;
UART_InitStruct.WordLength = UART_DATA_8_BITS;
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
UART_InitStruct.HardwareFlowControl = UART_HWFC_DISABLE;
UART_Init(uart_handle, &UART_InitStruct);
} else if (uart_handle == XC_UART2) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Mux = GPIO_Mux1;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Int = NOT_INT;
GPIO_InitStruct.FunSel = GPIO_Dx;
GPIO_InitStruct.Pin = GPIO_32; // uart2 tx
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
GPIO_Init(&GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_31; // uart2 rx
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
GPIO_Init(&GPIO_InitStruct);
UART_InitTypeDef UART_InitStruct = {0};
UART_InitStruct.Parity = UART_PARITY_DISABLE;
UART_InitStruct.StopBits = UART_STOP_1_BITS;
UART_InitStruct.WordLength = UART_DATA_8_BITS;
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
UART_InitStruct.HardwareFlowControl = UART_HWFC_DISABLE;
UART_Init(uart_handle, &UART_InitStruct);
}
UART_Enable_RxIT(uart_handle);
if (uart_handle == XC_UART0) {
NVIC_EnableIRQ(UART0_IRQn);
} else if (uart_handle == XC_UART1) {
NVIC_EnableIRQ(UART1_IRQn);
} else if (uart_handle == XC_UART2) {
NVIC_EnableIRQ(UART2_IRQn);
}
for (int i = 0; i < 26; i++) {
rxbuff[i] = i + 'A';
}
UART_SendData(uart_handle, rxbuff, 26);
uint16_t data_len = 0;
while (1) {
data_len = UART_ReceiveData(uart_handle, rxbuff);
if (data_len) {
UART_SendData(uart_handle, rxbuff, data_len);
data_len = 0;
}
}
}