/* ###################################################################
**     This component module is generated by Processor Expert. Do not modify it.
**     Filename    : McuRTT.h
**     Project     : FRDM-K64F_Generator
**     Processor   : MK64FN1M0VLL12
**     Component   : SeggerRTT
**     Version     : Component 01.092, Driver 01.00, CPU db: 3.00.000
**     Compiler    : GNU C Compiler
**     Date/Time   : 2021-12-17, 13:26, # CodeGen: 759
**     Abstract    :
**
**     Settings    :
**          Component name                                 : McuRTT
**          Version                                        : V6.86e
**          Number of Up Channels                          : 3
**          Number of Down Channels                        : 3
**          Max Blocked Interrupt Level                    : 3
**          Syscalls                                       : no
**          Channel 0                                      : Enabled
**            Name                                         : "Terminal"
**            Up Buffer Size (Tx)                          : 512
**            Down Buffer Size (Rx)                        : 64
**            Up Buffer Mode                               : Skip (Default)
**            Down Buffer Mode                             : Skip (Default)
**            Blocking Send                                : Disabled
**            Printf Buffer Size                           : 64
**          SDK                                            : McuLib
**          Shell                                          : McuShell
**          Source Folders                                 : 
**            Source Folder                                : SEGGER_RTT
**            Config Folder                                : SEGGER_RTT
**     Contents    :
**         Read             - int McuRTT_Read(unsigned BufferIndex, const char* pBuffer, unsigned NumBytes);
**         Write            - int McuRTT_Write(unsigned BufferIndex, char* pBuffer, unsigned BufferSize);
**         WriteString      - unsigned McuRTT_WriteString(unsigned BufferIndex, const char* s);
**         printf           - int McuRTT_printf(unsigned BufferIndex, const char* sFormat, ...);
**         GetKey           - dword McuRTT_GetKey(void);
**         WaitKey          - long McuRTT_WaitKey(void);
**         HasKey           - long McuRTT_HasKey(void);
**         SetTerminal      - int McuRTT_SetTerminal(char TerminalId);
**         TerminalOut      - int McuRTT_TerminalOut(char TerminalId, const char* s);
**         ConfigUpBuffer   - int McuRTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, char*...
**         ConfigDownBuffer - int McuRTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, char*...
**         RecvChar         - uint8_t McuRTT_RecvChar(uint8_t *c);
**         SendChar         - uint8_t McuRTT_SendChar(uint8_t ch);
**         GetCharsInRxBuf  - uint16_t McuRTT_GetCharsInRxBuf(void);
**         StdIOKeyPressed  - bool McuRTT_StdIOKeyPressed(void);
**         StdIOReadChar    - void McuRTT_StdIOReadChar(uint8_t *c);
**         StdIOSendChar    - void McuRTT_StdIOSendChar(uint8_t ch);
**         GetStdio         - %@Shell@'ModuleName'%.ConstStdIOTypePtr McuRTT_GetStdio(void);
**         Deinit           - void McuRTT_Deinit(void);
**         Init             - void McuRTT_Init(void);
**
** * (c) Copyright Segger, 2020-2021
**  * http      : www.segger.com
**  * See separate Segger licensing terms.
**  *
**  * Processor Expert port: Copyright (c) 2016-2021 Erich Styger
**  * Web:         https://mcuoneclipse.com
**  * SourceForge: https://sourceforge.net/projects/mcuoneclipse
**  * Git:         https://github.com/ErichStyger/McuOnEclipse_PEx
**  * 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.
**  *
**  * 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.
** ###################################################################*/
/*!
** @file McuRTT.h
** @version 01.00
** @brief
**
*/         
/*!
**  @addtogroup McuRTT_module McuRTT module documentation
**  @{
*/         

/* MODULE McuRTT. */

#include "McuRTT.h"
#if McuRTT_CONFIG_BLOCKING_SEND
  #include "McuWait.h"
#endif

/* default standard I/O struct */
McuShell_ConstStdIOType McuRTT_stdio = {
    .stdIn = (McuShell_StdIO_In_FctType)McuRTT_StdIOReadChar,
    .stdOut = (McuShell_StdIO_OutErr_FctType)McuRTT_StdIOSendChar,
    .stdErr = (McuShell_StdIO_OutErr_FctType)McuRTT_StdIOSendChar,
    .keyPressed = McuRTT_StdIOKeyPressed, /* if input is not empty */
  #if McuShell_CONFIG_ECHO_ENABLED
    .echoEnabled = false,
  #endif
  };
uint8_t McuRTT_DefaultShellBuffer[McuShell_DEFAULT_SHELL_BUFFER_SIZE]; /* default buffer which can be used by the application */

/*
** ===================================================================
**     Method      :  Read (component SeggerRTT)
**
**     Description :
**         Read from buffer
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Buffer/channel to be used.
**                           0 for terminal.
**       * pBuffer         - Pointer to buffer
**         BufferSize      - Number of bytes to write
**     Returns     :
**         ---             - Number of bytes that have been read
** ===================================================================
*/
/**
int McuRTT_Read(unsigned BufferIndex, const char* pBuffer, unsigned NumBytes)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  Write (component SeggerRTT)
**
**     Description :
**         Write to buffer
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Buffer/channel to be used.
**                           0 for terminal.
**       * pBuffer         - Pointer to buffer
**         BufferSize      - Size of buffer
**     Returns     :
**         ---             - Number of bytes which have been written to
**                           the up buffer
** ===================================================================
*/
/**
int McuRTT_Write(unsigned BufferIndex, char* pBuffer, unsigned BufferSize)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  WriteString (component SeggerRTT)
**
**     Description :
**         Write to buffer
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Buffer/channel to be used.
**                           0 for terminal.
**       * s               - Pointer to 
**     Returns     :
**         ---             - Number of bytes which have been stored in
**                           the "Up"-buffer.
** ===================================================================
*/
/**
unsigned McuRTT_WriteString(unsigned BufferIndex, const char* s)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  GetKey (component SeggerRTT)
**
**     Description :
**         Returns a character/key
**     Parameters  : None
**     Returns     :
**         ---             - character code
** ===================================================================
*/
/**
dword McuRTT_GetKey(void)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  WaitKey (component SeggerRTT)
**
**     Description :
**         Waits for a key and returns it.
**     Parameters  : None
**     Returns     :
**         ---             - >=0    Character which has been read.
** ===================================================================
*/
/**
long McuRTT_WaitKey(void)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  HasKey (component SeggerRTT)
**
**     Description :
**         Checks if at least one character for reading is available in
**         the SEGGER RTT buffer
**     Parameters  : None
**     Returns     :
**         ---             - 0: No characters are available to read; 1:
**                           At least one character is available.
** ===================================================================
*/
/**
long McuRTT_HasKey(void)
{
  Implemented as macro in the header file.
}
*/

/*
** ===================================================================
**     Method      :  StdIOKeyPressed (component SeggerRTT)
**
**     Description :
**         StdIO handler for Shell
**     Parameters  : None
**     Returns     :
**         ---             - True if there are characters in teh input
**                           buffer
** ===================================================================
*/
bool McuRTT_StdIOKeyPressed(void)
{
  return McuRTT_HasKey()!=0;
}

/*
** ===================================================================
**     Method      :  StdIOReadChar (component SeggerRTT)
**
**     Description :
**         StdIO Handler for reading a character. It returns a zero
**         byte if there is no character in input buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * c               - Pointer to where to store the received
**                           character
**     Returns     : Nothing
** ===================================================================
*/
void McuRTT_StdIOReadChar(uint8_t *c)
{
  int res;

  res = McuRTT_GetKey();
  if (res==-1) { /* no character present */
    *c = '\0';
  } else {
    *c = (uint8_t)res; /* return character */
  }
}

/*
** ===================================================================
**     Method      :  StdIOSendChar (component SeggerRTT)
**
**     Description :
**         StdIO handler to sends a character.
**     Parameters  :
**         NAME            - DESCRIPTION
**         ch              - Character to send
**     Returns     : Nothing
** ===================================================================
*/
void McuRTT_StdIOSendChar(uint8_t ch)
{
#if McuRTT_CONFIG_BLOCKING_SEND
  #if McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS>0
    #define RTT_STDIO_CNTR (100)
    static uint8_t cntr = 0; /* counter to avoid blocking too long if there is no RTT client on the host */
  #endif
  #if McuRTT_CONFIG_BLOCKING_SEND_TIMEOUT_MS>0 && McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS>0
  int timeoutMs = McuRTT_CONFIG_BLOCKING_SEND_TIMEOUT_MS;
  #endif

  for(;;) { /* will break */
    if (McuRTT_Write(0, (const char*)&ch, 1)==1) { /* non blocking send, check that we were able to send */
    #if McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS>0
      if (cntr>0) {
        cntr--;
      }
    #endif
      break; /* was able to send character, get out of waiting loop */
    }
  #if McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS>0
    cntr++;
    if (cntr>=RTT_STDIO_CNTR) { /* waiting for too long, give up */
      cntr = RTT_STDIO_CNTR;
      return;
    }
    McuWait_WaitOSms(McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS);
    #if McuRTT_CONFIG_BLOCKING_SEND_TIMEOUT_MS>0
    if(timeoutMs<=0) {
      break; /* timeout */
    }
    timeoutMs -= McuRTT_CONFIG_BLOCKING_SEND_WAIT_MS;
    #endif
  #endif
  } /* for */
#else
  (void)McuRTT_Write(0, &ch, 1); /* non blocking send, might loose characters */
#endif
}

/*
** ===================================================================
**     Method      :  RecvChar (component SeggerRTT)
**
**     Description :
**         Receives a character from channel 0. Returns ERR_RXEMPTY if
**         no character available
**     Parameters  :
**         NAME            - DESCRIPTION
**       * c               - Pointer to where to store the received
**                           character
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
uint8_t McuRTT_RecvChar(uint8_t *c)
{
  int res;

  res = McuRTT_GetKey();
  if (res==-1) { /* no character present */
    return ERR_RXEMPTY;
  }
  *c = (uint8_t)res; /* return character */
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  SendChar (component SeggerRTT)
**
**     Description :
**         Sends a character to channel 0.
**     Parameters  :
**         NAME            - DESCRIPTION
**         ch              - Character to send
**     Returns     :
**         ---             - Error code. ERR_OK if character has been
**                           sent, ERR_TXFULL otherwise.
** ===================================================================
*/
uint8_t McuRTT_SendChar(uint8_t ch)
{
  int res;

  res = SEGGER_RTT_Write(0, (const char*)&ch, 1);
  if (res!=1) {
    return ERR_TXFULL; /* character not sent? */
  }
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  GetCharsInRxBuf (component SeggerRTT)
**
**     Description :
**         Returns the number of characters in the receive buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input buffer,
**                           zero for none available.
** ===================================================================
*/
/**
uint16_t McuRTT_GetCharsInRxBuf(void)
{
  // Function is implemented as macro in the header file
  if (SEGGER_RTT_HasKey()) {
    return 1; // at least one available
  }
  return 0; // none available
}
*/

/*
** ===================================================================
**     Method      :  TerminalOut (component SeggerRTT)
**
**     Description :
**         Writes a string to the given terminal without changing the
**         terminal for channel 0.
**     Parameters  :
**         NAME            - DESCRIPTION
**         TerminalId      - TerminalId, 0..15
**       * s               - Pointer to string
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
/**
int McuRTT_TerminalOut(char TerminalId, const char* s)
{
  Function is implemented as macro in the header file
}
*/

/*
** ===================================================================
**     Method      :  SetTerminal (component SeggerRTT)
**
**     Description :
**          Sets the terminal to be used for output on channel 0.
**     Parameters  :
**         NAME            - DESCRIPTION
**         TerminalId      - Terminal ID, 0..15
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
/**
int McuRTT_SetTerminal(char TerminalId)
{
  Function is implemented as macro in the header file
}
*/

/*
** ===================================================================
**     Method      :  ConfigUpBuffer (component SeggerRTT)
**
**     Description :
**         Configures the Up (device to host) buffer
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Buffer index
**         sName           - Buffer name
**       * pBuffer         - Pointer to buffer
**         intBufferSize   - Size of buffer in bytes
**         Flags           - SEGGER_RTT_MODE_NO_BLOCK_SKIP,
**                           SEGGER_RTT_MODE_NO_BLOCK_TRIM or
**                           SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
**     Returns     :
**         ---             - Error code, >=0 OK, <0 Error
** ===================================================================
*/
/**
int McuRTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags)
{
  Function is implemented as macro in the header file
}
*/

/*
** ===================================================================
**     Method      :  ConfigDownBuffer (component SeggerRTT)
**
**     Description :
**         Configures the Down (host to device) buffer
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Buffer index
**         sName           - Buffer name
**       * pBuffer         - Pointer to buffer
**         intBufferSize   - Size of buffer in bytes
**         Flags           - SEGGER_RTT_MODE_NO_BLOCK_SKIP,
**                           SEGGER_RTT_MODE_NO_BLOCK_TRIM or
**                           SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
**     Returns     :
**         ---             - Error code, >=0 OK, <0 Error
** ===================================================================
*/
/**
int McuRTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags)
{
  Function is implemented as macro in the header file
}
*/

/*
** ===================================================================
**     Method      :  printf (component SeggerRTT)
**
**     Description :
**         Stores a formatted string in SEGGER RTT control block. This
**         data is sent to the host.
**     Parameters  :
**         NAME            - DESCRIPTION
**         BufferIndex     - Index of "Up"-buffer to be
**                           used. (e.g. 0 for "Terminal")
**         sFormat         - Pointer to format string, followed
**                           by the arguments for conversion
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
/**
int McuRTT_printf(unsigned BufferIndex, const char* sFormat, ...)
{
  Function is implemented as macro in the header file
}
*/

/*
** ===================================================================
**     Method      :  Deinit (component SeggerRTT)
**
**     Description :
**         Driver deinitialization
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void McuRTT_Deinit(void)
{
  /* noting to de-initialize */
}

/*
** ===================================================================
**     Method      :  Init (component SeggerRTT)
**
**     Description :
**         Initializes the RTT Control Block.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void McuRTT_Init(void)
{
  SEGGER_RTT_Init();
}

/*
** ===================================================================
**     Method      :  GetStdio (component SeggerRTT)
**
**     Description :
**         Returns a pointer to the standard I/O
**     Parameters  : None
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
McuShell_ConstStdIOTypePtr McuRTT_GetStdio(void)
{
  return &McuRTT_stdio;
}

/* END McuRTT. */

/*!
** @}
*/
