KT24-1110_65E-HA-651B/include_lib/driver/cpu/br25/asm/uart_dev.h
2024-11-10 18:44:17 +08:00

176 lines
5.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _UART_DEV_H_
#define _UART_DEV_H_
#include "typedef.h"
#include "os/os_api.h"
#include "jiffies.h"
#include "irq.h"
/* #include "jtime.h" */
#define CONFIG_ENABLE_UART_SEM 1
#define SET_INTERRUPT ___interrupt
#define irq_disable(x) bit_clr_ie(x)
#define irq_enable(x) bit_set_ie(x)
#ifndef time_after
#define time_after(a,b) (((long)(b) - (long)(a)) < 0)
#endif
#ifndef time_before
#define time_before(a,b) time_after(b,a)
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
static inline u32 ut_get_jiffies(void)
{
#if 1
return jiffies;
#endif
#if 0
return Jtime_updata_jiffies();
#endif
}
static inline u32 ut_msecs_to_jiffies(u32 msecs)
{
if (msecs >= 10) {
msecs /= 10;
} else if (msecs) {
msecs = 1;
}
return msecs;
}
#if CONFIG_ENABLE_UART_SEM
typedef OS_SEM UT_Semaphore ;
static inline void UT_OSSemCreate(UT_Semaphore *sem, u32 count)
{
os_sem_create(sem, count);
}
static inline void UT_OSSemPost(UT_Semaphore *sem)
{
os_sem_post(sem);
}
static inline u32 UT_OSSemPend(UT_Semaphore *sem, u32 timeout)
{
return os_sem_pend(sem, timeout);
}
static inline void UT_OSSemSet(UT_Semaphore *sem, u32 count)
{
os_sem_set(sem, count);
}
static inline void UT_OSSemClose(UT_Semaphore *sem)
{
}
static inline void ut_sleep()
{
os_time_dly(1);
}
#else
typedef volatile u32 UT_Semaphore;
static inline void UT_OSSemCreate(UT_Semaphore *sem, u32 count)
{
*sem = count;
}
static inline void UT_OSSemPost(UT_Semaphore *sem)
{
(*sem)++;
}
static inline u32 UT_OSSemPend(UT_Semaphore *sem, u32 timeout)
{
u32 _timeout = timeout + ut_get_jiffies();
extern void clr_wdt();
while (1) {
if (*sem) {
(*sem) --;
break;
}
if ((timeout != 0) && time_before(_timeout, ut_get_jiffies())) {
return -1;
}
clr_wdt();
}
return 0;
}
static inline void UT_OSSemSet(UT_Semaphore *sem, u32 count)
{
*sem = count;
}
static inline void UT_OSSemClose(UT_Semaphore *sem)
{
}
static inline void ut_sleep()
{
extern void clr_wdt();
clr_wdt();
}
#endif
typedef void (*ut_isr_cbfun)(void *ut_bus, u32 status);
struct uart_platform_data_t {
u8 tx_pin; ///< 作为发送引脚的引脚号可从参考gpio.h枚举中选当引脚为空时则填 -1
u8 rx_pin; ///< 作为接收引脚的引脚号可从参考gpio.h枚举中选当引脚为空时则填 -1
void *rx_cbuf; ///< 如果使用中断DMA接收则写入循环buf的首地址ut中断使能如果不使用则写入NULL无中断
u32 rx_cbuf_size; ///< 循环buf的大小,必须为2的多少几次幂如果不用循环buf该值无效可写NULL
u32 frame_length; ///< 产生RT中断的字节数如无中断该值无效
u32 rx_timeout; ///< 产生OT中断的时间值单位ms如无中断该值无效
ut_isr_cbfun isr_cbfun; ///< ut中断的回调函数句柄不用回调函数则写入NULL如无中断句柄无效
void *argv; ///< ut中断的回调函数的一个扩展形参可供用户设定如无回调函数此参数无效
u32 is_9bit: 1; ///< ut九位模式使能位0关闭1使能
u32 baud: 24; ///< ut的波特率
};
/**
* @brief 循环buf结构体类型定义
*/
typedef struct {
u8 *buffer; ///<循环buf的首地址
u32 buf_size; ///<循环buf的大小
u32 buf_in; ///<循环buf的写偏移量
u32 buf_out; ///<循环buf的读偏移量
} KFIFO;
enum {
UT_TX = 1,
UT_RX,
UT_RX_OT
};
/**
* @brief ut初始化函数的返回结构体含各函数指针供外部使用
*/
typedef struct {
ut_isr_cbfun isr_cbfun; ///< ut中断的回调函数句柄不用回调函数则写入NULL如无中断句柄无效
void *argv; ///< ut中断的回调函数的一个扩展形参,在此返回
void (*putbyte)(char a); ///< ut发送一个byte
u8(*getbyte)(u8 *buf, u32 timeout); ///< ut接收一个bytebuf字节存放地址timeout超时时间单位ms返回0失败返回1成功
u32(*read)(u8 *inbuf, u32 len, u32 timeout); ///< ut接收一个字符串inbuf字符串存放首地址len预接收长度timeout超时时间单位ms返回实际接收的长度
void (*write)(const u8 *outbuf, u32 len); ///< ut发送一个字符串outbuf字符串首地址len发送的字符串长度
void (*set_baud)(u32 baud); ///< ut设置波特率baud波特率值
u32 frame_length;
u32 rx_timeout;
KFIFO kfifo; ///< ut用的循环buf结构体的指针
UT_Semaphore sem_rx;
UT_Semaphore sem_tx;
} uart_bus_t;
const uart_bus_t *uart_dev_open(const struct uart_platform_data_t *arg);
u32 uart_dev_close(uart_bus_t *ut);
////////////////////////////////////////////////////////////////////////////////
#endif