From f2027360a0748556b83ae92c380d757679d2d735 Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Sun, 6 May 2012 18:45:43 +0000 Subject: [PATCH] --- animations/borg_time.c | 4 +- can/borg_can.c | 47 +-- can/can.c | 683 +++++++++++++++++++++-------------------- can/can.h | 79 ++--- can/lap.c | 34 +- can/lap.h | 126 +++++--- can/spi.c | 35 ++- can/spi.h | 18 -- 8 files changed, 525 insertions(+), 501 deletions(-) diff --git a/animations/borg_time.c b/animations/borg_time.c index 6b08b54..44cb144 100644 --- a/animations/borg_time.c +++ b/animations/borg_time.c @@ -6,7 +6,7 @@ #include #include -#include +#include "../compat/pgmspace.h" #include "../config.h" #include "../can/can.h" #include "../can/lap.h" @@ -21,7 +21,7 @@ #endif //hackhack -extern can_addr myaddr; +extern can_addr_t myaddr; //send a time request packet via can void time_request(void) diff --git a/can/borg_can.c b/can/borg_can.c index c547d03..b49247e 100644 --- a/can/borg_can.c +++ b/can/borg_can.c @@ -4,13 +4,19 @@ #include "spi.h" #include "../borg_hw/borg_hw.h" -#include #include -#include -#include + +#ifdef __AVR__ +# include +# include +# include +#else +# include "../compat/eeprom.h" +#endif + #include -can_addr myaddr; +can_addr_t myaddr; extern jmp_buf newmode_jmpbuf; #ifdef LAP_TIME_EXTENSION @@ -45,9 +51,11 @@ void process_mgt_msg(pdo_message *msg) switch(msg->cmd) { case FKT_MGT_RESET: +#ifdef __AVR__ timer0_off(); cli(); while(1); +#endif break; case FKT_MGT_PING: rmsg = (pdo_message *)can_buffer_get(); @@ -75,26 +83,27 @@ void process_borg_msg(pdo_message *msg) { unsigned char i, j; - switch(msg->cmd) { + switch (msg->cmd) { case FKT_BORG_MODE: longjmp(newmode_jmpbuf, msg->data[0]); break; + case FKT_BORG_SCROLLTEXT_RESET: - for(i=0; i < msg->dlc-1; i++) { + for (i = 0; i < msg->dlc - 1; i++) { scrolltext_text[i] = msg->data[i]; } scrolltext_text[i] = 0; - break; + case FKT_BORG_SCROLLTEXT_APPEND: - j=0; - while(scrolltext_text[j]) j++; + j = 0; + while (scrolltext_text[j]) + j++; - for(i=0; i < msg->dlc-1; i++) { - scrolltext_text[i+j] = msg->data[i]; + for (i = 0; i < msg->dlc - 1; i++) { + scrolltext_text[i + j] = msg->data[i]; } - scrolltext_text[i+j] = 0; - + scrolltext_text[i + j] = 0; break; #ifdef Hansi_hat_gelernt_Werte_vorher_zu_definieren @@ -129,21 +138,19 @@ void process_borg_msg(pdo_message *msg) } } -void bcan_process_messages() -{ +void bcan_process_messages() { pdo_message *msg = (pdo_message*) can_get_nb(); - while(msg) { + while (msg) { if (!msg) return; - if(msg->addr_dst == myaddr && msg->port_dst == PORT_MGT) + if (msg->addr_dst == myaddr && msg->port_dst == PORT_MGT) process_mgt_msg(msg); - if(msg->addr_dst == myaddr && msg->port_dst == PORT_BORG) + if (msg->addr_dst == myaddr && msg->port_dst == PORT_BORG) process_borg_msg(msg); msg = (pdo_message*) can_get_nb(); - }; - + } } diff --git a/can/can.c b/can/can.c index 3497a6d..0f6f539 100644 --- a/can/can.c +++ b/can/can.c @@ -1,7 +1,9 @@ #ifndef __C64__ -#include -#include +#ifdef __AVR__ + #include + #include +#endif #define asm asm volatile #endif @@ -121,376 +123,393 @@ void mcp_bitmod(unsigned char reg, unsigned char mask, unsigned char val); unsigned char mcp_status(); //unsigned char mcp_rx_status(); -// Functions -/* -unsigned char mcp_rx_status(){ - unsigned char d; - spi_set_ss(); - spi_data(RX_STATUS); - d = spi_data(0); - spi_clear_ss(); - return d; -} -*/ - -unsigned char mcp_status(){ - unsigned char d; - spi_set_ss(); - spi_data(READ_STATUS); - d = spi_data(0); - spi_clear_ss(); - return d; -} - -void mcp_bitmod(unsigned char reg, unsigned char mask, unsigned char val){ - spi_set_ss(); - spi_data(BIT_MODIFY); - spi_data(reg); - spi_data(mask); - spi_data(val); - spi_clear_ss(); -} - -//load a message to mcp2515 and start transmission -void message_load(can_message_x * msg){ - unsigned char x; - - spi_set_ss(); - 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<msg.port_dst & 0x03) ); - spi_data(msg->msg.addr_src); - spi_data(msg->msg.addr_dst); - spi_data(msg->msg.dlc); - for(x=0;xmsg.dlc;x++){ - spi_data(msg->msg.data[x]); +#ifdef __AVR__ + // Functions + /* + unsigned char mcp_rx_status() { + unsigned char d; + spi_set_ss(); + spi_data(RX_STATUS); + d = spi_data(0); + spi_clear_ss(); + return d; } - spi_clear_ss(); - spi_set_ss(); - spi_data(WRITE); - spi_data(TXB0CTRL); - spi_data( (1<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.addr_src = spi_data(0); - msg->msg.addr_dst = spi_data(0); - msg->msg.dlc = spi_data(0) & 0x0F; - for(x=0;xmsg.dlc;x++){ - msg->msg.data[x] = spi_data(0); + */ + + unsigned char mcp_status() { + unsigned char d; + spi_set_ss(); + spi_data(READ_STATUS); + d = spi_data(0); + spi_clear_ss(); + return d; } - spi_clear_ss(); - mcp_bitmod(CANINTF, (1<flags & 0x01) ) { - message_fetch(&RX_BUFFER[RX_HEAD]); - RX_BUFFER[RX_HEAD].flags |= 0x01;//mark buffer as used - if( ++RX_HEAD == CAN_RX_BUFFER_SIZE) RX_HEAD = 0; - }else{ - //buffer overflow - //just clear the Interrupt condition, and lose the message - mcp_bitmod(CANINTF, (1<msg.port_src << 2)) | + (msg->msg.port_dst >> 4)); + spi_data((unsigned char)((msg->msg.port_dst & 0x0C) << 3) | + (1<msg.port_dst & 0x03)); + spi_data(msg->msg.addr_src); + spi_data(msg->msg.addr_dst); + spi_data(msg->msg.dlc); + for(x=0;xmsg.dlc;x++) { + spi_data(msg->msg.data[x]); } - } else if ( status & 0x08 ) { // TX1 empty - if(((can_message_x*)&TX_BUFFER[TX_TAIL])->flags & 0x01) { - ((can_message_x*)&TX_BUFFER[TX_TAIL])->flags &= ~0x01; - TX_INT = 1; - message_load(&TX_BUFFER[TX_TAIL]); - if(++TX_TAIL == CAN_TX_BUFFER_SIZE) TX_TAIL = 0; - }else{ - TX_INT = 0; + spi_clear_ss(); + spi_set_ss(); + spi_data(WRITE); + spi_data(TXB0CTRL); + spi_data((1<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.addr_src = spi_data(0); + msg->msg.addr_dst = spi_data(0); + msg->msg.dlc = spi_data(0) & 0x0F; + for(x=0;xmsg.dlc;x++) { + msg->msg.data[x] = spi_data(0); } - mcp_bitmod(CANINTF, (1<flags & 0x01) ) { + message_fetch(&RX_BUFFER[RX_HEAD]); + RX_BUFFER[RX_HEAD].flags |= 0x01;//mark buffer as used + if( ++RX_HEAD == CAN_RX_BUFFER_SIZE) RX_HEAD = 0; + }else{ + //buffer overflow + //just clear the Interrupt condition, and lose the message + mcp_bitmod(CANINTF, (1<flags & 0x01) { + ((can_message_x*)&TX_BUFFER[TX_TAIL])->flags &= ~0x01; + TX_INT = 1; + message_load(&TX_BUFFER[TX_TAIL]); + if(++TX_TAIL == CAN_TX_BUFFER_SIZE) TX_TAIL = 0; + }else{ + TX_INT = 0; + } + mcp_bitmod(CANINTF, (1<msg); + #ifdef CAN_INTERRUPT + + // configure IRQ: this only configures the INT Output of the mcp2515, + // not the int on the Atmel + mcp_write( CANINTE, (1<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; -} + #ifdef CAN_INTERRUPT + //returns next can message in buffer, or 0 Pointer if buffer is empty + can_message * can_get_nb() { + can_message_x *p; + if(RX_HEAD == RX_TAIL) { + return 0; + } else { + p = &RX_BUFFER[RX_TAIL]; + if(++RX_TAIL == CAN_RX_BUFFER_SIZE) RX_TAIL = 0; + return &(p->msg); + } + } + can_message * can_get() { + can_message_x *p; -//returns pointer to the next can TX buffer -can_message * can_buffer_get(){ - can_message_x *p; - p = &TX_BUFFER[TX_HEAD]; - while (p->flags&0x01); //wait until buffer is free - if(++TX_HEAD == CAN_TX_BUFFER_SIZE) TX_HEAD = 0; - return &(p->msg); -} + while(RX_HEAD == RX_TAIL) {}; + p = &RX_BUFFER[RX_TAIL]; + if(++RX_TAIL == CAN_RX_BUFFER_SIZE) RX_TAIL = 0; -//start transmitting can messages, and mark message msg as transmittable -void can_transmit(can_message* msg2){ - can_message_x* msg=(can_message_x*) msg2; - if(msg){ - msg->flags |= 0x01; - } - if(!TX_INT){ - if(((can_message_x*)&TX_BUFFER[TX_TAIL])->flags & 0x01){ - ((can_message_x*)&TX_BUFFER[TX_TAIL])->flags &= ~0x01; - TX_INT = 1; - message_load(&TX_BUFFER[TX_TAIL]); - if(++TX_TAIL == CAN_TX_BUFFER_SIZE) TX_TAIL = 0; + return &(p->msg); } - } -} -#else // NON INTERRUPT VERSION + //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; + } -can_message_x RX_MESSAGE, TX_MESSAGE; + //returns pointer to the next can TX buffer + can_message * can_buffer_get() { + can_message_x *p; + p = &TX_BUFFER[TX_HEAD]; + while (p->flags&0x01); //wait until buffer is free + if(++TX_HEAD == CAN_TX_BUFFER_SIZE) TX_HEAD = 0; + return &(p->msg); + } -can_message * can_get_nb(){ - //check the pin, that the MCP's Interrup output connects to - if(SPI_REG_PIN_MCP_INT & (1<flags |= 0x01; + } + if(!TX_INT) { + if(((can_message_x*)&TX_BUFFER[TX_TAIL])->flags & 0x01) { + ((can_message_x*)&TX_BUFFER[TX_TAIL])->flags &= ~0x01; + TX_INT = 1; + message_load(&TX_BUFFER[TX_TAIL]); + if(++TX_TAIL == CAN_TX_BUFFER_SIZE) TX_TAIL = 0; + } + } + } + #else // NON INTERRUPT VERSION + can_message_x RX_MESSAGE, TX_MESSAGE; + + can_message * can_get_nb() { + //check the pin, that the MCP's interrupt output connects to + if(SPI_REG_PIN_MCP_INT & (1< -typedef unsigned char can_addr; -typedef unsigned char can_port; +#include + +typedef unsigned char can_addr_t; +typedef unsigned char can_port_t; typedef uint16_t can_channel_t; typedef uint8_t can_subchannel_t; -typedef struct -{ +typedef struct { uint32_t id; uint8_t dlc; uint8_t data[8]; } can_message_raw; -typedef struct{ - can_addr addr_src; - can_addr addr_dst; - can_port port_src; - can_port port_dst; +typedef struct { + can_addr_t addr_src; + can_addr_t addr_dst; + can_port_t port_src; + can_port_t port_dst; unsigned char dlc; unsigned char data[8]; -}can_message; +} can_message; -typedef struct -{ - can_channel_t channel; +typedef struct { + can_channel_t channel; can_subchannel_t subchannel; - can_addr addr_src; - can_addr addr_dst; - uint8_t dlc; - uint8_t data[8]; + can_addr_t addr_src; + can_addr_t addr_dst; + uint8_t dlc; + uint8_t data[8]; } can_message_v2; - -typedef enum { normal, mode_sleep, loopback, listenonly, config } can_mode_t ; - +typedef enum { + normal, mode_sleep, loopback, listenonly, config +} can_mode_t; /***************************************************************************** * Global variables */ #ifdef CAN_HANDLEERROR - extern unsigned char can_error; +extern unsigned char can_error; #endif - /***************************************************************************** * Management */ void can_init(); void can_setfilter(); -void can_setmode(can_mode_t); +void can_setmode( can_mode_t); void can_setled(unsigned char led, unsigned char state); - /***************************************************************************** * Sending */ can_message * can_buffer_get(); -void can_transmit( can_message *msg ); - +void can_transmit(can_message *msg); /***************************************************************************** * Receiving @@ -87,19 +82,17 @@ can_message *can_get_nb(); // this is only needed for Interrupt driven Version #ifndef CAN_INTERRUPT // # define can_free(m) - void can_free(can_message * msg); +void can_free(can_message * msg); #else - void can_free(can_message * msg); +void can_free(can_message * msg); #endif - /***************************************************************************** * Sending */ can_message_raw * can_buffer_get_raw(); -void can_transmit_raw( can_message_raw *msg ); - +void can_transmit_raw(can_message_raw *msg); /***************************************************************************** * Receiving @@ -111,20 +104,16 @@ can_message_raw *can_get_raw_nb(); // this is only needed for Interrupt driven Version #ifndef CAN_INTERRUPT // # define can_free(m) - void can_free_raw(can_message_raw * msg); +void can_free_raw(can_message_raw * msg); #else - void can_free_raw(can_message_raw * msg); +void can_free_raw(can_message_raw * msg); #endif - - - /***************************************************************************** * Sending */ -void can_transmit_v2( can_message_v2 *msg ); - +void can_transmit_v2(can_message_v2 *msg); /***************************************************************************** * Receiving @@ -134,6 +123,4 @@ can_message_v2 *can_get_v2_nb(); void can_free_v2(can_message_v2 *msg); - - #endif diff --git a/can/lap.c b/can/lap.c index 5bc9909..e02bde8 100644 --- a/can/lap.c +++ b/can/lap.c @@ -9,46 +9,33 @@ */ // send ping to dst -void lap_ping( can_addr dst ) -{ - pdo_message *msg = (pdo_message *)can_buffer_get(); +void lap_ping(can_addr_t dst) { + pdo_message *msg = (pdo_message *) can_buffer_get(); msg->addr_src = 0; msg->addr_dst = dst; msg->port_src = PORT_MGT; msg->port_dst = PORT_MGT; - msg->dlc = 1; - msg->cmd = FKT_MGT_PING; + msg->dlc = 1; + msg->cmd = FKT_MGT_PING; - can_transmit( (can_message *)msg ); + can_transmit((can_message *) msg); } // send reset request to dst -void lap_reset( can_addr dst ) -{ - pdo_message *msg = (pdo_message *)can_buffer_get(); +void lap_reset(can_addr_t dst) { + pdo_message *msg = (pdo_message *) can_buffer_get(); msg->addr_src = 0; msg->addr_dst = dst; msg->port_src = PORT_MGT; msg->port_dst = PORT_MGT; - msg->dlc = 1; - msg->cmd = FKT_MGT_RESET; + msg->dlc = 1; + msg->cmd = FKT_MGT_RESET; - can_transmit( (can_message *)msg ); + can_transmit((can_message *) msg); } - - - - - - - - - - - /* char *sdo_readbuf(lap_message *first_msg, unsigned char int length, unsigned char &reallength) @@ -118,6 +105,5 @@ unsigned char sdo_sendbuf_nb(lap_message *fst_msg, unsigned char *buf, unsigned can_transmit(); } // XXX wait for ACK - } */ diff --git a/can/lap.h b/can/lap.h index 285e624..d0297eb 100644 --- a/can/lap.h +++ b/can/lap.h @@ -15,10 +15,10 @@ // "inherits" from can_message typedef struct { - can_addr addr_src; - can_addr addr_dst; - can_port port_src; - can_port port_dst; + can_addr_t addr_src; + can_addr_t addr_dst; + can_port_t port_src; + can_port_t port_dst; unsigned char dlc; unsigned char cmd; uint16_t index; @@ -27,11 +27,11 @@ typedef struct { } sdo_message; // "inherits" from can_message -typedef struct{ - can_addr addr_src; - can_addr addr_dst; - can_port port_src; - can_port port_dst; +typedef struct { + can_addr_t addr_src; + can_addr_t addr_dst; + can_port_t port_src; + can_port_t port_dst; unsigned char dlc; unsigned char cmd; unsigned char data[7]; @@ -41,42 +41,74 @@ typedef struct{ * Known ports and services */ -typedef enum { PORT_MGT=0x30, PORT_LAMPE=0x20, PORT_SDO=0x15, PORT_SDO_DATA=0x16, PORT_LAPD=0x18, - PORT_BORG=0x23, PORT_MOOD=0x17, PORT_REMOTE=0x21, PORT_GATE=0x22, PORT_CHUCK=0x26 } ports; - -typedef enum { FKT_MGT_PING=0x00, FKT_MGT_PONG=0x01, - FKT_MGT_RESET=0x02, FKT_MGT_AWAKE=0x03, FKT_MGT_TIMEREQUEST=0x04, FKT_MGT_TIMEREPLY=0x05 } lap_mgt_fkts; - -typedef enum { FKT_LAMPE_SET=0x00, FKT_LAMPE_SETMASK=0x01, - FKT_LAMPE_SETDELAY=0x02, FKT_LAMPE_ADD=0x03 } lap_lampe_fkts; - -typedef enum { FKT_BORG_INFO=0x00, FKT_BORG_MODE=0x01, FKT_BORG_SCROLLTEXT_RESET=0x02, - FKT_BORG_SCROLLTEXT_APPEND=0x03 } lap_borg_fkts; - -typedef enum { FKT_ONOFF_INFO=0, FKT_ONOFF_SET=1, FKT_ONOFF_GET=2, - } lap_lapd_fkts; - -typedef enum { FKT_MOOD_INFO=0x00, FKT_MOOD_GET=0x01, FKT_MOOD_SET=0x02, FKT_MOOD_ONOFF=0x03} lap_mood_fkts; -#define SDO_CMD_READ 0x20 -#define SDO_CMD_REPLY 0x21 -#define SDO_CMD_INFO 0x22 -#define SDO_CMD_READ_BLK 0x40 -#define SDO_CMD_READ_BLK_ACK 0x41 -#define SDO_CMD_WRITE_BLK 0x48 -#define SDO_CMD_WRITE_BLK_ACK 0x49 - - -#define SDO_CMD_ERROR_INDEX 0x80 - -#define SDO_TYPE_UINT8_RO 0x00 -#define SDO_TYPE_UINT8_RW 0x01 -#define SDO_TYPE_UINT16_RO 0x04 -#define SDO_TYPE_UINT16_RW 0x05 -#define SDO_TYPE_UINT32_RO 0x08 -#define SDO_TYPE_UINT32_RW 0x09 -#define SDO_TYPE_STRING_RO 0x80 -#define SDO_TYPE_STRING_RW 0x81 -#define SDO_TYPE_STRING_WO 0x82 +typedef enum { + PORT_MGT = 0x30, + PORT_LAMPE = 0x20, + PORT_SDO = 0x15, + PORT_SDO_DATA = 0x16, + PORT_LAPD = 0x18, + PORT_BORG = 0x23, + PORT_MOOD = 0x17, + PORT_REMOTE = 0x21, + PORT_GATE = 0x22, + PORT_CHUCK = 0x26 +} ports; + +typedef enum { + FKT_MGT_PING = 0x00, + FKT_MGT_PONG = 0x01, + FKT_MGT_RESET = 0x02, + FKT_MGT_AWAKE = 0x03, + FKT_MGT_TIMEREQUEST = 0x04, + FKT_MGT_TIMEREPLY = 0x05 +} lap_mgt_fkts; + +typedef enum { + FKT_LAMPE_SET = 0x00, + FKT_LAMPE_SETMASK = 0x01, + FKT_LAMPE_SETDELAY = 0x02, + FKT_LAMPE_ADD = 0x03 +} lap_lampe_fkts; + +typedef enum { + FKT_BORG_INFO = 0x00, + FKT_BORG_MODE = 0x01, + FKT_BORG_SCROLLTEXT_RESET = 0x02, + FKT_BORG_SCROLLTEXT_APPEND = 0x03 +} lap_borg_fkts; + +typedef enum { + FKT_ONOFF_INFO = 0, + FKT_ONOFF_SET = 1, + FKT_ONOFF_GET = 2 +} lap_lapd_fkts; + +typedef enum { + FKT_MOOD_INFO = 0x00, + FKT_MOOD_GET = 0x01, + FKT_MOOD_SET = 0x02, + FKT_MOOD_ONOFF = 0x03 +} lap_mood_fkts; + +#define SDO_CMD_READ 0x20 +#define SDO_CMD_REPLY 0x21 +#define SDO_CMD_INFO 0x22 +#define SDO_CMD_READ_BLK 0x40 +#define SDO_CMD_READ_BLK_ACK 0x41 +#define SDO_CMD_WRITE_BLK 0x48 +#define SDO_CMD_WRITE_BLK_ACK 0x49 + +#define SDO_CMD_ERROR_INDEX 0x80 + +#define SDO_TYPE_UINT8_RO 0x00 +#define SDO_TYPE_UINT8_RW 0x01 +#define SDO_TYPE_UINT16_RO 0x04 +#define SDO_TYPE_UINT16_RW 0x05 +#define SDO_TYPE_UINT32_RO 0x08 +#define SDO_TYPE_UINT32_RW 0x09 +#define SDO_TYPE_STRING_RO 0x80 +#define SDO_TYPE_STRING_RW 0x81 +#define SDO_TYPE_STRING_WO 0x82 /**************************************************************************** @@ -84,10 +116,10 @@ typedef enum { FKT_MOOD_INFO=0x00, FKT_MOOD_GET=0x01, FKT_MOOD_SET=0x02, FKT_MOO */ // send ping to dst -void lap_ping( can_addr dst ); +void lap_ping(can_addr_t dst); // send reset request to dst -void lap_reset( can_addr dst ); +void lap_reset(can_addr_t dst); #ifdef LAP_TIME_EXTENSION //variables to save the last received hours and minutes diff --git a/can/spi.c b/can/spi.c index 30b7ea0..8d3c6dd 100644 --- a/can/spi.c +++ b/can/spi.c @@ -1,6 +1,10 @@ +#ifdef __AVR__ #include +#endif + #include "spi.h" +#ifdef __AVR__ void spi_init(){ //set output SPI pins to output @@ -27,21 +31,21 @@ void spi_init(){ } #ifndef SPI_HARDWARE -unsigned char spi_data(unsigned char c){ +unsigned char spi_data(unsigned char c) { unsigned char x, d=d; - for(x=0;x<8;x++){ - if(c & 0x80){ - SPI_PORT |= (1<