/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
* @file lin.h
*
* @brief This file contains the user lin function..
*
*/

#ifndef __LIN_H
#define __LIN_H
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "stdint.h"
#include "ac780x_gpio.h"

/* ============================================  Define  ============================================ */
#define LIN_INSTANCE            (2U)                  /* LIN instance */
#define LIN_NODE_MODE           (MASTER)              /* LIN master mode */
#define LIN_BAUDRATE            (19200U)              /* LIN baudrate */
#define LIN_TIME_BASE           (5U)                  /* LIN base time 5 ms */
#define LIN_TIMEOUT             (4U)                  /* LIN timeout value, decided by baudrate timeout = LIN_TIMEOUT x LIN_TIME_BASE*/
#define LIN_TIMER_CHANNEL       (0U)                  /* Use timer channel */

#define LIN_BREAK_LENGTH        BREAK_LENGTH_13BIT    /* LIN break length for master */
#define LIN_BREAK_THRESHOLD     BREAK_THRESHOLD_11BIT /* LIN break detect threshold */
#define LIN_SYNC_BAUDRATE       (1U)                  /* LIN auto sync baudrate for slave */

#define LIN_SLEEP_GPIO          (GPIOB)
#define LIN_SLEEP_PIN           (GPIO_PIN14)
#define LIN_TX_GPIO             (GPIOB)
#define LIN_TX_PIN              (GPIO_PIN9)
#define LIN_RX_GPIO             (GPIOB)
#define LIN_RX_PIN              (GPIO_PIN10)

/* ===========================================  Typedef  ============================================ */
/*!
 * @brief LIN checksum type.
 */
typedef enum
{
    LIN_ENHANCED_CHECKSUM = 0U,      /* Enhanced checksum type */
    LIN_CLASSIC_CHECKSUM             /* Classic checksum type */
} lin_checksum_t;

/*!
 * @brief LIN data direction type.
 */
typedef enum
{
    LIN_DIR_RX = 0U,                 /* receive direction */
    LIN_DIR_TX                       /* transmit direction */
} lin_direction_t;

/*!
 * @brief LIN frame information struct.
 */
typedef struct
{
    uint8_t          id;             /* LIN message frame ID for this frame */
    lin_direction_t  dir;            /* The direction of the reponse data */
    lin_checksum_t   cst;            /* Checksum type*/
    uint8_t          length;         /* Length of bytes in the data field */
    uint16_t         delayTime;      /* Actual slot time in ms for the frame */
    uint8_t          *data;          /* Address of the structure that lists the signals */
} lin_frame_info_t;

/*!
 * @brief LIN current node state
 */
typedef struct
{
    uint8_t schTblIdx;               /* Schedule table index */
    uint8_t frameNum;                /* Schedule table frame number */
    uint8_t frameIdx;                /* Current frame index */
    uint8_t delay;                   /* Current frame delay count */
    lin_frame_info_t *schedule;      /* Schedule table pointer */
} lin_cur_node_t;

/*!
 * @brief LIN schedule table struct.
 */
typedef struct
{
    uint8_t frameNum;                /* Number of frame slots in the schedule table */
    lin_frame_info_t *schedule;      /* Address of the schedule table */
} lin_schedule_table_t;

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */
/*!
 * @brief       LIN_Init
 *              LIN module and pin initialization required for LIN module
 *              LIN is configured as master mode, and Baudrate is 19200
 * @param[in]   instance: LIN module instance
 * @param[in]   mode: Configure the lin node to either host mode or slave mode
 * @param[in]   tblIdx
 * @param[in]   baudrate: LIN module baudrate
 * @return      none
 *
 */
void LIN_Init(uint8_t instance, uint8_t mode, uint8_t tblIdx, uint16_t baudrate);

/*!
 * @brief LIN_InitScheduleTimer
 *        Initialize timer.
 *
 * @param[in] time: time value  ms
 * @return : none
 */
void LIN_InitScheduleTimer(uint32_t time);
/* ======================================  Functions define  ======================================== */

#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /*__LIN_H*/

/* =============================================  EOF  ============================================== */
