163 lines
4.9 KiB
C
163 lines
4.9 KiB
C
#ifndef __CHRG_TTY_H__
|
|
#define __CHRG_TTY_H__
|
|
|
|
#include <rtthread.h>
|
|
#include <rtdevice.h>
|
|
#include <rthw.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef enum {
|
|
IDX_TTY_LCD = 0, // 0
|
|
IDX_TTY_NORTH, // 1
|
|
IDX_TTY_SOUTH, // 2
|
|
|
|
TOTAL_TTY
|
|
} eIDX_TTY;
|
|
|
|
|
|
//#define TTY_USING_TEST //使用测试功能
|
|
//#define TTY_USING_SAMPLE_SLAVE //使用从机示例
|
|
//#define TTY_USING_SAMPLE_MASTER //使用主机示例
|
|
//#define TTY_USING_DMA_RX //使用DMA接收
|
|
//#define TTY_USING_INT_TX //使用中断发送
|
|
//#define TTY_USING_DMA_TX //使用DMA发送
|
|
|
|
#ifndef TTY_SW_DLY_US
|
|
#define TTY_SW_DLY_US 0 // 发送引脚控制切换延时
|
|
#endif
|
|
|
|
#define SIZE_BUF_TTY 256
|
|
|
|
#define TTY_TX_COMP_TMO_MAX (3 * RT_TICK_PER_SECOND) // 最大DMA传输完成超时
|
|
#define TTY_BYTE_TMO_MIN 2 // 最小字节超时
|
|
#define TTY_BYTE_TMO_MAX 200 // 最大字节超时
|
|
|
|
#define TTY_EVT_RX_IND (1<<0)
|
|
#define TTY_EVT_RX_BREAK (1<<1)
|
|
|
|
struct chrg_tty_t {
|
|
rt_device_t dev; // serial device handle
|
|
const char *devname;
|
|
rt_mutex_t lock; // mutex handle
|
|
rt_event_t evt; // event handle
|
|
rt_uint8_t status; // connect status
|
|
rt_uint8_t level; // control pin send mode level, 0--low, 1--high
|
|
rt_int16_t pin; // control pin number used, -1--no using
|
|
rt_int32_t timeout; // receive block timeout, ms
|
|
rt_int32_t byte_tmo; // receive byte interval timeout, ms
|
|
#ifdef TTY_USING_DMA_TX
|
|
rt_int32_t tx_dly_ms;
|
|
struct rt_completion tx_comp; // send completion
|
|
#endif
|
|
};
|
|
|
|
/*
|
|
* @brief create tty instance dynamically
|
|
* @param serial - serial device name
|
|
* @param baudrate - serial baud rate
|
|
* @param parity - serial parity mode
|
|
* @param pin - mode contrle pin
|
|
* @param level - send mode level
|
|
* @retval instance handle
|
|
*/
|
|
struct chrg_tty_t *chrg_tty_create (const char *name, int baudrate, int parity, int pin, int level);
|
|
|
|
/*
|
|
* @brief destory tty instance created dynamically
|
|
* @param pTTY - instance handle
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_destory (struct chrg_tty_t *pTTY);
|
|
|
|
/*
|
|
* @brief config tty params
|
|
* @param pTTY - instance handle
|
|
* @param baudrate - baudrate of communication
|
|
* @param databits - data bits, 5~8
|
|
* @param parity - parity bit, 0~2, 0 - none, 1 - odd, 2 - even
|
|
* @param stopbits - stop bits, 0~1, 0 - 1 stop bit, 1 - 2 stop bits
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_config (struct chrg_tty_t *pTTY,
|
|
int baudrate, int databits, int parity, int stopbits);
|
|
|
|
extern void chrg_tty_mode_set (struct chrg_tty_t *pTTY, int mode);
|
|
|
|
/*
|
|
* @brief set wait datas timeout for receiving
|
|
* @param pTTY - instance handle
|
|
* @param tmo_ms - receive wait timeout, 0--no wait, <0--wait forever, >0--wait timeout, default = 0
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_set_recv_tmo (struct chrg_tty_t *pTTY, int tmo_ms);
|
|
|
|
/*
|
|
* @brief set byte interval timeout for receiving
|
|
* @param pTTY - instance handle
|
|
* @param tmo_ms - byte interval timeout, default is calculated from baudrate
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_set_byte_tmo (struct chrg_tty_t *pTTY, int tmo_ms);
|
|
|
|
/*
|
|
* @brief open tty connect
|
|
* @param pTTY - instance handle
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_connect (struct chrg_tty_t *pTTY);
|
|
|
|
/*
|
|
* @brief close tty connect
|
|
* @param pTTY - instance handle
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_disconn (struct chrg_tty_t *pTTY);
|
|
|
|
/*
|
|
* @brief receive datas from tty
|
|
* @param pTTY - instance handle
|
|
* @param buf - buffer addr
|
|
* @param size - maximum length of received datas
|
|
* @retval >=0 - length of received datas, <0 - error
|
|
*/
|
|
extern int chrg_tty_recv (struct chrg_tty_t *pTTY, void *buf, int size);
|
|
|
|
/*
|
|
* @brief send datas to tty
|
|
* @param pTTY - instance handle
|
|
* @param buf - buffer addr
|
|
* @param size - length of send datas
|
|
* @retval >=0 - length of sent datas, <0 - error
|
|
*/
|
|
extern int chrg_tty_send (struct chrg_tty_t *pTTY, void *buf, int size);
|
|
|
|
/*
|
|
* @brief break tty receive wait
|
|
* @param pTTY - instance handle
|
|
* @retval 0 - success, other - error
|
|
*/
|
|
extern int chrg_tty_break_recv (struct chrg_tty_t *pTTY);
|
|
|
|
/*
|
|
* @brief send data to tty and then receive response data from tty
|
|
* @param pTTY - instance handle
|
|
* @param send_buf - send buffer addr
|
|
* @param send_len - length of send datas
|
|
* @param recv_buf - recv buffer addr
|
|
* @param recv_size - maximum length of received datas
|
|
* @retval >=0 - length of received datas, <0 - error
|
|
*/
|
|
extern int chrg_tty_send_then_recv (struct chrg_tty_t *pTTY,
|
|
void *send_buf, int send_len, void *recv_buf, int recv_size);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|