#include "sja1000.h"
unsigned int T_flag0=0;
unsigned int T_flag1=0;
//unsigned char data[11] = {'\0'};
//unsigned char Rx_Buf0[11] = {'\0'};
unsigned char Tx_Buf0[9] = {'\0'};
unsigned char Tx_Buf1[9] = {'\0'};
//unsigned char Rx_Buf1[11] = {'\0'};
/*writ to the register with tht data
chanal--通道号,可选CAN总线1,CAN总线2
reg-----寄存器号
data----寄存器数据*/
void setcanport(unsigned char chanal, unsigned int reg, unsigned char data1)
{
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
printf("\nThe %d will be writed to register %d\n ",data1,reg);
__asm
{
STRB reg,[BASEADD,#4]
STRB data1,[BASEADD]
}
}
/*read the data of the register to the return
chanal--通道号,可选CAN总线1,CAN总线2
reg-----寄存器号
返回值--寄存器数据*/
unsigned char getcanport(unsigned char chanal, unsigned char reg)
{
unsigned char temp;
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
__asm
{
STRB reg,[BASEADD,#4]
LDRB temp,[BASEADD]
}
return (temp);
}
/*Receive one message
chanal--通道号,可选CAN总线1,CAN总线2
返回值--数据个数*/
unsigned char getdata(unsigned char chanal, unsigned char *buf)
{
int num,i;
unsigned char data[11] = {'\0'};
if(!chanal)
BASEADD = 50000000;
else
BASEADD = 60000000;
do
{
}while((getcanport(chanal,StatusReg) & RBS_Bit) != RBS_Bit);
data[0] = getcanport(chanal,RxBuffer1);
data[1] = getcanport(chanal,RxBuffer2);
data[2] = getcanport(chanal,RxBuffer3);
data[3] = getcanport(chanal,RxBuffer4);
data[4] = getcanport(chanal,RxBuffer5);
data[5] = getcanport(chanal,RxBuffer6);
data[6] = getcanport(chanal,RxBuffer7);
data[7] = getcanport(chanal,RxBuffer8);
data[8] = getcanport(chanal,RxBuffer9);
data[9] = getcanport(chanal,RxBuffer10);
num = data[1]&0x0f;
for(i=0;i
*(buf++)=data[i+2];
}
setcanport(chanal,CommandReg,RRB_Bit);
*buf=0;
return (num);
}
/*Transmit one message
num-----数据个数
data----数据
返回值--0 忙*/
unsigned char senddata(unsigned char chanal, unsigned char *buf)
{
unsigned char temp = 0;
unsigned char data[11] = {'\0'};
int i;
if(!chanal)
BASEADD = 0x50000000;
else
BASEADD = 0x60000000;
//temp = getcanport(chanal, StatusReg);
if((getcanport(chanal,StatusReg) & TBS_Bit) == TBS_Bit)
{
for(i=0;i<8;i++)
{
data[i]=*(buf+i);
}
temp = getcanport(chanal, StatusReg);
setcanport(chanal,TxBuffer1,0x00);
setcanport(chanal,TxBuffer2,0x08);
setcanport(chanal,TxBuffer3,data[0]);
setcanport(chanal,TxBuffer4,data[1]);
setcanport(chanal,TxBuffer5,data[2]);
setcanport(chanal,TxBuffer6,data[3]);
setcanport(chanal,TxBuffer7,data[4]);
setcanport(chanal,TxBuffer8,data[5]);
setcanport(chanal,TxBuffer9,data[6]);
setcanport(chanal,TxBuffer10,data[7]);
setcanport(chanal,CommandReg,0x01);
do
{
temp = getcanport(chanal, StatusReg);
if ((temp & TCS_Bit)==TCS_Bit)
break;
} while(1); //If it is transmitted succeded?
return (1);
}
else if(!chanal)
{
if(!T_flag0)
{
for(i=0;i<8;i++)
{
Tx_Buf0[i]=*(buf+i);
}
T_flag0=1;//传输标志,1表示一个报文等待发送
}
else return (0);
}
else
{
if(!T_flag1)
{
for(i=0;i<8;i++)
{
Tx_Buf1[i]=*(buf+i);
}
T_flag1=1;//传输标志,1表示一个报文等待发送
}
else return (0);
}
}
/*Initial the SJA1000 It just is BasicCAN */
void init_sja1000(unsigned char chanal)
{
unsigned char temp = 0;
//?SJA中断禁止
//外部中断禁止
if(!chanal)//禁止can0中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
else//禁止can1中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
//进入复位请求设置模式
while((temp&RM_RR_Bit)!=RM_RR_Bit)
{
printf("\n0x01 will be set to ModeControlReg\n");
setcanport(chanal, ModeControlReg,0x07);/*Config MOD&CR register,初始化INT*/
temp = getcanport(chanal, ModeControlReg);
printf("\nModeControlReg=%d\n",temp);
}
//设置接收码
setcanport(chanal, AcceptCodeReg, 0x01);
temp = getcanport(chanal, AcceptCodeReg);
//设置屏蔽码
setcanport(chanal, AcceptMaskReg, 0xff);
temp = getcanport(chanal, AcceptMaskReg);
//设置总线bps
setcanport(chanal, BusTiming0Reg, 0x47);
temp = getcanport(chanal, BusTiming0Reg);//400kbps
setcanport(chanal, BusTiming1Reg, 0xaf);
temp = getcanport(chanal, BusTiming1Reg);
//设置CAN输出模式
setcanport(chanal, OutControlReg, 0xda);
temp = getcanport(chanal,OutControlReg);
//设置时钟分频寄存器
setcanport(chanal, ClockDivideReg, 0x48);
temp = getcanport(chanal, ClockDivideReg);
/*清除复位模式位*/
do
{
setcanport(chanal, ModeControlReg, 0X06);/*Config MOD&CR register*/
temp = getcanport(chanal, ModeControlReg);
}while(((getcanport(chanal, ModeControlReg)) & RM_RR_Bit)!=ClrByte);
//外部中断使能
if(!chanal)//禁止can0中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
else//禁止can1中断
{
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
}
//?SJA中断使能
}
int main()
{
unsigned char *buf;
int i;
unsigned char d[5][9]={"bbbbbbbb;","12345678","90123456","78787878","abcdefgh"};
unsigned long t;
// init irq1 an irq2 for sja1000 u2 u3
//nIRQ AIC_IECR&AIC_IDCR ENABLE INIT
__asm
{
str 0x00000000,[0xfffff120]
}
__asm
{
str 0x00000000,[0xfffff124]
}
//nIRQ AIC_SMR,&AIC_SVR config int
__asm
{
ldr t,[0xfffff004]
}
__asm
{
orr t,t,#0x03//priority level3,Level Sensitive(interal) Low-level Sensitive(external)
str t,[0xfffff004]
}
__asm
{
ldr t,[0xfffff008]
}
__asm
{
orr t,t,#0x03//priority level3,Level Sensitive(interal) Low-level Sensitive(external)
str t,[0xfffff008]
}
//配置AIC_SVR
__asm
{
str t,[0xfffff084]
}
__asm
{
str t,[0xfffff088]
}
init_sja1000(0);
//
for(i=0;i<5;i++)
{
buf=&d[i][0];
senddata(0,buf);
};
while(1){
getdata(0,buf);
};
}
说明:
系统是测试CAN0接口和CAN卡的通信程序
波特率设置为:
BAR0=0x47=71;
BAR1=0xaf=175;
接口为:
9200的CAN口:3脚为CANL,4脚为CANH
对应CAN卡: 2脚为CANL,4脚为CANH
上一篇:STM32L151内部RTC时钟
下一篇:基于AT91RM9200的I/O驱动程序(linux)
推荐阅读最新更新时间:2024-03-16 15:06