/*******************************************************************************
 * @file    dr_piezo.h
 * @brief   Piezo Transducer Driver (2MHz Signal Generator)
 * @author  Charles KWON
 * @date    2025-12-09
 * 
 * @note    Hardware: nRF52840 + MD1822K6-G MOSFET Driver + TC7920K6-G MOSFET
 *          Output: �20V at 2MHz, 3~5 cycles
 * 
 * @details Timing Sequence:
 *          1. PE = HIGH (enable)
 *          2. P_OUT/N_OUT = 2MHz pulses (3~5 cycles)
 *          3. DMP = HIGH (dump)
 *          4. DMP = LOW
 *          5. PE = LOW (disable)
 *          
 *          All signals (P_OUT, N_OUT, DMP) operate within PE HIGH period.
 ******************************************************************************/

#ifndef DR_PIEZO_H
#define DR_PIEZO_H

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

/*==============================================================================
 * POWER CONTROL PINS (DC/DC Converter +/-20V)
 *============================================================================*/
//#define DR_PIEZO_PWR_SHDN       NRF_GPIO_PIN_MAP(0, 21)  /**< SHDN_VPP/VNN (LT3463) */    // P0.21 : PZT_EN_MUXA  jhChun 0128
//#define DR_PIEZO_PWR_EN_10V     NRF_GPIO_PIN_MAP(0, 22)  /**< EN_+10V (MCP1804) */        // P0.22 : NIRS PIN     

#define DR_PIEZO_PWR_EN       NRF_GPIO_PIN_MAP(1, 9)  /** Power Enable   jhChun 0128 */

/*==============================================================================
 * TX SIGNAL PINS (MOSFET Driver Control)
 *============================================================================*/
//#define DR_PIEZO_PIN_PE         NRF_GPIO_PIN_MAP(1, 5)   /**< Pulse Enable */
//#define DR_PIEZO_PIN_DMP        NRF_GPIO_PIN_MAP(1, 9)   /**< Dump control */
//#define DR_PIEZO_PIN_P_OUT      NRF_GPIO_PIN_MAP(1, 3)   /**< Positive output */
//#define DR_PIEZO_PIN_N_OUT      NRF_GPIO_PIN_MAP(1, 2)   /**< Negative output */

#define DR_PIEZO_PIN_PE         NRF_GPIO_PIN_MAP(0, 25)   /**< Pulse Enable */           // P1.05 -> P0.25
#define DR_PIEZO_PIN_DMP        NRF_GPIO_PIN_MAP(1, 0)   /**< Dump control */            // P1.9 -> P1.0
#define DR_PIEZO_PIN_P_OUT      NRF_GPIO_PIN_MAP(1, 7)   /**< Positive output */         // P1.3 -> P1.7
#define DR_PIEZO_PIN_N_OUT      NRF_GPIO_PIN_MAP(1, 6)   /**< Negative output */         // P1.2 -> P1.6  jhChun 0128
 
/*==============================================================================
 * MUX CONTROL PINS (Echo Signal Path Selection)
 *============================================================================*/
//#define DR_PIEZO_MUX_SEL1       NRF_GPIO_PIN_MAP(1, 13)  /**< MUX Select 1 (HIGH) */
//#define DR_PIEZO_MUX_SEL2       NRF_GPIO_PIN_MAP(1, 12)  /**< MUX Select 2 (LOW) */
//#define DR_PIEZO_MUX_SEL3       NRF_GPIO_PIN_MAP(1, 11)  /**< MUX Select 3 (LOW) */

/* Piezo MUX pins (8ch)    jhChun 0129 */
#define DR_PIEZO_EN_MUXA        NRF_GPIO_PIN_MAP(0, 21)  /**< MUXA Enable */
#define DR_PIEZO_EN_MUXB        NRF_GPIO_PIN_MAP(0, 23)  /**< MUXB Enable */
#define DR_PIEZO_MUX_SEL0       NRF_GPIO_PIN_MAP(1, 10)  /**< MUX Select 0 */
#define DR_PIEZO_MUX_SEL1       NRF_GPIO_PIN_MAP(0, 28)  /**< MUX Select 1 */

/*==============================================================================
 * CONFIGURATION
 *============================================================================*/
/**
 * @note Actual operating frequency is defined in dr_piezo.c as PIEZO_FREQ_MHZ.
 *       Change PIEZO_FREQ_MHZ in dr_piezo.c to adjust the burst frequency.
 *       Current setting: 2.1 MHz
 */
#define DR_PIEZO_FREQ_HZ        2100000     /**< Target frequency (set PIEZO_FREQ_MHZ in .c) */
#define DR_PIEZO_DEFAULT_CYCLES 5           /**< Default burst cycles (3~5) */
#define DR_PIEZO_MIN_CYCLES     3
#define DR_PIEZO_MAX_CYCLES     7       /* 3 -> 7  jhChun 26.03.12 */
#define DR_PIEZO_MUX_SETTLING_US 1300   /**< MUX settling delay (us) */

/*==============================================================================
 * POWER CONTROL FUNCTIONS
 *============================================================================*/

/**
 * @brief Power ON piezo system (+/-20V DC/DC converter)
 */
void dr_piezo_power_on(void);

/**
 * @brief Power OFF piezo system
 */
void dr_piezo_power_off(void);

/*==============================================================================
 * TX DRIVER FUNCTIONS
 *============================================================================*/

/**
 * @brief Initialize piezo TX driver (Timer + PPI + GPIOTE)
 */
void dr_piezo_init(void);

/**
 * @brief Uninitialize piezo TX driver
 */
void dr_piezo_uninit(void);

/**
 * @brief Transmit a burst of 2MHz pulses
 * @param cycles Number of cycles to transmit (3~10)
 */
void dr_piezo_burst(uint8_t cycles);

/**
 * @brief Transmit default burst (5 cycles)
 */
void dr_piezo_pulse(void);

/**
 * @brief Enable TX output (prepare for transmission)
 */
void dr_piezo_enable(void);

/**
 * @brief Disable TX output (return to idle state)
 */
void dr_piezo_disable(void);

/**
 * @brief Check if TX is currently active
 * @return true if transmitting
 */
bool dr_piezo_is_busy(void);

/**
 * @brief Set TX frequency (for testing)
 * @param freq_hz Frequency in Hz (100kHz ~ 4MHz)
 */
void dr_piezo_set_frequency(uint32_t freq_hz);

/**
 * @brief Test all pins manually (for debugging with oscilloscope)
 */
void dr_piezo_test_pins(void);

/**
 * @brief Initialize MUX control pins for echo signal path
 */
void dr_piezo_mux_init(void);

/**
 * @brief Select piezo channel (0~7) via 8ch MUX
 * @param channel Piezo channel number (0~7)
 *
 * Channel mapping (EN_MUXA, EN_MUXB, SEL0, SEL1):
 *   CH0=A0(1,0,0,0) CH1=A2(1,0,1,0) CH2=A1(1,0,0,1) CH3=A3(1,0,1,1)
 *   CH4=B0(0,1,1,1) CH5=B1(0,1,0,1) CH6=B2(0,1,1,0) CH7=B3(0,1,0,0)
 *
 * @note MUX settling time: 1.3ms delay after switching
 */
void dr_piezo_select_channel(uint8_t channel);

/*==============================================================================
 * SYSTEM FUNCTIONS (Power + TX combined)
 *============================================================================*/

/**
 * @brief Full system initialization (power + TX driver)
 */
void dr_piezo_system_init(void);

/**
 * @brief Full system shutdown
 */
void dr_piezo_system_uninit(void);

/**
 * @brief Transmit with power check
 * @param cycles Number of cycles
 */
void dr_piezo_transmit(uint8_t cycles);

/**
 * @brief Software-based burst (CPU-controlled, no Timer/PPI)
 * @param cycles Number of cycles (1~20)
 * @note Default frequency: 2.1 MHz
 */
void dr_piezo_burst_sw(uint8_t cycles);

/**
 * @brief Software-based burst at 1.8 MHz
 * @param cycles Number of cycles (1~20)
 * @note Fixed frequency: 1.8 MHz
 */
void dr_piezo_burst_sw_18mhz(uint8_t cycles);

/**
 * @brief Software-based burst at 2.0 MHz
 * @param cycles Number of cycles (1~20)
 * @note Fixed frequency: 2.0 MHz
 */
void dr_piezo_burst_sw_20mhz(uint8_t cycles);

/**
 * @brief Software-based burst at 2.2 MHz
 * @param cycles Number of cycles (1~20)
 * @note Fixed frequency: 2.2 MHz
 */
void dr_piezo_burst_sw_22mhz(uint8_t cycles);

/**
 * @brief Software-based burst at 1.7 MHz
 * @param cycles Number of cycles (1~20)
 * @note Fixed frequency: 1.7 MHz
 */
void dr_piezo_burst_sw_17mhz(uint8_t cycles);

/**
 * @brief Software-based burst at 1.9 MHz
 * @param cycles Number of cycles (1~20)
 * @note Fixed frequency: 1.9 MHz
 */
void dr_piezo_burst_sw_19mhz(uint8_t cycles);

#endif /* DR_PIEZO_H */

