Files
kunlun/inc/uart/iot_uart_h.h
2024-09-28 14:24:04 +08:00

214 lines
6.7 KiB
C

/****************************************************************************
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.
****************************************************************************/
#ifndef IOT_UART_H_H
#define IOT_UART_H_H
#include "iot_config.h"
#include "iot_frame_parse_api.h"
#include "os_task_api.h"
#include "os_lock_api.h"
#include "os_timer_api.h"
#include "iot_mem_pool_api.h"
#if (IOT_UART_DMA_SUPPORT == 1)
#include "osif_dma_uart.h"
#else
typedef void* iot_dma_uart_h;
#endif
/* export includes */
#include "iot_uart_api.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Type of uart phy-port..
*/
typedef enum _iot_uart_type_e {
UART_TYPE_OC = 0, /* Uart is on KL chip. */
UART_TYPE_EXT, /* Uart is on an ext-uart-chip. */
UART_TYPE_VP /* Uart is virtual port, by USB transfered. */
} iot_uart_type_e;
typedef struct _iot_uart {
//iot_list_head_t link;
#ifdef _MSC_VER
/*receive task handle for win simulator*/
os_task_h receive_task_handle;
void *uart_handle;
#else
int uart_handle;
iot_dma_uart_h uart_dma_handle;
#endif
int uart_port;
/* the callback after receive a frame*/
iot_uart_recv_func_t recv_func;
/* save callback function before taken */
iot_uart_recv_func_t old_recv_func;
/*the receive buffer per uart port*/
uint8_t* recv_buffer;
uint32_t recv_buffer_len;
iot_mem_pool_t *mem_pool;
ringbuf_t recv_ring;
/*frame format per uart port*/
iot_frame_fmt *frame_fmt;
/* save old frame format */
iot_frame_fmt old_frame_fmt;
/*the lock for receive buffer*/
os_mutex_h frame_buffer_lock;
/*if receive the preamble code, will create a timer to receive the back code,
if timeout happen, will clear the data in receive buffer, the time out value
can be changed in iot_frame_fmt format*/
timer_id_t frame_timer;
/*send buffer pre uart port*/
ringbuf_t send_ring;
uint8_t* send_buffer;
//uint32_t send_cur_pos;
uint32_t send_buffer_len;
/* memory pool for send buffer */
iot_mem_pool_t *send_mem_pool;
/* flag to mark already tanken */
uint8_t taken;
/* 1 is dma mode*/
uint8_t is_dma_mode;
uint32_t dma_rx_pending_cnt;
uint32_t bandrate;
/* timeout for no formate frame checking */
uint32_t no_fmt_timeout;
} iot_uart_h_t;
/**
* @brief Check if a logic-port is an onchip port.
*/
uint32_t iot_uart_is_onchip(uint8_t lport);
/**
* @brief Check if a logic-port is an external port.
*/
uint32_t iot_uart_is_external(uint8_t lport);
/**
* @brief Check if a logic-port is an virtual port.
*/
uint32_t iot_uart_is_virtual(uint8_t lport);
/**
* @brief Convert logic-port to phy-port.
*/
uint8_t iot_uart_lport_to_pport(uint8_t lport);
/**
* @brief Convert phy-port to logic-port.
*/
uint8_t iot_uart_pport_to_lport(iot_uart_type_e type, uint8_t pport);
/*
* @brief iot_uart_init() - init uart internal resource.
* @param ftm_mode flag to mark if current firmware is working in FTM mode
*
* @return: true - success
* @return: otherwise - failure
*/
bool_t iot_uart_init(uint8_t ftm_mode);
/*
* iot_uart_deinit() - deinit uart internal resource.
*
* return:
* true - success
* otherwise - failure
*/
bool_t iot_uart_deinit();
/**
* @brief iot_uart_taken() - for the already opened uart, get the existing uart
* handle by uart port id, and install the new receive
* callback function and format. If the uart port is
* not opened yet, the behavior is same as
* iot_uart_open. NOTE that this function is tricky,
* the callback function can be called before this
* function returned.
* @param uart_port: uart port id
* @param func: callback function to receive data
* @receive_buffer_len: the buffer size to receive a full frame.
* @p_frame_fmt: the format structure of a full frame, if it is null,
* recv_func will be invoked once receive any uart message.
* @retval: NULL -- fail.
* @retval: otherwise -- pointer to uart handle.
*/
iot_uart_h iot_uart_taken(const uint8_t uart_port, iot_uart_recv_func_t func,
uint32_t receive_buffer_len, iot_frame_fmt *p_frame_fmt);
/**
* @brief iot_uart_release() - release already taken uart port. NOTE that this
* function is tricky, the callback function can be
* called after this function returned.
* @param uart_port: uart port id
*/
void iot_uart_release(const uint8_t uart_port);
/**
* @brief iot_uart_send_taken() - send the buffer by already taken uart port,
* it is a asyn func.
* @param uart_h: handle of the UART obtained by the iot_uart_taken
* function.
* @param pkt: the iot pkt buffer.
* @param p_frame_fmt: the format structor of the frame.
* @retval: 0 -- success.
* @retval: otherwise -- error code.
*/
uint32_t iot_uart_send_taken(iot_uart_h uart_h, iot_pkt_t *pkt,
iot_frame_fmt *p_frame_fmt);
/**
* @brief iot_uart_set_config_taken() - set paramter to the already taken
* uart port.
* @param uart_h: handle of the UART obtained by the iot_uart_taken
* @param baud: baudrate
* @param parity: see IOT_UART_PARITY_XXX
* @param data: data bits
* @param stop: stop flag
*
* @retval: false -- for failure case
* @retcal: true -- success
*/
bool_t iot_uart_set_config_taken(iot_uart_h uart_h, uint32_t baud,
uint8_t parity, uint8_t data, uint8_t stop);
/*
* iot_uart_task_post_ext_isr_event() - Post isr event to uart task.
*/
void iot_uart_task_post_ext_isr_event(void);
/**
* @brief iot_uart_ctrl_ir_loopback() - set paramter to crtl ir loopback
*
* @param en: true-close ir loopback false-open ir loopback
*
* @retval: false -- fail
* @retcal: true -- success
*/
bool_t iot_uart_ctrl_ir_loopback(iot_uart_h uart_h, bool_t en);
#ifdef __cplusplus
}
#endif
#endif /* IOT_UART_H_H */