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.
 
 
 
 

286 lines
8.1 KiB

//////////////////////////////////////////////////////////////////////////
/// COPYRIGHT NOTICE
/// Copyright (c) 2023 CCSENS
/// 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为UART2 RXD TXD
//P5_SEL1 |= BITN0 +BITN1;
////20221217_213321 CCmodify new uart struct for road protocol
/*****************************************************************************
update by cc @201700110
针对多串口 和 单一串口 有区别 每个串口是独立的还是分开的有讲究 程序是复杂的还是软件应用简单是
个需要平衡的事情.
c_lib/c_lib.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)串行数据保存到指向特定协议的缓冲中
--------------------------------------------------------------------------------------------
msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面
L0_UART2_Init
UART2_IRQHandler
L0_Usend_uc------UserDef
-----------------------------------------------------------------------------------------
********************************************************************************/
#include "msp_UART2.h"
///#include "msp_time2.h"
#include "c_crc.h"
#include "c_delay.h"
///#include "tpc_debug.h"
#include "debug_drv.h"
#include "tpc_road.h"
//#define _USE_485
///static volatile TS_Uart_Send_ ts_uart_send_shop;
///>>>>>>>>>>>>>>>>和cpu相关
#if(MainFre_5M == D_sys_MainFre)
#elif(MainFre_22M == D_sys_MainFre)
#if(BRT_115200 == D_UART2_BRT)
void L0_uart2_init(void) /// void UartInit(void) //115200bps@22.1184MHz
{
S2CON = 0x50; //8位数据,可变波特率
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xFC; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
}
#elif(BRT_460800 == D_UART2_BRT)
#elif(BRT_921600 == D_UART2_BRT)
#elif(BRT_19200 == D_UART2_BRT)
#elif(BRT_9600== D_UART2_BRT)
#else///9600
#endif//D_sys_MainFre)
#elif(MainFre_27M == D_sys_MainFre)
#elif(MainFre_44M == D_sys_MainFre)
#if(BRT_115200 == D_UART2_BRT)
void L0_uart2_init(void) /// void UartInit(void) //115200bps@44.2368MHz
{
S2CON = 0x50; //8位数据,可变波特率
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xF8; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
}
#elif(BRT_460800 == D_UART2_BRT)
#elif(BRT_921600 == D_UART2_BRT)
#elif(BRT_19200 == D_UART2_BRT)
#elif(BRT_9600== D_UART2_BRT)
#else///9600
#endif//D_sys_MainFre)
#else ///MainFre_11M
#if(BRT_115200 == D_UART2_BRT)
void L0_uart2_init(void) ///void UartInit(void) //115200bps@11.0592MHz
{
S2CON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0xE8; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
}
#elif(BRT_19200 == D_UART2_BRT)
#elif(BRT_9600== D_UART2_BRT)
#else///9600
#endif//D_sys_MainFre)
#endif//D_sys_MainFre)
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>和cpu相关
void L0_uart2_buf_init(void)
{
/// ts_uart[uNum2].p = &ts_uart_send_shop;
ts_Usend[D_UART2].max = ts_Usend[D_UART2].now = 0;
ts_Usend[D_UART2].ok = D_ready;
L0_uartN_sbuf(D_UART2);
ts_Usend[D_UART2].max_def = D_send2_max;
D_s2b_road2_init();
D_stdIO_P1(BITN1);
D_stdIO_P1(BITN0);
L0_uart2_init();
L0_uart2_IntRIClear();
L0_uart2_IntTIClear();
D_UART2_ES_INT(1); //打开串口中断
#if (D_UART2_485_TYPE != TYPE_485_NONE)
D_UART2_485_RX() //默认处于接收状态
#endif
L1_UART2_timer_isFree_init();
}
////U8 overtime_t; //超时溢出的计数器
////U8 overtime_flag; ///0:没有超时溢出 1:超时溢出
////U8 over_flag; ////串口接收结束的状态 0:超时溢出 1:超出max接收
///D_LED4_REV();
void L2_uart2_overtime_callback(void)
{////必须清除当前的idle
if(0 == ts_Urec[D_UART2].idle)
{
if(ts_Urec[D_UART2].overtime_t >= 2)
{
ts_Urec[D_UART2].idle = 1;//总线空闲
ts_Urec[D_UART2].head = 0;
//// printf(" ts_s2b_debug.fifo = %d ",(int)ts_s2b_debug.fifo);
if(ts_Urec[D_UART2].fifo > 0)
{ts_Urec[D_UART2].fifo = 0;
ts_Urec[D_UART2].ok = 1;/// 接收到的数据结束 : 总线空闲+buf非空
//// L1_uartD_sendArray((U8 *)ts_debug_rec.filter, 5);
/*******
L1_uartD_sendArrayhex((U8 *)&ts_debug_rec.filter, 5);
ts_debug_rec.filter = 0x31;
ts_debug_rec.R1 = 0x32;
ts_debug_rec.R2 = 0x33;
ts_debug_rec.R3 = 0x34;
ts_debug_rec.ocr = 0x35;
L1_uartD_sendArrayhex((U8 *)&ts_debug_rec.filter, 5);
*********/
}
}
ts_Urec[D_UART2].overtime_t ++;
}
}
/*************************************************
UART 中断
*************************************************/
void INTERRUPT_UART2(void) D_SERVE_UART2
{
if(L0_uart2_IntRI()) //如果是U0接收中断
{
L0_uart2_IntRIClear(); //清除接收中断标志
///>>>>>>>>>>>>>接收协议 可以更改为其他协议>>>>>>>>>>>>>>>>>>>>>>>>
ts_Urec[D_UART2].reg = L0_uart2_get();
ts_Urec[D_UART2].idle = 0;
ts_Urec[D_UART2].overtime_t = 0;///需要和tick 定时器中的配合 作为接收超时判定
/// L1_s2b_PH5_debug(&ts_s2b_debug);
/// L1_s2b_PH5_debug(TS_s2b_debug);
/// L1_s2b_road2(&ts_Urec[D_UART2]);
L1_s2b_road2(D_UART2);
///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
if(L0_uart2_IntTI()) //如果是U0发送中断
{
L0_uart2_IntTIClear(); //清除发送中断标志
if(ts_Usend[D_UART2].max != ts_Usend[D_UART2].now)
{
///L0_uartN_set(uNum2,ts_uart[uNum2].p->p[ts_uart[uNum2].p->now]);
L0_uart2_set(ts_Usend[D_UART2].sbuf[ts_Usend[D_UART2].now]);
ts_Usend[D_UART2].now ++;
}
else
{
ts_Usend[D_UART2].ok = D_ready;
ts_Usend[D_UART2].max = 0;
ts_Usend[D_UART2].now = 0;//可以发送下一个数据
#if (D_UART2_485_TYPE != TYPE_485_NONE)
D_UART2_485_RX() //切换到接收状态
///gfgfgfh
#endif
}
}
//NOP(); NOP(); NOP();
}
///L2_callback_UART2_free(&s_UART2_rec);
#if 0
void L1_uart2_sendArray(U8 * buf, U16 len)
{
#if (D_UART2_485_TYPE != TYPE_485_NONE)
D_UART2_485_TX() //切换到输出状态
#endif
L0_uartN_sendArray(uNum2,buf,len);
}
void L2_callback_UART2_free(Ts_uart_rev_ *p)
{
///Ts_uart_rev_ *p = &s_UART2_rec;
#define D_s_modbus_min 4 //modbus协议的最小长度
/// D_P24_REV();
////while(9) D_P24_REV();
if((p->head == 1) && (p->num >= D_s_modbus_min))
{
//LED1 ^= 1;
p->head = 0;
#ifdef D_use_crc
crc16qq(p->crc,p->buf,p->num - 2); ////fixme 20220311 ///crc16qq(p->crc,p->buf,p->num - 2); //校验CRC
#endif
// crc16(p->crc,p->buf,p->num - 2); //校验CRC
if(p->crc[0] == p->buf[p->num-2] && p->crc[1] == p->buf[p->num-1])
{
p->ok = 1;
/// LED0 ^= 1;
}
p->debugok = 1;////s_UART2_rec.debugok
}
L1_UART2_isFree_over();
//// D_motor1_FI_rev();
}
#endif