RTK_base/user_uart/user_uart.c
2022-06-14 19:43:20 +08:00

69 lines
2.2 KiB
C

/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-06-14 fize the first version
*/
#include "user_uart.h"
static rt_err_t uart3_tx_complete(rt_device_t dev, void *buffer)
{
//todo: uart3_tx_complete sem: do nothing for now.
//since dma for tx not enabled,so this callback will not be called at any time.
LOG_I("massage sent %s",strlen(buffer));
return RT_EOK;
}
//dev 设备句柄(回调函数参数)
//size 缓冲区数据大小(回调函数参数)
//still in ISR func,so can only use ulog at asynchronous mode.
static rt_err_t uart3_rx_complete(rt_device_t dev, rt_size_t size)
{
char testbuff[20];
uart3_simpack.rx_num=size;
rt_sem_release(&uart3_simpack.rx_sem);
rt_device_read(serial3,0,testbuff, size);
// can only use ulog at asynchronous mode.
LOG_I("massage received : %s",testbuff);
return RT_EOK;
}
rt_device_t uart3_init(void)
{
struct serial_configure config;
serial3 = rt_device_find(USER_UART_NAME);
if (serial3 == RT_NULL)
{
LOG_E("could not find device: %s", USER_UART_NAME);
return RT_NULL;
}
config.baud_rate = BAUD_RATE_115200; //修改波特率为 115200
config.data_bits = DATA_BITS_8; //数据位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 2048; //修改缓冲区 buff size 为 2048
config.parity = PARITY_NONE; //无奇偶校验位
rt_device_control(serial3, RT_DEVICE_CTRL_CONFIG, &config);
if ((rt_device_set_tx_complete(serial3, uart3_tx_complete) || rt_device_set_rx_indicate(serial3, uart3_rx_complete))
!= RT_EOK)
{
LOG_E("could not set %s 's rx|tx callback func", USER_UART_NAME);
return RT_NULL;
}
//do not ust dma for rx and tx at same time
if ( rt_device_open(serial3, RT_DEVICE_FLAG_INT_TX |RT_DEVICE_FLAG_DMA_RX|RT_DEVICE_OFLAG_RDWR ))
{
LOG_E("could not open device: %s", USER_UART_NAME);
return RT_NULL;
}
rt_sem_init(&uart3_simpack.rx_sem, "rx3_sem", 0, RT_IPC_FLAG_PRIO);
rt_sem_init(&uart3_simpack.tx_sem, "tx3_sem", 0, RT_IPC_FLAG_PRIO);
return serial3;
}