#include "wk2xxx.h"
#include "delay.h"
#include "usart.h"
#include "io.h"
#include "ucos_ii.h"
//---芯片驱动函数,可以精简
static u8 SpiSendGetByte(char send_data);
static void Wk2124SetComSet(u8 port,u8 databit, u8 stopbit, u8 oddeven);
static void WK2124_Enable_TXF_Empty_Int(u8 port);
static void WK2124_Disable_TXF_Empty_Int(u8 port);
static u8 Wk2124ReadReg(u8 port,u8 reg);
static void Wk2124Init(u8 port);
static void Wk2124SetBaud(u8 port,int baud);
static void Wk2124WriteFifo(u8 port,u8 *wbuf,u16 len);
static void Wk2124ReadFifo(u8 port,u8 *rbuf,u16 len);
static u16 Wk2124SendBuf(u8 port, u8 *sendbuf,u16 len);
static void WK2XXX_EXTIX_Init(void);
static void WK2XXX_SPI_Init(void);
//---串口收发缓存,在头文件修改缓存大小
static unsigned char aux_uart1_rcv[AUX_UART1_RECV_SIZE];
static unsigned char aux_uart2_rcv[AUX_UART2_RECV_SIZE];
static unsigned char aux_uart3_rcv[AUX_UART3_RECV_SIZE];
static unsigned char aux_uart4_rcv[AUX_UART4_RECV_SIZE];
static unsigned char aux_uart1_snd[AUX_UART1_SEND_SIZE];
static unsigned char aux_uart2_snd[AUX_UART2_SEND_SIZE];
static unsigned char aux_uart3_snd[AUX_UART3_SEND_SIZE];
static unsigned char aux_uart4_snd[AUX_UART4_SEND_SIZE];
static unsigned char uart_int_flag = 0;//中断标志位,读取之后清零
usr_uart_t aux_uart[AUX_UART_NUM];//串口收发队列
OS_EVENT *WK0RSem;//任务驱动信号量
/***************************************************************************
Function: uart_buf_init
Description: 初始化单个串口接收和发送缓存队列,复位指针
Input: uart - 串口收发数据队列地址
rbuf - 接收数据缓存
rsize - 接收数据缓存大小
sbuf - 发送数据缓存
ssize- 发送数据缓存大小
Return:
HRG - 2022/3/10 - V0.1
Others:
***************************************************************************/
static void single_uart_buf_init(usr_uart_t * uart, u8 *rbuf, int rsize, u8 *sbuf, int ssize)
{
INT8U err;
uart->recv.in = 0;
uart->recv.out = 0;
uart->send.total = 0;
uart->recv.total = 0;
uart->send.in = 0;
uart->send.out = 0;
uart->recv.buffer = rbuf;
uart->recv.size = rsize;
uart->send.buffer = sbuf;
uart->send.size = ssize;
uart->recv.mut = OSMutexCreate(3, &err);
uart->send.mut = OSMutexCreate(4, &err);
}
/***************************************************************************
Function: usr_uart_buff_init
Description: 初始化接收和发送缓存队列
Input: void
Return:
HRG - 2022/3/10 - V0.1
Others:
***************************************************************************/
static void usr_uart_buff_init(void)
{
single_uart_buf_init(&aux_uart[AUX_UART1_INDEX], aux_uart1_rcv, AUX_UART1_RECV_SIZE, aux_uart1_snd, AUX_UART1_SEND_SIZE);
single_uart_buf_init(&aux_uart[AUX_UART2_INDEX], aux_uart2_rcv, AUX_UART2_RECV_SIZE, aux_uart2_snd, AUX_UART2_SEND_SIZE);
single_uart_buf_init(&aux_uart[AUX_UART3_INDEX], aux_uart3_rcv, AUX_UART3_RECV_SIZE, aux_uart3_snd, AUX_UART3_SEND_SIZE);
single_uart_buf_init(&aux_uart[AUX_UART4_INDEX], aux_uart4_rcv, AUX_UART2_RECV_SIZE, aux_uart4_snd, AUX_UART4_SEND_SIZE);
}
/***************************************************************************
Function: EXTI9_5_IRQHandler
Description: WK2124接收触发中断函数
Input: void
Return:
HRG - 2022/3/10 - V0.1
Others:
***************************************************************************/
void EXTI9_5_IRQHandler(void)
{
OS_CPU_SR cpu_sr=0;
OS_ENTER_CRITICAL();
EXTI_ClearITPendingBit(EXTI_Line8);//清除LINE4上的中断标志位
uart_int_flag = Wk2124ReadReg(WK2XXX_GPORT,WK2XXX_GIFR) & 0xf;
OSSemPost(WK0RSem);
OS_EXIT_CRITICAL();
}
/***************************************************************************
Function: init_wk2124_hard_port
Description: 初始化端口和数据队列函数,在初始中调用1次
Input: void
Return:
HRG - 2022/3/11 - V0.1
Others:
***************************************************************************/
void init_wk2124_hard_port(void)
{
WK0RSem = OSSemCreate(0); //WK0
WK2XXX_SPI_Init();
usr_uart_buff_init();
}
/***************************************************************************
Function: init_aux_uart
Description: 初始化串口函数,在初始中调用1次
Input: void
Return:
HRG - 2022/3/11 - V0.1
Others:
***************************************************************************/
void init_aux_uart(u8 uartport,u32 baud, u8 databit, u8 stopbit, u8 oddeven)
{
Wk2124Init(uartport+1);
Wk2124SetComSet(uartport+1,databit,stopbit,oddeven);
Wk2124SetBaud(uartport+1,baud);
}
/***************************************************************************
Function: os_get_uart_int_flag
Description: 获取中断触发的串口标志,因为在中断中已经读取了串口标志,直接让任务方位,省去重复读取,且获取之后立刻清空标志
Input:
Return:
HRG - 2022/3/10 - V0.1
Others:
***************************************************************************/
unsigned char os_get_uart_int_flag(void)
{
unsigned char ret = uart_int_flag;
uart_int_flag = 0;
return ret;
}
/***************************************************************************
Function: os_aux_uart_recvdata
Description: 串口接收数据处理函数,这个函数需要在任务中调用
Input: uartindex - 串口号
timeout_tick - 接收强制超时退出时间,单位系统tick,
Return: 读取状态 0-读取完成,1-超时
HRG - 2022/3/10 - V0.1
Others: 这个函数需要联合接收中断,在外部中断中发送信号量,然后再在在任务中调用该函数接收数据
***************************************************************************/
int os_aux_uart_recvdata(u8 uartindex, int timeout_tick)
{
INT8U err;
int i;
u8 tembuf[256];
int intick;
int ret = 0;
u16 rfcnt = 0;
u8 fsr;
uartindex = uartindex%AUX_UART_NUM;
intick = OSTimeGet();
//OSMutexPend(WK_SPI_mut,1000,&err);//等待互斥解锁,这里设置的是1000个tick,按照5mstick为5s时间
fsr = Wk2124ReadReg(uartindex+1,WK2XXX_FSR);
if(fsr & WK2XXX_RDAT)
{
rfcnt = Wk2124ReadReg(uartindex+1,WK2XXX_RFCNT);
if(rfcnt == 0)
{
rfcnt = 256;
}
}
while(rfcnt)
{
//这里可以开一个256字节缓存,先读数据,再从缓存中copy到数据接口中,担心这样会造成不必要的浪费,这样的话,互斥等待时间会比较长,依赖spi读取的时间。
OSMutexPend(aux_uart[uartindex].recv.mut,200,&err);//等待接收数据互斥量,防止某些任务中正在使用接收数据的情况
/*Wk2124ReadFifo(uartindex+1,tembuf,rfcnt);
for(i=0;i<rfcnt;i++)
{
aux_uart[uartindex].recv.buffer[aux_uart[uartindex].recv.in%aux_uart[uartindex].recv.size] = tembuf[i];
aux_uart[uartindex].recv.in++;
aux_uart[uartindex].recv.total++;
}
aux_uart[uartindex].recv.in %= aux_uart[uartindex].recv.size;*/
if(rfcnt < (aux_uart[uartindex].recv.size - aux_uart[uartindex].recv.in))
{
Wk2124ReadFifo(uartindex+1,&aux_uart[uartindex].recv.buffer[aux_uart[uartindex].recv.in],rfcnt);
aux_uart[uartindex].recv.in += rfcnt;
aux_uart[uartindex].recv.total += rfcnt;
}
else
{
rfcnt = aux_uart[uartindex].recv.size - aux_uart[uartindex].recv.in;
Wk2124ReadFifo(uartindex+1, &aux_uart[uartindex].recv.buffer[aux_uart[uartindex].recv.in],rfcnt);
aux_uart[uartindex].recv.in += rfcnt;
aux_uart[uartindex].recv.total += rfcnt;
}
aux_uart[uartindex].recv.in %= aux_uart[uart
评论0