LWL/nlib/cantalk/canframe.h

285 lines
6.8 KiB
C
Raw Normal View History

2024-07-10 06:01:39 +00:00
#ifndef _CANFRAME_H_
#define _CANFRAME_H_
#include "stdBool.h"
/*
*/
/*******************<2A><>*********************/
/*<2A><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>ʶ<EFBFBD><CAB6>*/
#define CAN_MAXSTD 0x20
/*<2A><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ*/
#define CAN_MAXADDR 0x30
/*<2A><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ϵͳ<CFB5>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>*/
#define CAN_LOCK 0
#define CAN_DATA_SIZE 4
/**********<2A><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>***********/
typedef unsigned char CFUint8;
typedef unsigned short CFUint16;
typedef unsigned int CFUint32;
typedef signed char CFInt8;
typedef signed short CFInt16;
typedef signed int CFInt32;
/*CAN<41><4E><EFBFBD><EFBFBD>֡<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ*/
/*<2A><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ݣ<EFBFBD><DDA3>Խ<EFBFBD><D4BD>ܷ<EFBFBD><DCB7><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
#define CAN_WRITE 1
/*<2A><><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3>Խ<EFBFBD><D4BD>ܷ<EFBFBD>û<EFBFBD><C3BB>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
#define CAN_READ 0
/*CANͨ<4E><CDA8><EFBFBD>ϵ<EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB6A8>*/
/*
* 8BIT<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 0BIT:<EFBFBD>жϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>Ļ<EFBFBD><EFBFBD>DZ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 1-3BIT:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 4-7BIT:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#pragma pack(1)
typedef struct
{
CFUint8 WR:1;
CFUint8 len:3;
CFUint8 type:4;
}CFCmd;
/*canͨ<6E><CDA8><EFBFBD>ϵĸ<CFB5>ʽ*/
typedef struct
{
CFCmd cmd; /*<2A><>Э<EFBFBD><D0AD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
CFUint8 addr; /*<2A><>Э<EFBFBD><D0AD>֡<EFBFBD>ĵ<EFBFBD>ַ*/
CFUint8 data[CAN_DATA_SIZE]; /*Э<><D0AD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
CFUint8 sn; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ÿ<>η<EFBFBD><CEB7>ͳɹ<CDB3><C9B9><EFBFBD>1*/
}CanFormat;
#pragma pack()
/*<2A><><EFBFBD><EFBFBD><E5BAAF>ָ<EFBFBD><D6B8>*/
typedef void(*TFunc)(void* parameter);
/*<2A><>¼<EFBFBD><C2BC>ǰ״̬*/
typedef enum
{
CanTalk_Normal,
CanTalk_SendFailed, /*<2A><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>*/
CanTalk_RecTimeOut, /*<2A><><EFBFBD>ܳ<EFBFBD>ʱ*/
}CanTalkStu;
/*ģʽ <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
/*<2A><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
#define CANTalk_Listen 0x00
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
#define CANTalk_Listen_Read 0x01
/*<2A><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
#define CANTalk_Write 0x02
/*<2A><><EFBFBD><EFBFBD>д<EFBFBD>󶨣<EFBFBD><F3B6A8A3>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
#define CANTalk_Write_needRead 0x03
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
#define CANTalk_Period 0x00
#define CANTalk_Trigger 0x01
#if CAN_LOCK == 1
typedef void* CANMutex;
#endif
/*<2A><>¼<EFBFBD><C2BC>ǰ<EFBFBD>ķ<EFBFBD>ʽ*/
typedef struct
{
CFUint8 mode:3;
CFUint8 trigger:2;
CFUint8 other:3;
}CanTalkWay;
/*CANͨ<4E>żܹ<C5BC><DCB9><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>¼*/
typedef struct
{
/*<2A><><EFBFBD>ڼ<EFBFBD>¼ʱ<C2BC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
CFInt16 sendLastTime; /*<2A><>¼<EFBFBD><C2BC><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1>*/
CFInt16 sendPeriodTimeout; /*<2A><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD>ʱʱ<CAB1><CAB1>*/
CFInt16 sendTimeout; /*<2A><>¼<EFBFBD><C2BC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>*/
CFInt16 recLastTime; /*<2A><>¼<EFBFBD><C2BC>һ<EFBFBD>ν<EFBFBD><CEBD><EFBFBD>ʱ<EFBFBD><CAB1>*/
CFInt16 recTimeOut; /*<2A><>¼<EFBFBD><C2BC><EFBFBD>ܳ<EFBFBD>ʱ*/
bool isSend; /*<2A>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
bool isRUpdate; /*<2A>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
bool isWUpdate; /*<2A>Ƿ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
bool isSendSucess; /*<2A>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ͳɹ<CDB3>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>ģʽ*/
CanTalkStu stu; /*<2A><><EFBFBD>ڼ<EFBFBD>¼ͨ<C2BC><CDA8>״̬*/
CanTalkWay way; /*<2A><>¼<EFBFBD><C2BC>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>ӵķ<D3B5>ʽ*/
TFunc rfunc; /*<2A><><EFBFBD>ڽ<EFBFBD><DABD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>*/
TFunc sfunc; /*<2A><><EFBFBD>ڷ<EFBFBD><DAB7>ͳɹ<CDB3><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>*/
TFunc efunc; /*<2A><><EFBFBD>ڳ<EFBFBD><DAB3>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>*/
/*CANͨ<4E><CDA8><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD>λ*/
CFUint8 wdata[CAN_DATA_SIZE];
CFUint8 rdata[CAN_DATA_SIZE];
/*<2A>󶨵<EFBFBD><F3B6A8B5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>*/
CFUint8 bindlen;
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
CFUint8 sn;
/*<2A><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
CFUint8 rn;
#if CAN_LOCK == 1
/*<2A><><EFBFBD>ڶ<EFBFBD><DAB6>̵߳<DFB3><CCB5><EFBFBD>*/
CANMutex mutex;
#endif
}CanTalkInfo;
typedef CanTalkInfo* CanTalkHandle;
typedef struct
{
CanTalkHandle* fifo;
unsigned int std;
unsigned int addr;
}CanHandle;
extern CanHandle allCanHandle;
//extern CanTalkHandle allCanHandle[CAN_MAXSTD][CAN_MAXADDR];
/******************************************************<2A><><EFBFBD><EFBFBD>**************************************************************/
extern void FCanFrameInit(void);
/*<2A><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>CANͨ<4E><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
extern CanTalkHandle createCanHandle(CanHandle* handle,unsigned int std,unsigned int addr,unsigned int bindlen,unsigned char mode,unsigned char trigger);
#define FCreateCanHandle(a,b,c,d,e) createCanHandle(&allCanHandle,a,b,c,d,e)
/************************<2A>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>***************************/
/*<2A><><EFBFBD><EFBFBD>CAN<41>ϵķ<CFB5><C4B7>ͳɹ<CDB3><C9B9><EFBFBD><EFBFBD><EFBFBD>*/
extern void FBindCanWriteSucessFuc(CanTalkHandle handle,TFunc func);
/*<2A><><EFBFBD><EFBFBD>CAN<41>ϵĶ<CFB5>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĻص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>*/
extern void FBindCanReadyreadFuc(CanTalkHandle handle,TFunc func);
/*<2A><><EFBFBD><EFBFBD>CAN<41><4E><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>*/
extern void FBindCanErrorFuc(CanTalkHandle handle,TFunc func);
/************************************************<2A><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>********************************************************/
/*<2A><><EFBFBD><EFBFBD>CANͨ<4E><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵķ<CFB5><C4B7>ͳ<EFBFBD>ʱʱ<CAB1><CAB1>*/
extern void FSetCanSendTimeout(CanTalkHandle handle,int timeOut);
/*<2A><><EFBFBD><EFBFBD>CANͨ<4E><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7>ͳ<EFBFBD>ʱʱ<CAB1><CAB1>*/
extern void FSetCanSendPeriodTimeout(CanTalkHandle handle,int timeOut);
/*<2A><><EFBFBD><EFBFBD>CANͨ<4E><CDA8><EFBFBD>ϵĽ<CFB5><C4BD>ܳ<EFBFBD>ʱʱ<CAB1><CAB1>*/
extern void FSetCanRecTimeout(CanTalkHandle handle,int timeOut);
/**********************************************<2A><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>**********************************************************/
/*<2A><>ȡCAN<41>õ<EFBFBD><C3B5><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>*/
extern void FGetCanWData(CanTalkHandle handle,void* rData,const int len);
extern int getCanWDataS(CanHandle* handle,unsigned int std,unsigned int addr,void* rData,const int len);
#define FGetCanWDataS(a,b,c,d) getCanWDataS(&allCanHandle,a,b,c,d)
/*<2A><>ȡCAN<41>õ<EFBFBD><C3B5>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD>*/
extern void FGetCanRData(CanTalkHandle handle,void* rData,const int len);
extern int getCanRDataS(CanHandle* handle,unsigned int std,unsigned int addr,void* rData,const int len);
#define FGetCanRDataS(a,b,c,d) getCanRDataS(&allCanHandle,a,b,c,d)
/****************************************************<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*********************************************************/
/*<2A><><EFBFBD><EFBFBD>CAN<41>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
extern int FSetCanData(CanTalkHandle handle,void* data,const int len);
extern int setCanDataS(CanHandle* handle,unsigned int std,unsigned int addr,void* data,const int len);
#define FSetCanDataS(a,b,c,d) setCanDataS(&allCanHandle,a,b,c,d)
/*CAN<41><4E>ʱ<EFBFBD><CAB1><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>*/
extern void canTask(CanHandle* handle);
#define FCanTask() canTask(&allCanHandle)
/*<2A><>Ӳ<EFBFBD><D3B2><EFBFBD>еõ<D0B5><C3B5><EFBFBD><EFBFBD><EFBFBD>Դ*/
extern int pushCanFrame(CanHandle* handle,const int stdvalue, CanFormat rdata);
#define FPushCanFrame(a,b) pushCanFrame(&allCanHandle,a,b)
/*<2A><><EFBFBD><EFBFBD>can<61>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
extern int setCansn(CanHandle* handle,unsigned int std,unsigned int addr,unsigned char sn);
#define FSetCansn(a,b,c) setCansn(&allCanHandle,a,b,c)
/*<2A><>ȡcan<61>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
extern int getCansn(CanHandle* handle,unsigned int std,unsigned int addr);
#define FGetCansn(a,b) getCansn(&allCanHandle,a,b)
/*<2A><>ȡcan<61>Ľ<EFBFBD><C4BD>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
extern int getCanrn(CanHandle* handle,unsigned int std,unsigned int addr);
#define FGetCanrn(a,b) getCanrn(&allCanHandle,a,b)
/*
*<EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD>Ⱥ<EFBFBD>ţ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD>ٲöγ<EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD>Ⱥ<EFBFBD>ţ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD>ٲó<EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD>ٲö<EFBFBD>8λ<EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>4λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD>
*can<EFBFBD>ı<EFBFBD>־֡<EFBFBD><EFBFBD><EFBFBD>ٲö<EFBFBD><EFBFBD><EFBFBD>12λ
*/
extern void setClusterNumber(unsigned char number);
/************************************************<2A><><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA>ӿ<EFBFBD>*******************************************/
/********************************************<2A><>Ҫ<EFBFBD><D2AA>ֲ****************************************************/
/***********<2A><>Ҫ<EFBFBD><D2AA><EFBFBD>ݲ<EFBFBD>ͬƽ̨<C6BD><CCA8><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>*************/
/*дCAN<41><4E><EFBFBD><EFBFBD>*/
extern int FWriteCan(unsigned int std,void* wdata,const unsigned int len);
#if CAN_LOCK == 1
extern CANMutex cancreateMutex(void);
extern void canLock(CANMutex mutex);
extern void canUnlock(CANMutex mutex);
#else
#define cancreateMutex()
#define canLock(x)
#define canUnlock(x)
#endif
#define can_malloc malloc
#define can_free free
#endif