/*! * \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 -------------------------------------------------------------------------------------*/ typedef struct { uint8_t r_buff[26]; uint8_t w_buff[26]; uint8_t r_len; bool rx_done; } uart_user_t; uart_user_t uart_user_ctl; /*------------------------------------------------------------------------------------ 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++; } if (cnt == sizeof(uart_user_ctl.r_buff)) { cnt = 0; uart_user_ctl.rx_done = true; } } /** * @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); } 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); while (1) { if (uart_user_ctl.rx_done == true) { // printf("\nrun\n"); xc_uart_send_data(uart_handle, uart_user_ctl.r_buff, uart_user_ctl.r_len); uart_user_ctl.rx_done = false; uart_user_ctl.r_len = 0; } } }