#ifndef __j1939def
#define __j1939def

#include "MYCAN.DEF"

    #define J1939_STARTING_ADDRESS  MY_ADDRESS
#if !defined J1939_CA_NAME
    #define J1939_CA_NAME           HOME_NAME_PFX
#endif
    #define J1939_CA_MASK_EXIT      J1939_TRUE

#define J1939_ARBITRARY_ADDRESS 0x00

#define J1939_ACCEPT_CMDADD J1939_FALSE
#if !defined (J1939_RX_QUEUE_SIZE)
#define J1939_RX_QUEUE_SIZE 8
#endif
#if !defined (J1939_TX_QUEUE_SIZE)
#define J1939_TX_QUEUE_SIZE 8
#endif

#if !defined (J1939_OVERWRITE_TX_QUEUE)
#define J1939_OVERWRITE_TX_QUEUE J1939_TRUE
#endif
#if !defined (J1939_OVERWRITE_RX_QUEUE)
#define J1939_OVERWRITE_RX_QUEUE J1939_TRUE
#endif

#if !defined (J1939_POLL_ECAN)
#define J1939_POLL_ECAN       J1939_FALSE   
#endif

#define J1939_PRIORITIZED_INT J1939_TRUE
#define ECAN_RX_INTERRUPT_PRIORITY 0x00
#define ECAN_TX_INTERRUPT_PRIORITY 0x00
#define ECAN_INTERRUPT_PRIORITY (ECAN_RX_INTERRUPT_PRIORITY | ECAN_TX_INTERRUPT_PRIORITY)
#define ECAN_LEGACY_MODE J1939_FALSE
#if !defined (ECAN_EXTRA_RX_BUFFERS)
#define ECAN_EXTRA_RX_BUFFERS 6
#endif
#define ECAN_SJW 4        // synchronization jump width time (quanta) 1-4

#if defined(__18F25K80) || defined(__18F26K80) || defined(__18F46K80) // 64mhz
   #define ECAN_BRP 32   // baud rate prescaler  1-64 [64=minimum speed]
#endif
#if defined(__18F2682) || defined(__18F4682)                          // 32mhz
   #define ECAN_BRP 32   // baud rate prescaler  1-64
#endif

#define ECAN_BRGCON1 (((ECAN_SJW-1)<<6) | (ECAN_BRP-1))
//      ECAN_BRGCON1    00111111
//                        111111-> PRESCALER         64
//                        011111-> PRESCALER         32
//                      00-------> SYNCH JUMP TIME 1 TQ
//                      11-------> SYNCH JUMP TIME 4 TQ
#define ECAN_SEG2PHTS 0x80   // phase 2 selectable
#define ECAN_SAM 0x00        // sample one time
#define ECAN_SEG1PH 8        // phase 1 duratione (quanta) 1-8
#define ECAN_PRSEG 8         // propagation delay (quanta) 1-8
#define ECAN_BRGCON2 (ECAN_SEG2PHTS | ECAN_SAM | ((ECAN_SEG1PH-1)<<3) | (ECAN_PRSEG-1))
//      ECAN_BRGCON2    10111111
//                           111-> PROPAGATION TIME 2 8TQ
//                        111----> PHASE 1 8TQ
//                       0-------> ONE SAMPLE
//                      1--------> PHASE 2 PROGRAMMABLE
#define ECAN_WAKDIS 0x80
#define ECAN_WAKFIL 0x00
#define ECAN_SEG2PH 8        // phase 2 duratione (quanta) 1-8
#define ECAN_BRGCON3 (ECAN_WAKDIS | ECAN_WAKFIL | (ECAN_SEG2PH-1))
//      ECAN_BRGCON2    10111111
//                           111-> PHASE SEGMENT 2 8 TQ
//                       0-------> CAN FILTER NOT USED FOR WAKE-UP
//                      1--------> DISABLE CAN WAKE-UP FEATURE
//-brp:16----------------------------------------------------------------------
// TQ = 2*BRP / fosc = 2*16 / 32000000 = 1uS
// Tbit=synch+prop+ps1+ps2 = 1+8+8+8 = 25TQ
// baud = 1000/25*1us = 40kbit/sec at 32Mhz
// extended msg 8 data bytes: 67+64bits=131bits*25uS=3275uS=3.3mS = 305 msg/sec
// a 64 Mhz    80Kbaud    610 msg/sec

//-brp:32----------------------------------------------------------------------
// TQ = 2*BRP / fosc = 2*32 / 32000000 = 2uS
// Tbit=synch+prop+ps1+ps2 = 4+8+8+8 = 28TQ
// baud = 1000/28*2us = 35714bit/sec at 64Mhz 

//-brp:64----------------------------------------------------------------------
// TQ = 2*BRP / fosc = 2*64 / 32000000 = 4uS
// Tbit=synch+prop+ps1+ps2 = 1+8+8+8 = 25TQ
// baud = 1000/25*4us = 10kbit/sec at 32Mhz
// extended msg 8 data bytes: 67+64bits=131bits*100uS=13100uS=13mS = 77 msg/sec
// a 64 Mhz    20Kbaud    153 msg/sec

//-brp:64----------------------------------------------------------------------
// TQ = 2*BRP / fosc = 2*64 / 64000000 = 2uS
// Tbit=synch+prop+ps1+ps2 = 4+8+8+8 = 28TQ
// baud = 1000/28*2us = 17.8kbit/sec at 64Mhz

//-----------------------------------------------------------------------------

#if defined(__18F25K80) || defined(__18F26K80) || defined(__18F46K80)
    #define CAN_PIR     PIR5
    #define CAN_PIRbits PIR5bits
    #define CAN_PIE     PIE5
    #define CAN_PIEbits PIE5bits
    #define CAN_IPR     IPR5
#else
    #define CAN_PIR     PIR3
    #define CAN_PIRbits PIR3bits
    #define CAN_PIE     PIE3
    #define CAN_PIEbits PIE3bits
    #define CAN_IPR     IPR3
#endif

#endif
