Browse Source

feature/2015
Christian Kroll 13 years ago
parent
commit
f2027360a0
  1. 4
      animations/borg_time.c
  2. 25
      can/borg_can.c
  3. 63
      can/can.c
  4. 41
      can/can.h
  5. 18
      can/lap.c
  6. 82
      can/lap.h
  7. 11
      can/spi.c
  8. 18
      can/spi.h

4
animations/borg_time.c

@ -6,7 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#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)

25
can/borg_can.c

@ -4,13 +4,19 @@
#include "spi.h"
#include "../borg_hw/borg_hw.h"
#include <avr/pgmspace.h>
#include <setjmp.h>
#ifdef __AVR__
# include <avr/pgmspace.h>
# include <avr/interrupt.h>
# include <avr/eeprom.h>
#else
# include "../compat/eeprom.h"
#endif
#include <string.h>
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();
@ -79,22 +87,23 @@ void process_borg_msg(pdo_message *msg)
case FKT_BORG_MODE:
longjmp(newmode_jmpbuf, msg->data[0]);
break;
case FKT_BORG_SCROLLTEXT_RESET:
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++;
while (scrolltext_text[j])
j++;
for (i = 0; i < msg->dlc - 1; i++) {
scrolltext_text[i + j] = msg->data[i];
}
scrolltext_text[i + j] = 0;
break;
#ifdef Hansi_hat_gelernt_Werte_vorher_zu_definieren
@ -129,8 +138,7 @@ 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) {
@ -144,6 +152,5 @@ void bcan_process_messages()
process_borg_msg(msg);
msg = (pdo_message*) can_get_nb();
};
}
}

63
can/can.c

@ -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

41
can/can.h

@ -5,7 +5,7 @@
* Simple CAN Library
*
* #define CAN_INTERRUPT 1 // set this to enable interrupt driven
* and buffering version
* // and buffering version
* #define CAN_RX_BUFFER_SIZE 2 // only used for Interrupt
* #define CAN_TX_BUFFER_SIZE 2 // only used for Interrupt
*/
@ -16,40 +16,38 @@
#include <stdint.h>
typedef unsigned char can_addr;
typedef unsigned char can_port;
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;
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;
typedef struct
{
typedef struct {
can_channel_t channel;
can_subchannel_t subchannel;
can_addr addr_src;
can_addr addr_dst;
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
@ -58,7 +56,6 @@ typedef enum { normal, mode_sleep, loopback, listenonly, config } can_mode_t ;
extern unsigned char can_error;
#endif
/*****************************************************************************
* Management
*/
@ -68,7 +65,6 @@ void can_setfilter();
void can_setmode( can_mode_t);
void can_setled(unsigned char led, unsigned char state);
/*****************************************************************************
* Sending
*/
@ -76,7 +72,6 @@ void can_setled(unsigned char led, unsigned char state);
can_message * can_buffer_get();
void can_transmit(can_message *msg);
/*****************************************************************************
* Receiving
*/
@ -92,7 +87,6 @@ can_message *can_get_nb();
void can_free(can_message * msg);
#endif
/*****************************************************************************
* Sending
*/
@ -100,7 +94,6 @@ can_message *can_get_nb();
can_message_raw * can_buffer_get_raw();
void can_transmit_raw(can_message_raw *msg);
/*****************************************************************************
* Receiving
*/
@ -116,16 +109,12 @@ can_message_raw *can_get_raw_nb();
void can_free_raw(can_message_raw * msg);
#endif
/*****************************************************************************
* Sending
*/
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

18
can/lap.c

@ -9,8 +9,7 @@
*/
// send ping to dst
void lap_ping( can_addr dst )
{
void lap_ping(can_addr_t dst) {
pdo_message *msg = (pdo_message *) can_buffer_get();
msg->addr_src = 0;
@ -24,8 +23,7 @@ void lap_ping( can_addr dst )
}
// send reset request to dst
void lap_reset( can_addr dst )
{
void lap_reset(can_addr_t dst) {
pdo_message *msg = (pdo_message *) can_buffer_get();
msg->addr_src = 0;
@ -38,17 +36,6 @@ void lap_reset( can_addr dst )
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
}
*/

82
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;
@ -28,10 +28,10 @@ typedef struct {
// "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;
unsigned char data[7];
@ -41,22 +41,55 @@ 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,
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;
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
@ -65,7 +98,6 @@ typedef enum { FKT_MOOD_INFO=0x00, FKT_MOOD_GET=0x01, FKT_MOOD_SET=0x02, FKT_MOO
#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
@ -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

11
can/spi.c

@ -1,6 +1,10 @@
#ifdef __AVR__
#include <avr/io.h>
#endif
#include "spi.h"
#ifdef __AVR__
void spi_init(){
//set output SPI pins to output
@ -53,3 +57,10 @@ unsigned char spi_data(unsigned char c) {
return (c);
}
#endif
#else
void spi_init(){}
unsigned char spi_data(unsigned char c){return 0;}
#endif

18
can/spi.h

@ -3,26 +3,8 @@
#include "../config.h"
/**
* this enables the use of Hardware SPI on ATMEGA
*
* #define SPI_HARDWARE 1
* #define SPI_PORT PORTB
* #define SPI_PIN PINB
* #define SPI_DDR DDRB
*
* #define SPI_PIN_MOSI PB5
* #define SPI_PIN_MISO PB6
* #define SPI_PIN_SCK PB7
* #define SPI_PIN_SS PB4
*
*/
void spi_init();
unsigned char spi_data(unsigned char c);
#endif

Loading…
Cancel
Save