📄 can.c
字号:
}
}
}
for(i=0;i<2;i++)
{
if(NULL==can[i])
continue;
outportb(can[i]->can_base,CanIntrReg);
CanIntrStus[i]=inportb(can[i]->can_base+1); //读中断状态寄存器
}
}
//printf("Int Req id %x\n",inport(0xff2c));
outport(IntEoiReg,CANCanEoi);
}
void can_puts(unsigned char str[],int len,CAN_TYPE *pcan)
{
static unsigned int overtimer=0;
int i;
unsigned int putstimer;
int mask;
if(NULL==pcan)
return;
if (len>BUFFSIZE)
len=BUFFSIZE;
pcan->out.read_index=0;
pcan->out.write_index=0;
for(i=0;i<len;i++)
{
pcan->out.buffer[i] = str[i];
}
pcan->out.write_index=8;
mask=inport(IntMaskReg);
mask|=~Int1Enable; //关闭总线中断IRQ5(CAN板CAN口)
outport(IntMaskReg,mask);
outportb(pcan->can_base,CanStusReg);
putstimer=timer;
while((inportb(pcan->can_base+1) & TBS_Bit) != TBS_Bit)//;//发送缓冲区空?
{
if(GetPass(putstimer,timer)>=10)
{
if(GetPass(overtimer,timer)>=500)
{
if(pcan==can[0])
i=1;
else
i=2;
can_reset(i);
overtimer=timer;
}
mask=inport(IntMaskReg);
mask&=Int1Enable; //开放总线中断IRQ5(CAN板CAN口)
outport(IntMaskReg,mask);
return;
}
outportb(pcan->can_base,CanStusReg);
}
outportb(pcan->can_base,16);
outportb(pcan->can_base+1,0x08); //帧信息,标准格式,数据帧,本帧数据长度8字节
outportb(pcan->can_base,17);
outportb(pcan->can_base+1,pcan->out.pri[0]); //标识符1,发向0x68号站点(优先级为0x68的站点)
outportb(pcan->can_base,18);
outportb(pcan->can_base+1,(pcan->out.zhenhao[0]<<5)&0xe0); //标识符2,高3位为帧号
for(i=0;i<8;i++)
{
outportb(pcan->can_base,19+i); //发送缓冲区填数
outportb(pcan->can_base+1,pcan->out.buffer[i]);
}
//if(pcan->out.zhenhao[0]==0 && pcan->out.pri[0]==0x63 && (pcan->out.buffer[1]!=1 || pcan->out.buffer[2]!=12))
//{
// //port_puts(pcan->out.buffer,16,com2);
// printf("error!%x,%x,%x,%x,%x,%x,%x,%x",pcan->out.buffer[0],pcan->out.buffer[1],pcan->out.buffer[2],
// pcan->out.buffer[3],pcan->out.buffer[4],pcan->out.buffer[5],pcan->out.buffer[6],pcan->out.buffer[7]);
//}
outportb(pcan->can_base,CanStusReg);//,0x02,看是否正在发送状态
putstimer=timer;
while((inportb(pcan->can_base+1) & TS_Bit) != 0)//;//&0x20 !=0x00
{
if(GetPass(putstimer,timer)>=10)
{
mask=inport(IntMaskReg);
mask&=Int1Enable; //开放总线中断IRQ5(CAN板CAN口)
outport(IntMaskReg,mask);
return;
}
outportb(pcan->can_base,CanStusReg);
}
outportb(pcan->can_base,CanCmdReg);//,0x01
outportb(pcan->can_base+1,TR_Bit);//,0x01,请求发送
outportb(pcan->can_base,CanIntrEnbReg);
outportb(pcan->can_base+1,RIE_Bit); //打开接收与发送中断
pcan->out.read_index=0;
pcan->out.write_index=0;
//for(i=0;i<8;i++)
//{
// outportb(pcan->can_base,19+i); //du发送缓冲区
// if(((unsigned char)inportb(pcan->can_base+1))!=pcan->out.buffer[i])
// printf("error");
//}
mask=inport(IntMaskReg);
mask&=Int1Enable; //开放总线中断IRQ5(CAN板CAN口)
outport(IntMaskReg,mask);
DebugData(5,1,len,str);
}
int can_setframehead(unsigned char *pzhh,unsigned char *ppri,int len,CAN_TYPE *pcan)
{
int i,j;
if(NULL==pcan)
return(0);
j=pcan->out.write_index/8;
i=10-j;
//if(len>i)
// len=i;
j=0;
for(i=0;i<len;i++)
{
pcan->out.zhenhao[j]=pzhh[i];
pcan->out.pri[j]=ppri[i];
j++;
}
return(len);
}
int can_gets(unsigned char str[],int len,CAN_TYPE *pcan)
{
int i;
if(NULL==pcan)
return(0);
i=can_getlen(pcan);
if(len>i)
len=i;
str[0]=0;
if(pcan==NULL || len==0)
return(0);
if(pcan->in.read_index== pcan->in.write_index)
return(0);
for(i=0;i<len;i++)
{
if(pcan->in.read_index== pcan->in.write_index)
break;
str[i]=pcan->in.buffer[pcan->in.read_index++];
if(pcan->in.read_index>=BUFFSIZE)
pcan->in.read_index=0;
}
return(i);
}
void can_clear(CAN_TYPE *pcan)
{
if(NULL==pcan)
return;
pcan->in.read_index=0;
pcan->in.write_index=0;
}
int can_getlen(CAN_TYPE *pcan)
{
int len;
if(NULL==pcan)
return(0);
if(pcan->in.write_index>=pcan->in.read_index)
len=pcan->in.write_index-pcan->in.read_index;
else
len=BUFFSIZE-pcan->in.read_index+pcan->in.write_index;
return(len);
}
int can_reset(int canid)//CAN口初始化
{
CAN_TYPE *pcan;
int mask;
unsigned int reset_timer=0;
unsigned char temp;
if(canid<1 || canid>3)
return(0);
switch(canid)
{
case 0x01:
pcan=can[0];
pcan->can_base=Can1PortBase;
break;
case 0x02:
pcan=can[1];
pcan->can_base=Can2PortBase;
break;
//case 0x03:
// pcan=can[2];
// pcan->can_base=Can3PortBase;
// break;
default: return(0);
}
pcan->in.write_index = pcan->in.read_index = 0;
pcan->out.write_index = pcan->out.read_index = 0;
outportb(pcan->can_base,CanModeCtrlReg);//0x00,模式寄存器
reset_timer=timer;
while((inportb(pcan->can_base+1) & RM_Bit) == 0)//&0x01 ==0x00
{
if(GetPass(reset_timer,timer)>10)
{
break;//return(0);
}
outportb(pcan->can_base+1,inportb(pcan->can_base+1) | RM_Bit);//|0x01进入复位模式
}
outportb(pcan->can_base,CanClkDivReg);//,0x1f
outportb(pcan->can_base+1,CANMode_Bit|ClkOff_Bit | CBP_Bit);//CLKOUT off,CBP=1,PeliCAN模式
outportb(pcan->can_base,CanIntrEnbReg);//0x04
outportb(pcan->can_base+1,0x00); //中断禁能
outportb(pcan->can_base,CanAcceptCode0);//0x10,
outportb(pcan->can_base+1,0x64);
outportb(pcan->can_base,CanAcceptCode1);//0x11
outportb(pcan->can_base+1,0xa0);
outportb(pcan->can_base,CanAcceptCode2);//0x12
outportb(pcan->can_base+1,0x20);
outportb(pcan->can_base,CanAcceptCode3);//0x13
outportb(pcan->can_base+1,0xa0);
outportb(pcan->can_base,CanAcceptMask0);//0x14
outportb(pcan->can_base+1,0xff);
outportb(pcan->can_base,CanAcceptMask1);//0x15
outportb(pcan->can_base+1,0xff);
outportb(pcan->can_base,CanAcceptMask2);//0x16
outportb(pcan->can_base+1,0xff);
outportb(pcan->can_base,CanAcceptMask3);//0x17
outportb(pcan->can_base+1,0xff);
outportb(pcan->can_base,CanBusTime0Reg);//,0x06
outportb(pcan->can_base+1,SJW_KB_16 |Presc_KB_16);//,0xc0|0x00
outportb(pcan->can_base,CanBusTime1Reg);//,0x07
outportb(pcan->can_base+1,TSEG2_KB_16 | TSEG1_KB_16);//,0x30,0x0a,以上设定波特率等
outportb(pcan->can_base,CanOutCtrlReg);//,0x08
outportb(pcan->can_base+1,Tx1Float | Tx0PshPull | NormalMode);//,0x00|0x18|0x02
outportb(pcan->can_base,CanModeCtrlReg);//,0x00
reset_timer=timer;
do{
if(GetPass(reset_timer,timer)>10)
{
break;//return(0);
}
outportb(pcan->can_base+1,0);//AFM_Bit);//单滤波,关闭自测试、仅听模式,清除休眠,进入操作模式
}while((inportb(pcan->can_base+1) & RM_Bit) != 0);//&0x01 !=0x00
outportb(pcan->can_base,CanIntrEnbReg);
outportb(pcan->can_base+1,RIE_Bit); //打开接收中断
disable();
mask=inport(IntMaskReg);
mask&=Int1Enable; //开放总线中断IRQ5(CAN板CAN口)
outport(IntMaskReg,mask);
outport(Int1CtrlReg,Int1PriInit); //设置中断优先级
setvect(CANCanIntr,Can_int);
enable();
return(1);
}
CAN_TYPE* init_can(int canid)
{
CAN_TYPE *pcan;
if ((pcan = malloc( sizeof( CAN_TYPE ))) == NULL)
return( NULL );
switch(canid)
{
case 1:
can[0]=pcan;
break;
case 2:
can[1]=pcan;
break;
//case 3:
// can[2]=pcan;
// break;
default:
return(NULL);
}
if(can_reset(canid))
return(pcan);
else
{
free(pcan);
return(NULL);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -