?? 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 + -