/***************************************************************************//**
 *   @file   ECAN1Drv.h
 *   @brief  Contient les fonctions de gestion du bus CAN
 *   @author Kévin MARCHAND(Kevin.marchand@intelligence-electronique.com)
********************************************************************************
 * Copyright 2015(c) PLCD, Inc.
 *
 * Fonctions de gestion du bus CAN
 *
********************************************************************************
 *   SVN Revision: 1
*******************************************************************************/

#ifndef _ECAN_H_
#define _ECAN_H_

#if defined(__XC16__)
    #include <xc.h>
#elif defined(__C30__)
    #if defined(__dsPIC33E__)
    	#include <p33Exxxx.h>
    #elif defined(__dsPIC33F__)
    	#include <p33Fxxxx.h>
    #endif
#endif
#include "../system.h"

// Définition du Bit Rate
#define ECAN_BITRATE    125000      // MAX = 1M
#define ECAN_FCY	FCY


/* Define the number of ECAN RX buffers.
 * Only one TX buffer is used. This
 * is explained in the readme file along with
 * this code example. */
#define NUM_OF_RX_BUFFERS 8
#define BUFFER_TX_SIZE 7
#define BUFFER_RX_SIZE 8


extern __eds__ int ecanTxMsgBuffer[BUFFER_TX_SIZE] __attribute__((space(dma),eds,aligned(256)));
extern __eds__ int ecanRxMsgBuffer[NUM_OF_RX_BUFFERS][BUFFER_RX_SIZE] __attribute__((space(dma),eds,aligned(256)));
//int ecanTxMsgBuffer[7] __attribute__((space(dma)));
//int ecanRxMsgBuffer[NUM_OF_RX_BUFFERS][8] __attribute__((space(dma)));

extern volatile int receivedECANmsg;
extern volatile int transmitNextECANmsg;
extern int sentPacketECANMSG;
extern volatile int rxECANBufferIndex;
extern int rxECANIndex;
extern int txECANBufferIndex;

void ECAN1DMAConfig(int numOfRxBuffers);
void ECAN1ClockConfig(void);
void ECAN1InterruptConfig(void);
void ECAN1TxRxBuffersConfig(void);
void ECAN1SetOPMode(void);
void ECAN1RxFiltersConfig(void);
void ECANCreateSIDPacket(int * data, int SID,int * output);
int ECAN1SendPacket(void);
void ECANCreateEIDPacket(int * data,int sid, long EID,__eds__ int * output);

#endif
