/** @brief Interface to enable watchdog functions
 *
 * @copyright 2020 Remsdaq Ltd.
 * @file c4_watchdog.h
 *
 * The iMXRT1062 processor has three watchdog options:
 * WDOG1, WDOG2 and RTWDOG
 *
 * @date 23 Jul 2020
 * @author Dan Smithers <dwhs@remsdaq.com>
 *
 */



#ifndef C4_WATCHDOG_H_
#define C4_WATCHDOG_H_


/*******************************************************************************
 * Includes
 ******************************************************************************/

#include "c4_error_codes.h"

#include <stdint.h>

/*******************************************************************************
 * Defines
 ******************************************************************************/

/*******************************************************************************
 * Structures
 ******************************************************************************/

/** @brief Enumeration of watchdogs. */
typedef enum
{
	C4_WDOG1,   ///< Watchdog 1 - resets processor
	C4_WDOG2,   ///< Watchdog 2 - for SNVS secure world
	C4_NUM_WDOG ///< Number of watchdogs
} c4_wdog_t;

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/** @brief Initialise watchdog subsystem.
 *
 * @param[in] wdog Watchdog to programme.
 * @param[in] to  Timeout in deci-seconds.
 * @param[in] ito Interrupt ante-delay in deci-seconds. If this is set greater
 *                than or equal to the watchdog timeout (to) then no interrupt is set.
 *
 * @return exit code
 * @retval ERR_OK - success.
 */
exit_code_t c4_initWatchdog(c4_wdog_t wdog, uint16_t to, uint16_t ito);

/** @brief Set timeout on watchdog.
 *
 * This enables the watchdog for the specified timeout.
 *
 * @param[in] wdog Watchdog to programme.
 * @param[in] to Timeout in deci-seconds.
 *
 * @return exit code
 * @retval ERR_OK - success.
 */
exit_code_t c4_setWdogTimeout(c4_wdog_t wdog, uint16_t to);

/** @brief disables the watchdog.
 *
 * @param[in] wdog Watchdog to disable.
 *
 * @return exit code
 * @retval ERR_OK - success.
 */
 exit_code_t c4_disableWdog(c4_wdog_t wdog);

 /** @brief enables the watchdog.
  *
  * @param[in] wdog Watchdog to enable.
  *
  * @return exit code
  * @retval ERR_OK - success.
  */
  exit_code_t c4_enableWdog(c4_wdog_t wdog);

/** @brief Enable interrupt on watchdog before expiry.
 *
 * This specifies that an interrupt is raised a fixed time before the timeout is
 * due to occur. If the watchdog is reset before the interrupt occurs, then no
 * interrupt will be raised.
 *
 * @note This can only be set once and cannot be changed before the next system reset.
 *
 * @param[in] wdog Watchdog to programme.
 * @param[in] to Timeout in deci-seconds.
 *
 * @return exit code
 * @retval ERR_OK - success.
 * @retval ERR_ALREADY_SET - Interrupt has already been set.
 */
exit_code_t c4_setWdogInterrupt(c4_wdog_t wdog, uint16_t to);

/** @brief Retrieve reset history from watchdog registers and from System Register.
 *
 * @return exit code
 * @retval ERR_OK - success.
 */
exit_code_t c4_getResetHistory();

#endif /* C4_WATCHDOG_H_ */
