/*!
 * @file        user_tmr_config.h
 *
 * @brief       TMR and SENT protocol configuration
 *
 * @version     V1.0.0
 *
 * @date        2026-03-27
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 */

#ifndef USER_TMR_CONFIG_H
#define USER_TMR_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes */
#include "g32a10xx.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_tmr.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_dma.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_InputCapture_SENT
  @{
*/

/** @defgroup TMR_InputCapture_SENT_Macros Macros
  @{
*/

#define SENT_TICK_US         3.5f     /*!< Base Tick Time (us) */
#define SENT_SYNC_TICKS      56U      /*!< Number of Sync Pulses */
#define SENT_DATA_NUM        6U       /*!< Number of half bytes of data */
#define SENT_CRC_POLY        0x0DU    /*!< CRC Check Polynomial */
#define SENT_MAX_TICKS       27U      /*!< Maximum number of ticks */
#define SENT_MIN_DATA_TICKS  12U      /*!< Minimum Data Tick Count */

/* SENT receiving pin definition */
#define SENT_RX_GPIO_PORT    GPIOA
#define SENT_RX_GPIO_PIN     GPIO_PIN_0
#define SENT_RX_GPIO_AF      GPIO_AF_PIN2

/* SENT sending pin definition */
#define SENT_TX_GPIO_PORT    GPIOA
#define SENT_TX_GPIO_PIN     GPIO_PIN_6
#define SENT_TX_GPIO_AF      GPIO_AF_PIN1

/**@} end of group TMR_InputCapture_SENT_Macros */

/** @defgroup TMR_InputCapture_SENT_Enumerations Enumerations
  @{
*/

/*!
 * @brief SENT receiving state machine
 */
typedef enum {
    SENT_STATE_IDLE,        /*!< Idle State */
    SENT_STATE_SYNC,        /*!< Synchronization Pulse Detection */
    SENT_STATE_DATA,        /*!< Data Reception */
    SENT_STATE_CRC,         /*!< CRC Reception */
    SENT_STATE_VALIDATE     /*!< Verify CRC */
} Tmr_SentStateType;

/**@} end of group TMR_InputCapture_SENT_Enumerations */

/** @defgroup TMR_InputCapture_SENT_Structures Structures
  @{
*/

/*!
 * @brief SENT handler structure definition
 */
typedef struct {
    Tmr_SentStateType state;           /*!< Current State */
    uint16_t send_DMA_buf[30];      /*!< Sending Buffer */
    uint32_t capture_prev;          /*!< Last captured value */
    uint32_t capture_curr;          /*!< Current captured value */
    uint32_t tick_us;               /*!< Timer count corresponds to us value */
    uint8_t nibble_idx;             /*!< Half-byte Index */
    uint8_t nibble_buf[8];          /*!< Half-byte buffer */
    uint8_t data_buf[6];            /*!< Final data buffer */
    uint8_t crc_received;           /*!< Received CRC value */
    uint8_t crc_calculated;         /*!< Calculated CRC value */
    uint8_t frame_valid;            /*!< Frame Valid Flag */
} Tmr_SentHandleType;

extern Tmr_SentHandleType sent_typ;
extern volatile uint32_t g_tick;

/**@} end of group TMR_InputCapture_SENT_Structures */

/** @defgroup TMR_InputCapture_SENT_Variables Variables
  @{
*/

/* Tmr_Delay timer */
extern Tmr_TimeBaseType g_tmrDelayConfig;

/* SENT timer */
extern Tmr_TimeBaseType g_tmr2SentBaseConfig;
extern Tmr_TimeBaseType g_tmr3SentBaseConfig;

/* Input capture / output compare */
extern Tmr_IcConfigType  g_tmrSentIcConfig;
extern Tmr_OcConfigType  g_tmrSentOcConfig;

/* DMA */
extern Dma_ConfigType g_dmaSentConfig;

/* GPIO */
extern Gpio_ConfigType g_gpioSentRxConfig;
extern Gpio_ConfigType g_gpioSentTxConfig;

/**@} end of group TMR_InputCapture_SENT_Variables */

void Tmr_SentReceive(void);

/**@} end of group TMR_InputCapture_SENT */
/**@} end of group TMR_Examples */
/**@} end of group Examples */

#ifdef __cplusplus
}
#endif

#endif /* USER_TMR_CONFIG_H */
