/*
 * Copyright (c) 2023, NVIDIA CORPORATION.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * 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.
 *  * Neither the name of NVIDIA CORPORATION 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 ``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 OWNER 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 UTC__UTC_PORT_H
#define UTC__UTC_PORT_H
#define FSP__UTC__UTC_PORT_H                      1

#include <stddef.h>
#include <stdint.h>

/**
 * @brief Receive a char on the UTC RX sw buffer with timeout.
 *
 * @param[in] rx_queue        address of rx_queue
 * @param[in] c               pointer to where the char is written
 * @param[in] timeout         timeout to block until
 *
 * @retval    E_SUCCESS         rx queue creation success.
 * @retval    E_UTC_NULL_PARAM  null rx_queue/c parameters.
 * @retval    other values      port specific error codes.
 */
error_t utc_port_rx_queue_receive(uintptr_t rx_queue,
                                  char *c,
                                  uint32_t timeout);

/**
 * @brief Send a char to the UTC RX sw buffer from interrupt context
 *
 * @param[in] rx_queue        address of rx_queue
 * @param[in] c_in            buffer that holds the char
 * @param[in] isr_yield_flag  pointer to where the context switch state is
 *                            stored.
 *
 * @retval    E_SUCCESS         rx queue creation success.
 * @retval    E_UTC_NULL_PARAM  null rx_queue/c parameters.
 * @retval    other values      port specific error codes.
 */
error_t utc_port_rx_queue_send_from_isr(uintptr_t rx_queue,
                                        char *c_in,
                                        bool *isr_yield_flag);

/**
 * @brief isr exit callout
 *
 * @param[in] isr_yield_flag    flag that saves the context switch state
 *                              from a previous call to *_send_from_isr()
 */
void utc_port_isr_exit(bool isr_yield_flag);

/**
 * @brief Create the UTC RX sw buffer/queue for interrupt driven RX
 *
 * @param[in] rx_queue          address of rx_queue

 * @retval    E_SUCCESS         rx queue creation success.
 * @retval    other values      port specific error codes.
 */
error_t utc_port_rx_queue_create(uintptr_t *rx_queue);

/**
 * @brief returns the elapsed microseconds from a reference.
 *
 * @param[in] start      value of time stamp counter at the start of the period of
 *                       when the elapsed time is being computed.
 *
 * @retval               the number of microseconds that have elapsed between when
 *                       the start time was taken and this function called.
 */
uint64_t utc_port_get_elapsed_usecs(const uint64_t start);

#endif
