/**
 * @file X-WL55_WLE5_53L0X.h
 *
 */

#ifndef _X_WL55_WLE5_53L0X_H_
#define _X_WL55_WLE5_53L0X_H_

#include "stm32wlxx_hal.h"

/**
 * @defgroup VL53L0X_config  VL53L0X static configuration
 * @brief Configure BSP support for USART2 (nucleo virtual com)
 * UART support implies that BSP exclusively manages:
 *  @li HAL_UART_MspInit and HAL_UART_MspDeInit
 *  @li DMA channel and interrupt
 */


/** @ingroup VL53L0X_config
 * @{*/

#define MAX_TOF_COUNT		3

#ifndef VL53L0X_HAVE_UART
/**
 * Define this macro at project level if UART is available
 */
#define VL53L0X_HAVE_UART 1
#endif


#if VL53L0X_HAVE_UART

/**
 * User override of baud rate programmed for uart2 (default to 115200)
 */
#ifndef USART2_BAUD_RATE
#   define USART2_BAUD_RATE    115200
#endif


#ifndef VL53L0X_UART_DMA_RX
/** configure use of dma for uart rx
 *
 * Default to not use DMA RX when not defined explicitly
 *
 * require @a VL53L0X_HAVE_UART
 */
#define VL53L0X_UART_DMA_RX   0
#endif //VL53L0X_UART_DMA_RX

#ifndef VL53L0X_UART_DMA_TX
/** configure use of dma for uart tx
 *
 * Default to use DMA TX when not defined explicitly by user
 *
 * require @a VL53L0X_HAVE_UART
 */
#define VL53L0X_UART_DMA_TX   1
#endif //VL53L0X_UART_DMA_RX


#ifndef  VL53L0X_UART_DMA_RX_IRQ_PRI
/** configure usart  max irq priority
 *
 * Default to 0 when not defined explicitly by user
 * @warning may not be support by all mcu (only F401 tested)
 */
#   define  VL53L0X_UART_DMA_RX_IRQ_PRI   0
#endif

#ifndef  VL53L0X_UART_DMA_TX_IRQ_PRI
/** configure usart dma tx irq priority
 *
 * Default to 0 when not defined explicitly
 * @warning only F401
 */

#define  VL53L0X_UART_DMA_TX_IRQ_PRI   0
#endif

#ifndef VL53L0X_UART_IRQ_PRI
/**
 * User override default uart irq priority 0 to fit application needs
 * @warning only supported for f401
 */
#define VL53L0X_UART_IRQ_PRI    0
#endif

/* provided by MSP part */
extern void XNUCLEO53L1A1_USART2_UART_Init(void);

#else
#	define XNUCLEO53L1A1_USART2_UART_Init(...) (void)0
#endif

/** @}  */

/**
 * Configure interrupt pins pins pull up/down
 *
 * set to GPIO_NOPULL or GPIO_PULLUP or GPIO_PULLDOWN
 */
#define VL53L0X_INTR_PIN_PUPD GPIO_PULLUP



/**
 * @ingroup VL53L0X_GPIO1_MAP
 * @{
 */

#ifndef VL53L0X_GPIO1_SHARED
/**
 * @brief select use of shared interrupt
 *
 * Must be set to 0 (or not defined) for one interrupt per sensor
 * Must be set to non 0 for shared interrupt line
 * see @sa VL53L0X_GPIO1_C_OPTION
 */
#define VL53L0X_GPIO1_SHARED  1
#endif


#ifndef VL53L0X_GPIO1_C_OPTION
/**
 * @def VL53L0X_GPIO1_C_OPTION
 * @brief select GPIO1 to exti mapping for center or shared interrupt
 *
 * Set option value or un-define it to match with board configuration
 * @li not defined or 0  : U14=On  and U17=off  => GPIO1_C = PA4
 * @li defined and not 0 : U14=Off and U17=on  => GPIO1_C = PC1
 */
#define VL53L0X_GPIO1_C_OPTION 1
#endif
#if 0
/* ############ FOR SHARED GPIO1 INTR TO MCU    ###### */
#define VL53L0X_GPIO1_C_GPIO_PORT   GPIOA
#define VL53L0X_GPIO1_C_CLK_ENABLE  __GPIOA_CLK_ENABLE
#define VL53L0X_GPIO1_C_GPIO_PIN    GPIO_PIN_10
#define VL53L0X_GPIO1_C_INTx        EXTI15_10_IRQn

#define VL53L0X_GPIO1_L_GPIO_PORT   GPIOA
#define VL53L0X_GPIO1_L_CLK_ENABLE  __GPIOA_CLK_ENABLE
#define VL53L0X_GPIO1_L_GPIO_PIN    GPIO_PIN_10
#define VL53L0X_GPIO1_L_INTx        EXTI15_10_IRQn

#define VL53L0X_GPIO1_R_GPIO_PORT   GPIOA
#define VL53L0X_GPIO1_R_CLK_ENABLE  __GPIOA_CLK_ENABLE
#define VL53L0X_GPIO1_R_GPIO_PIN    GPIO_PIN_10
#define VL53L0X_GPIO1_R_INTx        EXTI15_10_IRQn
/* ############ FOR SHARED GPIO1 INTR TO MCU    ###### */
#endif
/* ############ FOR SHARED GPIO1 INTR TO MCU    ###### */
#ifndef RM2
#define VL53L0X_GPIO1_C_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_C_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_C_GPIO_PIN    GPIO_PIN_3
#define VL53L0X_GPIO1_C_INTx        EXTI3_IRQn

#define VL53L0X_GPIO1_L_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_L_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_L_GPIO_PIN    GPIO_PIN_3
#define VL53L0X_GPIO1_L_INTx        EXTI3_IRQn

#define VL53L0X_GPIO1_R_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_R_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_R_GPIO_PIN    GPIO_PIN_3
#define VL53L0X_GPIO1_R_INTx        EXTI3_IRQn
#elif 	defined(RM2)
#define VL53L0X_GPIO1_C_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_C_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_C_GPIO_PIN    GPIO_PIN_5
#define VL53L0X_GPIO1_C_INTx        EXTI9_5_IRQn

#define VL53L0X_GPIO1_L_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_L_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_L_GPIO_PIN    GPIO_PIN_5
#define VL53L0X_GPIO1_L_INTx        EXTI9_5_IRQn

#define VL53L0X_GPIO1_R_GPIO_PORT   GPIOB
#define VL53L0X_GPIO1_R_CLK_ENABLE  __GPIOB_CLK_ENABLE
#define VL53L0X_GPIO1_R_GPIO_PIN    GPIO_PIN_5
#define VL53L0X_GPIO1_R_INTx        EXTI9_5_IRQn

#endif
/* ############ FOR SHARED GPIO1 INTR TO MCU    ###### */
/** @} */ /* defgroup L53L1A1_GPIO1_MAP */

#ifndef  XNUCLEO53L1A1_TRACE
/**
 * @brief enable error output via trace
 *
 * when undefined (default) no trace no error logging is done, it is safe
 * to at least count error just to see if any errors ever occur.
 *
 * Traces formating and output is end user defined via #trace_printf
 */
#define XNUCLEO53L1A1_TRACE    TRACE_UART
#endif // XNUCLEO53L1A1_TRACE


/**
 * @def XNUCLEO53L1A1_ErrLog(...)
 * Macro used to report error log messages with printf format
 *
 * Our testing version use externally trace_printf,
 * We trace out function names and line numbers plus any text formated with some extra arguments
 */
#if XNUCLEO53L1A1_TRACE
#   define XNUCLEO53L1A1_ErrLog( msg, ...) trace_printf("[Err] %s l %d \t" msg "\n", __func__, __LINE__, ##__VA_ARGS__)
#else
#   define XNUCLEO53L1A1_ErrLog(...) (void)0
#endif



/** @} */ /* group XNUCLEO53L1A1_Debugging */


/**
 * @defgroup X-WL55_WLE5_53L0X_Interface X-WL55_WLE5_53L0X BSP API
 * @{ */

/**
 * 53L0X Device selector
 *
 * @note Most functions are using a device selector as input. ASCII 'c', 'l' or 'r' are also accepted.
 */
enum XNUCLEO53L1A1_dev_e{
    XNUCLEO53L0X_LEFT =  0,    //!< left satellite device P21 header : 'l'
    XNUCLEO53L0X_CENTER  =  1, //!< center (built-in) vl053 device : 'c"
    XNUCLEO53L0X_RIGHT=  2     //!< Right satellite device P22 header : 'r'
};

/**
 * I2C1 handle
 * @note setup and configured by @ref XNUCLEO53L1A1_Init
 */
extern I2C_HandleTypeDef  X_WL55_WLE5_53L0X_hi2c;
/** UART2 handle
 *
 * UART2 is the nucleo Virtual Com Port
 * @note setup and configured by @ref  XNUCLEO53L1A1_Init*/
#if VL53L0X_HAVE_UART
extern UART_HandleTypeDef huart2;

#if VL53L0X_UART_DMA_RX
/** UART2 DMA RX available only if support activated*/
extern DMA_HandleTypeDef hdma_usart2_rx;
#endif

#if VL53L0X_UART_DMA_TX
/** UART2 DMA TX available only if support activated */
extern DMA_HandleTypeDef hdma_usart2_tx;
#endif
#endif//ifdef VL53L0X_HAVE_UART

/**
 * Initialize VL053L1A1 STM32 expansion board
 *
 * @note All VL53L0X devices XSDN are asserted and display is turned off
 * @return 0 on success
 */
int XWL55_WLE5_53L0X_Init(void);

/**
 * Set Reset (XSDN) state of a given "id" device
 * @param  DevNo The device number use  @ref XNUCLEO53L1A1_dev_e. Char 't' 'c' 'r' can also be used
 * @param  state  State of the device reset (xsdn) pin @warning reset  pin is active low
 * @return 0 on success
 */

void XWL55_WLE5_53L0X_ResetId(uint8_t DevNo, int state);

/**
 * Enable Disable interrupt at MCU level (MSP)
 *
 * Enable/Disable the interrupt for a given sensor Dev number
 *
 * End user is expected to override VL53L0X_EXTI_Callback to catch interrupts.
 * When the vector is shared applicaton is passed the device and pin corretcy
 * In shared line mode application is responsible to find what sensor cause the interrupt and deal with races on interrupt line
 *
 * @param EnableIntr  "boolean" 0 to disable interrupt otherwise interrupt is enabled
 * @param DevNo       Device number "name"  of the sensor see @ref XNUCLEO53L1A1_dev_e
 *
 * @warning In shared interrupt mode, use only center : other values are not supported and will return an error
 * @warning If several lines share the same interrupt vector (l053 mcu), only one sensor interrupt shall be enable at a time
 *          otherwise the handler may fail to locate and clear the right source (no sharing management)
 *
 * @return  0 on success \n
 *         <0 for error (invalid id)
 *         >0 interrupt configured but with potential sharing on EXTI groups see @ref VL53L0X_GPIO1_MAP
 */
//int XNUCLEO53L1A1_SetIntrStateId(int EnableIntr, int DevNo);
int XWL55_WLE5_53L0X_SetIntrStateId(int EnableIntr, int DevNo);
int sts_tof_vl53l0x_DetectSensors(void);
void STS_TOF_VL53L0X_Range_Process(void);
#endif /* _X_WL55_WLE5_53L0X_H_ */
