/*******************************************************************************
 * ON Semiconductor                                                            *
 * NCV7383 Evaluation Kit                                                      *
 *                                                                             *
 * FlexRay_CC.h                                                                *
 * FlexRay module handling function prototypes                                 *
 *                                                                             *
 * Created by: Filip Brtan                                                     *
 * Date: 30.07.2012                                                            *
 *******************************************************************************/

/* Standard and driver types */
#include "Fr_UNIFIED_types.h" 
#include "Fr_UNIFIED.h"   


#pragma DATA_SEG FlexRay_CC_DATA
#pragma CODE_SEG FlexRay_CC_CODE

/*******************************************************************************/
/* Global variables                                                            */
/*******************************************************************************/

/* FlexRay Frame Payload length definition */
extern byte payload_length;

/* FkexRay Transmit and receive data arrays */
extern word tx_data[5][16];
extern word rx_data[5][16];

/* Indication LEDs buffer */
extern byte LEDS;


extern bool FlexRay_CC_initialized;
extern bool Enter_STOP_MODE;

/* Function Prototypes */

/* Error function for debugging */
void Failed(byte u8number);

/* Function for transmission */
void CC_interrupt_1(byte buffer_idx);
void CC_interrupt_2(byte buffer_idx);
void CC_interrupt_3(byte buffer_idx);
void CC_interrupt_4(byte buffer_idx);
void CC_interrupt_5(byte buffer_idx);
/* Functions for reception */
void CC_interrupt_rx_1(byte buffer_idx);
void CC_interrupt_rx_2(byte buffer_idx);
void CC_interrupt_rx_3(byte buffer_idx);
void CC_interrupt_rx_4(byte buffer_idx);
void CC_interrupt_rx_5(byte buffer_idx);

/* Function for FlexRay Timer 1 */
void CC_interrupt_timer_1(void);

/* Function for FlexRay Timer 2 */
void CC_interrupt_timer_2(void);

/* Function for FlexRay wakeup interrupt */
//void CC_interrupt_wakeup(void);
void CC_interrupt_wakeup(Fr_channel_type wakeup_channel);

/* Function for FlexRay protocol interrupt */
void CC_interrupt_protocol(void);

/* Function for FlexRay cycle start interrupt */
void CC_interrupt_cycle_start(void);

/* Function for FlexRay protocol state changed interrupt */
void CC_interrupt_protocol_state_changed(void);

/* FlexRay Message buffers first initialization */
Fr_return_type Fr_init_message_buffers(void);

/*  */
void FlexRay_CC_configuration(void);

/** FlexRay module configuration */
void FlexRay_Init(void);

/* CRC Calculation */
#define vCrcInit ((word) 0x1A)
#define vCrcPolynomial ((word) 0x385)
word Calculate_Header_Crc(bool SyncFrame, bool StartupFrame, word FrameId, byte PayloadLength);


/* Single transmit MB */
#define FR_TX_BUFFER_1    0  
#define FR_TX_BUFFER_2    1 
#define FR_TX_BUFFER_3    2 
#define FR_TX_BUFFER_4    3 
#define FR_TX_BUFFER_5    4  

/* Receive MB */
#define FR_RX_BUFFER_1    5  
#define FR_RX_BUFFER_2    6 
#define FR_RX_BUFFER_3    7  
#define FR_RX_BUFFER_4    8
#define FR_RX_BUFFER_5    9  

/* FlexRay signals field user in data segment */
#define FR_SIGNAL_MODES    0
#define FR_SIGNAL_COUNTERH 1
#define FR_SIGNAL_COUNTERL 2
#define FR_SIGNAL_GENERAL  3
#define FR_SIGNAL_VOLTAGE  4
#define FR_SIGNAL_5        5

/* FlexRay signal - External BTN down */
#define FR_SIGNAL_MODES_BD_GO_TO_STANDBY  0x8000
#define FR_SIGNAL_GENERAL_BTN_IRQ_DOWN    0x0400

#pragma DATA_SEG DEFAULT

/*******************************************************************************/