This commit is contained in:
moyuhai
2026-06-11 13:04:09 +08:00
commit 966451d245
2022 changed files with 513671 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
/*!
* \file main.c
*
* \brief Target main 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 "main.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
void clock_init(void)
{
CLOCK_InitCfg_t clock_cfg;
clock_cfg.hfclk_src = CLOCK_HFCLK_SRC_XTAL;
clock_cfg.hfclk_in = CLOCK_HFCLK_IN_32M;
clock_cfg.lfclk_src = CLOCK_LFCLK_SRC_RC;
if(clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32768;
} else if(clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_RC) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
}
xc_clock_init_cfg(&clock_cfg);
SysTick_Config(xc_clock_hfclk_in_get( ) / 100);
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}
void app_uart_init(void)
{
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.FunSel = UART0_TX;
gpio_cfg.Pin = GPIO_18;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
xc_gpio_init(&gpio_cfg);
gpio_cfg.FunSel = UART0_RX;
gpio_cfg.Pin = GPIO_19;
gpio_cfg.Dir = GPIO_DIR_INPUT;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_STOP_1_BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(UART0_IDX, &uart_cfg);
}
/**
* @brief main
* @details
* @param[in] void
* @param[out] void
* @retval int - 0
* @retval void
* @par identifier
* reserve
* @par other
* void
* @par change log
* Alex created on 2023-05-31
*/
int main(void)
{
// Clock Initialization
clock_init();
app_uart_init();
DEBUG("__START__\n");
/* uart demo */
uart_demo();
while(1)
{
/* main loop */
;
}
}
+192
View File
@@ -0,0 +1,192 @@
/*!
* \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 "xc_drv_uart.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
#define UART0_BUFFER_LEN 128
typedef struct
{
uint8_t r_buff[26];
uint8_t w_buff[26];
uint8_t r_len;
bool rx_done;
} uart_user_t;
uint8_t uart0_buff[UART0_BUFFER_LEN];
uart_user_t uart_user_ctl;
ring_buffer_t uart0_ring_buff;
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
void uart_receive_cb(uint8_t *buff, uint16_t len)
{
static uint8_t cnt = 0;
for (int i = 0; i < len; i++) {
uart_user_ctl.r_buff[cnt++] = buff[i];
uart_user_ctl.r_len++;
ring_buffer_queue(&uart0_ring_buff, buff[i]);
}
// if (cnt == sizeof(uart_user_ctl.r_buff))
{
uart_user_ctl.rx_done = true;
cnt = 0;
}
}
/**
* @brief uart_demo
* @details
* @param void
* @retval void
*/
void uart_demo(void)
{
DEBUG("__UART_DEMO__\n");
uint8_t uart_handle = UART0_IDX;
if (uart_handle == UART0_IDX) {
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.Pin = GPIO_18;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
gpio_cfg.FunSel = UART0_TX;
xc_gpio_init(&gpio_cfg);
gpio_cfg.Pin = GPIO_19;
gpio_cfg.Dir = GPIO_DIR_INPUT;
gpio_cfg.FunSel = UART0_RX;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_TCR_STOP_1BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(uart_handle, &uart_cfg);
} else if (uart_handle == UART1_IDX) {
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.Pin = GPIO_3;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
gpio_cfg.FunSel = UART1_TX;
xc_gpio_init(&gpio_cfg);
gpio_cfg.Pin = GPIO_2;
gpio_cfg.Dir = GPIO_DIR_INPUT;
gpio_cfg.FunSel = UART1_RX;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_TCR_STOP_1BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(uart_handle, &uart_cfg);
} else if (uart_handle == UART2_IDX) {
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux1;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.FunSel = GPIO_Dx;
gpio_cfg.Pin = GPIO_32; // uart2 tx
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
xc_gpio_init(&gpio_cfg);
gpio_cfg.Pin = GPIO_31; // uart2 rx
gpio_cfg.Dir = GPIO_DIR_INPUT;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_STOP_1_BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(uart_handle, &uart_cfg);
}
// ring_buffer_create(&uart0_ring_buff, uart0_buff, UART0_BUFFER_LEN);
// xc_uart_register_receive_cb(uart_receive_cb);
xc_uart_enable_rx_it(uart_handle);
if (uart_handle == UART0_IDX) {
NVIC_EnableIRQ(UART0_IRQn);
} else if (uart_handle == UART1_IDX) {
NVIC_EnableIRQ(UART1_IRQn);
} else if (uart_handle == UART2_IDX) {
NVIC_EnableIRQ(UART2_IRQn);
}
delay_us(20);
for (int i = 0; i < 26; i++) {
uart_user_ctl.w_buff[i] = i + 'A';
}
xc_uart_send_data(uart_handle, uart_user_ctl.w_buff, 26);
uint16_t ttt=0; uint16_t zzz=0;
// while (1) {
// if(!ring_buffer_is_empty(&uart0_ring_buff)){
// uint8_t uart_rx_buff[128];
// uint8_t len = ring_buffer_num_items(&uart0_ring_buff);
// ring_buffer_dequeue_arr(&uart0_ring_buff, uart_rx_buff, len);
// for(int i=0;i<len;i++){
// DEBUG("ddd=%x", uart_rx_buff[i]);
// }
// }
// }
}