

#include "if_rtt.h"
#include "stdint.h"
#include "stdio.h"
#include "SEGGER_RTT.h"
#include "string.h"

static int g_inited;
static int _open(void)
{
  if(g_inited==0)
  {
    //SEGGER_RTT_Init();
    SEGGER_RTT_ConfigUpBuffer(0, "log", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
    //SEGGER_RTT_ConfigUpBuffer(1, "info", buff, 256, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
//    SEGGER_RTT_ConfigUpBuffer(1, "info", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
    g_inited=1;
  }
  return 0;
}



static int _close(void)
{
  return 0;
}


static int _putc(int c)
{
  uint8_t d=c;
  if(SEGGER_RTT_Write(0, &d, 1)==1)
    return c;
  else
    return EOF;
}

static int _write(const uint8_t *ptr,int size)
{
//  if(strncmp("info",(char *)ptr,4)==0)
//    SEGGER_RTT_SetTerminal(1);
//  else
//    SEGGER_RTT_SetTerminal(0);
  return SEGGER_RTT_Write(0, ptr, size);
}

static int _getc(void)
{
  uint8_t d;
  if(SEGGER_RTT_Read(0,&d,1)==1)
    return d;
  else
    return EOF;
}

static int _read(uint8_t *ptr,int size)
{
  int rb=0;
  if(rb=SEGGER_RTT_Read(0,ptr,size),rb)
    return rb;
  else
    return EOF;
}


static  void *set_irqfun(void (*fun)(uint8_t d,void *context),void *context)
{
  return 0;
}




static rtt_typedef rtt_def=
{
  .init=_open,
  .write=_write,
  .read=_read,
  .set_irqfun=set_irqfun,
};


rtt_typedef *rtt(void)
{
  return &rtt_def;
}





