📄 can-mcp2510.c
字号:
#endif
return(0);
}
//set priority.
b_TXBaCTRL=mcp2510_read(TXBaCTRL);
b_TXBaCTRL.txpri=priority;
mcp2510_write(TXBaCTRL, (int)b_TXBaCTRL);
//set tx mask
can_set_id(TXRXBaEIDL, id, ext);
//set tx data count
b_TXBaDLC=len;
b_TXBaDLC.rtr=rtr;
mcp2510_write(TXBaDLC, (int)b_TXBaDLC);
//write to buffer
for (i=TXRXBaD0; i<(TXRXBaD0 + len); i++) {
mcp2510_write(i,*data);
data++;
}
//enable transmission
b_TXBaCTRL=mcp2510_read(TXBaCTRL);
b_TXBaCTRL.txreq=1;
mcp2510_write(TXBaCTRL, (int)b_TXBaCTRL);
#if CAN_DO_DEBUG
can_debug("\r\nCAN_PUTD(): BUFF=%U ID=%LX LEN=%U PRI=%U EXT=%U RTR=%U\r\n", port, id, len, priority, ext, rtr);
if ((len)&&(!rtr)) {
data-=len;
can_debug(" DATA = ");
for (i=0;i<len;i++) {
can_debug("%X ",*data);
data++;
}
can_debug("\r\n");
}
#endif
return(1);
}
////////////////////////////////////////////////////////////////////////
//
// can_getd()
//
// Gets data from a receive buffer, if the data exists
//
// Returns:
// id - ID who sent message
// data - pointer to array of data
// len - length of received data
// stat - structure holding some information (such as which buffer
// recieved it, ext or standard, etc)
//
// Returns:
// Function call returns a TRUE if there was data in a RX buffer, FALSE
// if there was none.
//
////////////////////////////////////////////////////////////////////////
int1 can_getd(int32 & id, int * data, int & len, struct rx_stat & stat)
{
int i;
struct struct_RXB0CTRL b_RXB0CTRL;
struct struct_RXB1CTRL b_RXB1CTRL;
struct struct_EFLG b_EFLG;
int RXBaDLC;
struct rxbNdlc_struct b_RXBaDLC;
int TXRXBaSIDL;
struct struct_TXRXBaSIDL b_TXRXBaSIDL;
int RXBaD0;
struct struct_CANINTF b_CANINTF;
b_CANINTF=mcp2510_read(CANINTF);
b_RXB0CTRL=mcp2510_read(RXB0CTRL);
b_RXB1CTRL=mcp2510_read(RXB1CTRL);
b_EFLG=mcp2510_read(EFLG);
if (b_CANINTF.rx0if) {
stat.buffer=0;
stat.err_ovfl=b_EFLG.rx0ovr;
b_EFLG.rx0ovr=0;
mcp2510_write(EFLG, (int)b_EFLG);
if (b_RXB0CTRL.bukt) {
stat.filthit=b_RXB0CTRL.filhit0;
}
RXBaDLC=RXB0DLC;
TXRXBaSIDL=RXB0SIDL;
RXBaD0=RXB0D0;
}
else if (b_CANINTF.rx1if)
{
stat.buffer=1;
stat.err_ovfl=b_EFLG.rx1ovr;
b_EFLG.rx1ovr=0;
mcp2510_write(EFLG, (int)b_EFLG);
stat.filthit=b_RXB1CTRL.filhit0;
RXBaDLC=RXB1DLC;
TXRXBaSIDL=RXB1SIDL;
RXBaD0=RXB1D0;
}
else {
#if CAN_DO_DEBUG
can_debug("\r\nFAIL ON CAN_GETD(): NO MESSAGE IN BUFFER\r\n");
#endif
return (0);
}
//get count
b_RXBaDLC=mcp2510_read(RXBaDLC);
len = b_RXBaDLC.dlc;
stat.rtr=b_RXBaDLC.rtr;
//was it extended or standard?
b_TXRXBaSIDL=mcp2510_read(TXRXBaSIDL);
stat.ext=b_TXRXBaSIDL.ext;
id=can_get_id(TXRXBaSIDL + 2,stat.ext);
//get data
for ( i = RXBaD0; i < (RXBaD0 + len); i++ ) {
*data=mcp2510_read(i);
data++;
}
stat.inv=b_CANINTF.merrf;
if (b_CANINTF.merrf) {
b_CANINTF.merrf=0;
}
if (stat.buffer) {
b_CANINTF.rx1if=0;
}
else {
b_CANINTF.rx0if=0;
}
mcp2510_write(CANINTF, (int)b_CANINTF);
#if CAN_DO_DEBUG
can_debug("\r\nCAN_GETD(): BUFF=%U ID=%LX LEN=%U OVF=%U ", stat.buffer, id, len, stat.err_ovfl);
can_debug("FILT=%U RTR=%U EXT=%U INV=%U", stat.filthit, stat.rtr, stat.ext, stat.inv);
if ((len)&&(!stat.rtr)) {
data-=len;
can_debug("\r\n DATA = ");
for (i=0;i<len;i++) {
can_debug("%X ",*data);
data++;
}
}
can_debug("\r\n");
#endif
return(1);
}
////////////////////////////////////////////////////////////////////////
//
// can_kbhit()
//
// Returns TRUE if there is data in the receive buffers
//
//////////////////////////////////////////////////////////////////////////////
int1 can_kbhit(void) {
struct struct_CANINTF b_CANINTF;
b_CANINTF=mcp2510_read(CANINTF);
if (b_CANINTF.rx0if || b_CANINTF.rx1if)
{return(1);}
return(0);
}
////////////////////////////////////////////////////////////////////////
//
// can_tbe()
//
// Returns TRUE if the transmit buffers are empty and ready to transmit data
//
//////////////////////////////////////////////////////////////////////////////
int1 can_tbe(void) {
struct txbNctrl_struct b_TXB0CTRL, b_TXB1CTRL, b_TXB2CTRL;
b_TXB0CTRL=mcp2510_read(TXB0CTRL);
b_TXB1CTRL=mcp2510_read(TXB1CTRL);
b_TXB2CTRL=mcp2510_read(TXB2CTRL);
if (!b_TXB0CTRL.txreq || !b_TXB1CTRL.txreq || !b_TXB2CTRL.txreq)
{return(1);}
return(0);
}
////////////////////////////////////////////////////////////////////////
//
// can_abort()
//
// Aborts all pending tranmissions.
//
//////////////////////////////////////////////////////////////////////////////
void can_abort(void) {
struct struct_CANCTRL b_CANCTRL;
b_CANCTRL=mcp2510_read(CANCTRL);
b_CANCTRL.abat=1;
mcp2510_write(CANCTRL, (int)b_CANCTRL);
delay_ms(5);
b_CANCTRL.abat=0;
mcp2510_write(CANCTRL, (int)b_CANCTRL);
}
///////////////////
///
//
// SPI CODE
//
///
//////////////////
//data clocked in on rising edge
//data driven out on falling edge
int mcp2510_read(int address) {
int command[2];
int i;
int data;
command[1]=0x03;
command[0]=address;
output_low(EXT_CAN_CS);
for (i=0;i<16;i++) {
output_bit(EXT_CAN_SI, shift_left(&command[0],2,0));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
for (i=0;i<8;i++) {
shift_left(&data,1,input(EXT_CAN_SO));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
output_high(EXT_CAN_CS);
return(data);
}
int mcp2510_status(void) {
int command;
int data;
int i;
command=0xA0;
output_low(EXT_CAN_CS);
for (i=0;i<8;i++) {
output_bit(EXT_CAN_SI, shift_left(&command,1,0));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
for (i=0;i<8;i++) {
shift_left(&data,1,input(EXT_CAN_SO));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
for (i=0;i<8;i++) {
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
output_high(EXT_CAN_CS);
return(data);
}
void mcp2510_write(int address, int data) {
int command[3];
int i;
command[2]=0x02;
command[1]=address;
command[0]=data;
output_low(EXT_CAN_CS);
for (i=0;i<24;i++) {
output_bit(EXT_CAN_SI, shift_left(&command[0],3,0));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
output_high(EXT_CAN_CS);
}
void mcp2510_command(int command) {
int i;
output_low(EXT_CAN_CS);
for (i=0;i<8;i++) {
output_bit(EXT_CAN_SI, shift_left(&command[0],1,0));
output_high(EXT_CAN_SCK);
output_low(EXT_CAN_SCK);
}
output_high(EXT_CAN_CS);
}
void mcp2510_init(void) {
output_high(EXT_CAN_CS);
output_low(EXT_CAN_SCK);
#ifdef EXT_CAN_TX0RTS
output_high(EXT_CAN_TX0RTS);
#endif
#ifdef EXT_CAN_TX1RTS
output_high(EXT_CAN_TX1RTS);
#endif
#ifdef EXT_CAN_TX2RTS
output_high(EXT_CAN_TX2RTS);
#endif
#ifdef EXT_CAN_TX0RTS
output_high(EXT_CAN_RESET);
output_low(EXT_CAN_RESET);
output_high(EXT_CAN_RESET);
delay_ms(5);
#endif
mcp2510_command(0xC0); //reset
delay_ms(5);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -