/*$$$LICENCE_NORDIC_STANDARD<2016>$$$*/

/**
 *
 * @defgroup APP_DEBUG_PIN  Pin-based debug utilities
 * @ingroup APP_DEBUG
 * @{
 * @brief Functions that control pin-based debug utilities.
 *
 */

#ifndef __APP_DEBUG_PIN_H__
#define __APP_DEBUG_PIN_H__

#include <stdint.h>

#include "nrf.h"
#include "sr3_config.h"

#if CONFIG_DEBUG_PIN_ENABLED

#define DBG_PIN_PULSE(pin)          debug_pin_pulse(pin)
#define DBG_PIN_SET(pin)            debug_pin_set(pin)
#define DBG_PIN_CLEAR(pin)          debug_pin_clear(pin)
#define DBG_PIN_TOGGLE(pin)         debug_pin_toggle(pin)
#define DBG_PIN_FLAG(pin, flag)     debug_pin_flag(pin, flag)
#define DBG_PIN_SEND(word, bits)    debug_pin_send(word, bits)

/**
 * @brief Initialize the debug lines and startup pulse.
 */
void debug_pin_init(void);

/**
 * @brief Signal 1 µs pulse on the selected line.
 * @param[in] pin   Line to be pulsed.
 */
void debug_pin_pulse(const unsigned int pin);

/**
 * @brief Set the selected line.
 * @param[in] pin   Line to be set.
 */
void debug_pin_set(const unsigned int pin);

/**
 * @brief Clear the selected line.
 * @param[in] pin   Line to be cleared.
 */
void debug_pin_clear(const unsigned int pin);

/**
 * @brief Toggle the selected line.
 * @param[in] pin   Line to be toggled.
 */
void debug_pin_toggle(const unsigned int pin);

/**
 * @brief Set a line according to a flag.
 * @param[in] pin   Line to be set.
 * @param[in] flag  Flag to be set.
 */
void debug_pin_flag(const unsigned int pin, bool flag);

/**
 * @brief Function for displaying an error code on the scope.
 *
 * @warning Bits are sent MSB first with data sampling on falling clock and positive frame signal.
 *
 * @param[in] word  Word to be sent.
 * @param[in] bits  Word length in bits (1-32).
 */
void debug_pin_send(const uint32_t word, const uint8_t bits);

/**
 * @brief Function for error handling, which is called when an error has occurred.
 *
 * @param[in] error_code  Error code supplied to the handler.
 * @param[in] line_num    Line number where the handler is called.
 * @param[in] p_file_name Pointer to the file name.
 */
void debug_pin_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);

#else /* !CONFIG_DEBUG_PIN_ENABLED */

#define DBG_PIN_PULSE(pin)          do { } while (0)
#define DBG_PIN_SET(pin)            do { } while (0)
#define DBG_PIN_CLEAR(pin)          do { } while (0)
#define DBG_PIN_TOGGLE(pin)         do { } while (0)
#define DBG_PIN_FLAG(pin, flag)     do { } while (0)
#define DBG_PIN_SEND(word, bits)    do { } while (0)

__STATIC_INLINE void debug_pin_init(void)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_pulse(const unsigned int pin)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_set(const unsigned int pin)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_clear(const unsigned int pin)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_toggle(const unsigned int pin)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_flag(const unsigned int pin, bool flag)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_send(const uint32_t word, const uint8_t bits)
{
    // Do nothing.
}

__STATIC_INLINE void debug_pin_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
{
    // Do nothing.
}

#endif /* CONFIG_DEBUG_PIN_ENABLED */
#endif /* __APP_DEBUG_PIN_H__ */
/** @} */

