Files
kunlun/driver/extern/virtualchannel/src/vp_uart.c

152 lines
3.3 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
*
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
* be copied by any method or incorporated into another program without
* the express written consent of Aerospace C.Power. This Information or any portion
* thereof remains the property of Aerospace C.Power. The Information contained herein
* is believed to be accurate and Aerospace C.Power assumes no responsibility or
* liability for its use in any way and conveys no license or title under
* any patent or copyright and makes no representation or warranty that this
* Information is free from patent or copyright infringement.
*
* ****************************************************************************/
/* iot common header files */
#include "iot_utils_api.h"
#include "iot_io_api.h"
#include "iot_errno_api.h"
#include "iot_board_api.h"
#include "iot_board.h"
#include "uart.h"
/* iot others header files */
#include "vp_uart.h"
#include "vc_task.h"
#if IOT_VRITUAL_UART_PORT_ENABLE
vp_uart_t vp_uart_handle[VP_UART_PORTID_MAX] = {0};
uint32_t vp_uart_send(uint8_t lport, iot_pkt_t *p_pkt)
{
int chn = -1;
uint8_t *buf = NULL;
uint32_t len;
uint8_t port = vp_uart_lport_to_pport(lport);
if (NULL == p_pkt) {
return ERR_FAIL;
}
if (port >= iot_board_get_virtual_uart_port_num()) {
goto err_out;
}
chn = vp_uart_port_to_channel(port);
buf = (uint8_t *)iot_pkt_data(p_pkt);
len = iot_pkt_data_len(p_pkt);
vc_channel_send(chn, buf, len);
iot_pkt_free(p_pkt);
return ERR_OK;
err_out:
iot_pkt_free(p_pkt);
return ERR_FAIL;
}
static uint32_t vp_uart_recv(vc_chn_id_e chn, iot_pkt_t *p_pkt)
{
int port, lport;
uint32_t len;
vp_uart_rcv_func func = NULL;
if (NULL == p_pkt) {
return 0;
}
if (!vp_uart_channel_valid(chn)) {
goto err_out;
}
len = iot_pkt_data_len(p_pkt);
port = vp_uart_channel_to_port(chn);
lport = vp_uart_pport_to_lport(port);
func = vp_uart_handle[port].recv_func;
if (NULL != func) {
func(lport, p_pkt);
}
return len;
err_out:
iot_pkt_free(p_pkt);
return 0;
}
int vp_uart_open(uint8_t lport, vp_uart_rcv_func rcv_func)
{
uint8_t vc_chn, ret = 0;
uint8_t port = vp_uart_lport_to_pport(lport);
if ((port >= iot_board_get_virtual_uart_port_num()) || (NULL == rcv_func)) {
ret = 1;
goto err_out;
}
/* vp uart open. */
vc_chn = vp_uart_port_to_channel(port);
if (ERR_OK != vc_channel_open(vc_chn, vp_uart_recv, VC_DRVID_UART)) {
ret = 2;
goto err_out;
}
vp_uart_handle[port].recv_func = rcv_func;
vp_uart_handle[port].pport = port;
iot_printf("[virtual]uart %d open successful!\n", port);
return lport;
err_out:
iot_printf("[virtual][err]vp uart open failed, ret:%d.\n", ret);
return -1;
}
#else
int vp_uart_open(uint8_t lport, vp_uart_rcv_func rcv_func)
{
(void)lport;
(void)rcv_func;
return ERR_OK;
}
uint32_t vp_uart_send(uint8_t lport, iot_pkt_t *p_pkt)
{
(void)lport;
(void)p_pkt;
if (NULL != p_pkt) {
iot_pkt_free(p_pkt);
}
return ERR_OK;
}
#endif