|
|
@ -1,7 +1,9 @@ |
|
|
|
|
|
|
|
#ifndef __C64__ |
|
|
|
#ifdef __AVR__ |
|
|
|
#include <avr/io.h> |
|
|
|
#include <avr/interrupt.h> |
|
|
|
#endif |
|
|
|
#define asm asm volatile |
|
|
|
#endif |
|
|
|
|
|
|
@ -121,6 +123,7 @@ void mcp_bitmod(unsigned char reg, unsigned char mask, unsigned char val); |
|
|
|
unsigned char mcp_status(); |
|
|
|
//unsigned char mcp_rx_status();
|
|
|
|
|
|
|
|
#ifdef __AVR__ |
|
|
|
// Functions
|
|
|
|
/*
|
|
|
|
unsigned char mcp_rx_status() { |
|
|
@ -159,8 +162,10 @@ void message_load(can_message_x * msg){ |
|
|
|
spi_data(WRITE); |
|
|
|
spi_data(TXB0SIDH); |
|
|
|
|
|
|
|
spi_data( ((unsigned char)(msg->msg.port_src << 2)) | (msg->msg.port_dst >> 4 ) ); |
|
|
|
spi_data( (unsigned char)((msg->msg.port_dst & 0x0C) << 3) | (1<<EXIDE) | (msg->msg.port_dst & 0x03) ); |
|
|
|
spi_data(((unsigned char)(msg->msg.port_src << 2)) | |
|
|
|
(msg->msg.port_dst >> 4)); |
|
|
|
spi_data((unsigned char)((msg->msg.port_dst & 0x0C) << 3) | |
|
|
|
(1<<EXIDE) | (msg->msg.port_dst & 0x03)); |
|
|
|
spi_data(msg->msg.addr_src); |
|
|
|
spi_data(msg->msg.addr_dst); |
|
|
|
spi_data(msg->msg.dlc); |
|
|
@ -187,7 +192,8 @@ void message_fetch(can_message_x * msg){ |
|
|
|
msg->msg.port_src = tmp1 >> 2; |
|
|
|
tmp2 = spi_data(0); |
|
|
|
tmp3 = (unsigned char)((unsigned char)(tmp2 >> 3) & 0x0C); |
|
|
|
msg->msg.port_dst = ((unsigned char)(tmp1 <<4 ) & 0x30) | tmp3 | (unsigned char)(tmp2 & 0x03); |
|
|
|
msg->msg.port_dst = ((unsigned char)(tmp1 <<4 ) & 0x30) | tmp3 | |
|
|
|
(unsigned char)(tmp2 & 0x03); |
|
|
|
msg->msg.addr_src = spi_data(0); |
|
|
|
msg->msg.addr_dst = spi_data(0); |
|
|
|
msg->msg.dlc = spi_data(0) & 0x0F; |
|
|
@ -198,9 +204,10 @@ void message_fetch(can_message_x * msg){ |
|
|
|
|
|
|
|
mcp_bitmod(CANINTF, (1<<RX0IF), 0x00); |
|
|
|
} |
|
|
|
#ifdef CAN_INTERRUPT |
|
|
|
|
|
|
|
static can_message_x RX_BUFFER[CAN_RX_BUFFER_SIZE], TX_BUFFER[CAN_TX_BUFFER_SIZE]; |
|
|
|
#ifdef CAN_INTERRUPT |
|
|
|
static can_message_x RX_BUFFER[CAN_RX_BUFFER_SIZE], |
|
|
|
TX_BUFFER[CAN_TX_BUFFER_SIZE]; |
|
|
|
unsigned char RX_HEAD=0;volatile unsigned char RX_TAIL=0; |
|
|
|
unsigned char TX_HEAD= 0;volatile unsigned char TX_TAIL=0; |
|
|
|
static volatile unsigned char TX_INT; |
|
|
@ -240,7 +247,6 @@ ISR(INT0_vect) { |
|
|
|
#endif // CAN_HANDLEERROR
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
@ -303,7 +309,6 @@ void can_setmode( can_mode_t mode ) { |
|
|
|
mcp_write( CANCTRL, val ); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void can_setfilter() { |
|
|
|
//RXM1 RXM0
|
|
|
|
// 0 0 receive matching filter
|
|
|
@ -364,12 +369,13 @@ void can_init(){ |
|
|
|
#else |
|
|
|
#error Can Baudrate is only defined for 8, 16 and 20 MHz |
|
|
|
#endif |
|
|
|
|
|
|
|
mcp_write( CNF1, 0x40 | CNF1_T ); |
|
|
|
mcp_write( CNF2, 0xf1 ); |
|
|
|
mcp_write( CNF3, 0x05 ); |
|
|
|
|
|
|
|
// configure IRQ
|
|
|
|
// this only configures the INT Output of the mcp2515, not the int on the Atmel
|
|
|
|
// configure IRQ: this only configures the INT Output of the mcp2515, not
|
|
|
|
// the int on the Atmel
|
|
|
|
mcp_write( CANINTE, (1<<RX0IE) | (1<<TX0IE) ); |
|
|
|
|
|
|
|
can_setfilter(); |
|
|
@ -377,11 +383,10 @@ void can_init(){ |
|
|
|
|
|
|
|
#ifdef CAN_INTERRUPT |
|
|
|
|
|
|
|
// configure IRQ
|
|
|
|
// this only configures the INT Output of the mcp2515, not the int on the Atmel
|
|
|
|
// configure IRQ: this only configures the INT Output of the mcp2515,
|
|
|
|
// not the int on the Atmel
|
|
|
|
mcp_write( CANINTE, (1<<RX0IE) | (1<<TX0IE) ); |
|
|
|
|
|
|
|
|
|
|
|
#ifdef __C64__ |
|
|
|
#error not implemented yet |
|
|
|
#elif ATMEGA |
|
|
@ -392,10 +397,9 @@ void can_init(){ |
|
|
|
MCUCR |= (1<<ISC01); |
|
|
|
GIMSK |= (1<<INT0); |
|
|
|
#endif //ATMEGA
|
|
|
|
|
|
|
|
#else //CAN_INTERRUPT
|
|
|
|
// configure IRQ
|
|
|
|
// this only configures the INT Output of the mcp2515, not the int on the Atmel
|
|
|
|
// configure IRQ: this only configures the INT Output of the mcp2515,
|
|
|
|
// not the int on the Atmel
|
|
|
|
mcp_write( CANINTE, (1<<RX0IE) ); //only turn RX int on
|
|
|
|
#endif //CAN_INTERRUPT
|
|
|
|
} |
|
|
@ -424,14 +428,12 @@ can_message * can_get(){ |
|
|
|
return &(p->msg); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//marks a receive buffer as unused again so it can be overwritten in Interrupt
|
|
|
|
void can_free(can_message * msg) { |
|
|
|
can_message_x * msg_x = (can_message_x *) msg; |
|
|
|
msg_x->flags = 0; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//returns pointer to the next can TX buffer
|
|
|
|
can_message * can_buffer_get() { |
|
|
|
can_message_x *p; |
|
|
@ -441,7 +443,6 @@ can_message * can_buffer_get(){ |
|
|
|
return &(p->msg); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//start transmitting can messages, and mark message msg as transmittable
|
|
|
|
void can_transmit(can_message* msg2) { |
|
|
|
can_message_x* msg=(can_message_x*) msg2; |
|
|
@ -457,13 +458,11 @@ void can_transmit(can_message* msg2){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#else // NON INTERRUPT VERSION
|
|
|
|
|
|
|
|
can_message_x RX_MESSAGE, TX_MESSAGE; |
|
|
|
|
|
|
|
can_message * can_get_nb() { |
|
|
|
//check the pin, that the MCP's Interrup output connects to
|
|
|
|
//check the pin, that the MCP's interrupt output connects to
|
|
|
|
if(SPI_REG_PIN_MCP_INT & (1<<SPI_PIN_MCP_INT)) { |
|
|
|
return 0; |
|
|
|
} else { |
|
|
@ -492,5 +491,25 @@ void can_transmit(can_message * msg){ |
|
|
|
|
|
|
|
void can_free(can_message * msg) { |
|
|
|
} |
|
|
|
|
|
|
|
#endif /* CAN_INTERRUPT */ |
|
|
|
#else /* ifdef __AVR__ */ |
|
|
|
/* stubs for simulator */ |
|
|
|
static can_message null_msg = {0}; |
|
|
|
unsigned char mcp_status() {return 0;} |
|
|
|
void mcp_bitmod(unsigned char reg, unsigned char mask, unsigned char val){} |
|
|
|
void message_load(can_message_x * msg){} |
|
|
|
void message_fetch(can_message_x * msg){} |
|
|
|
void mcp_reset(){} |
|
|
|
void mcp_write(unsigned char reg, unsigned char data){} |
|
|
|
unsigned char mcp_read(unsigned char reg){return 0;} |
|
|
|
void can_setmode(can_mode_t mode) {} |
|
|
|
void can_setfilter() {} |
|
|
|
void can_setled(unsigned char led, unsigned char state){} |
|
|
|
void delayloop(){} |
|
|
|
void can_init(){} |
|
|
|
can_message * can_get_nb(){return &null_msg;} |
|
|
|
can_message * can_get(){return &null_msg;} |
|
|
|
can_message * can_buffer_get(){return &null_msg;} |
|
|
|
void can_transmit(can_message * msg){} |
|
|
|
void can_free(can_message * msg){} |
|
|
|
#endif |
|
|
|