/*
 * LPUART_TJA1124_LIN_Comm.h
 *
 *  Created on: Sep 24, 2019
 *      Author: nxa07657
 */

#ifndef LPUART_TJA1124_LIN_COMM_H_
#define LPUART_TJA1124_LIN_COMM_H_

#include "bsp.h"
#include "Lpuart_Lin_Ip.h"


#define TJA1124_EnterSleepMode()   Siul2_Dio_Ip_WritePin(PTF_L_HALF, TJA1124_SLP_PIN, 1)
#define TJA1124_EnterNormalMode()  Siul2_Dio_Ip_WritePin(PTF_L_HALF, TJA1124_SLP_PIN, 0)


#define TIMEOUT  200  /* the SPI communication timeout */

#define LIN_CH   4    /* define the LIN channel number to be used */

#define FRAME_SLAVE_RECEIVE_DATA_ID1 	0x10   /* master send slave receive LIN message ID for channel 1 */
#define FRAME_SLAVE_RECEIVE_DATA_ID2 	0x11   /* master send slave receive LIN message ID for channel 2 */
#define FRAME_SLAVE_RECEIVE_DATA_ID3 	0x12   /* master send slave receive LIN message ID for channel 3 */
#define FRAME_SLAVE_RECEIVE_DATA_ID4 	0x13   /* master send slave receive LIN message ID for channel 4 */

#define FRAME_SLAVE_RECEIVE_DATA_ID_MASK 0xFC  /* master send slave receive LIN message ID mask */

#define FRAME_MASTER_RECEIVE_DATA_ID1 	0x20   /* master receive slave send LIN message ID for channel 1 */
#define FRAME_MASTER_RECEIVE_DATA_ID2 	0x21   /* master receive slave send LIN message ID for channel 2 */
#define FRAME_MASTER_RECEIVE_DATA_ID3 	0x22   /* master receive slave send LIN message ID for channel 3 */
#define FRAME_MASTER_RECEIVE_DATA_ID4 	0x23   /* master receive slave send LIN message ID for channel 4 */

#define FRAME_MASTER_RECEIVE_DATA_ID_MASK 0xFC /* master receive slave send LIN message ID mask */

#define FRAME_GO_TO_SLEEP			0x30   /* go to sleep LIN message ID */

#define LIN_FRAME_DATA_LEN          8      /* LIN frame data length */

#define LIN_SERVICE_CHECKWAKEUP_TYPE OSIF_COUNTER_DUMMY

#define TJA1124_CH1_LIN_INST  6
#define TJA1124_CH2_LIN_INST  7
#define TJA1124_CH3_LIN_INST  9
#define TJA1124_CH4_LIN_INST  12


#define LIN_PIT1_INST 1U

/* PIT Channel used - 0,1,2,3 */
#define CH_0 0U
#define CH_1 1U
#define CH_2 2U
#define CH_3 3U

/* PLAT_CLK_FREQ==40000000 */
/* PIT time-out period  */
#define PIT1_PERIOD_1MS 		40000

#define PIT1_MS2CNT(ms)  (ms * PIT1_PERIOD_1MS)


#define TJA1124_LIN_CH1_TEST  1  /* enable this macro to test TJA1124 LIN channel 1*/
#define TJA1124_LIN_CH2_TEST  1  /* enable this macro to test TJA1124 LIN channel 2*/
#define TJA1124_LIN_CH3_TEST  1  /* enable this macro to test TJA1124 LIN channel 3*/
#define TJA1124_LIN_CH4_TEST  1  /* enable this macro to test TJA1124 LIN channel 4*/

/* *****************important!!!!******************************************** */
/* user must ensure the TJA1124 LIN channel has proper LIN salve node connected
 * and setup with the expected response ID message before enable its test,
 * or it will block the entire test task if slave response is enabled.
 */
//#define TJA1124_LIN_CH1_RECV_ENABLE 1 /* enable the lin channel 1 receiving*/
//#define TJA1124_LIN_CH2_RECV_ENABLE 1 /* enable the lin channel 2 receiving*/
//#define TJA1124_LIN_CH3_RECV_ENABLE 1 /* enable the lin channel 3 receiving*/
//#define TJA1124_LIN_CH4_RECV_ENABLE 1 /* enable the lin channel 4 receiving*/

typedef enum{
	ENHANCED = 0,
	CLASSICAL = 1
}Checksum_Type;


Std_ReturnType LIN_Frame_Transmit(uint32_t instance, Lpuart_Lin_Ip_PduType *Message);
Std_ReturnType LIN_Frame_Receive (uint32_t instance, Lpuart_Lin_Ip_PduType *Message);

void LPUART_LIN_TJA1124_Test_Init(void);
void LPUART_LIN_TJA1124_Test(void);
void LPUART_LIN1_TJA1124_Test(void);
void LPUART_LIN2_TJA1124_Test(void);
void LPUART_LIN3_TJA1124_Test(void);
void LPUART_LIN4_TJA1124_Test(void);


extern uint32_t wakeupSignalFlag;

#endif /* LPUART_TJA1124_LIN_COMM_H_ */
