/*
 * Copyright (c) 2013 - 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _NT_SAFETY_H_
#define _NT_SAFETY_H_

/**
 * \defgroup safety Safety
 * \ingroup ntapi
 * Safety represent the check layer for the FMEA tests in the NXP Touch system;
 * it is the layer that is tighly coupled with the hardware module available on
 * the NXP MCU device and the GPIO pins.
 * \{
 */

#include "nt_types.h"
#include "nt_filters.h"
#include "nt_electrodes.h"

/* Forward declaration */
struct nt_module;
struct nt_electrode;

struct nt_module_safety_interface;



/** GPIO interface structure for the Safety tests */
struct nt_module_safety_gpio_params {
    const struct nt_module_gpio_user_interface *user_interface;    
};


/**
 * \defgroup gsafety General API
 * \ingroup safety
 * General Function definition of the safety.
 *
 * \{
 */

/**
 * The TSI module interface structure.
 */
extern const struct nt_module_safety_interface nt_safety_interface;   /*!< Can't be NULL. */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Check the safety interface. 
 * \return the gpio user interface status
 *
 * Check the status safety gpio user interface and check if gpio pins and timer function are properly defined.    
  \code
    // Complete safety test of all electrodes from module where is defined my_electrode  
    int32_t testResult;
    testResult = nt_module_safety_gpio_check_data(nt_electrode_get_module_data(&my_electrode));
  \endcode
 */ 
int32_t nt_module_safety_gpio_check_data(const struct nt_module_data *module);

/**
 * \brief Short to ground (GND) test.
 * \return The test result status success or failure.
 *
 * This function tests the possible electrode short to GND. At first, the function
 * switches electrode pin to GPIO input mode with pull-up enabled, the pin value
 * is read from port. The "logic One" value is expected for good, undamaged electrode and 
 * the success result. If "logic Zero" was detected the failure status is returned.
 * When the mutual-cap mode is defined for electrode, both of the associated Rx and Tx pin  
 * shorts are tested.
  \code
    // Test if "my_electrode" is shorted to GND
    int32_t testResult;
    testResult = nt_module_safety_check_electrode_short_gnd(&my_electrode);
  \endcode
 */
int32_t nt_module_safety_check_electrode_short_gnd(const struct nt_electrode *elec);

/**
 * \brief Short to power supply (VDD) test.
 * \return The test result status success or failure.
 *
 * This function tests the possible electrode short to VDD. At first, the function
 * switches electrode pin to GPIO input mode with pull-down enabled, the pin value
 * is read from port. The logic zero value is expected for good, undamaged electrode and 
 * the success result. If logic one was detected the failure status is returned.
 * When the mutual-cap mode is defined for electrode, both of the associated Rx and Tx pin  
 * shorts are tested.
  \code
    // Test if "my_electrode" is shorted to VDD
    int32_t testResult;
    testResult = nt_module_safety_check_electrode_short_vdd(&my_electrode);
  \endcode
 */
int32_t nt_module_safety_check_electrode_short_vdd(const struct nt_electrode *elec);

/**
 * \brief Short one electrode with other one test.
 * \return The test result status success or failure.
 *
 * This function tests possisble electrode short to other one electrode. At first, 
 * the function change all electrode pins except the one tested electrode to GPIO mode 
 * with the logic zero output. The tested electrode is set as GPIO input with pull-up enabled.
 * Then the level of the tested electrode is read from port. The "logic One" value is expected 
 * for the undamaged electrode and the success result. If "logic Zero" is detected 
 * the failure status is returned.
 * When the mutual-cap mode is defined for electrode, both of the associated Rx and Tx pins  
 * are tested for shorts.
  \code
    // Test if "my_electrode" is shorted to another electrode
    int32_t testResult;
    testResult = nt_module_safety_check_electrode_short_adj(&my_electrode);
  \endcode
 */
int32_t nt_module_safety_check_electrode_short_adj(const struct nt_electrode *elec);

/**
 * \brief Electrode baseline too low level test.
 * \return The test result status success or failure.
 *
 * This function reads the actual baseline level of tested electrode and compare it 
 * with stored baseline level. If the actual baseline value is lower about 12.5% below then 
 * stored baseline the function return the failure status. In case the actual baseline
 * level is higher then 87.5% of the stored baseline the function returns 
 * success status. 
  \code
    // Test the baseline low level for already defined my_electrode 
    int32_t testResult;
    testResult = nt_module_safety_check_baseline_low(&my_electrode);
  \endcode
 */
int32_t nt_module_safety_check_baseline_low(const struct nt_electrode *elec);

/**
 * \brief Electrode baseline too high level test.
 * \return The test result status success or failure.
 *
 * This function reads the actual baseline level of tested electrode and compare it 
 * with stored baseline level. If the actual baseline value is higher then 25% above the 
 * stored baseline the function returns the failure status. In case the actual baseline
 * level is lower then 125% of the stored baseline the function returns 
 * success status. If all test passed the function return success status if not the 
 * failure status will be return. 
  \code
    // Test the baseline high level for already defined my_electrode 
    int32_t testResult;
    testResult = nt_module_safety_check_baseline_high(&my_electrode);
  \endcode
 */
int32_t nt_module_safety_check_baseline_high(const struct nt_electrode *elec);

/**
 * \brief Execute all safety tests.
 * \return The test result status success or failure.
 *
 * The function is high level safety function to check all electrodes for short to 
 * GND, VDD or other electrode and execute baseline too low and high level tests. 
 * To make all test the electrodes must be switched to GPIO mode firstly using the 
 * _nt_module_safety_switch_electrodes_gpio funtion and after test the electrodes are 
 * switched back using _nt_module_safety_switch_electrodes_tsi function.  
  \code
    // Complete safety test of all electrodes from module where is defined my_electrode  
    int32_t testResult;
    testResult = nt_module_safety_check_electrodes (nt_electrode_get_module_data(&my_electrode));
  \endcode
 */
int32_t nt_module_safety_check_electrodes(struct nt_module_data *module);

#ifdef __cplusplus
}
#endif

/** \} end of gsafety_api group */
/** \} end of safety group */

#endif
