/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _LIN_CFG_H_
#define _LIN_CFG_H_

/*! \brief Contains public interface to various functions related
 *         to the lin module
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include <stdbool.h>

/*! \addtogroup lin_cfg
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

/* LIN Communication protocol version */
#define LIN_PROTOCOL_VER_22  (0x22u)
#define LIN_PROTOCOL_VER_21  (0x21u)
#define LIN_PROTOCOL_VER_20  (0x20u)
#define LIN_PROTOCOL_VER_13  (0x13u)
#define LIN_PROTOCOL_VER_12  (0x12u)
#define LIN_PROTOCOL_VER_11  (0x11u)
#define LINCFG_PROTOCOL_VER  LIN_PROTOCOL_VER_22

#define LINCFG_NODE_ADDRESS  (0x00)

/* Lin communication baudrate */
#define LINCFG_BAUDRATE      (19200u)

/* Node address for sleep command */
#define LINCFG_NAD_FOR_SLEEP (0u)

/* Node feature type select, be used as master or slave node */
#define LINCFG_NODE_MASTER   (1u)
#define LINCFG_NODE_SLAVE    (0u)
#define LINCFG_NODE_USED     LINCFG_NODE_MASTER

/* the actual physical channel in MCU that be supported to be used as lin bus */
#define LINCFG_CH_IDX_0      (0u)
#define LINCFG_CH_IDX_1      (1u)
#define LINCFG_CH_IDX_2      (2u)
#define LINCFG_CH_IDX_3      (3u)

/* Define how many hard physical channel are used */
#define LINCFG_CH_ACTIVE     (1u)
#define LINCFG_CH_INACTIVE   (0u)

/* Select the physical channel as master used */
#if LINCFG_NODE_USED == LINCFG_NODE_MASTER
/* Define the actual channel is used by master node, the rest channels are used for slave node */
#define LINCFG_CH0_USED         (LINCFG_CH_ACTIVE)
#define LINCFG_CH1_USED         (LINCFG_CH_INACTIVE)
#define LINCFG_CH2_USED         (LINCFG_CH_INACTIVE)
#define LINCFG_CH3_USED         (LINCFG_CH_INACTIVE)
#define LINCFG_CH_NUM           (LINCFG_CH0_USED + LINCFG_CH1_USED + LINCFG_CH2_USED + LINCFG_CH3_USED)

#define LINCFG_MASTER_CH_IDX    LINCFG_CH_IDX_0

#else
/* Define the actual channel is used by master node, the rest channels are used for slave node */
#define LINCFG_CH0_USED                 (LINCFG_CH_ACTIVE)
#define LINCFG_CH1_USED                 (LINCFG_CH_INACTIVE)
#define LINCFG_CH2_USED                 (LINCFG_CH_INACTIVE)
#define LINCFG_CH3_USED                 (LINCFG_CH_INACTIVE)
#define LINCFG_CH_NUM                   (LINCFG_CH0_USED + LINCFG_CH1_USED + LINCFG_CH2_USED + LINCFG_CH3_USED)

/* Select the physical channel as slave used if more than one channels are used in the current slave node, should select one channel as a wakeup source,
   No need all channels send the wakeup signal if the internal requirement
*/
#define LINCFG_SLAVE_CH_AS_WAKEUP_IDX   LINCFG_CH_IDX_0

#endif

/* Define the feature of hardware detected break with sync signal together */
#if LINCFG_NODE_USED == LINCFG_NODE_MASTER
#define LINCFG_AUTO_SYNC_EN (0u)
#else
#define LINCFG_AUTO_SYNC_EN (1u)
#endif

/* Frame time slot base tick = 1ms */
#define LINCFG_TICK_BASE                         (1u)

/* When node is as a subscriber, no response from last received data is more than this time, timeout is existed */
#define LINCFG_RESP_BYTE_TIMEOUT_MAX_CNT         (LINCFG_TICK_BASE * 5u)

/* Define the wakeup signal duration time, unit: ms default value: 2ms */
#define LINCFG_WAKEUP_SIGNAL_CHECK_VALID_MIN_CNT (2u)
#define LINCFG_WAKEUP_SIGNAL_DURATION_MIN_CNT    (4u)

/* Define the duration to start communication after wakeup by bus or by self, unit: ms */
#define LINCFG_START_COM_AFTER_WKP_CNT           (10u)
/* After idle timeout , bus enter into sleep mode, unit: ms */
#define LINCFG_BUS_IDLE_TICK_CNT                 (10000u)

/* Define the first received header for slave node after be wakeup by bus */
#define LINCFG_FAST_WAKEUP_PERIOD                (100u)
/* The delay time of retry wakeup bus again after failed to wakeup bus that is up to 3 times, unit: ms */
#define LINCFG_SLOW_WAKEUP_PEIROD                (1500u)

/* Define the value of retry to wakeup the bus */
#define LINCFG_WAKEUP_RETRY_MAX_CNT              (3u)

/* If slave node wakeup the bus actively, but in the period, no valid header is received from master, the slave can retry to wakeup */
#define LINCFG_WAKEUP_FAILED_TIMEOUT             (200u)

/*******************************************************************************
 * the typedefs
 ******************************************************************************/
typedef struct
{
    uint32_t protocolVersion;
    uint8_t  configNad;
    uint8_t  initialNad;
    uint32_t productId;
    uint32_t p2Min;
    uint32_t stMin;
    uint32_t timeoutNAs;
    uint32_t timeoutNCr;
    uint32_t linSpeed;
} Lin_CfgType;

#if LINCFG_CH0_USED == LINCFG_CH_ACTIVE

#endif

#if LINCFG_CH1_USED == LINCFG_CH_ACTIVE

#endif

#if LINCFG_CH2_USED == LINCFG_CH_ACTIVE

#endif

#if LINCFG_CH3_USED == LINCFG_CH_ACTIVE

#endif

/*******************************************************************************
 * the globals
 ******************************************************************************/
extern const Lin_CfgType gLin_Cfg;

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

#ifdef __cplusplus
}
#endif /* extern "C" */

/*! @}*/

#endif /* _LIN_CFG_H_ */
