//!/////////////////////////////////////////////////////////////////////////
/// can_io.c                                                              //
///                                                                       //
/// Version:     0.2                                                      //
/// Date:        11/01/2009                                               //
/// Author:      Michael Skutek (michael@skutek.de)                       //
///                                                                       //
/// Description: CAN I/O routines for SJA1000                             //
///                                                                       //
/// Platform:    AVR ATmega (tested on ATmega16)                          //
///                                                                       //
/// Include Files:                                                        //
///              avr/io.h:             port accessing (part of avr-gcc)   //
///              util/delay.h:         time delays (part of avr-gcc)      //
///              port_definitions.h:   definition of input/output ports   //
///              can_io.h.h:           defintions (e.g. return values)    //
///                                                                       //
///                                                                       //
/// This program is free software: you can redistribute it and/or modify  //
/// it under the terms of the GNU Lesser General Public License as        //
/// published by the Free Software Foundation, either version 3 of the    //
/// License, or (at your option) any later version.                       //
///                                                                       //
/// This program is distributed in the hope that it will be useful,       //
/// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          //
/// GNU Lesser General Public License for more details.                   //
///                                                                       //
/// You should have received a copy of the GNU Lesser General Public      //
/// License along with this program. If not, see                          //
/// <http://www.gnu.org/licenses/>.                                       //
//!/////////////////////////////////////////////////////////////////////////





#ifndef CAN_IO_H_INCLUDED
#define CAN_IO_H_INCLUDED


///definitions for signal timing

#define CAN_TIMING_POWERON_MS           30        //wait time after power on [ms]
#define CAN_TIMING_RESET_MS              1        //reset pulse width [ms]
#define CAN_TIMING_AL_US              0.05        //ALE/AS pulse width [us]
#define CAN_TIMING_ALH_US             0.05        //Address Hold after ALE Low [us]
#define CAN_TIMING_WR_US              0.05        //WR pulse width [us]
#define CAN_TIMING_RD_US              0.05        //RD pulse width [us]
#define CAN_TIMING_WRH_US             0.05        //Data Hold after WR High [us]
#define CAN_TIMING_RDH_US             0.05        //Data Hold after RD High [us]


///definitions for register addresses and register values

//addresses
#define CAN_MOD_ADDR                  0x00        //Address of Mode Register (Mode) - only for PELICAN Mode
#define CAN_CR_ADDR                   0x00        //Address of Control Register (CR) - only for Basic CAN Mode
#define CAN_CMR_ADDR                  0x01        //Address of Command Register (CMR) - only for Basic CAN Mode
#define CAN_STAT_ADDR                 0x02        //Address of Status Register (STAT) - READ ONLY
#define CAN_CDR_ADDR                  0x1f        //Address of Clock Divider Register (CDR)
#define CAN_ACRBAS_ADDR               0x04        //Address of Acceptance Code Register (ACR) - Basic CAN Mode
#define CAN_ACRPEL_ADDR               0x10        //Start Adress of Acceptance Code Register (ACR) - PELICAN Mode
#define CAN_AMRBAS_ADDR               0x05        //Address of Acceptance Mask Register (ACR) - Basic CAN Mode
#define CAN_AMRPEL_ADDR               0x14        //Start Adress of Acceptance Mask Register (ACR) - PELICAN Mode
#define CAN_BTR0_ADDR                 0x06        //Address of Bus Timing Register 0 (BTR0)
#define CAN_BTR1_ADDR                 0x07        //Address of Bus Timing Register 1 (BTR1)
#define CAN_OCR_ADDR                  0x08        //Address of Output Control Register (OCR)
#define CAN_IR_ADDR                   0x03        //Address of Interrupt Register (IR) - READ ONLY
#define CAN_IERPELI_ADDR              0x04        //Address of Interrupt Enable Register (IER) - PELICAN Mode

#define CAN_TXBAS_ADDR                0x0a        //Start Address of TX Buffer - BASIC CAN Mode
#define CAN_TXPELI_ADDR               0x10        //Start Address of TX Buffer - PELICAN Mode

#define CAN_RXBAS_ADDR                0x14        //Start Address of RX Buffer - BASIC CAN Mode
#define CAN_RXPELI_ADDR               0x10        //Start Address of RX Buffer - PELICAN Mode


//register values
#define CAN_CDR_PELICAN_VAL           0xe8        //Value for CDR (PELICAN Mode)
#define CAN_CDR_BASICCAN_VAL          0x68        //Value for CDR (BASIC CAN Mode)
#define CAN_OCR_VAL                   0x1a        //Value for OCR (recommended if using a PCA82C250, see Appnote AN97076)
#define CAN_CR_RESET_VAL              0x01        //Value for CR (Reset Mode)
#define CAN_CR_BASICCAN_RUN_VAL       0x12        //Value for CR (BASIC CAN Mode, RCV Overrun Interrupt and Receive Interrupt enabled)
#define CAN_MOD_RUN_VAL               0x00        //Value for MOD (normal Mode - end of Reset Mode)
#define CAN_IER_VAL                   0x09        //Value for IER (Interrupt at "Received Message" and at "Data Overrun")
#define CAN_RCVINTERRUPT_VAL          0x01        //Value for IR for receive interrupt

//values for BTR0 and BTR1 (from http://www.esacademy.com/faq/calc/sja1000.htm)
#define CAN_BTR0_100K_VAL             0x44        //Value for BTR0 (100 kBit/s, 16 MHz)
#define CAN_BTR1_100K_VAL             0x1c        //Value for BTR1 (100 kBit/s, 16 MHz)
#define CAN_BTR0_250K_VAL             0x41        //Value for BTR0 (250 kBit/s, 16 MHz)
#define CAN_BTR1_250K_VAL             0x1c        //Value for BTR1 (250 kBit/s, 16 MHz)
#define CAN_BTR0_500K_VAL             0x40        //Value for BTR0 (500 kBit/s, 16 MHz)
#define CAN_BTR1_500K_VAL             0x1c        //Value for BTR1 (500 kBit/s, 16 MHz)
#define CAN_BTR0_1M_VAL               0x40        //Value for BTR0 (1 MBit/s, 16 MHz)
#define CAN_BTR1_1M_VAL               0x14        //Value for BTR1 (1 MBit/s, 16 MHz)



///definitions for return values and modes
#define CAN_INIT_OK                      0        //return value of init function (no error occured)
#define CAN_INIT_ERR                     1        //return value of init function (error occured)

#define CAN_MODE_BASIC                   0        //Basic CAN Mode
#define CAN_MODE_PELICAN                 1        //PELICAN Mode

#define CAN_MODESET_TIMEOUT             10        //number of unsuccessful cycles of modesetting until init returns an error


///definitions for baudrates
#define CAN_100_KBIT                     0        //Baudrate Mode: 100 kBit
#define CAN_250_KBIT                     1        //Baudrate Mode: 250 kBit
#define CAN_500_KBIT                     2        //Baudrate Mode: 500 kBit
#define CAN_1_MBIT                       3        //Baudrate Mode: 1 MBit


///definitions for message sending and receiving
#define CAN_TRANS_OK                     0        //successful CAN transmission
#define CAN_TRANS_ERR                    1        //error occured during CAN transmission

#define CAN_TRSTATUS_RELEASED_VALUE   0x04        //transmit buffer released (bi 2 is set)

#define CAN_CMR_TRREQ_VALUE           0x01        //transmission request (CMR)

#define CAN_SFF_MESSAGE                0x0        //Standard Frame Format
#define CAN_EFF_MESSAGE                0x1        //Extended Frame Format
#define CAN_DATA_TRANS                 0x0        //Data
#define CAN_RTR_TRANS                  0x1        //Remote Transmission Request

#define CAN_TRANSMISSION_TIMEOUT        10        //number of unsuccessful cycles of transmission retries
                                                  //until function returns an error


#define CAN_RCV_OK                       0        //successful CAN message received
#define CAN_RCV_ERR                      1        //error occured or no CAN message in buffer
#define CAN_RCV_OVERRUN_WARN             2        //rcv buffer overrun occured

#define CAN_RCVBUFFER_FILLED_VALUE    0x01        //receive buffer contains data (bit 1 is set)
#define CAN_RELEASERCVFIFO_VALUE      0x04        //CMR command to release the RX FIFO message buffer
#define CAN_STAT_RCVOVERRUN_VALUE     0x02        //Receive Buffer Overrun occured
#define CAN_CMR_CLEAROVERRUN_VALUE    0x08        //Clear RCV Overrun Bit in CMR


///structure definitions

//Structure for CAN Message in SFF and EFF (Standard Frame, Extended Frame)
typedef struct can_message_struct_type {

  uint32_t id;                        //identifier (SFF: Byte0, Byte1; EFF: Byte0, Byte1, Byte2, Byte3)
  uint8_t dlc;                        //Data Length Code (number of bytes in message, max. 8)
  uint8_t data0;                      //data byte 0
  uint8_t data1;                      //data byte 1
  uint8_t data2;                      //data byte 2
  uint8_t data3;                      //data byte 3
  uint8_t data4;                      //data byte 4
  uint8_t data5;                      //data byte 5
  uint8_t data6;                      //data byte 6
  uint8_t data7;                      //data byte 7

} can_message_struct;


///function definitions
void can_io_send_data(unsigned char, unsigned char);
unsigned char can_io_read_data(unsigned char);
unsigned char can_io_send_message(can_message_struct *, uint8_t, uint8_t, uint8_t);
uint8_t can_io_receive_message(volatile can_message_struct *, uint8_t);
unsigned char can_io_init(uint8_t, uint8_t, uint32_t, uint32_t);
void can_io_pin_init(void);


#endif // CAN_IO_H_INCLUDED
