fix CAN support, add menuconfig options

This commit is contained in:
kju 2010-01-22 00:17:29 +00:00
parent 68dee06285
commit 9b44776ec8
8 changed files with 349 additions and 22 deletions

View File

@ -2,7 +2,7 @@
#include "lap.h"
#include "borg_can.h"
#include "spi.h"
#include "blinken.h"
#include "../borg_hw/borg_hw.h"
#include <avr/pgmspace.h>
#include <setjmp.h>
@ -28,7 +28,7 @@ void bcan_init()
msg->cmd = FKT_BORG_INFO;
msg->data[0] = NUM_ROWS;
msg->data[1] = NUM_COLS;
msg->data[2] = NUM_PLANES;
msg->data[2] = NUMPLANE;
can_transmit((can_message *)msg);
}

View File

@ -355,11 +355,11 @@ void can_init(){
// 0x03 : 125kbit/16MHz
// 0x04 : 125kbit/20MHz
#if F_MCP == 16000000
#if FREQ == 16000000
#define CNF1_T 0x03
#elif F_MCP == 8000000
#elif FREQ == 8000000
#define CNF1_T 0x01
#elif F_MCP == 20000000
#elif FREQ == 20000000
#define CNF1_T 0x04
#else
#error Can Baudrate is only defined for 8, 16 and 20 MHz

74
can/config.in Normal file
View File

@ -0,0 +1,74 @@
dep_bool_menu "CAN bus support" CAN_SUPPORT y
choice 'SPI Port' \
"PORTA 0 \
PORTB 1 \
PORTC 2 \
PORTD 3" \
'PORTB' SPI_PORTIDX
choice 'Bit MOSI' \
"Bit0 0 \
Bit1 1 \
Bit2 2 \
Bit3 3 \
Bit4 4 \
Bit5 5 \
Bit6 6 \
Bit7 7" \
'Bit5' SPI_PIN_MOSI
choice 'Bit MISO' \
"Bit0 0 \
Bit1 1 \
Bit2 2 \
Bit3 3 \
Bit4 4 \
Bit5 5 \
Bit6 6 \
Bit7 7" \
'Bit6' SPI_PIN_MISO
choice 'Bit SCK' \
"Bit0 0 \
Bit1 1 \
Bit2 2 \
Bit3 3 \
Bit4 4 \
Bit5 5 \
Bit6 6 \
Bit7 7" \
'Bit7' SPI_PIN_SCK
choice 'Bit SS' \
"Bit0 0 \
Bit1 1 \
Bit2 2 \
Bit3 3 \
Bit4 4 \
Bit5 5 \
Bit6 6 \
Bit7 7" \
'Bit4' SPI_PIN_SS
bool "Use interrupt" CAN_INTERRUPT
choice 'Interrupt Port' \
"PINA PINA \
PINB PINB \
PINC PINC \
PIND PIND" \
'PIND' SPI_REG_PIN_MCP_INT
choice 'Interrupt Bit' \
"Bit0 0 \
Bit1 1 \
Bit2 2 \
Bit3 3 \
Bit4 4 \
Bit5 5 \
Bit6 6 \
Bit7 7" \
'Bit2' SPI_PIN_MCP_INT
endmenu

123
can/lap.c Normal file
View File

@ -0,0 +1,123 @@
#include <stdio.h>
#include <string.h>
#include "can.h"
#include "lap.h"
/****************************************************************************
* STUBS: LAP Core Services
*/
// send ping to dst
void lap_ping( can_addr 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;
can_transmit( (can_message *)msg );
}
// send reset request to dst
void lap_reset( can_addr 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;
can_transmit( (can_message *)msg );
}
/*
char *sdo_readbuf(lap_message *first_msg,
unsigned char int length, unsigned char &reallength)
{
unsigned char len = first_message->data[0];
char *buf = malloc(length);
char *cur = buf;
reallength = 0;
while( reallength < length ) {
can_message *msg = can_get();
if ( msg->addr_src != first_msg->addr_src ||
msg->addr_dest != fisrt_msg->addr_dest ||
msg->port_src != fisrt_msg->port_src ||
msg->port_dest != fisrt_msg->port_dest )
{
return buf;
}
memcpy(cur, msg->data, msg->dlc);
reallength+=msg->dlc;
msg->flags = 0x00;
}
return buf;
}
// unsigned char sdo_sendpacket(lap_message *msg)
// {
// }
//
// unsigned char sdo_sendpacket_nb(lap_message *msg)
// {
// }
unsigned char sdo_sendbuf(lap_message *fst_msg, unsigned char *buf, unsigned char len)
{
while(len > 0) {
can_message *msg = can_buffer_get();
msg->addr_src = fst_msg->addr_src;
msg->addr_dest = fst_msg->addr_dest;
msg->port_src = fst_msg->port_src;
msg->port_dest = fst_msg->port_dest;
msg->dlc = len > 8 ? 8 : len;
memcpy(msg->data, buf, msg->dlc);
msg->flags = 0x01;
can_transmit();
}
// XXX wait for ACK
}
unsigned char sdo_sendbuf_nb(lap_message *fst_msg, unsigned char *buf, unsigned char len)
{
while(len > 0) {
can_message *msg = can_buffer_get();
msg->addr_src = fst_msg->addr_src;
msg->addr_dest = fst_msg->addr_dest;
msg->port_src = fst_msg->port_src;
msg->port_dest = fst_msg->port_dest;
msg->dlc = len > 8 ? 8 : len;
memcpy(msg->data, buf, msg->dlc);
msg->flags = 0x01;
can_transmit();
}
// XXX wait for ACK
}
*/

120
can/lap.h Normal file
View File

@ -0,0 +1,120 @@
#ifndef LAP_H
#define LAP_H
/****************************************************************************
* Labor Automation Protocol
*
*/
#include <inttypes.h>
/****************************************************************************
* Types
*/
// "inherits" from can_message
typedef struct {
can_addr addr_src;
can_addr addr_dst;
can_port port_src;
can_port port_dst;
unsigned char dlc;
unsigned char cmd;
uint16_t index;
uint16_t size;
uint16_t address;
} sdo_message;
// "inherits" from can_message
typedef struct{
can_addr addr_src;
can_addr addr_dst;
can_port port_src;
can_port port_dst;
unsigned char dlc;
unsigned char cmd;
unsigned char data[7];
} pdo_message;
/****************************************************************************
* 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
/****************************************************************************
* STUBS: LAP Core Services
*/
// send ping to dst
void lap_ping( can_addr dst );
// send reset request to dst
void lap_reset( can_addr dst );
/**
* ServiceDataObject routinen
*
unsigned char *sdo_readbuf(sdo_message *first_message,
unsigned char length, unsigned char *actuallength);
unsigned char sdo_sendbuf(sdo_message *fst_msg, unsigned char *buf, unsigned char len);
unsigned char sdo_sendbuf_nb(sdo_message *fst_msg, unsigned char *buf, unsigned char len);
*/
/////////////////////////////////////////////////////////////////////////////
/* Usage
while(1) {
lap_message msg = lap_rcvpacket();
switch( msg->fkt_id ) {
case FKT_BLA:
unsigned char length = data[0]
data
char *buf = lap_read(msg, length);
if ( !buf ) continue;
// interpret buffer
}
*/
#endif

View File

@ -1,7 +1,7 @@
#ifndef SPI_H
#define SPI_H
#include "config.h"
#include "../config.h"
/**
* this enables the use of Hardware SPI on ATMEGA

View File

@ -12,30 +12,34 @@
#define SNAKE_DELAY 100
/*
#define BORG_CAN
#ifdef CAN_SUPPORT
#define BORG_CAN
// spi.[ch] defines
#define SPI_HARDWARE
#define SPI_PORT PORTB //for slave select
#define SPI_PIN PINB //for slave select
#if SPI_PORTIDX == 0
#define SPI_PORT PORTA
#define SPI_DDR DDRA
#define SPI_PIN PINA
#elif SPI_PORTIDX == 1
#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_PIN_MOSI PB5
#define SPI_PIN_MISO PB6
#define SPI_PIN_SCK PB7
#define SPI_PIN_SS PB4 // for slave select
//interrupt pin of MCP2515 for non interrupt driven can
#define SPI_REG_PIN_MCP_INT PIND
#define SPI_PIN_MCP_INT PD2
#define SPI_PIN PINB
#elif SPI_PORTIDX == 2
#define SPI_PORT PORTC
#define SPI_DDR DDRC
#define SPI_PIN PINC
#elif SPI_PORTIDX == 3
#define SPI_PORT PORTD
#define SPI_DDR DDRD
#define SPI_PIN PIND
#endif
// can.[ch] defines
#undef CAN_INTERRUPT //set this to enable interrupt driven and buffering version
#define CAN_RX_BUFFER_SIZE 2 //only used for Interrupt
#define CAN_TX_BUFFER_SIZE 2 //only used for Interrupt
#define F_MCP F_CPU
*/
#endif
#define INIT_EEPROM

View File

@ -45,8 +45,14 @@ source scrolltext/config.in
source joystick/config.in
###############################################################################
############################ CAN Menu #########################################
source can/config.in
###############################################################################
dep_bool "menu support" MENU_SUPPORT $JOYSTICK_SUPPORT
############################ Game Menu ########################################
source games/config.in
###############################################################################