You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
6.7 KiB
237 lines
6.7 KiB
4 years ago
|
//////////////////////////////////////////////////////////////////////////
|
||
|
/// COPYRIGHT NOTICE
|
||
|
/// Copyright (c) 2015, 传控科技
|
||
|
/// All rights reserved.
|
||
|
///
|
||
|
/// @file main.c
|
||
|
/// @brief main app
|
||
|
///
|
||
|
///(本文件实现的功能的详述)
|
||
|
///
|
||
|
/// @version 1.1 CCsens technology
|
||
|
/// @author CC
|
||
|
/// @date 20150102
|
||
|
///
|
||
|
///
|
||
|
/// 修订说明:最初版本
|
||
|
/// Modified by:
|
||
|
/// Modified date:
|
||
|
/// Version:
|
||
|
/// Descriptions:
|
||
|
// 20160413 CC-ACC-VH02
|
||
|
// 连接至 J22 RXD0 TXD0
|
||
|
//P5_DIR &= ~BITN1; //p5.1输出TXD
|
||
|
//P5_DIR |= BITN0; //p5.0输入RXD
|
||
|
//P5_SEL0 &= ~(BITN0 +BITN1); //设置P5.0 P5.1为UART0 RXD TXD
|
||
|
//P5_SEL1 |= BITN0 +BITN1;
|
||
|
|
||
|
/*****************************************************************************
|
||
|
update by cc @201700110
|
||
|
针对多串口 和 单一串口 有区别 每个串口是独立的还是分开的有讲究 程序是复杂的还是软件应用简单是
|
||
|
个需要平衡的事情.
|
||
|
|
||
|
clib/clib.c:
|
||
|
公用的函数 和硬件无关
|
||
|
放置串行模式(串口等其他通讯总线类的输出)输出的函数,
|
||
|
一些覆盖模式输出的(lcd等固屏输出的)的也可使用
|
||
|
void Lc_print(void (*L0pf_send_uc)(char ww), char *dat,...)
|
||
|
-----------------------------------------------------------------------------------------
|
||
|
uartcom/Uprotocol2app
|
||
|
协议到应用
|
||
|
为了适应不同的通讯协议需要不同的uart口来对应 和应用相关
|
||
|
|
||
|
typedef struct _ts_lcm_pro_; 应用协议包的定义? LCM的协议------------
|
||
|
L3_UARTcom0_exp_protocol 解析应用协议
|
||
|
-----------------------------------------------------------------------------------------
|
||
|
uartcom/urec2protocol: 接收到的数据放入到指向特定协议的缓存中,和协议的格式有关 一般分为 标头式或者标尾式
|
||
|
公用的串口通讯定义
|
||
|
struct _s_uart_rec_ 的公共协议包(关键的结构体)的声明------struct _s_uart_rec_
|
||
|
void L1_uart_2buf(struct _s_uart_rec_ *p)串行数据保存到指向特定协议的缓冲中
|
||
|
--------------------------------------------------------------------------------------------
|
||
3 years ago
|
msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面idata
|
||
4 years ago
|
L0_UART0_Init
|
||
|
UART0_IRQHandler
|
||
|
L0_Usend_uc------UserDef
|
||
|
-----------------------------------------------------------------------------------------
|
||
|
********************************************************************************/
|
||
3 years ago
|
#include "msp_uart_x.h"
|
||
4 years ago
|
|
||
3 years ago
|
TS_uart_reg ts_uart[SERIAL_MAX_NUM] = {0};
|
||
4 years ago
|
|
||
|
void L0_uartN_set(U8 uartx,U8 x)
|
||
|
{
|
||
|
switch(uartx)
|
||
|
{
|
||
|
case 0:SBUF = (x);break;
|
||
|
case 1:S2BUF = (x);break;
|
||
|
case 2:S3BUF = (x);break;
|
||
|
case 3:S4BUF = (x);break;
|
||
|
default:break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
U8 L0_uartN_get(U8 uartx)
|
||
|
{
|
||
|
U8 x = 0;
|
||
|
switch(uartx)
|
||
|
{
|
||
|
case 0:x = SBUF; break;
|
||
|
case 1:x = S2BUF;break;
|
||
|
case 2:x = S3BUF;break;
|
||
|
case 3:x = S4BUF;break;
|
||
|
default:break;
|
||
|
}
|
||
|
return x;
|
||
|
}
|
||
|
|
||
|
void L0_waitFree_uartN(U8 uartx)
|
||
|
{
|
||
|
ts_uart[uartx].p->over = 0;
|
||
|
while(ts_uart[uartx].p->ok != D_ready)
|
||
|
{
|
||
3 years ago
|
#if 10 //发送数据特别快时,某些情况下会导致数据发送出错
|
||
|
if(ts_uart[uartx].p->over ++ > 6000000)
|
||
4 years ago
|
{
|
||
|
break;
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void L0_uartN_sendArray(U8 uartx,void *buf,U16 len)
|
||
|
{
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
ts_uart[uartx].p->ok = D_clear;
|
||
|
ts_uart[uartx].p->over = 0;
|
||
|
ts_uart[uartx].p->max = len;
|
||
|
ts_uart[uartx].p->now = 1;
|
||
|
if(len <= D_UART_send_buf_max)
|
||
|
{
|
||
|
//将参数buf拷贝至内部buf
|
||
|
for(ts_uart[uartx].p->num = 0;ts_uart[uartx].p->num < len;ts_uart[uartx].p->num ++)
|
||
|
{
|
||
|
ts_uart[uartx].p->buf[ts_uart[uartx].p->num] = ((U8*)buf)[ts_uart[uartx].p->num];
|
||
|
}
|
||
|
ts_uart[uartx].p->p = ts_uart[uartx].p->buf;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//不使用内部buf,如果再发送完毕之前,参数buf被回收,发送会出错
|
||
|
ts_uart[uartx].p->p = (U8 *)buf;
|
||
|
}
|
||
|
L0_uartN_set(uartx,ts_uart[uartx].p->p[0]);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_uc(U8 uartx,U8 ww)
|
||
|
{
|
||
|
L0_uartN_sendArray(uartx,&ww,1);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_us(U8 uartx,vU16 ww)
|
||
|
{
|
||
|
U_U16 uStemp;
|
||
|
uStemp.word = ww;
|
||
|
ts_uart[uartx].p->buf3[0] = uStemp.BYTE2.h;
|
||
|
ts_uart[uartx].p->buf3[1] = uStemp.BYTE2.l;
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,2);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_ul(U8 uartx,vU32 ww)
|
||
|
{
|
||
|
U_U32 uStemp;
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
uStemp.dWord = ww;
|
||
|
ts_uart[uartx].p->buf3[0] = uStemp.BYTE4.byte0;
|
||
|
ts_uart[uartx].p->buf3[1] = uStemp.BYTE4.byte1;
|
||
|
ts_uart[uartx].p->buf3[2] = uStemp.BYTE4.byte2;
|
||
|
ts_uart[uartx].p->buf3[3] = uStemp.BYTE4.byte3;
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,4);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_0d0a(U8 uartx)
|
||
|
{
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
ts_uart[uartx].p->buf3[0] = 0x0d;
|
||
|
ts_uart[uartx].p->buf3[1] = 0x0a;
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,2);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_uchex(U8 uartx,U8 ww)
|
||
|
{
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
ts_uart[uartx].p->buf3[0] = cguHex2Char[D_uc_high(ww)][1];
|
||
|
ts_uart[uartx].p->buf3[1] = cguHex2Char[D_uc_low (ww)][1];
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,2);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_ushex(U8 uartx,vU16 ww)
|
||
|
{
|
||
|
U_F16 k;
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
k.us = ww;
|
||
|
ts_uart[uartx].p->buf3[0] = cguHex2Char[D_uc_high(k.BYTE2.H)][1];
|
||
|
ts_uart[uartx].p->buf3[1] = cguHex2Char[D_uc_low (k.BYTE2.H)][1];
|
||
|
ts_uart[uartx].p->buf3[2] = cguHex2Char[D_uc_high(k.BYTE2.L)][1];
|
||
|
ts_uart[uartx].p->buf3[3] = cguHex2Char[D_uc_low (k.BYTE2.L)][1];
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,4);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_ulhex(U8 uartx,U32 ww)
|
||
|
{
|
||
|
U_U32 k;
|
||
|
L0_waitFree_uartN(uartx);
|
||
|
k.dWord = ww;
|
||
|
ts_uart[uartx].p->buf3[0] = cguHex2Char[D_uc_high(k.BYTE4.byte0)][1];
|
||
|
ts_uart[uartx].p->buf3[1] = cguHex2Char[D_uc_low (k.BYTE4.byte0)][1];
|
||
|
ts_uart[uartx].p->buf3[2] = cguHex2Char[D_uc_high(k.BYTE4.byte1)][1];
|
||
|
ts_uart[uartx].p->buf3[3] = cguHex2Char[D_uc_low (k.BYTE4.byte1)][1];
|
||
|
ts_uart[uartx].p->buf3[4] = cguHex2Char[D_uc_high(k.BYTE4.byte2)][1];
|
||
|
ts_uart[uartx].p->buf3[5] = cguHex2Char[D_uc_low (k.BYTE4.byte2)][1];
|
||
|
ts_uart[uartx].p->buf3[6] = cguHex2Char[D_uc_high(k.BYTE4.byte3)][1];
|
||
|
ts_uart[uartx].p->buf3[7] = cguHex2Char[D_uc_low (k.BYTE4.byte3)][1];
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->buf3,8);
|
||
|
}
|
||
|
|
||
|
void L0_uartN_sendstr(U8 uartx,U8 *str)
|
||
|
{
|
||
|
L0_uartN_sendArray(uartx,str,Lc_strlen(str));
|
||
|
}
|
||
|
|
||
|
void L0_uartN_uchexArray(U8 uartx,vU8 *buf,U16 n)
|
||
|
{
|
||
|
int i;
|
||
|
for(i=0;i<n;i++)
|
||
|
{
|
||
|
L0_uartN_uchex(uartx,buf[i]);
|
||
|
L0_uartN_uc(uartx,' ');
|
||
|
}
|
||
|
L0_uartN_0d0a(uartx);
|
||
|
}
|
||
|
|
||
3 years ago
|
void L1_uartN_uchexArray(U8 uartx,vU8 *buf,U16 n)
|
||
|
{
|
||
|
int i;
|
||
|
for(i=0;i<n;i++)
|
||
|
{
|
||
|
L0_uartN_uchex(uartx,buf[i]);
|
||
|
L0_uartN_uc(uartx,' ');
|
||
|
}
|
||
|
L0_uartN_0d0a(uartx);
|
||
|
}
|
||
|
#if 0
|
||
|
这样的好处是效率高 缺点是空间大
|
||
|
void L1_uartN_uchexArray(U8 uartx,U8 *buf,U16 len)
|
||
|
{
|
||
|
D_485_TX(); //切换到输出状态
|
||
|
if(len >= D_UART_send_buf_max)
|
||
|
{
|
||
|
len = D_UART_send_buf_max - 1;
|
||
|
|
||
|
}
|
||
|
Lc_hex2ascii(ts_uart[uartx].p->bufhex,buf,len);
|
||
|
L0_uartN_sendArray(uartx,ts_uart[uartx].p->bufhex,len*3);
|
||
|
}
|
||
|
|
||
|
#endif
|
||
4 years ago
|
|