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

/** UNIFIED driver implementation */         
#include "Fr_UNIFIED.h"           


/* Type definition of the FlexRay bus driver modes */
typedef enum
{
    FR_BD_NORMAL = 0,         // Normal mode
    FR_BD_STANDBY,            // Standby mode
} Fr_BD_mode_type;

/* Type definition of the FlexRay bus driver Error indication modes */
typedef enum
{
    FR_BD_LATCHED = 0,        // Error latched indication mode
    FR_BD_TRACK,              // Error track indication mode
    FR_BD_OTHER,
} Fr_BD_error_mode_type;


/* NCV7383 Status Register Flags Definitions */

#pragma OPTION ADD V30toV31Compatible "-BfaGapLimitBits4294967295" /*this guarantee correct bitfield positions*/

typedef union {
  word Word;
  struct { 
    word RemoteWakeUp     :1;
    word Mode             :1;
    word TransmitterReady :1;
    word BGE_Feedback     :1;
    word Poweron          :1;
    word BusError         :1;
    word ThermalShutdown  :1;
    word TxenTimeout      :1;
    word VccUV            :1;
    word VioUV            :1; 
    word SPI_Error        :1;
    word AlwaysLow1       :1;
    word AlwaysHigh1      :1;
    word AlwaysLow2       :1;
    word AlwaysHigh2      :1;
    word Parity           :1;
  } Bits;
} Fr_bd_flags;

typedef union {
  word Word;
  struct { 
    word S16              :1;
    word S17              :1;
    word S18              :1;
    word S19              :1;
    word S20              :1;
    word S21              :1;
    word S22              :1;
    word S23              :1;
    word S24              :1;
    word S25              :1;
    word S26              :1;
    word S27              :1;
    word S28              :1;
    word S29              :1;
    word S30              :1;
    word S31              :1;
  } Bits;
  struct {
    word Analog           :8;
    word Digital          :8;
  } MergedBits; 
} Fr_bd_version;

typedef struct{
  Fr_bd_flags Flags;  
  Fr_bd_version Version;
} Fr_bd_status_register;


void wait_100ns(UINT16 count100ns);
void wait_1us(UINT16 count1us);
void wait_1ms(void);

/* Status Register readout */
Fr_bd_status_register fr_bd_read_status(Fr_channel_type channel);
Fr_bd_status_register fr_bd_read_status_A(void);
Fr_bd_status_register fr_bd_read_status_B(void);
void fr_bd_read_status_AB(Fr_bd_status_register *status1, Fr_bd_status_register *status2);

/* Status Register readout with different number of clocks */
void fr_bd_read_status_special(byte no_of_bits, word delay);

/* Mode change */
void fr_bd_change_mode(Fr_channel_type channel, Fr_BD_mode_type mode);

/* Change Error indication mode (Latched/tracked) */
void fr_bd_change_error_mode(Fr_channel_type channel, Fr_BD_error_mode_type mode);

/* Read current Error indication mode */
Fr_BD_error_mode_type fr_bd_read_error_indication_mode(Fr_channel_type channel);



/*******************************************************************************/
/* Test Patterns                                                               */
/*******************************************************************************/

/* Valid Remote Wakeup Test Patterns */
void TestPatternRemoteWakeup(Fr_channel_type channel);
/* Valid Remote Wakeup Test Patterns - Long */
void TestPatternRemoteWakeupLong(Fr_channel_type channel);
/* Alternative Remote Wakeup Test Patterns */
void TestPatternRemoteWakeupAlternative(Fr_channel_type channel);
/* Shorted Remote Wakeup Test Patterns */
void TestPatternRemoteWakeupShorted(Fr_channel_type channel);
/* Remote Wakeup Frame Payload Test Patterns */
void TestPatternRemoteWakeupFrame(Fr_channel_type channel);
/* Remote Wakeup Frame Payload Test Patterns */
void TestPatternRemoteWakeupFrameWrong(Fr_channel_type channel);
/* Non Valid Remote Wakeup Test Patterns - short IDLE*/
void TestPatternRemoteWakeupFailShortIdle(Fr_channel_type channel);
/* Non Valid Remote Wakeup Test Patterns - long IDLE*/
void TestPatternRemoteWakeupFailLongIdle(Fr_channel_type channel);
/* Non Valid Remote Wakeup Test Patterns - short firs LOW */
void TestPatternRemoteWakeupFailShortLow1(Fr_channel_type channel);
/* Non Valid Remote Wakeup Test Patterns - short second LOW */
void TestPatternRemoteWakeupFailShortLow2(Fr_channel_type channel);


/* One 10gdBit High Test Pattern */
void TestPattern_HIGH(Fr_channel_type channel);
/* One 10gdBit LOW Test Pattern */
void TestPattern_LOW(Fr_channel_type channel);
/* TSS Test Patterns */
void TestPatternTSS(Fr_channel_type channel);
/* TSS + one 10gdBit LOW Test Patterns */
void TestPatternTSS_LOW(Fr_channel_type channel);
/* TSS + one 10gdBit HIGH Test Patterns */
void TestPatternTSS_HIGH(Fr_channel_type channel);
/* TSS + one 10gdBit High + one 10gdBit LOW Test Pattern */
void TestPatternTSS_HIGH_LOW(Fr_channel_type channel);
/* TSS + one 50/50 Test Pattern */
void TestPatternTSS_50_50(Fr_channel_type channel);
/* TSS + one 10/90 Test Pattern */
void TestPatternTSS_10_90(Fr_channel_type channel);
/* Two 50/50 Test Patterns */
void TestPattern_Two_50_50(Fr_channel_type channel);
/* Current Measurement Test Patterns - long IDLE */
void TestPatternCurrentMeasurement(Fr_channel_type channel);

/* Configuration of CPU pins connected to FR bus driver */
void Fr_bd_ports_init(void);

#ifndef __V30COMPATIBLE__
//#pragma OPTION DEL V30toV31Compatible
#endif
/*******************************************************************************/