V1.0
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
/*!
|
||||
* \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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file uart.h
|
||||
*
|
||||
* \brief The head file of uart.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __UART_H__
|
||||
#define __UART_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void uart_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __UART_H__ */
|
||||
@@ -0,0 +1,103 @@
|
||||
/*!
|
||||
* \file uart_dma_test.c
|
||||
*
|
||||
* \brief Target uart test 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_dma.h"
|
||||
#include "xc60xx_uart.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief uart_dma_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void uart_dma_demo(void) {
|
||||
DEBUG("__UART_DMA_DEMO__\n");
|
||||
|
||||
uint8_t __attribute__((aligned(4))) rxbuff[26];
|
||||
UART_TypeDef *uart_handle = XC_UART0;
|
||||
|
||||
UART_InitTypeDef UART_InitStruct = {0};
|
||||
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 = UART0_TX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_2;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = UART0_RX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
UART_InitStruct.Parity = UART_PARITY_DISABLE;
|
||||
UART_InitStruct.StopBits = UART_STOP_1_BITS;
|
||||
UART_InitStruct.WordLength = UART_UARTx_TCR_CLS_8BITS;
|
||||
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
|
||||
UART_InitStruct.HardwareFlowControl = UART_UARTx_MCR_AFCE_DISABLE;
|
||||
|
||||
UART_Init(uart_handle, &UART_InitStruct);
|
||||
|
||||
UART_DMA_IT_Register_TxCallback(uart_handle);
|
||||
UART_DMA_IT_Register_RxCallback(uart_handle);
|
||||
|
||||
if (uart_handle == XC_UART0) {
|
||||
UART_DMA_IT_Enable(XC_UART0,
|
||||
1 << (DMA_CH_RCV_UART0 + 16) | 1 << DMA_CH_SEND_UART0);
|
||||
} else if (uart_handle == XC_UART1) {
|
||||
UART_DMA_IT_Enable(XC_UART1,
|
||||
1 << (DMA_CH_RCV_UART1 + 16) | 1 << DMA_CH_SEND_UART1);
|
||||
}
|
||||
|
||||
NVIC_EnableIRQ(DMAS_IRQn);
|
||||
|
||||
UARTx_DMA_Receive_Config(uart_handle);
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
uart_txdmabuff[i] = i + 'A';
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (UART_DMA_ReceiveData(uart_handle, rxbuff)) {
|
||||
UART_DMA_SendData(uart_handle, rxbuff, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file xc60xx_uart.h
|
||||
*
|
||||
* \brief The head file of xc60xx_uart.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __UART_DMA_H__
|
||||
#define __UART_DMA_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void uart_dma_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __UART_DMA_H__ */
|
||||
Reference in New Issue
Block a user